Arithmetic evaluation: Difference between revisions

Content added Content deleted
Line 1,346: Line 1,346:
.
.
subr init0
subr init0
a$[] = [ ]
astop$[] = [ ]
ale[] = [ ]
astleft[] = [ ]
ari[] = [ ]
astright[] = [ ]
err = 0
err = 0
.
.
Line 1,359: Line 1,359:
.
.
proc ast_print nd . .
proc ast_print nd . .
write "AST: " & nd
write "AST:"
for i to len a$[]
for i to len astop$[]
write " ( "
write " ( "
write a$[i] & " "
write astop$[i] & " "
write ale[i] & " "
write astleft[i] & " "
write ari[i]
write astright[i]
write " )"
write " )"
.
.
print ""
print " Start: " & nd
.
.
func node .
func node .
a$[] &= ""
astop$[] &= ""
ale[] &= 0
astleft[] &= 0
ari[] &= 0
astright[] &= 0
return len a$[]
return len astop$[]
.
.
#
#
Line 1,381: Line 1,381:
if tok$ = "n"
if tok$ = "n"
nd = node
nd = node
a$[nd] = "n"
astop$[nd] = "n"
ale[nd] = tokv
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
ale[nd] = ndx
astleft[nd] = ndx
a$[nd] = tok$
astop$[nd] = tok$
ntok
ntok
ari[nd] = parse_factor
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
ale[nd] = ndx
astleft[nd] = ndx
a$[nd] = tok$
astop$[nd] = tok$
ntok
ntok
ari[nd] = parse_term
astright[nd] = parse_term
ndx = nd
ndx = nd
.
.
Line 1,424: Line 1,424:
.
.
func eval nd .
func eval nd .
if a$[nd] = "n"
if astop$[nd] = "n"
return ale[nd]
return astleft[nd]
.
.
le = eval ale[nd]
le = eval astleft[nd]
ri = eval ari[nd]
ri = eval astright[nd]
a$ = a$[nd]
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>