近一周,学习Scheme有点上瘾,我对Scheme的兴趣源自其简单的语法,只有成对的()。学习Scheme,加上自己的Python编程,能很快的抄出一个Scheme解释器:
https://github.com/zhangyun00...
仔细看程序注释里的论文链接,介绍了绍python实现的scheme子集。我拿来改了改,加了for和while循环,break跳出循环,可以使用元组、列表,字典,class定义类。
运行python ZhScheme.py
ZhScheme> (tuple 1 2)
['tuple', 1, 2]
(1, 2)
ZhScheme> (list 5 6 6 )
['list', 5, 6, 6]
[5, 6, 6]
ZhScheme> (dict (list (list 5 6) (list 56 34 )))
['dict', ['list', ['list', 5, 6], ['list', 56, 34]]]
{5: 6, 56: 34}
ZhScheme> (sin 12)
['sin', 12]-0.5365729180004349ZhScheme> (sin 34)
['sin', 34]0.5290826861200238ZhScheme> (list 3 4 5)
['list', 3, 4, 5]
(3 4 5)
ZhScheme> (list (list 3 4 5) (list 456 45))
['list', ['list', 3, 4, 5], ['list', 456, 45]]
((3 4 5) (456 45ZhScheme> (define i 4)
['define', 'i', 4]
ZhScheme> ii4ZhScheme> (while (< i 23) (begin (print i) (set i (+ i 1))(if (eq? i 12) break))
)
['while', ['<', 'i', 23], ['begin', ['print', 'i'], ['set', 'i', ['+', 'i', 1]],
['if', ['eq?', 'i', 12], 'break']]]4567891011ZhScheme> (for (set i 23) (< i 45) (set i (+ i 2)) (begin (print i) (if (eq? i3) break)))
['for', ['set', 'i', 23], ['<', 'i', 45], ['set', 'i', ['+', 'i', 2]], ['begin'
['print', 'i'], ['if', ['eq?', 'i', 43], 'break']]]2325272931333537394143ZhScheme> (env)
['env']
variables ...
+ : <built-in function add>- : <built-in function sub>* : <built-in function mul>/ : <built-in function truediv>> : <built-in function gt>< : <built-in function lt>>= : <built-in function ge><= : <built-in function le>= : <built-in function eq>not : <built-in function not_>eq? : <built-in function is_>equal? : <built-in function eq>max : <built-in function max>min : <built-in function min>abs : <built-in function abs>round : <built-in function round>car : <function <lambda> at 0x0000cdr : <function <lambda> at 0x0000list : <function <lambda> at 0x000list-ref : <function <lambda> at 0append : <built-in function add>len : <built-in function len>map : <class 'map'>
print : <built-in function print>exit : Use exit() or Ctrl-Z plus R
open : <function <lambda> at 0x000call/cc : <function callcc at 0x00procedure? : <built-in function canull? : <function <lambda> at 0x00number? : <function <lambda> at 0xstring? : <function <lambda> at 0xlist? : <function <lambda> at 0x00struct? : <function <lambda> at 0xdict? : <function <lambda> at 0x00int : {}
__name__ : math
__doc__ : This module is always av
mathematical functions defined by th
__package__ :
__loader__ : <class '_frozen_impor
__spec__ : ModuleSpec(name='math',
orter'>, origin='built-in')acos : <built-in function acos>acosh : <built-in function acosh>asin : <built-in function asin>asinh : <built-in function asinh>atan : <built-in function atan>atan2 : <built-in function atan2>atanh : <built-in function atanh>ceil : <built-in function ceil>copysign : <built-in function copycos : <built-in function cos>cosh : <built-in function cosh>degrees : <built-in function degreerf : <built-in function erf>erfc : <built-in function erfc>exp : <built-in function exp>expm1 : <built-in function expm1>fabs : <built-in function fabs>factorial : <built-in function facfloor : <built-in function floor>fmod : <built-in function fmod>frexp : <built-in function frexp>fsum : <built-in function fsum>gamma : <built-in function gamma>gcd : <built-in function gcd>hypot : <built-in function hypot>isclose : <built-in function iscloisfinite : <built-in function isfiisinf : <built-in function isinf>isnan : <built-in function isnan>ldexp : <built-in function ldexp>lgamma : <built-in function lgammalog : <built-in function log>log1p : <built-in function log1p>log10 : <built-in function log10>log2 : <built-in function log2>modf : <built-in function modf>pow : <built-in function pow>radians : <built-in function radiasin : <built-in function sin>sinh : <built-in function sinh>sqrt : <built-in function sqrt>tan : <built-in function tan>tanh : <built-in function tanh>trunc : <built-in function trunc>pi : 3.141592653589793e : 2.718281828459045tau : 6.283185307179586inf : infnan : nani : 43struct ...
总之,这个scheme的目标是可以调用Python里的各种常用函数,以上env结果中的变量和函数都是你可以使用的。
现在可解释执行,以行为单位的文件了:
python ZhScheme.py test.s
test.s文件内容:
(quote must write code as lines)
(+ 2 5)
(define i 1)i(if (< i 19) (print (+ i 1)))
(while (< i 23) (begin (print i) (set i (+ i 1))(if (eq? i 12) break)))
(for (set i 23) (< i 45) (set i (+ i 2)) (begin (print i) (if (eq? i 43) break)))
(define f (open test.ss r))
((. f read))
(define define 12)
define
(class point (list (list n 2)(list m (lambda x (* 2 x)))))
(define x (point))
(. x n)
((. x m) 4)
有在ZhScheme加入静态数据类型的想法,也就是说,除了能define x 12之外,还可以使用 int y 34,定义一个整形变量,如果set y 2.3会导致一个类型错误 -- 把浮点值付给了整型值,多数静态类型语言都是这么做的。