UNPKG

925 Btext/coffeescriptView Raw
1# 'for' function
2
3###
4x=0
5y=2
6for(do(x=sqrt(2+x),y=2*y/x),k,1,9)
7float(y)
8
9X: k
10B: 1...9
11
121st parameter is the body
132nd parameter is the variable to loop with
143rd and 4th are the limits
15
16
17###
18
19#define A p3
20#define B p4
21#define I p5
22#define X p6
23
24Eval_for = ->
25 i = 0
26 j = 0
27 k = 0
28
29 loopingVariable = caddr(p1)
30 if (!issymbol(loopingVariable))
31 stop("for: 2nd arg should be the variable to loop over")
32
33 push(cadddr(p1))
34 Eval()
35 j = pop_integer()
36 if (isNaN(j))
37 push p1
38 return
39
40 push(caddddr(p1))
41 Eval()
42 k = pop_integer()
43 if (isNaN(k))
44 push p1
45 return
46
47
48 # remember contents of the index
49 # variable so we can put it back after the loop
50 p4 = get_binding(loopingVariable)
51
52 for i in [j..k]
53 push_integer(i)
54 p5 = pop()
55 set_binding(loopingVariable, p5)
56 push(cadr(p1))
57 Eval()
58 pop()
59
60 # put back the index variable to original content
61 set_binding(loopingVariable, p4)
62
63 # return value
64
65 push_symbol(NIL)
66