1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-18 14:44:48 +00:00
janet/examples/sysir/samples.janet

47 lines
983 B
Plaintext
Raw Normal View History

2024-06-12 00:37:11 +00:00
(use ./frontend)
(def simple
'(defn simple [x:int]
(def xyz:int (+ 1 2 3))
(return (* x 2 x))))
(def myprog
2024-06-12 23:28:05 +00:00
'(defn myprog:int []
2024-06-12 00:37:11 +00:00
(def xyz:int (+ 1 2 3))
(def abc:int (* 4 5 6))
2024-06-12 23:28:05 +00:00
(def x:boolean (= (the int 5) xyz))
2024-06-12 00:37:11 +00:00
(var i:int 0)
2024-06-12 23:28:05 +00:00
(while (< i (the int 10))
(set i (the int (+ 1 i)))
2024-06-12 00:37:11 +00:00
(printf "i = %d\n" i))
(printf "hello, world!\n%d\n" (the int (if x abc xyz)))
2024-06-12 23:28:05 +00:00
(return (* abc xyz))))
2024-06-12 00:37:11 +00:00
(def doloop
'(defn doloop [x:int y:int]
(var i:int x)
(while (< i y)
(set i (the int (+ 1 i)))
(printf "i = %d\n" (the int i)))
2024-06-12 23:28:05 +00:00
(myprog)
2024-06-12 00:37:11 +00:00
(return x)))
(def main-fn
'(defn _start:void []
(syscall 1 1 "Hello, world!\n" 14)
(doloop 10 20)
(exit (the int 0))
(return)))
####
2024-06-12 23:28:05 +00:00
(compile1 myprog)
2024-06-12 00:37:11 +00:00
(compile1 doloop)
(compile1 main-fn)
#(dump)
#(dumpc)
(dumpx64)
# Run with
# janet examples/sysir/scratch.janet > temp.nasm && nasm -felf64 temp.nasm -l temp.lst && ld temp.o && ./a.out