UNPKG

726 Btext/coffeescriptView Raw
1# Push expression factors onto the stack. For example...
2#
3# Input
4#
5# 2
6# 3x + 2x + 1
7#
8# Output on stack
9#
10# [ 3 ]
11# [ x^2 ]
12# [ 2 ]
13# [ x ]
14# [ 1 ]
15#
16# but not necessarily in that order. Returns the number of factors.
17
18
19
20# Local U *p is OK here because no functional path to garbage collector.
21
22factors = (p) ->
23 h = tos
24 if (car(p) == symbol(ADD))
25 p = cdr(p)
26 while (iscons(p))
27 push_term_factors(car(p))
28 p = cdr(p)
29 else
30 push_term_factors(p)
31 return tos - h
32
33# Local U *p is OK here because no functional path to garbage collector.
34
35push_term_factors = (p) ->
36 if (car(p) == symbol(MULTIPLY))
37 p = cdr(p)
38 while (iscons(p))
39 push(car(p))
40 p = cdr(p)
41 else
42 push(p)