Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Elena}}
(→{{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.
▲ ]
}
#class Node
Line 978 ⟶ 979:
#field theRight.
#role
{
#method += aNode
[
]
}
#role Empty
#role LeftAssigned▼
{
#method += aNode
[
]
}
#initializer
[
#shift
]
Line 1,008 ⟶ 1,009:
]
| [
aNode += self.
^ aNode.
].
Line 1,022 ⟶ 1,024:
theRight := aNode += theRight.
].
]▼
[▼
#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
#method += aChar [ $self fail. ]
}
#initializer
▲ [
theParser := arithmeval'Parser.▼
theCounter := Integer << 1.▼
▲ ]
#method parse_order'get = 0.
Line 1,079 ⟶ 1,080:
#method append : aChar
[
#
#if control if:(aCode == 41)▼
[
theCounter -= 1.
]
| if:(
[
theCounter += 1.
].
[ #shift EOF. ^ $self. ].
theParser evaluate:aChar.
]
#method numeric = theParser numeric.
[▼
▲ theParser := arithmeval'Parser.
▲ theCounter := Integer << 1.
]▼
}
#class Parser
{
Line 1,107 ⟶ 1,102:
#field theTopNode.
#role Brackets▼
{▼
#method evaluate : aChar▼
[▼
theToken += aChar.▼
| [▼
].▼
]▼
} ▼
#role Start
{
Line 1,115 ⟶ 1,122:
theToken := SubExpression.
theTopNode := theToken.
#shift Brackets.▼
]
| [
theToken := Token.
theTopNode := theToken.
theToken += aChar.
#shift.
].
]
}
▲ #role Brackets
▲ {
▲ #method evaluate : aChar
▲ [
▲ theToken += aChar.
▲ #if theToken eof'is
▲ [
▲ #shift.
▲ ].
▲ ]
▲ }
#role Operator
{
Line 1,161 ⟶ 1,157:
]
}
#initializer
▲ [
▲ ]
#method numeric = theTopNode numeric.
Line 1,204 ⟶ 1,205:
]
#method
[
^ self numeric.
▲ #shift Brackets.
]
}
#symbol Program =
[
#var aText := String.
#
[
#var aParser := Parser.
Console << "=" <<
▲ [
▲ ^ aParser numeric.
▲ ]
Console << "%n".
Line 1,237 ⟶ 1,229:
=== ELENA VM script ===
<lang elena>
numeric ::=
factor ::= number factor_r;
factor ::= "(" sub_expr;
sum ::= "+" factor ;
Line 1,264 ⟶ 1,257:
sum => $body ^add;
difference => $body ^subtract;
neg_expression => 0 $
number => $terminal $body;</lang>
=={{header|Factor}}==
|