Arithmetic evaluation: Difference between revisions
Content added Content deleted
Line 1,346: | Line 1,346: | ||
. |
. |
||
subr init0 |
subr init0 |
||
astop$[] = [ ] |
|||
astleft[] = [ ] |
|||
astright[] = [ ] |
|||
err = 0 |
err = 0 |
||
. |
. |
||
Line 1,359: | Line 1,359: | ||
. |
. |
||
proc ast_print nd . . |
proc ast_print nd . . |
||
write "AST: |
write "AST:" |
||
for i to len |
for i to len astop$[] |
||
write " ( " |
write " ( " |
||
write |
write astop$[i] & " " |
||
write |
write astleft[i] & " " |
||
write |
write astright[i] |
||
write " )" |
write " )" |
||
. |
. |
||
print "" |
print " Start: " & nd |
||
. |
. |
||
func node . |
func node . |
||
astop$[] &= "" |
|||
astleft[] &= 0 |
|||
astright[] &= 0 |
|||
return len |
return len astop$[] |
||
. |
. |
||
# |
# |
||
Line 1,381: | Line 1,381: | ||
if tok$ = "n" |
if tok$ = "n" |
||
nd = node |
nd = node |
||
astop$[nd] = "n" |
|||
astleft[nd] = tokv |
|||
ntok |
ntok |
||
elif tok$ = "(" |
elif tok$ = "(" |
||
Line 1,399: | Line 1,399: | ||
while tok$ = "*" or tok$ = "/" |
while tok$ = "*" or tok$ = "/" |
||
nd = node |
nd = node |
||
astleft[nd] = ndx |
|||
astop$[nd] = tok$ |
|||
ntok |
ntok |
||
astright[nd] = parse_factor |
|||
ndx = nd |
ndx = nd |
||
. |
. |
||
Line 1,411: | Line 1,411: | ||
while tok$ = "+" or tok$ = "-" |
while tok$ = "+" or tok$ = "-" |
||
nd = node |
nd = node |
||
astleft[nd] = ndx |
|||
astop$[nd] = tok$ |
|||
ntok |
ntok |
||
astright[nd] = parse_term |
|||
ndx = nd |
ndx = nd |
||
. |
. |
||
Line 1,424: | Line 1,424: | ||
. |
. |
||
func eval nd . |
func eval nd . |
||
if |
if astop$[nd] = "n" |
||
return |
return astleft[nd] |
||
. |
. |
||
le = eval |
le = eval astleft[nd] |
||
ri = eval |
ri = eval astright[nd] |
||
a$ = |
a$ = astop$[nd] |
||
if a$ = "+" |
if a$ = "+" |
||
return le + ri |
return le + ri |
||
Line 1,454: | Line 1,454: | ||
4 * 6 |
4 * 6 |
||
4.2 * ((5.3+8)*3 + 4) |
4.2 * ((5.3+8)*3 + 4) |
||
2.5 * 2 + 2 * 3.14 |
2.5 * 2 + 2 * 3.14 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||