Skip to content

Python, Lua, and Javascript with s-expression syntax and macros.

Notifications You must be signed in to change notification settings

bsummer4/moonlisp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Moonlisp
This is a simple lisp implementation that uses tables and strings instead of
lists and symbols as it's core data structures. It compiles to Lua, Javascript
and Python.

## Semantics
Types: (t ::= bool func str num foreign table)
All functions are of the form t→t.
Tables are mutable and map from t→t.
All values are immutable besides tables and foreigns.

## Basic Syntax
Reserved characters:: ““”#<>[]{}()":.$→ \t\r\n”
All-non-reserved characters can be used raw, in identifiers.
These are also valid identifiers:: ‘#’ ‘$’ ‘.’
Syntax for primitive data structures:: #true #false #nil sym <sym> 4.5
Keys in ‘key→val’ forms must be primitive data structures.
Syntactic forms:: (form p1 k1→v1)

## Special Forms with Syntax
Quoted Strings:: (_str foo) "foo" “foo”
Table literals:: (_tbl slot1 slot2 k1→v1) {slot1 slot2 k1→v1}
Function calls:: (_call f arg1 k1→v1) [f arg1 k1→v1]
Method calls:: (_call (_meth obj key) arg) [obj:key arg]
Bound Methods:: (def ok (_meth obj key)) (def ok obj:key)
Method Functions:: (_call (_methfn key) obj arg) [:key obj arg]
Table lookups:: (_get tbl key) tbl.key [.tbl key] [. "tbl" key]
Table Getters:: (_call (_getter key) tbl) [.key tbl]
Foreign Statements:: (_fstmt "require io") $“require io”
Reference a global variable:: (_fvar window) $window
Call a foreign method:: (_fmeth obj method a b c) $[obj:method a b c]
Call a foreign function:: (_fcall fn a b c) $[fn a b c]
Return multiple values:: (_fvalues a b c) $(a b c)

## More Special Forms
Primitive lisp forms:
	Lambdas:: (λ name exp) (fn name exp)
	Lambdas with multiple arguments:: (λ $(a b c) exp)
	Pattern Matching:: (match exp (pattern exp) ...)
	Imperative Sequences:: (do exp ...)

Patterns are just data-literals mixed with symbols::
	(match {1 {2} 3} ({1 b c} {b c}))
		This yields: {{2} 3}

## Supported Operations
. lookup :: {table t} → t         -- Table lookup
! mut    :: {table t t} → t       -- Table mutation
apply    :: {func t} → t          -- Call function explicitly.
keys     :: {table} → table       -- Table keys
# len    :: {table} → num         -- Array length
append   :: {table table} → table -- Array concatenation
not      :: {bool} → bool
~        :: {num} → num           -- num * ~1
+        :: {num num} → num
-        :: {num num} → num
* ·      :: {num num} → num
/ ÷      :: {num num} → num
% mod    :: {num num} → num
^ pow    :: {num num} → num
= eq     :: {t t} → bool          -- Equality
~= neq   :: {num num} → bool
<= ≤     :: {num num} → bool
>= ≥     :: {num num} → bool
<        :: {num num} → bool
>        :: {num num} → bool
..       :: {str str} → str       -- Concatenation.
nil?     :: {t} → bool
bool?    :: {t} → bool
func?    :: {t} → bool
str?     :: {t} → bool
num?     :: {t} → bool
foreign? :: {t} → bool
table?   :: {t} → bool

## Example Code
(do
	(match {1 2 k→c} ({a b k→c} (do
		[write a b c])
	(match {1 2 k→3} (e (do
		[write e])))
	(match (λx (match x ({a b k→c} [write a b c])))
		(hello [hello 1 2 k→3]))
	(match {
		(λ pair pair)
		(λ pair (match pair ({car cdr} car)))
		(λ pair (match pair ({car cdr} car)))}
		({cons car cdr} [= [car [cons car cdr]]])))))

About

Python, Lua, and Javascript with s-expression syntax and macros.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published