Arithmetic evaluation: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 928: | Line 928: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
<lang elena>#define |
<lang elena>#define std'basic'*. |
||
#define |
#define std'routines'*. |
||
#define |
#define std'patterns'*. |
||
#define std'basic'factories'*. |
|||
#define sys'io'*. |
|||
#define std'dictionary'*. |
|||
#subject parse_order. |
#subject parse_order. |
||
// --- Token --- |
|||
#class Token |
#class Token |
||
Line 938: | Line 943: | ||
#field theValue. |
#field theValue. |
||
#method parse_order'get = 0. |
#method parse_order'get = 0. |
||
#method += aChar |
#method += aChar |
||
[ |
[ |
||
theValue += aChar. |
theValue += aChar. |
||
] |
] |
||
#method + aNode |
#method + aNode |
||
[ |
[ |
||
^ aNode += self. |
^ aNode += self. |
||
] |
] |
||
#method new |
#method new |
||
[ |
[ |
||
theValue := |
theValue := String. |
||
] |
] |
||
#method real'get = basic'RealType evaluate:theValue. |
|||
#method numeric = Real64Convert eval:(theValue literal). |
|||
} |
} |
||
Line 963: | Line 972: | ||
#method += aNode |
#method += aNode |
||
[ |
[ |
||
theLeft := aNode. |
theLeft := aNode. |
||
⚫ | |||
$self $setLeftAssigned. |
$self $setLeftAssigned. |
||
] |
] |
||
} |
} |
||
#role LeftAssigned |
#role LeftAssigned |
||
{ |
{ |
||
#method += aNode |
#method += aNode |
||
[ |
[ |
||
theRight := aNode. |
theRight := aNode. |
||
⚫ | |||
#shift. |
#shift. |
||
] |
] |
||
} |
} |
||
#method $setLeftAssigned |
#method $setLeftAssigned |
||
[ |
[ |
||
#shift LeftAssigned. |
#shift LeftAssigned. |
||
] |
] |
||
#method + aNode |
#method + aNode |
||
[ |
[ |
||
Line 987: | Line 1,001: | ||
| [ |
| [ |
||
aNode += self. |
aNode += self. |
||
^ aNode. |
^ aNode. |
||
]. |
]. |
||
] |
] |
||
#method += aNode |
#method += aNode |
||
[ |
[ |
||
Line 999: | Line 1,015: | ||
theRight := aNode += theRight. |
theRight := aNode += theRight. |
||
]. |
]. |
||
] |
] |
||
#method new |
#method new |
||
[ |
[ |
||
Line 1,008: | Line 1,025: | ||
#class SummaryNode (Node) |
#class SummaryNode (Node) |
||
{ |
{ |
||
#method parse_order'get = 2. |
#method parse_order'get = 2. |
||
#method |
#method numeric = theLeft numeric + theRight numeric. |
||
} |
} |
||
#class DifferenceNode (Node) |
#class DifferenceNode (Node) |
||
{ |
{ |
||
#method parse_order'get = 2. |
#method parse_order'get = 2. |
||
#method |
#method numeric = theLeft numeric - theRight numeric. |
||
} |
} |
||
#class ProductNode (Node) |
#class ProductNode (Node) |
||
{ |
{ |
||
#method parse_order'get = 1. |
#method parse_order'get = 1. |
||
#method |
#method numeric = theLeft numeric * theRight numeric. |
||
} |
} |
||
#class FractionNode (Node) |
#class FractionNode (Node) |
||
{ |
{ |
||
#method parse_order'get = 1. |
#method parse_order'get = 1. |
||
#method |
#method numeric = theLeft numeric / theRight numeric. |
||
} |
} |
||
Line 1,037: | Line 1,058: | ||
#role EOF |
#role EOF |
||
{ |
{ |
||
#method eof'is [] |
#method eof'is [] |
||
#method += aChar [ $self fail. ] |
#method += aChar [ $self fail. ] |
||
} |
} |
||
#method parse_order'get = 0. |
#method parse_order'get = 0. |
||
#method + aNode |
#method + aNode |
||
[ |
[ |
||
^ aNode += self. |
^ aNode += self. |
||
] |
] |
||
#method += aChar |
|||
#method append &numeric:aCode |
|||
[ |
[ |
||
#if |
#if control if:(aCode == 41) |
||
[ |
[ |
||
theCounter -= 1. |
theCounter -= 1. |
||
] |
] |
||
| if:( |
| if:(aCode == 40) |
||
[ |
[ |
||
theCounter += 1. |
theCounter += 1. |
||
]. |
]. |
||
#if(theCounter == 0)? |
#if(theCounter == 0)? |
||
[ #shift EOF. ^ $self. ]. |
[ #shift EOF. ^ $self. ]. |
||
theParser evaluate: |
theParser evaluate:$param. |
||
] |
] |
||
#method |
#method numeric = theParser numeric. |
||
#method new |
#method new |
||
[ |
[ |
||
theParser := arithmeval'Parser. |
theParser := arithmeval'Parser. |
||
theCounter := |
theCounter := Integer << 1. |
||
] |
] |
||
} |
} |
||
Line 1,077: | Line 1,105: | ||
#method evaluate : aChar |
#method evaluate : aChar |
||
[ |
[ |
||
#if (aChar == 40)? |
#if (aChar numeric == 40)? |
||
[ |
[ |
||
theToken := SubExpression. |
theToken := SubExpression. |
||
theTopNode := theToken. |
theTopNode := theToken. |
||
⚫ | |||
$self $setBrackets. |
$self $setBrackets. |
||
] |
] |
||
| [ |
| [ |
||
theToken := Token. |
theToken := Token. |
||
theTopNode := theToken. |
theTopNode := theToken. |
||
theToken += aChar. |
|||
#shift. |
#shift. |
||
]. |
]. |
||
] |
] |
||
} |
} |
||
#role Brackets |
|||
{ |
{ |
||
#method evaluate : aChar |
#method evaluate : aChar |
||
[ |
[ |
||
theToken += aChar. |
|||
#if theToken eof'is |
|||
[ |
[ |
||
#shift. |
|||
]. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
] |
] |
||
} |
} |
||
#role Operator |
|||
{ |
{ |
||
#method evaluate : aChar |
#method evaluate &numeric:aCode &literal:aChar |
||
[ |
[ |
||
#if Control if:(aCode > 48) if:(aCode < 58) |
|||
⚫ | |||
[ |
[ |
||
theToken := (Token += aChar). |
|||
theTopNode += theToken. |
|||
#shift. |
#shift. |
||
] |
] |
||
| if:(aCode == 40) |
|||
[ |
|||
theToken := SubExpression. |
|||
theTopNode += theToken. |
|||
#shift Brackets. |
|||
] |
|||
⚫ | |||
] |
] |
||
} |
} |
||
#method real'get = theTopNode real. |
|||
#method |
#method numeric = theTopNode numeric. |
||
#method evaluate &numeric:aCode &literal:aChar |
|||
[ |
[ |
||
#if |
#if Control if:(aCode > 48) if:(aCode < 58) |
||
[ |
[ |
||
theToken += aChar. |
theToken += aChar. |
||
] |
] |
||
| if:( |
| if:(aCode == 42) // * |
||
[ |
[ |
||
theTopNode := theTopNode + ProductNode. |
theTopNode := theTopNode + ProductNode. |
||
#shift Operator. |
#shift Operator. |
||
] |
] |
||
| if:( |
| if:(aCode == 47) // / |
||
[ |
[ |
||
theTopNode := theTopNode + FractionNode. |
theTopNode := theTopNode + FractionNode. |
||
#shift Operator. |
#shift Operator. |
||
] |
] |
||
| if:( |
| if:(aCode == 43) // + |
||
[ |
[ |
||
theTopNode := theTopNode + SummaryNode. |
theTopNode := theTopNode + SummaryNode. |
||
#shift Operator. |
#shift Operator. |
||
] |
] |
||
| if:( |
| if:(aCode == 45) // - |
||
[ |
[ |
||
theTopNode := theTopNode + DifferenceNode. |
theTopNode := theTopNode + DifferenceNode. |
||
#shift Operator. |
#shift Operator. |
||
] |
] |
||
| if:( |
| if:(aCode == 40) |
||
[ |
[ |
||
theToken := SubExpression. |
theToken := SubExpression. |
||
theTopNode := theToken. |
theTopNode := theToken. |
||
#shift Brackets. |
#shift Brackets. |
||
] |
] |
||
| [ $self fail. ]. |
| [ $self fail. ]. |
||
] |
] |
||
#method new |
#method new |
||
[ |
[ |
||
#shift Start. |
#shift Start. |
||
] |
] |
||
#method $setBrackets |
#method $setBrackets |
||
[ |
[ |
||
Line 1,162: | Line 1,214: | ||
#symbol Program => |
#symbol Program => |
||
[ |
[ |
||
#var aText := |
#var aText := String. |
||
#loop (( |
#loop ((Console >> aText) length > 0)? |
||
[ |
[ |
||
#var aParser := Parser. |
#var aParser := Parser. |
||
io'Console << "=" << ctrl'Control do: => |
|||
Console << "=" << aText then: aText => |
|||
[ |
|||
ctrl'Control run &literal:aText &foreach: aParser. |
|||
Scan::aText run:aParser. |
|||
^ aParser numeric. |
|||
] |
] |
||
| << "Invalid Expression". |
| << "Invalid Expression". |
||
Console << "%n". |
|||
]. |
]. |
||
]. |
].</lang> |
||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |