A minimalist Lisp interpreter is available in MOROS to extend the capabilities of the Shell.
MOROS Lisp is a Lisp-1 dialect inspired by Scheme, Clojure, and Ruby!
- Basics:
bool
,list
,symbol
,string
- Number:
float
,int
,bigint
- Number:
2.5
,-25
,255
,0xFF
,0xDEAD_C0DE
,0b101010
- String:
"Hello, World!"
- Escape:
\b
,\e
,\n
,\r
,\t
,\"
,\\
quote
(abbreviated with'
)quasiquote
(abbreviated with`
)unquote
(abbreviated with,
)unquote-splice
(abbreviated with,@
)splice
(abbreviated with@
)atom?
equal?
(aliased toeq?
)head
tail
cons
if
cond
while
variable
(aliased tovar
)function
(aliased tofun
)macro
(aliased tomac
)set
define
(aliased todef
and equivalent todefine-function
)define-function
(aliased todef-fun
)define-macro
(aliased todef-mac
)apply
do
doc
eval
expand
load
type
,number/type
(aliased tonum/type
),parse
string
(aliased tostr
)string->number
andnumber->string
(aliased tostr->num
andnum->str
)string->binary
andbinary->string
(aliased tostr->bin
andbin->str
)number->binary
andbinary->number
(aliased tonum->bin
andbin->num
)regex/find
shell
(aliased tosh
)- Arithmetic operations:
+
,-
,*
,/
,^
,rem
(aliased to%
),trunc
- Trigonometric functions:
acos
,asin
,atan
,cos
,sin
,tan
- Comparisons:
>
,<
,>=
,<=
,=
- Enumerable:
length
(aliased tolen
),put
,get
,slice
,contains?
- String:
string/trim
andstring/split
(aliased tostr/trim
andstr/split
) - List:
list
,concat
,chunks
,sort
,unique
(aliased touniq
) - Dict:
dict
- File:
file/exists?
,file/size
,file/open
,file/close
,file/read
,file/write
- Net:
host
,socket/connect
,socket/listen
,socket/accept
nil
,nil?
,list?
,empty?
boolean?
(aliased tobool?
),string?
(aliased tostr?
),symbol?
(aliased tosym?
),number?
(aliased tonum?
)function?
(aliased tofun?
),macro?
(aliased tomac?
)abs
,mod
,min
,max
first
,second
,third
,last
,rest
,push
map
,reduce
,reverse
(aliased torev
),range
,filter
,reject
,intersection
not
,and
,or
let
string/join
(aliased tostr/join
),lines
,words
,chars
regex/match?
dirname
,filename
read
,write
,append
read-binary
,write-binary
,append-binary
read-line
,read-char
clock/boot
,clock/epoch
p
,print
,eprint
,error
floor
,ceil
,round
atom
,eq
,label
,lambda
,progn
,begin
car
,cdr
,caar
,cadr
,cdar
,cddr
The interpreter can be invoked from the shell:
> lisp
MOROS Lisp v0.7.0
> (+ 1 2 3)
6
> (quit)
And it can execute a file. For example a file located in /tmp/lisp/fibonacci.lsp
with the following content:
(load "/lib/lisp/core.lsp")
(def (fibonacci n)
(if (< n 2) n
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
(print
(if (nil? args) "Usage: fibonacci <num>"
(fibonacci (str->num (head args)))))
Would produce the following output:
> lisp /tmp/lisp/fibonacci.lsp 20
6755
(load "/lib/lisp/core.lsp")
(print "Hello, World!")
(var foo 42) # Variable definition
(set foo (+ 40 2)) # Variable assignement
(var double (fun (x) (* x 2))) # Function definition
(def (double x) (* x 2)) # Shortcut
(double foo) # => 84
(def-mac (++ x) # Macro definition
`(set ,x (+ ,x 1)))
(var i 0)
(while (< i 10)
(++ i))
(= i 10) # => true
(def (map f ls)
"Apply function to list"
(if (nil? ls) nil
(cons
(f (first ls))
(map f (rest ls)))))
(doc map) # => "Apply function to list"
(var bar (quote (1 2 3)))
(var bar '(1 2 3)) # Shortcut
(map double bar) # => (2 4 6)
(map (fun (x) (+ x 1)) '(4 5 6)) # => (5 6 7)
(var name "Alice")
(str "Hello, " name) # => "Hello, Alice"
(^ 2 64) # => 18446744073709551616
- Add
dirname
,filename
,eprint
, anderror
functions - Rename
uptime
toclk/boot
andrealtime
toclk/epoch
- Add
floor
,ceil
, andround
functions
- Add binary and hexadecimal number literals
- Test for truthiness (neither
false
nornil
) in conditions ofif
andwhile
- Rename
nth
toget
- Add
empty?
,reject
,put
,push
, andhost
functions` - Add
dict
type - Use
/
instead of.
as namespace separator - Add
number->string
(aliased tonum->str
) with an optional radix argument
- Add file, number, string, and regex namespaces
- Add socket functions
- Rename or add aliases to many functions
- Add full support for line and inline comments
- Add params to function representations
- Add docstring to functions
- Rewrite a lot of the code
- Add integer and big integer support
- Add tail call optimization (TCO)
- Add macro support
- Add new functions
- Rewrite parts of the code
- Add new functions and examples
- Rewrite the evaluation code
- Add new functions
- Add a core library
The whole implementation was refactored and the parser was rewritten to use Nom. This allowed the addition of strings to the language and reading from the filesystem.
MOROS Lisp started from Risp and was extended to include the seven primitive operators and the two special forms of John McCarthy's paper "Recursive Functions of Symbolic Expressions and Their Computation by Machine" (1960) and "The Roots of Lisp" (2002) by Paul Graham.