Skip to content

Commit 90d8a3d

Browse files
committed
the entire etude
1 parent c690c0c commit 90d8a3d

File tree

19 files changed

+1674
-727
lines changed

19 files changed

+1674
-727
lines changed

.github/workflows/ci.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: ci
2+
on:
3+
push:
4+
paths-ignore: ["README.md"]
5+
workflow_dispatch:
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v5
11+
- uses: oven-sh/setup-bun@v2
12+
- uses: extractions/setup-just@v3
13+
- run: just ci
14+
- run: just test-docker

Justfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
abc:
22

33
factorial:
4-
bun trac.ts "@#(ps,50!=)'" factorial.trac
4+
bun trac.ts "@#(ps,50!=)'" examples/factorial.trac
55

66
test-watch:
77
bun test --watch
8+
9+
ci:
10+
bun test

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# TRAC interpreter
2+
3+
[<img src="trac.jpg">](trac.jpg)
4+
5+
The repository contains a TRAC interpreter described in the book [Etudes for Programmers](https://dl.acm.org/doi/10.5555/1096892) (1978) by Charles Wetherell.
6+
7+
The interpreter is written in JavaScript and implements what is called the original TRAC-64 definition of the languages.
8+
9+
The interpreter also implements additional functions and an ability to call forms as functions by name, suggested by Charles Wetherell as an extension of the etude.
10+
11+
All functions are covered unit tests.
12+
13+
NOTE: However, there are a few unimplemented functions: external stream I/O (`ai`, `ao`, `sp`, `rp`) and external block I/O (`sb`, `fb`, `eb`).
14+
15+
## Examples
16+
17+
### calculate "e" constant
18+
19+
```sh
20+
node trac.ts examples/e.trac
21+
```
22+
23+
### Calculate "pi"
24+
25+
```sh
26+
node trac.ts examples/pi.trac
27+
```
28+
29+
### Calculate factorial (of `50!`)
30+
31+
```sh
32+
node trac.ts examples/factorial.trac
33+
```
34+
35+
### Solve Hanoi puzze recursively
36+
37+
```sh
38+
node trac.ts examples/hanoi.trac
39+
```
40+
41+
## Testing
42+
43+
### Prerequisites
44+
45+
- bun.sh
46+
47+
### Run tests
48+
49+
```sh
50+
bun test
51+
```

examples/e.trac

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#(ds,Factorial,(#(eq,X,1,1,(#(ml,X,#(cl,Factorial,#(su,X,1)))))))'
2+
#(ss,Factorial,X)'
3+
4+
#(ds,pow10,(#(eq,P,0,1,(#(ml,10,#(cl,pow10,#(su,P,1)))))))'
5+
#(ss,pow10,P)'
6+
7+
#(ds,zeros,(#(eq,K,0,,(0#(cl,zeros,#(su,K,1))))))'
8+
#(ss,zeros,K)'
9+
10+
#(ds,padfrac,(#(cl,zeros,#(su,P,#(sl,S)))S))'
11+
#(ss,padfrac,P,S)'
12+
13+
#(ds,esum,(#(eq,N,0,#(ad,A,#(dv,S,1)),(#(cl,esum,#(su,N,1),#(ad,A,#(dv,S,#(cl,Factorial,N))),S)))))'
14+
#(ss,esum,N,A,S)'
15+
16+
#(ds,N,15)'
17+
#(ds,P,10)'
18+
19+
#(ds,S,#(cl,pow10,#(cl,P)))'
20+
#(ds,T,#(cl,esum,#(cl,N),0,#(cl,S)))'
21+
22+
#(ps,#(dv,#(cl,T),#(cl,S)).#(cl,padfrac,#(cl,P),(#(su,#(cl,T),#(ml,#(dv,#(cl,T),#(cl,S)),#(cl,S))))))'
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
#(ds,Factorial,(#(eq,X,1,1,(#(ml,X,#(cl,Factorial,#(su,X,1)))))))'#(ss,Factorial,X)'#(cl,Factorial,50)'
1+
#(ds,Factorial,(#(eq,X,1,1,(#(ml,X,#(cl,Factorial,#(su,X,1)))))))'
2+
#(ss,Factorial,X)'
3+
#(cl,Factorial,50)'

examples/hanoi.trac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#(ds,other,(#(su,6,#(ad,this,that))))'
2+
#(ss,other,this,that)'
3+
4+
#(ds,hanoi,(#(gr,N,1,
5+
(#(cl,hanoi,this,#(cl,other,this,that),#(su,N,1))#(ps,from this to that##(dc,10))#(cl,hanoi,#(cl,other,this,that),that,#(su,N,1))),
6+
(#(ps,from this to that##(dc,10)))
7+
)))'
8+
#(ss,hanoi,this,that,N)'
9+
10+
#(hanoi,1,3,3)'

examples/pi.trac

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#(ds,pow10,(#(eq,P,0,1,(#(ml,10,#(cl,pow10,#(su,P,1)))))))'
2+
#(ss,pow10,P)'
3+
4+
#(ds,pow,(#(eq,E,0,1,(#(ml,Q,#(cl,pow,Q,#(su,E,1)))))))'
5+
#(ss,pow,Q,E)'
6+
7+
#(ds,padfrac,(#(gr,P,#(sl,S),(#(cl,padfrac,P,(0S))),S)))'
8+
#(ss,padfrac,P,S)'
9+
10+
#(ds,denominator,(#(ml,#(ad,#(ml,2,K),1),#(cl,pow,Q,#(ad,#(ml,2,K),1)))))'
11+
#(ss,denominator,K,Q)'
12+
13+
#(ds,term,(#(dv,scale,#(cl,denominator,K,Q))))'
14+
#(ss,term,scale,K,Q)'
15+
16+
#(ds,atan,(#(eq,K,#(ad,N,1),A,(#(cl,atan,#(ad,K,1),N,Q,scale,#(su,0,sign),#(ad,A,#(ml,sign,#(cl,term,scale,K,Q))))))) )'
17+
#(ss,atan,K,N,Q,scale,sign,A)'
18+
19+
#(ds,P,10)'
20+
#(ds,N5,14)'
21+
#(ds,N239,8)'
22+
23+
#(ds,S,#(cl,pow10,#(cl,P)))'
24+
25+
#(ds,T5,#(cl,atan,0,#(cl,N5),5,#(ml,16,#(cl,S)),1,0))'
26+
#(ds,T239,#(cl,atan,0,#(cl,N239),239,#(ml,4,#(cl,S)),1,0))'
27+
#(ds,T,#(su,#(cl,T5),#(cl,T239)))'
28+
29+
#(ps,#(dv,#(cl,T),#(cl,S)).#(cl,padfrac,#(cl,P),(#(su,#(cl,T),#(ml,#(dv,#(cl,T),#(cl,S)),#(cl,S))))))'

info/cowan/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist:
2+
shar READ.ME Makefile trac trac.1 trac.doc hanoi.trac | gzip >trac.shar.gz

info/cowan/READ.ME

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
This is an implementation of the macro-processing language TRAC.
2+
3+
The file `trac' is written in Perl, and implements a TRAC interpreter.
4+
You need to fetch a reasonably recent version of Perl (I used 4.0.36)
5+
if you don't have one already.
6+
7+
The file `trac.doc' is an ASCII file (not a man page) which describes the
8+
language tersely but completely, explaining both the traditional
9+
implementation and the Perl one.
10+
11+
The files `trac.1' and `Makefile' are there for the benefit of anal-retentives.
12+
The only `make' option is `make dist', which regenerates a shar file.
13+
14+
The file `hanoi.trac' implements the Towers of Hanoi. Commentary on it
15+
can be found in `trac.doc'. To run it from the `trac' interpreter, type:
16+
17+
#(ld,hanoi.trac)
18+
#(cl,hanoi,1,2,3)
19+
'

info/cowan/hanoi.trac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#(ds,the other,(##(su,6,##(ad,this,that))))
2+
#(ss,the other,this,that)
3+
#(ds,hanoi,(#(gr,N,1,
4+
(#(cl,hanoi,this,#(cl,the other,this,that),##(su,N,1))
5+
#(ps,from this to that(
6+
))
7+
#(cl,hanoi,#(cl,the other,this,that),that,##(su,N,1))),
8+
(#(ps,from this to that(
9+
))) )))
10+
#(ss,hanoi,this,that,N)

0 commit comments

Comments
 (0)