1- # clocalc
1+ # clo
22
3- ![ ] ( https://github.com/sdingcn/clocalc /actions/workflows/run_test.yml/badge.svg )
3+ ![ ] ( https://github.com/sdingcn/clo /actions/workflows/run_test.yml/badge.svg )
44
5- ** Clo** sure ** calc** ulus is an interpreted functional programming language.
6- See [ test/] ( test/ ) for code examples (` *.clo ` ).
5+ ** Clo** is an interpreted programming language.
76
8- ## syntax
7+ The distinguished feature of this language is the ability to save
8+ the current program state as a string.
9+ The built-in function ` .forkstate ` returns
10+ a string encoding the entire program state,
11+ and when the state is resumed using ` .eval ` it starts
12+ right after the ` .forkstate ` call but with a return value of Void type.
13+ ```
14+ {
15+ (.putstr "0\n")
16+ (.putstr "1\n")
17+ letrec (state (.forkstate)) {
18+ (.putstr "2\n")
19+ if (.= (.type state) 0) # if it is a void value
20+ (.putstr "3\n")
21+ (.eval state)
22+ }
23+ }
24+ ```
925
26+ The other features of this language is just a normal
27+ dynamically typed function programming language.
1028```
11- <comment> := #[^\n]*\n
12- <integer> := [+-]?[0-9]+ // C++ int
13- <string> := "([^"] | \")*" // see interpreter source for the supported alphabet
14- <variable> := [a-zA-Z_][a-zA-Z0-9_]*
15- <intrinsic> := .void // generates a Void object
16- | .+ | .- | .* | ./ | .% | .< | .<= | .> | .>= | .= | ./=
17- | .and | .or | .not // no short-circuit; use "if" for short-circuit
18- | .s+ | .s< | .s<= | .s> | .s>= | .s= | .s/= | .s|| | .s[] | .quote | .unquote
19- | .s->i | .i->s
20- | .type // 0 for Void, 1 for Int, 2 for String, 3 for Closure
21- | .eval
22- | .getchar | .getint | .putstr | .flush // IO
23- <vepair> := <variable> <expr>
24- <expr> := <integer>
25- | <string>
26- | <variable>
27- | lambda ( <variable>* ) <expr>
28- | letrec ( <vepair>* ) <expr>
29- | if <expr> <expr> <expr>
30- | { <expr>+ } // sequenced evaluation
31- | ( <intrinsic> <expr>* )
32- | ( <expr> <expr>* )
33- | @ <variable> <expr> // accesses a closure's environment variable
29+ letrec (
30+ sum lambda (n acc)
31+ if (.< n 1)
32+ acc
33+ (sum (.- n 1) (.+ acc n))
34+ )
35+ (sum (.getint) 0)
3436```
3537
38+ See [ test/] ( test/ ) for more code examples (` *.clo ` ).
39+
3640## dependencies
3741
3842This project was tested on macOS.
@@ -45,7 +49,7 @@ This project was tested on macOS.
4549
4650```
4751make -C src/ release
48- bin/clocalc <source-path>
52+ bin/clo <source-path>
4953```
5054
5155` python3 run_test.py ` (re-)builds the interpreter and runs all tests.
0 commit comments