Arithmetic evaluation: Difference between revisions

(→‎{{header|ooRexx}}: Add example for ooRexx)
Line 952:
{
#field theValue.
#initializer
[
theValue := String.
]
#method parse_order'get = 0.
Line 965 ⟶ 970:
]
#method numeric = Real64Value &&literal:theValue &:erealformatter.
#method new
[
theValue := String.
]
#method numeric = theValue~EReal64Convertor numeric.
}
 
 
#class Node
Line 978 ⟶ 979:
#field theRight.
#role EmptyLeftAssigned
{
#method += aNode
[
theLefttheRight := aNode.
$self $setLeftAssigned.
#shift.
]
}
#role Empty
#role LeftAssigned
{
#method += aNode
[
theRighttheLeft := aNode.
#shift.
#roleshift LeftAssigned.
]
}
#initializer
#method $setLeftAssigned
[
#shift LeftAssignedEmpty.
]
 
Line 1,008 ⟶ 1,009:
]
| [
aNode += self.
^ aNode.
].
Line 1,022 ⟶ 1,024:
theRight := aNode += theRight.
].
]
#method new
[
#shift Empty.
]
}
Line 1,036 ⟶ 1,033:
#method numeric = theLeft numeric + theRight numeric.
}
 
#class DifferenceNode (Node)
{
Line 1,043 ⟶ 1,039:
#method numeric = theLeft numeric - theRight numeric.
}
 
#class ProductNode (Node)
{
Line 1,050 ⟶ 1,045:
#method numeric = theLeft numeric * theRight numeric.
}
 
#class FractionNode (Node)
{
Line 1,057 ⟶ 1,051:
#method numeric = theLeft numeric / theRight numeric.
}
 
#class SubExpression
{
Line 1,065 ⟶ 1,058:
#role EOF
{
#method eofavailable'is [ control fail. ]
#method += aChar [ $self fail. ]
}
#initializer
[
theParser := arithmeval'Parser.
theCounter := Integer << 1.
]
#method newavailable'is []
#method parse_order'get = 0.
Line 1,079 ⟶ 1,080:
#method append : aChar
[
#varif aCodecontrol if:(aChar == Int32Value::aChar.41)
#if control if:(aCode == 41)
[
theCounter -= 1.
]
| if:(aCodeaChar == 40)
[
theCounter += 1.
].
#if(theCounter == 0)?
#if control if:(aCodetheCounter == 410)?
[ #shift EOF. ^ $self. ].
[
theParser evaluate:aChar.
]
#method numeric = theParser numeric.
#method new
[
theParser := arithmeval'Parser.
theCounter := Integer << 1.
]
}
 
#class Parser
{
Line 1,107 ⟶ 1,102:
#field theTopNode.
#role Brackets
{
#method evaluate : aChar
[
theToken += aChar.
#if theToken eofavailable'is
| [
] #shift.
].
]
}
#role Start
{
Line 1,115 ⟶ 1,122:
theToken := SubExpression.
theTopNode := theToken.
$self $setBrackets.
#shift Brackets.
]
| [
theToken := Token.
theTopNode := theToken.
^ aParser numeric.
theToken += aChar.
#shift.
].
]
}
#role Brackets
{
#method evaluate : aChar
[
theToken += aChar.
#if theToken eof'is
[
#shift.
].
]
}
#role Operator
{
Line 1,161 ⟶ 1,157:
]
}
#initializer
[
#shift EmptyStart.
]
#method numeric = theTopNode numeric.
Line 1,204 ⟶ 1,205:
]
#method newstart : aProcess
[
#shiftaProcess Startrun:self.
]
^ self numeric.
#method $setBrackets
[
#shift Brackets.
]
}
 
#symbol Program =>
[
#var aText := String.
#loopwhile ((Console >> aText) length > 0)?
[
#var aParser := Parser.
 
Console << "=" << aTextaParser thenstart:Scan:: aText =>| ^^"Invalid Expression".
[
Scan::aText run:aParser.
^ aParser numeric.
]
| << "Invalid Expression".
Console << "%n".
Line 1,237 ⟶ 1,229:
 
=== ELENA VM script ===
<lang elena>numericnumber ::= "(" sub_expr$numeric;
numeric ::= $numeric"(" sub_expr;
factor numeric ::= $numeric factor_rnumber;
factor ::= number factor_r;
factor ::= "(" sub_expr;
sum ::= "+" factor ;
Line 1,264 ⟶ 1,257:
sum => $body ^add;
difference => $body ^subtract;
neg_expression => 0 $tokenterminal ^subtract $body;</lang>
number => $terminal $body;</lang>
 
=={{header|Factor}}==
Anonymous user