UNPKG

812 Btext/coffeescriptView Raw
1# 'sum' function
2
3#define A p3
4#define B p4
5#define I p5
6#define X p6
7
8# leaves the sum at the top of the stack
9
10Eval_sum = ->
11 i = 0
12 j = 0
13 k = 0
14
15
16 # 1st arg
17 body = cadr(p1)
18
19 # 2nd arg (index)
20 indexVariable = caddr(p1)
21 if (!issymbol(p6))
22 stop("sum: 1st arg?")
23
24 # 3rd arg (lower limit)
25 push(cadddr(p1))
26 Eval()
27 j = pop_integer()
28 if (isNaN(j))
29 push p1
30 return
31
32 # 4th arg (upper limit)
33 push(caddddr(p1))
34 Eval()
35 k = pop_integer()
36 if (isNaN(k))
37 push p1
38 return
39
40
41 # remember contents of the index
42 # variable so we can put it back after the loop
43 p4 = get_binding(indexVariable)
44
45 push_integer(0)
46
47 for i in [j..k]
48 push_integer(i)
49 p5 = pop()
50 set_binding(indexVariable, p5)
51 push(body)
52 Eval()
53 add()
54
55 # put back the index variable to original content
56 set_binding(indexVariable, p4)