add lisp examples

This commit is contained in:
zongor 2025-07-20 23:40:15 -04:00
parent 02702315cb
commit 9326d12585
10 changed files with 54 additions and 34 deletions

View File

@ -119,7 +119,7 @@ typedef enum {
OP_UINT_TO_REAL, /* utor : dest = src1 as f32 */
OP_ADD_REAL, /* addr : dest = src1 + src2 */
OP_SUB_REAL, /* subr : dest = src1 - src2 */
OP_MUL_REAL, /* ulr : dest = src1 * src2 */
OP_MUL_REAL, /* mulr : dest = src1 * src2 */
OP_DIV_REAL, /* divr : dest = src1 / src2 */
OP_JEQ_REAL, /* jeqr : jump to address dest if src1 as real == src2 as real */
OP_JGE_REAL, /* jgtr : jump to address dest if src1 as real >= src2 as real */

View File

@ -1,8 +1,8 @@
main:
loadi $0 1 ;load 1 into r0
pushi $0 ;push onto stack
loadi $0 1 ;1
pushi $0
lodi $0 1 ;load 1 into r0
pshi $0 ;push onto stack
lodi $0 1 ;1
pshi $0
call &add ; call memory location for tag "add"
popi $0 ;get value from function call "add"
itos $1 $0 ;convert int to string
@ -13,5 +13,5 @@ add:
popi $0 ; int a
popi $1 ; int b
addi $2 $1 $0 ; a + b
pushi $2
return ; actually do the return
pshi $2
retn ; actually do the return

5
test/add.lisp Normal file
View File

@ -0,0 +1,5 @@
(defun add (int a int b)
(return (+ a b)))
(let ((sum (add 1 1)))
(print (itos sum)))

View File

@ -1,6 +1,6 @@
main:
loadi $0 35
pushi $0
lodi $0 35
pshi $0
call &fib
popi $0
itos $1 $0
@ -8,22 +8,22 @@ main:
halt
fib:
popi $0
loadi $1 2
loadi $2 &base_case
jlt $2 $0 $1
loadi $2 2
lodi $1 2
lodi $2 &base_case
jlti $2 $0 $1
lodi $2 2
subi $4 $0 $3
pushi $4
pshi $4
call &fib
load $2 1
lodi $2 1
subi $4 $0 $3
pushi $4
pshi $4
call &fib
popi $4
popi $5
addi $6 $5 $4
pushi $6
return
pshi $6
retn
base_case:
pushi $0
return
pshi $0
retn

7
test/fib.lisp Normal file
View File

@ -0,0 +1,7 @@
(defun fib (int n)
(if (n < 2)
(return n)
(return (+ (fib (- n 2))
(fib (- n 1))))))
(puts (itos (fib 35)))

View File

@ -1,4 +1,4 @@
fn fib(n) {
fn fib(int n) {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}

1
test/hello.lisp Normal file
View File

@ -0,0 +1 @@
(puts "nuqneH 'u'?")

View File

@ -1,20 +1,17 @@
.memory
.ascii Enter a string:
.code
main:
loadf $0 5.0
loadi $1 50000
loadi $2 0
loadi $3 -1
loop:
loadf $5 5.0
lodr $0 5.0
lodi $1 50000
lodi $2 0
lodi $3 -1
loop_body:
lodr $5 5.0
addr $0 $0 $5
addi $1 $1 $3
loadu $4 &loop
lodu $4 &loop_body
jgei $4 $1 $2
rtou $1 $0
loadu $5 0
strs "Enter a string:"
ladu $5 0
puts $5
gets $2
utos $3 $1

9
test/loop.lisp Normal file
View File

@ -0,0 +1,9 @@
(let ((a 5.0))
(do (i (5000 0 -1))
(set a (+ a 5.0)))
(let ((b (itou a)))
(puts "Enter a string:")
(let ((user-string (gets)))
(puts (rtos a))
(puts (utos b))
(puts user-string))))

View File

@ -1,8 +1,9 @@
let a = 5.0;
do (let i = 50000, 0, -1) {
a += 5.0;
a = a + 5.0;
}
let b = a as nat;
print("Enter a string:");
let user_string = gets();
print(a.toS());
print(b.toS());