UNPKG

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