Arithmetic evaluation: Difference between revisions

Content added Content deleted
(→‎{{header|ooRexx}}: Add example for ooRexx)
Line 952: Line 952:
{
{
#field theValue.
#field theValue.
#initializer
[
theValue := String.
]
#method parse_order'get = 0.
#method parse_order'get = 0.
Line 965: Line 970:
]
]
#method numeric = Real64Value &&literal:theValue &:erealformatter.
#method new
[
theValue := String.
]
#method numeric = theValue~EReal64Convertor numeric.
}
}



#class Node
#class Node
Line 978: Line 979:
#field theRight.
#field theRight.
#role Empty
#role LeftAssigned
{
{
#method += aNode
#method += aNode
[
[
theLeft := aNode.
theRight := aNode.
$self $setLeftAssigned.
#shift.
]
]
}
}
#role Empty
#role LeftAssigned
{
{
#method += aNode
#method += aNode
[
[
theRight := aNode.
theLeft := aNode.
#shift.
#shift LeftAssigned.
]
]
}
}
#initializer
#method $setLeftAssigned
[
[
#shift LeftAssigned.
#shift Empty.
]
]


Line 1,008: Line 1,009:
]
]
| [
| [
aNode += self.
aNode += self.
^ aNode.
^ aNode.
].
].
Line 1,022: Line 1,024:
theRight := aNode += theRight.
theRight := aNode += theRight.
].
].
]
#method new
[
#shift Empty.
]
]
}
}
Line 1,036: Line 1,033:
#method numeric = theLeft numeric + theRight numeric.
#method numeric = theLeft numeric + theRight numeric.
}
}

#class DifferenceNode (Node)
#class DifferenceNode (Node)
{
{
Line 1,043: Line 1,039:
#method numeric = theLeft numeric - theRight numeric.
#method numeric = theLeft numeric - theRight numeric.
}
}

#class ProductNode (Node)
#class ProductNode (Node)
{
{
Line 1,050: Line 1,045:
#method numeric = theLeft numeric * theRight numeric.
#method numeric = theLeft numeric * theRight numeric.
}
}

#class FractionNode (Node)
#class FractionNode (Node)
{
{
Line 1,057: Line 1,051:
#method numeric = theLeft numeric / theRight numeric.
#method numeric = theLeft numeric / theRight numeric.
}
}

#class SubExpression
#class SubExpression
{
{
Line 1,065: Line 1,058:
#role EOF
#role EOF
{
{
#method eof'is []
#method available'is [ control fail. ]
#method += aChar [ $self fail. ]
#method += aChar [ $self fail. ]
}
}
#initializer
[
theParser := arithmeval'Parser.
theCounter := Integer << 1.
]
#method available'is []
#method parse_order'get = 0.
#method parse_order'get = 0.
Line 1,079: Line 1,080:
#method append : aChar
#method append : aChar
[
[
#var aCode := Int32Value::aChar.
#if control if:(aChar == 41)
#if control if:(aCode == 41)
[
[
theCounter -= 1.
theCounter -= 1.
]
]
| if:(aCode == 40)
| if:(aChar == 40)
[
[
theCounter += 1.
theCounter += 1.
].
].
#if(theCounter == 0)?
#if (theCounter == 0)?
[ #shift EOF. ^ $self. ].
[ #shift EOF. ^ $self. ].
theParser evaluate:aChar.
theParser evaluate:aChar.
]
]
#method numeric = theParser numeric.
#method numeric = theParser numeric.
#method new
[
theParser := arithmeval'Parser.
theCounter := Integer << 1.
]
}
}

#class Parser
#class Parser
{
{
Line 1,107: Line 1,102:
#field theTopNode.
#field theTopNode.
#role Brackets
{
#method evaluate : aChar
[
theToken += aChar.
#if theToken available'is
| [
#shift.
].
]
}
#role Start
#role Start
{
{
Line 1,115: Line 1,122:
theToken := SubExpression.
theToken := SubExpression.
theTopNode := theToken.
theTopNode := theToken.
$self $setBrackets.
#shift Brackets.
]
]
| [
| [
theToken := Token.
theToken := Token.
theTopNode := theToken.
theTopNode := theToken.
theToken += aChar.
theToken += aChar.
#shift.
#shift.
].
].
]
]
}
}
#role Brackets
{
#method evaluate : aChar
[
theToken += aChar.
#if theToken eof'is
[
#shift.
].
]
}
#role Operator
#role Operator
{
{
Line 1,161: Line 1,157:
]
]
}
}
#initializer
[
#shift Start.
]
#method numeric = theTopNode numeric.
#method numeric = theTopNode numeric.
Line 1,204: Line 1,205:
]
]
#method new
#method start : aProcess
[
[
#shift Start.
aProcess run:self.
]
^ self numeric.
#method $setBrackets
[
#shift Brackets.
]
]
}
}


#symbol Program =>
#symbol Program =
[
[
#var aText := String.
#var aText := String.
#loop ((Console >> aText) length > 0)?
#while ((Console >> aText) length > 0)?
[
[
#var aParser := Parser.
#var aParser := Parser.


Console << "=" << aText then: aText =>
Console << "=" << aParser start:Scan::aText | ^^"Invalid Expression".
[
Scan::aText run:aParser.
^ aParser numeric.
]
| << "Invalid Expression".
Console << "%n".
Console << "%n".
Line 1,237: Line 1,229:


=== ELENA VM script ===
=== ELENA VM script ===
<lang elena>numeric ::= "(" sub_expr;
<lang elena>number ::= $numeric;
numeric ::= $numeric;
numeric ::= "(" sub_expr;
factor ::= $numeric factor_r;
numeric ::= number;
factor ::= number factor_r;
factor ::= "(" sub_expr;
factor ::= "(" sub_expr;
sum ::= "+" factor ;
sum ::= "+" factor ;
Line 1,264: Line 1,257:
sum => $body ^add;
sum => $body ^add;
difference => $body ^subtract;
difference => $body ^subtract;
neg_expression => 0 $token ^subtract;</lang>
neg_expression => 0 $terminal ^subtract $body;
number => $terminal $body;</lang>


=={{header|Factor}}==
=={{header|Factor}}==