# 在 Julia 当中实现 Cirru 解释器的初步想法

https://github.com/Cirru/CirruSepal.jl

````text````julia> ex2 = Expr(:call, :+, 1, 1)
:(1 + 1)
``````

### 元编程

• Symbol 类型

Symbol 是和 String 不一样的数据类型, 两者共同点是数据不可修改

http://stackoverflow.com/questions/23480722/what-is-a-symbol-in-julia/...

````text````julia> a = 1
1

julia> eval(a)
1

julia> eval(:a)
1
``````
• Quoting

Quote 可以得到表达式的 `expr` 对象, 对应表达式的 AST.

````text````julia> a = Expr(:call, :+, 1, 2)
:(1 + 2)

julia> b = :(1 + 2)
:(1 + 2)

julia> a == b
true

julia> eval(a)
3

julia> eval(b)
3
``````
• `parse` 函数

`parse` 可以将一段字符串解析成 AST, 或者也能看出了就是 Quote:

````text````julia> a = :(1 + 2)
:(1 + 2)

julia> c = parse("1 + 2")
:(1 + 2)

julia> a == c
true
``````

``````julia> dump(c)
Expr
args: Array(Any,(3,))
1: Symbol +
2: Int64 1
3: Int64 2
typ: Any
``````

``````julia> eval(c)
3
``````

### 思路

``````julia> a = parse("1 + 2")
:(1 + 2)

julia> b = dump(a)
Expr
args: Array(Any,(3,))
1: Symbol +
2: Int64 1
3: Int64 2
typ: Any
``````

``````julia> c = Expr(:call, :+,  1, 2)
:(1 + 2)

julia> d = eval(c)
3
``````

https://github.com/jakebolewski/JuliaParser.jl/blob/master/src/parser....

Like Lisp, Julia represents its own code as a data structure of the language itself. Since code is represented by objects that can be created and manipulated from within the language, it is possible for a program to transform and generate its own code. This allows sophisticated code generation without extra build steps, and also allows true Lisp-style macros operating at the level of abstract syntax trees.

``````julia> a = parse("1 + 2; 2 + 3")
:(\$(Expr(:toplevel, :(1 + 2), :(2 + 3))))

julia> dump(a)
Expr
args: Array(Any,(2,))
1: Expr
args: Array(Any,(3,))
1: Symbol +
2: Int64 1
3: Int64 2
typ: Any
2: Expr
args: Array(Any,(3,))
1: Symbol +
2: Int64 2
3: Int64 3
typ: Any
typ: Any
``````

### 实现问题

Cirru 在 JavaScript 当中是先转化成 JSON 对象, 然后操作的
Julia 当然少不了操作 JSON 的库, 我最初的思路是从 JSON 开始
https://github.com/JuliaLang/JSON.jl

http://en.wikibooks.org/wiki/Introducing_Julia/Arrays_and_tuples
Array 限定了元素类型一致, 可以不断增长, 而 Tuple 允许多种类型, 但不能增长

1. 生成基于缩进跟括号的树, 其中还没有出来 `\$``,` 形成的转换
2. 识别 `\$``,` 对树进行 desuger 的转换

https://github.com/drmeister/clasp
https://github.com/artagnon/rhine-ml
https://github.com/mylesmegyesi/lisp-compiler-llvm
https://github.com/eudoxia0/corvus

http://www.justinablog.com/archives/1604

``````julia> {"1", {"2", "3", {"4"}}}
2-element Array{Any,1}:
"1"
{"2","3",{"4"}}
``````

##### 题叶

ClojureScript 爱好者.

497 人关注
251 篇文章