Lisp interpreter implemented with C++

You can find the source code here

Architecture

  • Tokenizer
  • Text Parser
  • BuiltIn
  • Environment
  • AST
  • Macro Compiler
  • Evaluator
Lisp interpreter implemented with C++_第1张图片
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))
    )
  )
)
Lisp interpreter implemented with C++_第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)
      )
    )
  )
)
Lisp interpreter implemented with C++_第3张图片
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)
Lisp interpreter implemented with C++_第4张图片
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))
Lisp interpreter implemented with C++_第5张图片
result of function overloading

你可能感兴趣的:(Lisp interpreter implemented with C++)