summaryrefslogtreecommitdiff
path: root/tests/examplefiles/scope.cirru
blob: c3d1a2c6c2cf8f4cbb33c926cc71bf1c040639ed (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

-- demo

define a (read cd) $ if (> a cd)
  print demo
  print "not demo"

say $ print a $ save $ b $ x $ c 8

print fun

-- test on folding

a $

b $ c

d $ e $ f

g $ h $ i j $ k $

-- test on comma

print (, a)
  a
    , b
      , c (, d)

-- test on HTML

doctype

html
  head
    title $ = Cirru
    script (:defer) $ :src build/build.js
    link (:rel stylesheet) $ :href css/page.css
    link (:rel icon)
      :href http://logo.cirru.org/cirru-32x32.png?v=3
  body
    textarea.demo.source $ :placeholder "Source Code"
    textarea.demo.target $ :placeholder "Compiled Data"
    @insert ../html/ga.html

-- test on indentation

a $ b $ c

e f
  (g)
  h

-- test on parentheses

3 4 (1) 4

((((1))))

x

-- test on quotes

a b c d

"a b c d"

"a b \" c d"

"a b" "c d"

-- test on unfolding

set
  add 1 $
  , x y
  add 5 $
  add 2

-- test on HTML attributes

div
  div
    :class a
  div
    :class a b c d

  div
    :class a (@ b) (@ c) d

  div
    :class a
      @if (@ b)
        div b
        div c
  div
    :class a
      @if (@ b) b c

-- test on helpers

@if (@call a b) (div) (span)

@each members
  div (@ name)

@each a
  div (@ b)
    @each c
      div (@ d)

-- test on HTML structure

@rich more
  #demo-more-box
    #demo-more
      :data-lang-text demo-more
    #demo-more-list
      @each room
        .demo-more-room
          span.demo-name
            @ topic
          span.demo-join
            :data-lang-text demo-join
            :data-id (@ id)

-- text on bool

print #true
print #false
print #yes
print #no
print #t
print #f

-- test on Cirru js

set a 1
set a (= "This is a string")
set b #t

-- this is comment

number 1.4
string x
regex ^\s$
regex "^\\s-\"$"
sentence this is a string

array 1 2 3 (= nothing) #t (= #t)

set c (array 1 (= nothing))

set d $ object (a (= google))
  b (= reader)
  c 1
  d $ array 1 2 (= string)

1 c
-1 c

:b d
.log console a 2
.log console

set demo $ object
  call $ \ x (.log console x) (. this call)
. demo (.call 1) (.call 4)

=.x d 3

set d null

new Array 1 2 3

set x (:length c)
set str (= str)
set c (.toUpperCase str)

\ x (+ x 1)
\ (x y) (+ x y)
\ x (set aa 1) (+ aa x)

set f (\ x (+ x 1))

+ a 1 2
+= a 1

> 1 2 3

if (> 2 1) (+ a 1)
else 2

if (> a 2)
  .log console (= "large")
elseif (> a 1)
  .log console (= "still good")
else
  .log console (= "so so")

set a $ if (> 2 1) #t #f

switch a
  1 (.log console 1)
  2 (.log console 2)
  else (.log console (= "something else"))

set a $ array 2 +3 -4
for (a x i) (.log console x i)

set a 0
while (< a 10) (+= a 1) (.log console a)

-- WebAssembly variable names

-- ":(c) 2015 Andreas Rossberg"

module
  export :even $even
  export "odd" $odd

  func $even (param $n i32) (result i32)
    if (i32.eq (get_local $n) (i32.const 0))
      i32.const 1
      call $odd (i32.sub (get_local $n) (i32.const 1))

  func $odd (param $n i32) (result i32)
    store_global $scratch (get_local $n)
    if (i32.eq (get_local $n) (i32.const 0)
      i32.const 0
      call $even (i32.sub (get_local $n) (i32.const 1))

  global $scratch i32

assert_eq (invoke :even (i32.const 13)) (i32.const 0)
assert_eq (invoke :even (i32.const 20)) (i32.const 1)
assert_eq (invoke :odd (i32.const 13)) (i32.const 1)
assert_eq (invoke :odd (i32.const 20)) (i32.const 0)