-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.awk
executable file
·206 lines (198 loc) · 4.7 KB
/
expr.awk
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/awk -f
function top(s) {
return s[s["c"]]
}
function pop(s) {
if (s["c"])
return s[s["c"]--]
return
}
function push(s, e) {
s[++s["c"]] = e
}
function get(s, pos) {
return s[pos]
}
# returns the precedence order:
function ord(operator) {
if (operator == "") return 0
if (operator == ",") return 10
if (operator ~ /^[+-]$/) return 20
if (operator ~ /^[*\/]$/) return 30
if (operator == "^") return 40
if (operator ~ /^fn-/) return 50
}
function functions(fn, argc, argv) {
if (fn == "fn-neg") {
if (argc != 1) return "too many params"
argv["res"] = -argv[1]
} else if (fn == "fn-cos") {
if (argc != 1) return "too many params"
argv["res"] = cos(argv[1])
} else if (fn == "fn-sin") {
if (argc != 1) return "too many params"
argv["res"] = sin(argv[1])
} else {
return "invalid function"
}
}
# Specifies when right to left evaluation needed:
function rtl_evaluation_needed(pre, post) {
# a^b^c case:
if (pre == "^" && post == "^")
return 1
# a---b and -a^b cases:
if (pre == "fn-neg")
if (post == "fn-neg" || post == "^")
return 1
return 0
}
function parse_expr(text, fifo,
i, stack, d, n, s, last, count, A, paren, item) {
d = n = 0
gsub(/ */, " ", text)
sub(/^ */, "", text)
# the extra spaces at end of expression are not cleared
count = split(text, A, / */)
last = "nothing"
paren = 0
s = ""
for (i=1; i<=count; i++){
if (A[i] ~ /[a-zA-Z_]/ || last == "string" && A[i] ~ /[0-9]/) {
s = s A[i]
last = "string"
} else if (A[i] ~ /[0-9]/) {
if (d) {
n += A[i]*m
m /= 10
} else
n = n*10+A[i]
last = "num"
} else if (A[i] == ".") {
if (last == "string") {
print "\".\" not valid in this context"
return 0
}
d = 1
m = .1
last = "num"
} else if (A[i] == "(") {
paren++
if (last == "expr" || last == "num") {
print "unexpected open parenthesis"
return 0
}
if (last == "string") {
push(stack, "fn-" s)
s = ""
}
push(stack, "")
last = "nothing"
} else if (A[i] == ")") {
if (last == "oper" || last == "string") {
print "unexpected close parenthesis"
return 0
}
if (!paren--) {
print "unmatched close parenthesis"
return 0
}
if (last == "num")
push(fifo, n)
while ((item = pop(stack)) != "")
push(fifo, item)
last = "expr"
} else {
if (last == "nothing" || last == "oper" || last == "string") {
if (A[i] == "-") {
A[i] = "fn-neg"
} else {
print "unexpected operator " A[i] " at i=" i
return 0
}
} else if (last == "num")
push(fifo, n)
d = n = 0
# In the while we pop from the operand stack (and append in the fifo)
# the operands whose precedence orders are greater or equal than the
# operand being actualy parsed, A[i].
# We process the equal precendence case to keep the size of the
# eval_expr's rpn stack at its minimum posible size: For example see the
# 1+2+3 case and its two rpn representations: 1 2 3 + +, and 1 2 + 3 +,
# we use the latter, unless right to left evaluation is needed:
while (ord(A[i]) <= ord(top(stack)) && top(stack) != "" \
&& !rtl_evaluation_needed(top(stack), A[i])) {
push(fifo, pop(stack))
}
push(stack, A[i])
last = "oper"
}
}
push(fifo, "p")
return 1
}
function eval_expr(fifo,
i, n, x, y, res, count, rpn) {
for (i=1; (n=get(fifo, i)) != ""; i++) {
if (n ~ /[0-9]/) { push(rpn, n) }
else if (n == "+") { push(rpn, pop(rpn)+pop(rpn)) }
else if (n == "*") { push(rpn, pop(rpn)*pop(rpn)) }
else if (n == "-") { x = pop(rpn); y = pop(rpn); push(rpn, y-x) }
else if (n == "/") {
x = pop(rpn)
if (x == 0) {
print "division by 0"
return
}
y = pop(rpn)
push(rpn, y / x)
}
else if (n == "^") {
x = pop(rpn)
y = pop(rpn)
if (y==0 && x<0) {
print "division by 0"
return
}
if (y<0 && int(x)!=x) {
print "can't calculate negative roots"
return
}
push(rpn, y^x)
}
else if (n ~ /^fn-/) {
count = split(pop(rpn), args, ";")
res = functions(n, count, args)
if (res != "") {
print res
return
}
if ("res" in args)
push(rpn, args["res"])
}
else if (n == ",") {
x = pop(rpn)
y = pop(rpn)
push(rpn, y ";" x)
}
else if (n == "p")
return top(rpn)
else {
print "invalid operator " n
return
}
}
return
}
function dump_stack(stack, i) {
for (i=1; i in stack; i++)
print "\t" i ": " stack[i]
if ((i+1) in stack)
print "\tbroken stack: i+1 = " stack[i+1]
}
{
for (i in fifo) delete fifo[i]
parse_expr($0, fifo)
dump_stack(fifo)
print eval_expr(fifo)
}