Arithmetic evaluation: Difference between revisions
Content added Content deleted
(→{{header|ooRexx}}: Add example for ooRexx) |
|||
Line 952: | Line 952: | ||
{ |
{ |
||
#field theValue. |
#field theValue. |
||
⚫ | |||
#initializer |
|||
⚫ | |||
⚫ | |||
⚫ | |||
#method parse_order'get = 0. |
#method parse_order'get = 0. |
||
Line 965: | Line 970: | ||
] |
] |
||
#method numeric = Real64Value &&literal:theValue &:erealformatter. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
#method numeric = theValue~EReal64Convertor numeric. |
|||
} |
} |
||
#class Node |
#class Node |
||
Line 978: | Line 979: | ||
#field theRight. |
#field theRight. |
||
#role |
#role LeftAssigned |
||
{ |
{ |
||
#method += aNode |
#method += aNode |
||
[ |
[ |
||
theRight := aNode. |
|||
⚫ | |||
] |
] |
||
} |
} |
||
#role Empty |
|||
⚫ | |||
{ |
{ |
||
#method += aNode |
#method += aNode |
||
[ |
[ |
||
theLeft := aNode. |
|||
⚫ | |||
] |
] |
||
} |
} |
||
#initializer |
|||
#method $setLeftAssigned |
|||
[ |
[ |
||
#shift |
#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 |
|||
⚫ | |||
⚫ | |||
] |
] |
||
} |
} |
||
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 |
#method available'is [ control fail. ] |
||
#method += aChar [ $self fail. ] |
#method += aChar [ $self fail. ] |
||
} |
} |
||
⚫ | |||
#initializer |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
#method parse_order'get = 0. |
#method parse_order'get = 0. |
||
Line 1,079: | Line 1,080: | ||
#method append : aChar |
#method append : aChar |
||
[ |
[ |
||
# |
#if control if:(aChar == 41) |
||
⚫ | |||
[ |
[ |
||
theCounter -= 1. |
theCounter -= 1. |
||
] |
] |
||
| if:( |
| if:(aChar == 40) |
||
[ |
[ |
||
theCounter += 1. |
theCounter += 1. |
||
]. |
]. |
||
⚫ | |||
[ #shift EOF. ^ $self. ]. |
[ #shift EOF. ^ $self. ]. |
||
⚫ | |||
theParser evaluate:aChar. |
theParser evaluate:aChar. |
||
] |
] |
||
#method numeric = theParser numeric. |
#method numeric = theParser numeric. |
||
⚫ | |||
#method new |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
#class Parser |
#class Parser |
||
{ |
{ |
||
Line 1,107: | Line 1,102: | ||
#field theTopNode. |
#field theTopNode. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
#role Start |
#role Start |
||
{ |
{ |
||
Line 1,115: | Line 1,122: | ||
theToken := SubExpression. |
theToken := SubExpression. |
||
theTopNode := theToken. |
theTopNode := theToken. |
||
⚫ | |||
] |
] |
||
| [ |
| [ |
||
theToken := Token. |
theToken := Token. |
||
theTopNode := theToken. |
theTopNode := theToken. |
||
⚫ | |||
theToken += aChar. |
theToken += aChar. |
||
#shift. |
#shift. |
||
]. |
]. |
||
] |
] |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
#role Operator |
#role Operator |
||
{ |
{ |
||
Line 1,161: | Line 1,157: | ||
] |
] |
||
} |
} |
||
⚫ | |||
#initializer |
|||
⚫ | |||
⚫ | |||
⚫ | |||
#method numeric = theTopNode numeric. |
#method numeric = theTopNode numeric. |
||
Line 1,204: | Line 1,205: | ||
] |
] |
||
#method |
#method start : aProcess |
||
[ |
[ |
||
aProcess run:self. |
|||
^ self numeric. |
|||
#method $setBrackets |
|||
[ |
|||
⚫ | |||
] |
] |
||
} |
} |
||
#symbol Program = |
#symbol Program = |
||
[ |
[ |
||
#var aText := String. |
#var aText := String. |
||
# |
#while ((Console >> aText) length > 0)? |
||
[ |
[ |
||
#var aParser := Parser. |
#var aParser := Parser. |
||
Console << "=" << |
Console << "=" << aParser start:Scan::aText | ^^"Invalid Expression". |
||
⚫ | |||
Scan::aText run:aParser. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
| << "Invalid Expression". |
|||
Console << "%n". |
Console << "%n". |
||
Line 1,237: | Line 1,229: | ||
=== ELENA VM script === |
=== ELENA VM script === |
||
<lang elena> |
<lang elena>number ::= $numeric; |
||
numeric ::= |
numeric ::= "(" sub_expr; |
||
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 $ |
neg_expression => 0 $terminal ^subtract $body; |
||
number => $terminal $body;</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |