-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathproduct.coffee
More file actions
59 lines (48 loc) · 1.06 KB
/
product.coffee
File metadata and controls
59 lines (48 loc) · 1.06 KB
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
# 'product' function
#define A p3
#define B p4
#define I p5
#define X p6
# leaves the product at the top of the stack
Eval_product = ->
i = 0
j = 0
k = 0
# 1st arg
body = cadr(p1)
# 2nd arg (index)
indexVariable = caddr(p1)
if (!issymbol(indexVariable))
stop("sum: 2nd arg?")
# 3rd arg (lower limit)
push(cadddr(p1))
Eval()
j = pop_integer()
if (isNaN(j))
push p1
return
# 4th arg (upper limit)
push(caddddr(p1))
Eval()
k = pop_integer()
if (isNaN(k))
push p1
return
# remember contents of the index
# variable so we can put it back after the loop
oldIndexVariableValue = get_binding(indexVariable)
push_integer(1)
for i in [j..k]
push_integer(i)
p5 = pop()
set_binding(indexVariable, p5)
push(body)
Eval()
if DEBUG
console.log "product - factor 1: " + stack[tos-1].toString()
console.log "product - factor 2: " + stack[tos-2].toString()
multiply()
if DEBUG
console.log "product - result: " + stack[tos-1].toString()
# put back the index variable to original content
set_binding(indexVariable, oldIndexVariableValue)