Sunday, August 29, 2021

Pico8Lisp: A toy Lisp interpreter for PICO-8

Pico8lisp is a small lisp interpreter built on PICO-8 virtual machine!

The shell is currently inflexible. If your environment crashes, restart it with Command (⌘)-R

You can find a walkthrough of the programming language features on my github here: https://github.com/andrewguy9/pico8lisp

Copy and paste the snippet below into your HTML.

Language Specification:

Literals

Numbers

Literal numbers are just the numbers themselves.

Symbols

Symbols are used as names for forms/functions/constants/just about anything. Symbols can be written as series of letters a-z. More on that later.

Nil

Nil can be written as nil

Note that nil evaluated to an empty list (). That's because the symbol nil is bound to the empty list. This is the only false value in picolisp!

Boolean

Everything except nil is true.
But we do have a special value to represent truth.

Expressions

Calling Functions

The first element of a list is assumed to be a function.

You can even nest list expressions.

At the moment, we only have +.

Clear screen

If your repl gets confusing, clear the screen

Lists

You can write a list literal with the quote function.

> (quote (1 2 3))
  (1 2 3)

Quote prevents the list from being evaluated!

There is a special quote operator to make this to read.

Symbols

By default symbols are evaluated, but you can prevent that with quote.

Def

You can define global symbols with a value.

> (def age 38)
  38

> age
  38

local bindings

While def symbols are globally available, you can make locally scoped definitions with let

> (let (x 3 y 2) (+ x y))
  5

You can define functions

Use defn to make a function globally accessible.

> (defn inc (x) (+ x 1))
  (fn (x) (+ x 1))

> (inc 2)
  3

If statements

Branches are supported by if statements.

> (if (= 1 1) 'happy 'sad)
  happy
> (if (= 1 2) 'happy 'sad)
  sad

If else if..., else statements are supported via cond

> (def x 1)
> (cond (= x 0) 'zero (= x 1) 'one $t 'other)
  one


from Hacker News https://ift.tt/3ztzG70

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.