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. And SExpression.java is the implementation of S-expression
, which is the core of JCScheme.
FYI, diagram below is the S-expression
of (+ 1 2 (* 3 4))
behind 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:
int
,bool
,function
- keyword:
if
,def
,lambda
- literal:
true
,false
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
More new features can be found at Change Log below。
- 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
- 0.0.5-SNAPSHAT, 2015/10/11
- new datatype:
string
- new builtin function
str=?
, used for test equality of two string
- new datatype:
>> "abc"
"abc"
>> (def a "hello")
null
>> (def b "hello")
null
>> (str=? a b)
true
>> (str=? a "hello")
true
MIT License © Jiacai Liu