From 7d48b75f81a1cd507a93bf5a640fb55638917824 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 3 Jun 2023 14:19:02 -0500 Subject: [PATCH] Update README.md --- README.md | 111 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6319ac77..45d06b92 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,8 @@ Janet logo -**Janet** is a functional and imperative programming language and bytecode interpreter. It is a -Lisp-like language, but lists are replaced -by other data structures (arrays, tables (hash table), struct (immutable hash table), tuples). -The language also supports bridging to native code written in C, meta-programming with macros, and bytecode assembly. +**Janet** is a dynamic language and bytecode interpreter for system scripting, expressive automation, and +extending programs written in C or C++ with user scripting capabilities. There is a REPL for trying out the language, as well as the ability to run script files. This client program is separate from the core runtime, so @@ -21,14 +19,104 @@ If you'd like to financially support the ongoing development of Janet, consider
+## Examples + +See the examples directory for all provided example programs. + +### Game of Life + +```janet +# John Conway's Game of Life + +(def- window + (seq [x :range [-1 2] + y :range [-1 2] + :when (not (and (zero? x) (zero? y)))] + [x y])) + +(defn- neighbors + [[x y]] + (map (fn [[x1 y1]] [(+ x x1) (+ y y1)]) window)) + +(defn tick + "Get the next state in the Game Of Life." + [state] + (def cell-set (frequencies state)) + (def neighbor-set (frequencies (mapcat neighbors state))) + (seq [coord :keys neighbor-set + :let [count (get neighbor-set coord)] + :when (or (= count 3) (and (get cell-set coord) (= count 2)))] + coord)) + +(defn draw + "Draw cells in the game of life from (x1, y1) to (x2, y2)" + [state x1 y1 x2 y2] + (def cellset @{}) + (each cell state (put cellset cell true)) + (loop [x :range [x1 (+ 1 x2)] + :after (print) + y :range [y1 (+ 1 y2)]] + (file/write stdout (if (get cellset [x y]) "X " ". "))) + (print)) + +# Print the first 20 generations of a glider +(var *state* '[(0 0) (-1 0) (1 0) (1 1) (0 2)]) +(for i 0 20 + (print "generation " i) + (draw *state* -7 -7 7 7) + (set *state* (tick *state*))) +``` + +### TCP Echo Server + +```janet +# A simple TCP echo server using the built-in socket networking and event loop. + +(defn handler + "Simple handler for connections." + [stream] + (defer (:close stream) + (def id (gensym)) + (def b @"") + (print "Connection " id "!") + (while (:read stream 1024 b) + (printf " %v -> %v" id b) + (:write stream b) + (buffer/clear b)) + (printf "Done %v!" id) + (ev/sleep 0.5))) + +(net/server "127.0.0.1" "8000" handler) +``` + +### Windows FFI Hello, World! + +```janet +# Use the FFI to popup a Windows message box - no C required + +(ffi/context "user32.dll") + +(ffi/defbind MessageBoxA :int + [w :ptr text :string cap :string typ :int]) + +(MessageBoxA nil "Hello, World!" "Test" 0) +``` + ## Use Cases Janet makes a good system scripting language, or a language to embed in other programs. It's like Lua and Guile in that regard. It has more built-in functionality and a richer core language than -Lua, but smaller than GNU Guile or Python. +Lua, but smaller than GNU Guile or Python. However, it is much easier to embed and port than these languages. ## Features +* 600+ functions and macros in the core library +* Build in socket networking, threading, subprocesses, and more. +* Parsing Expression Grammars (PEG) engine as a more robust Regex alternative +* Macros +* Per-thread event loop for efficient IO (epoll/IOCP/kqueue) +* Built-in C FFI lets you load existing binaries and run them. +* Erlang-style supervision trees that integrate with the event loop * Configurable at build time - turn features on or off for a smaller or more featureful build * Minimal setup - one binary and you are good to go! * First-class closures @@ -38,19 +126,12 @@ Lua, but smaller than GNU Guile or Python. * Mutable and immutable arrays (array/tuple) * Mutable and immutable hashtables (table/struct) * Mutable and immutable strings (buffer/string) -* Macros * Multithreading -* Per-thread event loop for efficient evented IO * Bytecode interpreter with an assembly interface, as well as bytecode verification * Tail-call optimization -* Direct interop with C via abstract types and C functions +* Interface with C via abstract types and C functions * Dynamically load C libraries -* Functional and imperative standard library -* Lexical scoping -* Imperative programming as well as functional * REPL -* Parsing Expression Grammars built into the core library -* 400+ functions and macros in the core library * Embedding Janet in other programs * Interactive environment with detailed stack traces @@ -240,10 +321,6 @@ there is no need for dynamic modules, add the define See the [Embedding Section](https://janet-lang.org/capi/embedding.html) on the website for more information. -## Examples - -See the examples directory for some example Janet code. - ## Discussion Feel free to ask questions and join the discussion on the [Janet Gitter channel](https://gitter.im/janet-language/community).