Skip to content

Commit 557cca9

Browse files
committed
supporting pause&resume
1 parent c126e48 commit 557cca9

File tree

7 files changed

+271
-73
lines changed

7 files changed

+271
-73
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/bin/clo
2+
/bin/clo.dSYM/
13
/bin/clocalc
24
/bin/clocalc.dSYM/
35
/src/.vscode/

README.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
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

3842
This project was tested on macOS.
@@ -45,7 +49,7 @@ This project was tested on macOS.
4549

4650
```
4751
make -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.

run_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test() -> None:
3737
with open(iopath, "r") as f:
3838
io = json.loads(f.read())
3939
start = time.time()
40-
res = execute(["bin/clocalc", filepath], io["in"])
40+
res = execute(["bin/clo", filepath], io["in"])
4141
end = time.time()
4242
if (res[0] == 0 and res[1] == io["out"]):
4343
print(f"OK ({end - start:.3f} seconds)")

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CXX = clang++
22
WARNING = -Wall -Wextra -pedantic
33
STD = -std=c++20
44
SRC = main.cpp
5-
DST = -o ../bin/clocalc
5+
DST = -o ../bin/clo
66

77
debug: main.cpp
88
$(CXX) $(WARNING) $(STD) -g -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all $(SRC) $(DST)

0 commit comments

Comments
 (0)