Following: https://buildyourownlisp.com/
This LZP programming language is designed for simplicity and flexibility, enabling users to define functions, perform mathematical operations, manipulate strings, and handle errors. The language supports standard Lisp constructs, including S-expressions and Q-expressions, and provides built-in functions for various common tasks.
Starting an interactive shell
./lzp
Running a file
./lzp ./examples/pi.lzp
Lzp had a build in prelude that can be disabled by passing the -n
flag.
Have a look in the prelude.lzp file to see what is included.
- NUM: Integer numbers (long)
- FLT: Floating-point numbers (double)
- STR: Strings
- SYM: Symbols
- SEXPR: S-expressions
- QEXPR: Q-expressions
- FUN: Functions (both built-in and user-defined)
- ERR: Error messages
Converts arguments into a Q-expression.
lzp> list 42 {1 b 1.45} list
{42 {1 b 1.45} <list>}
Returns the first element of a Q-expression or string.
lzp> head {+ 3 - 4}
{x}
lzp> head "exmpl"
"e"
Returns all elements except the first of a Q-expression or string.
lzp> tail {+ 3 - 4}
{3 - 4}
lzp> tail "exmpl"
"xmpl"
Evaluates a Q-expression as an S-expression.
lzp> eval {+ 1 1}
2
Concatenates Q-expressions or strings.
lzp> join {+ 1} {2 3}
{+ 1 2 3}
lzp> join "test" "ing"
"testing"
Returns the length of a Q-expression or string.
lzp> len {+ 1 2 3}
4
lzp> len "testing"
7
lzp> + 1 -.5
0.5
lzp> - 10 .2
9.8
lzp> - 5
-5
lzp> * 10 2
20
lzp> 10 .5
5
lzp> / 10 2
5
lzp> / 0 10
0
lzp> % 13 5
3
lzp> ** 2 8
256
lzp> ** 225 .5
15
lzp> min -4 .2 10
-4
lzp> min -4 .2 10
10
Defines global variables.
lzp> def {x} 123
()
lzp> + x 100
223
Assigns values to variables in the current scope.
lzp> def {i} -1
lzp> def {j} -1
lzp> (\ {_} {def {i} 99}) ()
lzp> (\ {_} {= {j} 99}) ()
lzp> i
99
lzp> j
-1
lzp> > -10 2.1
0
lzp> < -10 2.1
1
lzp> >= -10 2.1
0
lzp> >= 2 2
1
lzp> <= -10 2.1
1
lzp> <= 2 2
1
lzp> == 10 10
1
lzp> == 0 12
0
lzp> != 10 10
0
lzp> != 0 12
1
lzp> if 1 { "true" } { "false" }
"true"
lzp> if 0 { "true" } { "false" }
"false"
lzp> ! 0
1
lzp> ! 5
0
lzp> || 0 0
0
lzp> || 0 0 1 0
1
lzp> && 1 0
0
lzp> && 1 1 1 1
1
Exits the program with a given status code.
lzp> exit 1
echo $?
1
Prints the current environment state.
lzp> def {x} 213
lzp> state ()
+ = <+>
- = <->
* = <*>
/ = </>
% = <%>
...
x = 213
Defines lambda functions.
The &
syntax allows for varible amount of vars as input.
lzp> (\ {a b} {+ a b}) 1 2
3
lzp> def {add} (\ {a b} {+ a b})
lzp> add 1 2
3
lzp> def {temp} (add 1)
lzp> temp 2
3
lzp> def {add} (\ {& l} {eval (join (list +) l)})
lzp> add 4
4
lzp> add 1 2 3 4 5
15
Loads and evaluates code from a file.
lzp> load "./examples/pi.lzp"
Calculating...
3.14149265359004
()
Parses and evaluates a string as code.
lzp> read "(def {r} 100); comment\n (show \"aaa\")"
aaa
()
lzp> r
100
Prints values, can be of any type.
lzp> print add
(\ {& l} {eval (join (list +) l)})
lpz> print +
<+>
lpz> print "string"
"string"
lzp> print "string\ncool"
"string\ncool"
Displays strings.
lzp> print "string"
string
lzp> show "string\ncool"
string
cool
Returns an error message.
lzp> error "not good"
Error: not good