-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sg
67 lines (54 loc) · 1.71 KB
/
main.sg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
; this is a comment
; variable definition and expressionism
(print (let meaningOfTheUniverse 42))
(print (* 2 meaningOfTheUniverse))
; operators
(let coolNumber 69.420)
(print (and coolNumber #true))
; simple function definitions
(def double (n) (* 2 n))
(let result (double 8))
; if-expressions
(if (== result 16) (print "it works!") (print "it doesnt!"))
; the environment is 'enclosed' in a function upon definition
; if it get's changed later, the function will retain values
; as they were on definition. aka calling (closure 6) after
; changing 'result' should not change behaviour.
(def closure (n) (+ n result))
(let result 2)
(print (if (== 18 (closure 2)) "omg closures!"))
; () is our nil type.
; this does nothing
(if (not #false) () (print "oops!"))
; this is a convoluted way to print; but it works!
((lambda (n) (print n)) (join '("hello" "world!") ", "))
; lists :)
(let x '(1 2))
(let x (push 3 x))
(print (sum x))
; recursion
; tail recursion not implemented...
(def fact (n) (if (!= n 1) (* n (fact (- n 1))) 1))
(print (fact 4))
; inspect pretty prints any expression and returns it,
; print stringifies compatible expressions (strings, atoms, nil, numbers)
; and returns ok
(inspect print)
(print (inspect #apple)) ; prints "#apple" and then "apple"
; this is a block: the last expression will be returned, and
; it has a seperate scope, but inherits from global scope
(print (do
(print "hello from scope!")
(let y (* 2 coolNumber))))
(inspect 1.0)
(inspect #apple)
(inspect #true)
(inspect "blah")
(inspect ())
(inspect '(wibble wobble))
(inspect (tie "hello" "world"))
(inspect fact)
(inspect print)
; y is undefined here because it's only defined in the block scope
(print "gonna crash now...")
(print y)