UNPKG

3.38 kBtext/coffeescriptView Raw
1
2
3# s is a string
4new_string = (s) ->
5 save()
6 p1 = new U()
7 p1.k = STR
8 p1.str = s
9 push(p1)
10 restore()
11
12out_of_memory = ->
13 stop("out of memory")
14
15# both ints
16push_zero_matrix = (i,j) ->
17 push(alloc_tensor(i * j))
18 stack[tos - 1].tensor.ndim = 2
19 stack[tos - 1].tensor.dim[0] = i
20 stack[tos - 1].tensor.dim[1] = j
21
22push_identity_matrix = (n) ->
23 push_zero_matrix(n, n)
24 i = 0
25 for i in [0...n]
26 stack[tos - 1].tensor.elem[i * n + i] = one
27
28 check_tensor_dimensions stack[tos - 1]
29
30push_cars = (p) ->
31 while (iscons(p))
32 push(car(p))
33 p = cdr(p)
34
35peek = ->
36 save()
37 p1 = pop()
38 push(p1)
39 restore()
40
41# see cmp_expr definition, this
42# function alone just does simple structure comparison
43# or compares numbers (either rationals or integers or doubles)
44# but can't be used alone to test
45# more complex mathematical equalities...
46equal = (p1,p2) ->
47 if (cmp_expr(p1, p2) == 0)
48 return 1
49 else
50 return 0
51
52lessp = (p1,p2) ->
53 if (cmp_expr(p1, p2) < 0)
54 return 1
55 else
56 return 0
57
58sign = (n) ->
59 if (n < 0)
60 return -1
61 else if (n > 0)
62 return 1
63 else
64 return 0
65
66# compares whether two expressions
67# have the same structure.
68# For example this method alone
69# would compare "1+1" and "2"
70# as different.
71# It just so happens though that one oftens
72# evaluates the two sides before passing them
73# to this function, so chances are that the two
74# sides have the same normal form.
75# Even a simple evaluation might not cut it
76# though... a simplification of both sides
77# would then help. And even that might not
78# cut it in some cases...
79cmp_expr = (p1, p2) ->
80 n = 0
81
82 if (p1 == p2)
83 return 0
84
85 if (p1 == symbol(NIL))
86 return -1
87
88 if (p2 == symbol(NIL))
89 return 1
90
91 if (isnum(p1) && isnum(p2))
92 return sign(compare_numbers(p1, p2))
93
94 if (isnum(p1))
95 return -1
96
97 if (isnum(p2))
98 return 1
99
100 if (isstr(p1) && isstr(p2))
101 return sign(strcmp(p1.str,p2.str))
102
103 if (isstr(p1))
104 return -1
105
106 if (isstr(p2))
107 return 1
108
109 if (issymbol(p1) && issymbol(p2))
110 return sign(strcmp(get_printname(p1),get_printname(p2)))
111
112 if (issymbol(p1))
113 return -1
114
115 if (issymbol(p2))
116 return 1
117
118 if (istensor(p1) && istensor(p2))
119 return compare_tensors(p1, p2)
120
121 if (istensor(p1))
122 return -1
123
124 if (istensor(p2))
125 return 1
126
127 # recursion here
128 while (iscons(p1) && iscons(p2))
129 n = cmp_expr(car(p1), car(p2))
130 if (n != 0)
131 return n
132 p1 = cdr(p1)
133 p2 = cdr(p2)
134
135 if (iscons(p2))
136 return -1
137
138 if (iscons(p1))
139 return 1
140
141 return 0
142
143length = (p) ->
144 n = 0
145 while (iscons(p))
146 p = cdr(p)
147 n++
148 return n
149
150unique = (p) ->
151 save()
152 p1 = symbol(NIL)
153 p2 = symbol(NIL)
154 unique_f(p)
155 if (p2 != symbol(NIL))
156 p1 = symbol(NIL)
157 p = p1
158 restore()
159 return p
160
161unique_f = (p) ->
162 if (isstr(p))
163 if (p1 == symbol(NIL))
164 p1 = p
165 else if (p != p1)
166 p2 = p
167 return
168 while (iscons(p))
169 unique_f(car(p))
170 if (p2 != symbol(NIL))
171 return
172 p = cdr(p)
173
174
175ssqrt = ->
176 push_rational(1, 2)
177 power()
178
179yyexpand = ->
180 prev_expanding = expanding
181 expanding = 1
182 Eval()
183 expanding = prev_expanding
184
185exponential = ->
186 push_symbol(E)
187 swap()
188 power()
189
190square = ->
191 push_integer(2)
192 power()
193
194#__cmp = (p1, p2) ->
195# return cmp_expr(p1, p2)
196
197# n an integer
198sort_stack = (n) ->
199 #qsort(stack + tos - n, n, sizeof (U *), __cmp)
200
201 h = tos - n
202 subsetOfStack = stack.slice(h,h+n)
203 subsetOfStack.sort(cmp_expr)
204 stack = stack.slice(0,h).concat(subsetOfStack).concat(stack.slice(h+n))
205
206
207$.equal = equal
208$.length = length