Lisp interpreter implemented with C++

You can find the source code here

Architecture

  • Tokenizer
  • Text Parser
  • BuiltIn
  • Environment
  • AST
  • Macro Compiler
  • Evaluator
Architecture

Features

true * == > or list rm_head cons foldl
false / <= def if push_back rm_tail max map
+ % >= set while head nth_element min
- ! < and func tail size filter

Sample of Abstract Syntax Tree

Fibonacci sequence recuesive version

(function fib (n) 
  (if 
    (< n 2) 
    n 
    (+ 
      (fib (- n 1)) 
      (fib (- n 2))
    )
  )
)
Sample of Abstract Syntax Tree

Sample Results

Fibonacci sequence iterative version

(def a (list 0 1 1))
(function fib(n) 
  (while 
    (< (size a) n)  
      (push_back a  
      (+
        (nth_element(- (size a) 1) a)
        (nth_element(- (size a) 2) a)
      )
    )
  )
)
result of iterative fibonacci sequence

Function and Higher Order Function

Example of the filter function.

(def a (list 1 2 3 4))
(function even (x)
  (if
    (== (% x 2) 0)
    true
    false
   )
)
(filter even a)
result of filter

Example of foldl function.

(def a (list 1 2 3 4))
(function sum(x y) (+ x y))
(foldl sum 10 a)
result of fold

You can also implement your own logic for map, foldl and filter function.

Function Overloading

Example of sum function overloading.

(function add (a b) (+ a b))
(function add (a b c) (+ a b c))
result of function overloading
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容