Arithmetic evaluation: Difference between revisions

No edit summary
Line 3,315:
</lang>
 
From BBC BASIC. In M2000 we can't call a user function which isn't a child function, so here we make all functions as members of same group, so now they call each other. A function as a member in a group can use other members, using a dot or This and a dot, so .Ast$() is same as This.Ast$().
 
<lang M2000 Interpreter>
Module CheckAst {
Group Eval {
Function Ast$ (&in$) {
Def ast$, oper$
Do {
Ast$+=.Ast1$(&in$)
in$=Trim$(in$)
oper$=left$(in$,1)
if Instr("+-", oper$)>0 then {
ast$+=oper$
in$=Mid$(in$, 2)
} else exit
} until len(in$)=0
="("+ast$+")"
}
Function Ast1$ (&in$) {
Def ast$, oper$
Do {
Ast$+=.Ast2$(&in$)
in$=Trim$(in$)
oper$=left$(in$,1)
if Instr("*/", oper$)>0 then {
ast$+=oper$
in$=Mid$(in$, 2)
} else exit
} until len(in$)=0
="("+ast$+")"
}
Function Ast2$ (&in$) {
Def ast$, oper$
in$=Trim$(in$)
if Asc(in$)<>40 then =.Number$(&in$) : exit
in$=Mid$(in$, 2)
ast$=.Ast$(&in$)
in$=Mid$(in$, 2)
=ast$
}
Function Number$ (&in$) {
Def ch$, num$
Do {
ch$=left$(in$,1)
if instr("0123456789", ch$)>0 Then {
num$+=ch$
in$=Mid$(in$, 2)
} Else Exit
} until len(in$)=0
=num$
}
}
Expr$ = "1+2 * (3 + (4 * 5 + 6 * 7 * 8) - 9) / 10"
Print Eval(Eval.Ast$(&Expr$))=71
}
CheckAst
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Anonymous user