aboutsummaryrefslogtreecommitdiff
path: root/examples/sample_code.nom
blob: 1e1ccc64791f4e5e9e2f89c5356ca6ab28342ace (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# This is just a comment
#.. Block comments
    start with a #.. and
    continue until dedent

require "lib/core.nom"

say "foo"

say (-4 + (1.5 - .25))

#.. "rule" is just a macro that takes a function call spec and a block of code to run,
    and stores the function definition
rule [fart] =: say "poot"

# multi-line strings:
say ".."
    |    Once upon a time
    |there was a very


    |long string
    |
    |    with   
    |       rather
    |   silly # fake comments and
    # Real comments look like this
    |  indentation

    |   and "quotes"
    |.." (even fakeouts like that) "
    |(done)
    |    
    |^and trailing spaces
    |

say "Strings have interpolation: \(1 + 2) <- like that"

rule [doublefart] =: # this farts twice
    say "poot"
    say "poot"

doublefart

rule [subex work] =: "subexpressions work"

say (subex work)

say (..)
    subex work

say ["lists", "work"]

say []

say [..]
    1, 2
    3

rule [say both %1 and %2] =:
    say %1
    say %2

say both [..]
    1,2
..and [..]
    3,4


rule [three] =: 3
say both
.."a list:"
..and [..]
    1,2,three,4

if 1: yes
..else: no

if 1 {yes} else {no}

say (do {return 5})

# Some variables
rule [do %one also %two] =:
    do %one
    do %two

do: say "one liner"
..also: say "another one liner"

say (do {return "wow"})

say (1 + (-(2 * 3)))

say (..)
    2 + (..)
        3 * 4

when:
    * (%x == 1):
        say "one"
    * (%x == 2):
        say "two"
    * %x:
        say "nonzero"
    * else:
        say "???"

when %x == ?:
    * 1:
        say "one"
    * 2:
        say "two"
    * else:
        say "???"


if %x:
    say "one"
..else:
    if %y:
        say "two"
    ..else:
        say "three"


say ".."
    |this is a longstring
    |
    |   with multiple lines
    |   and an interpolated expression: \2 + 5\

rule [%n bottles] =:
    lua block ".."
        |do
        |    print("running raw lua code...")
        |    for i=\(%n),1,-1 do
        |        print(tostring(i).." bottles of beer on the wall. Take one down, pass it around,")
        |    end
        |    print("no more bottles of beer on the wall.")
        |end
    nil
9 bottles

rule [dumsum %nums] =:
    %sum = 0
    for %n in %nums:
        %sum += %n
    return %sum

say (dumsum [1,2,3])