Arithmetic evaluation: Difference between revisions
Content added Content deleted
No edit summary |
m (Easylang) |
||
Line 1,311: | Line 1,311: | ||
? arithEvaluate("(1 + 2 / 2) * (5 + 5)") |
? arithEvaluate("(1 + 2 / 2) * (5 + 5)") |
||
# value: 20.0</syntaxhighlight> |
# value: 20.0</syntaxhighlight> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="easylang"> |
|||
</syntaxhighlight> |
|||
subr nch |
|||
if inp_ind > len inp$[] |
|||
ch$ = strchar 0 |
|||
else |
|||
ch$ = inp$[inp_ind] |
|||
inp_ind += 1 |
|||
. |
|||
ch = strcode ch$ |
|||
. |
|||
# |
|||
subr ntok |
|||
if ch = 0 |
|||
tok$ = "eof" |
|||
else |
|||
while ch$ = " " |
|||
nch |
|||
. |
|||
if ch >= 48 and ch <= 58 |
|||
tok$ = "n" |
|||
s$ = "" |
|||
while ch >= 48 and ch <= 58 or ch$ = "." |
|||
s$ &= ch$ |
|||
nch |
|||
. |
|||
tokv = number s$ |
|||
else |
|||
tok$ = ch$ |
|||
nch |
|||
. |
|||
. |
|||
. |
|||
subr init0 |
|||
a$[] = [ ] |
|||
ale[] = [ ] |
|||
ari[] = [ ] |
|||
err = 0 |
|||
. |
|||
proc init s$ . . |
|||
inp$[] = strchars s$ |
|||
inp_ind = 1 |
|||
nch |
|||
ntok |
|||
init0 |
|||
. |
|||
proc ast_print nd . . |
|||
write "AST: " & nd |
|||
for i to len a$[] |
|||
write " ( " |
|||
write a$[i] & " " |
|||
write ale[i] & " " |
|||
write ari[i] |
|||
write " )" |
|||
. |
|||
print "" |
|||
. |
|||
func node . |
|||
a$[] &= "" |
|||
ale[] &= 0 |
|||
ari[] &= 0 |
|||
return len a$[] |
|||
. |
|||
# |
|||
funcdecl parse_expr . |
|||
# |
|||
func parse_factor . |
|||
if tok$ = "n" |
|||
nd = node |
|||
a$[nd] = "n" |
|||
ale[nd] = tokv |
|||
ntok |
|||
elif tok$ = "(" |
|||
ntok |
|||
nd = parse_expr |
|||
if tok$ <> ")" |
|||
err = 1 |
|||
print "error: ) expected, got " & tok$ |
|||
. |
|||
ntok |
|||
. |
|||
return nd |
|||
. |
|||
func parse_term . |
|||
ndx = parse_factor |
|||
while tok$ = "*" or tok$ = "/" |
|||
nd = node |
|||
ale[nd] = ndx |
|||
a$[nd] = tok$ |
|||
ntok |
|||
ari[nd] = parse_factor |
|||
ndx = nd |
|||
. |
|||
return ndx |
|||
. |
|||
func parse_expr . |
|||
ndx = parse_term |
|||
while tok$ = "+" or tok$ = "-" |
|||
nd = node |
|||
ale[nd] = ndx |
|||
a$[nd] = tok$ |
|||
ntok |
|||
ari[nd] = parse_term |
|||
ndx = nd |
|||
. |
|||
return ndx |
|||
. |
|||
func parse s$ . |
|||
init s$ |
|||
return parse_expr |
|||
. |
|||
func eval nd . |
|||
if a$[nd] = "n" |
|||
return ale[nd] |
|||
. |
|||
le = eval ale[nd] |
|||
ri = eval ari[nd] |
|||
a$ = a$[nd] |
|||
if a$ = "+" |
|||
return le + ri |
|||
elif a$ = "-" |
|||
return le - ri |
|||
elif a$ = "*" |
|||
return le * ri |
|||
elif a$ = "/" |
|||
return le / ri |
|||
. |
|||
. |
|||
repeat |
|||
inp$ = input |
|||
until inp$ = "" |
|||
print "Inp: " & inp$ |
|||
nd = parse inp$ |
|||
ast_print nd |
|||
if err = 0 |
|||
print "Eval: " & eval nd |
|||
. |
|||
print "" |
|||
. |
|||
input_data |
|||
4 * 6 |
|||
4.2 * ((5.3+8)*3 + 4) |
|||
2.5 * 2 + 2 * 3.14 |
|||
{{out}} |
|||
<pre> |
|||
Inp: 4 * 6 |
|||
AST: 2 ( n 4 0 ) ( * 1 3 ) ( n 6 0 ) |
|||
Eval: 24 |
|||
Inp: 4.2 * ((5.3+8)*3 + 4) |
|||
AST: 2 ( n 4.20 0 ) ( * 1 8 ) ( n 5.30 0 ) ( + 3 5 ) ( n 8 0 ) ( * 4 7 ) ( n 3 0 ) ( + 6 9 ) ( n 4 0 ) |
|||
Eval: 184.38 |
|||
Inp: 2.5 * 2 + 2 * 3.14 |
|||
AST: 4 ( n 2.50 0 ) ( * 1 3 ) ( n 2 0 ) ( + 2 6 ) ( n 2 0 ) ( * 5 7 ) ( n 3.14 0 ) |
|||
Eval: 11.28 |
|||
</pre> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |