Lisp interpreter implemented wit

2017-09-26  本文已影响0人  WOWSCpp

You can find the source code here

Architecture

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
上一篇 下一篇

猜你喜欢

热点阅读