美文网首页
Lisp interpreter implemented wit

Lisp interpreter implemented wit

作者: WOWSCpp | 来源:发表于2017-09-26 21:44 被阅读0次

    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

    相关文章

      网友评论

          本文标题:Lisp interpreter implemented wit

          本文链接:https://www.haomeiwen.com/subject/guuysxtx.html