Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.21 KB

README.md

File metadata and controls

58 lines (44 loc) · 1.21 KB

TinyLISP

Build using Intellij Idea Scala plugin or sbt from the command line.

Building a Fat JAR

Running

sbt assembly

will produce a Fat JAR. See output for destination directory.

Run an example:

java -jar TinyLISP-assembly-0.1.0-SNAPSHOT.jar ../../examples/factorial.lsp

API Usage

// construct parser
val result: List[Item] = rep(whitespace(item()))(code) match
  case Accept(value, rem) => // parser accepted
    if (rem.forall(_.isWhitespace)) { // all input was read
      val instructions = CompilationManager().compile(value)
      val runtime = Runtime(
        mutable.Stack.from(instructions), // code stack
      )
      runtime.run()
      runtime.getResult // return stack as a List
    }
    else throw IllegalArgumentException(s"Unexpected input at the end: '$rem'")
  case Reject(_) => throw IllegalArgumentException()

Supported functions

Binary operations: +, -, *, /, <, >, =, cons.

Unary operations: car, cdr.

Built-ins: define, let, lambda, if, cons dot (.).

(= (cons a b) (a . b))

(define (funName arg1 arg2 ...)
    body)

(let (var1 var2) (val1 val2)
    body)

(lambda (arg1 arg2 ...)
    body)

(if cond
    trueBranch
    falseBrach)