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