几天前在 Hacker News 上看到一个很有意思的语言 Rockstar (Rockstar: A programming language where programs are also song lyrics | Hacker News),感觉它的文法很有趣,程序看起来很像歌词,也有着一种硬摇滚的感觉,于是便一时兴起抽出一天时间用 OCaml 写了一个解释器 rockstar-ml https://github.com/lkwq007/rockstar-ml。
rockstar-ml 基本上支持当前 specification 中的主要特性。由于还有其他事情要做,我没有实现 dec64 格式数值的支持。同时,由于 ocamllex
不支持 UTF-8,这个解释器也不符合 spec 中文件格式的要求。不过在其他方面,rockstar-ml 的表现都还可以。
写下来的总体的感觉是 ocamllex
和 menhir
真的挺好用,比起 lex
和 yacc
用起来感觉方便多了。用 OCaml 的模式匹配来解析抽象语法树也特别方便。之后有空的话,大概会实现 REPL 和编译器吧。
一个简单的 demo:
源码:
Midnight takes your heart and your soul
While your heart is as high as your soul
Put your heart without your soul into your heart
Give back your heart
Desire is a lovestruck ladykiller
My world is nothing
Fire is ice
Hate is water
Until my world is Desire
Build my world up
If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
Shout "FizzBuzz!"
Take it to the top
If Midnight taking my world, Fire is nothing
Shout "Fizz!"
Take it to the top
If Midnight taking my world, Hate is nothing
Say "Buzz!"
Take it to the top
Whisper my world
AST:
Midnight(your heart, your soul, )
{
WHILE(((Var: your heart)>=(Var: your soul)))
{
your heart=((Var: your heart)-(Var: your soul))
}
Return: (Var: your heart)
}
Define: (Var: Desire,100.)
Define: (Var: my world,Null)
Define: (Var: Fire,3.)
Define: (Var: Hate,5.)
UNTIL(((Var: my world)=(Var: Desire)))
{
Inc: my world
IF((((Call Midnight:[(Var: my world),(Var: Fire),])=(Val: Null))&&((Call Midnight:[(Var: my world),(Var: Hate),])=(Val: Null))))
{
Print: (Val: "FizzBuzz!")
Continue
}
ELSE
{
Nop
}
IF(((Call Midnight:[(Var: my world),(Var: Fire),])=(Val: Null)))
{
Print: (Val: "Fizz!")
Continue
}
ELSE
{
Nop
}
IF(((Call Midnight:[(Var: my world),(Var: Hate),])=(Val: Null)))
{
Print: (Val: "Buzz!")
Continue
}
ELSE
{
Nop
}
Print: (Var: my world)
}
运行结果:
1.2.Fizz!4.Buzz!Fizz!7.8.Fizz!Buzz!11.Fizz!13.14.FizzBuzz!16.17.Fizz!19.Buzz!Fizz!22.23.Fizz!Buzz!26.Fizz!28.29.FizzBuzz!31.32.Fizz!34.Buzz!Fizz!37.38.Fizz!Buzz!41.Fizz!43.44.FizzBuzz!46.47.Fizz!49.Buzz!Fizz!52.53.Fizz!Buzz!56.Fizz!58.59.FizzBuzz!61.62.Fizz!64.Buzz!Fizz!67.68.Fizz!Buzz!71.Fizz!73.74.FizzBuzz!76.77.Fizz!79.Buzz!Fizz!82.83.Fizz!Buzz!86.Fizz!88.89.FizzBuzz!91.92.Fizz!94.Buzz!Fizz!97.98.Fizz!Buzz!
1
cxxcoding 2018-08-06 09:03:56 +08:00
太高深了
|
2
go2sleep 2018-08-06 10:18:56 +08:00
有点牛逼啊
|
3
Death OP @cxxcoding
Rockstar 确实很神奇,感觉完全可以算一种 esoteric programming language |
4
laidycy 2018-08-07 16:26:01 +08:00
为什么我第一想到的是 R 星转行了?
|
5
RqPS6rhmP3Nyn3Tm 2018-08-08 00:35:42 +08:00 via iPhone
GTA6 靠你了
|