1
5
mirror of https://github.com/vinc/moros.git synced 2024-06-17 22:47:06 +00:00
moros/doc/lisp.md
Vincent Ollivier 892b697bd3
Add BigInt support to Lisp (#415)
* Add bigint support to Lisp

* Remove Box

* Replace BigInt by i64

* Add back big int

* Work around big int errors

* Print floats with a dot

* Add conversion to f64

* Use Number#parse instead of double

* Add trigo functions to Number

* Add conversion from number to byte

* Add addition to Number

* Add multiplication to Number

* Add negation and substraction to Number

* Add division to Number

* Add reminder to Number

* Add pow to Number

* Fix tests

* Re-enable BigInt

* Add parsing and printing of BigInt

* Add sign

* Add operations on BigInt

* Fix compilation issues

* Add support for add and mul overflow

* Fix bigint conversion to and from str

* Add number-type function

* Add tests

* Add support for pow overflow

* Fix tests

* Add more checks for overflow

* Add check for division by zero

* Fix typo

* Return inf for large exponential operations

* Check for division by zero in modulo

* Add shift operations

* Rewrite comparisons

* Add lazy eval to cond expressions

* Add set fonction

* Add loop function

* Add pi example

* Add builtin pi example to shell

* Update allocation error messages

* Rewrite number conversions

* Remove debug output from pi example

* Move pi command to a dedicated file

* Rewrite bytes->number and number->bytes

* Update doc

* Move op impl to Number

* Add macros to dry code

* Add more macros

* Run clippy
2022-10-17 20:58:08 +02:00

2.5 KiB

MOROS Lisp

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 and Clojure.

It 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.

In version 0.2.0 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.

Types

  • Basics: bool, list, symbol, string
  • Numbers: float, int, bigint

Seven Primitive Operators

  • quote (with the ' syntax)
  • atom (aliased to atom?)
  • eq (aliased to eq?)
  • car (aliased to first)
  • cdr (aliased to rest)
  • cons
  • cond

Two Special Forms

  • label (aliased to define and def)
  • lambda (aliased to function, fun, and fn)

Additional Builtins

  • defun (aliased to defn)

  • apply

  • type

  • string

  • string->number

  • string->bytes and bytes->string

  • number->bytes and bytes->number

  • regex-find

  • system

  • load

  • Arithmetic operations: +, -, *, /, %, ^

  • Trigonometric functions: acos, asin, atan, cos, sin, tan

  • Comparisons: >, <, >=, <=, =

  • Boolean operations: not, and, or

  • String operations: lines

  • File IO: read-file, read-file-bytes, write-file-bytes, append-file-bytes

Core Library

  • nil, nil?, eq?
  • atom?, string?, boolean?, symbol?, number?, list?, function?, lambda?
  • first, second, third, rest
  • map, reduce, append, reverse
  • string-join
  • read-line, read-char
  • print, println
  • write-file, append-file
  • uptime, realtime
  • regex-match?

Usage

The interpreter can be invoked from the shell:

> lisp
MOROS Lisp v0.4.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")

(define (fibonacci n)
  (cond
    ((< n 2) n)
    (true (+ (fibonacci (- n 1)) (fibonacci (- n 2))))))

(println
  (cond
    ((nil? args) "Usage: fibonacci <num>")
    (true (fibonacci (string->number (car args))))))

Would produce the following output:

> lisp /tmp/lisp/fibonacci.lsp 20
6755