After programming so many years, I finally write my own language !
JCScheme = Jiacai's Scheme ๐
JCScheme use S-expression internally to represent AST like any other Scheme.
SExpression.java is the implementation of S-expression
, which is the core part of JCScheme.
FYI, diagram below is the S-expression
of (+ 1 2 (* 3 4))
expressed inside SExpression.java
.
More explanations can be found in my Chinese blog ใๆ็็ฌฌไธไธช็ฉๅ ท่ฏญ่จ JCScheme ้ฎไธไบใใ
git clone [email protected]:jiacai2050/JCScheme.git
cd JCScheme; mvn clean package
java -jar target/JCScheme-*.jar
You can install rlwrap
to support line editing, persistent history and completion.
# ubuntu
sudo apt-get install rlwrap
# centos
sudo yum install rlwrap
# Mac
brew install rlwrap # for Homebrew
port install rlwrap # for MacPorts
Then, run JCScheme like this
rlwrap java -jar target/JCScheme-*.jar
The very first version of JCScheme support:
- datatype: Number, Bool, Function
- keyword:
if
,def
,lambda
- literal:
true
,false
More new features can be found at Change Logใ
rlwrap java -jar target/JCScheme-*.jar
>> (* 2 3 4 5)
120
>> (def a 4)
null
>> (def b 5)
null
>> (if (> a b) a b)
5
>> (def max (lambda (a b) (if (> a b) a b)))
null
>> (def c (max a b))
null
>> c
5
- 0.0.1-SNAPSHAT, 2015/10/03
- 0.0.2-SNAPSHAT, 2015/10/04
- support immediately invoked function
>> ((lambda (a b) (if (> a b) a b)) 3 4)
4
>> (lambda (a b) (if (> a b) a b))
Function :
Args : [a, b]
Body : ( if ( > a b ) a b )
- 0.0.3-SNAPSHAT, 2015/10/05
- new datatype:
pair
andlist
- new keyword:
cons
ใlist
- new builtin function:
car
ใcdr
ใempty?
- a new literal:
nil
, which stands for empty list
- new datatype:
>> (cons 1 2)
[1, 2]
>> (car (cons 1 2))
1
>> (cdr (cons 1 2))
2
>> (list) # identical to nil
nil
>> (list 1 2)
(1, 2)
>> (car (list 1 2))
1
>> (cdr (list 1 2)) # aware of the difference between this and (cdr (cons 1 2))
(2)
>> (empty? (cdr (list 1 2)))
false
>> (empty? (cdr (cdr (list 1 2))))
true
>> (empty? nil)
true
>> (cons 1 nil)
(1)
>> (list 1) # identical to (cons 1 nil)
(1)
# closure demo
>> (def adder (lambda (x) (lambda (y) (+ x y))))
null
>> (def add2 (adder 2))
null
>> (add2 3)
5
# currying demo
>> (def myadd (lambda (x y) (+ x y)))
null
>> (myadd 3)
Function :
Args : [y]
Body : [( + x y ) ]
>> ( (myadd 3) 4)
7
# function scope demo
>> (lambda () (def a 1) a)
Function :
Args : []
Body : [( def a 1 ) , a]
>> ((lambda () (def a 1) a))
1
>> a # variable a isn't in global scope
Error token: a
MIT License ยฉ Jiacai Liu