Arithmetic evaluation: Difference between revisions

No edit summary
Line 928:
 
=={{header|Elena}}==
<lang elena>#define basic'* = std'basic'*.
#define ctrl'* = std'patternsroutines'*.
#define iostd'* = ext'iopatterns'*.
#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 := basic'String.
]
#method real'get = basic'RealType evaluate:theValue.
#method numeric = Real64Convert eval:(theValue literal).
}
 
Line 963 ⟶ 972:
#method += aNode
[
theLeft := aNode.
]
$self $setLeftAssigned.
]
}
#role LeftAssigned
{
#method += aNode
[
theRight := aNode.
#shift.
#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 real'getnumeric = theLeft real'getnumeric + theRight real'getnumeric.
}
 
#class DifferenceNode (Node)
{
#method parse_order'get = 2.
#method real'getnumeric = theLeft real'getnumeric - theRight real'getnumeric.
}
 
#class ProductNode (Node)
{
#method parse_order'get = 1.
#method real'getnumeric = theLeft real'getnumeric * theRight real'getnumeric.
}
 
#class FractionNode (Node)
{
#method parse_order'get = 1.
#method real'getnumeric = theLeft real'getnumeric / theRight real'getnumeric.
}
 
Line 1,037 ⟶ 1,058:
#role EOF
{
#method eof'is []
#method += aChar [ $self fail. ]
}
#method parse_order'get = 0.
#method + aNode
[
^ aNode += self.
]
#method += aChar
#method append &numeric:aCode
[
#if ctrl'Controlcontrol if:(aCharaCode == 41)
[
theCounter -= 1.
]
| if:(aCharaCode == 40)
[
theCounter += 1.
].
#if(theCounter == 0)?
[ #shift EOF. ^ $self. ].
 
theParser evaluate:aChar$param.
]
#method real'getnumeric = theParser realnumeric.
#method new
[
theParser := arithmeval'Parser.
theCounter := basic'Integer << 1.
]
}
Line 1,077 ⟶ 1,105:
#method evaluate : aChar
[
#if (aChar numeric == 40)?
[
theToken := SubExpression.
theTopNode := theToken.
#if theToken eof'is
$self $setBrackets.
]
| [
theToken := Token.
theTopNode := theToken.
theToken += aChar.
theToken += aChar.
#shift.
].
]
}
#role Operator
#role Brackets
{
#method evaluate : aChar
[
#iftheToken ctrl'Control+= if:(aChar > 48) if:(aChar < 58).
#if theToken eof'is
[
theToken := (Token += aChar)#shift.
theTopNode += theToken].
]
| [ $self fail. ].
#shift.
]
}
#role Brackets
#role Operator
{
#method evaluate &numeric:aCode &literal:aChar
[
theToken += aChar. #if Control if:(aCode > 48) if:(aCode < 58)
#if theToken eof'is
[
theToken := (Token += aChar).
theTopNode += theToken.
#shift.
].
| if:(aCode == 40)
[
theToken := SubExpression.
theTopNode += theToken.
#shift Brackets.
]
| [ $self fail. ].
]
}
#method real'get = theTopNode real.
#method evaluatenumeric := aChartheTopNode numeric.
#method evaluate &numeric:aCode &literal:aChar
[
#if ctrl'Control if:(aCharaCode > 48) if:(aCharaCode < 58)
[
theToken += aChar.
]
| if:(aCharaCode == 42) // *
[
theTopNode := theTopNode + ProductNode.
#shift Operator.
]
| if:(aCharaCode == 47) // /
[
theTopNode := theTopNode + FractionNode.
#shift Operator.
]
| if:(aCharaCode == 43) // +
[
theTopNode := theTopNode + SummaryNode.
#shift Operator.
]
| if:(aCharaCode == 45) // -
[
theTopNode := theTopNode + DifferenceNode.
#shift Operator.
]
| if:(aCharaCode == 40)
[
theToken := SubExpression.
theTopNode := theToken.
#shift Brackets.
]
| [ $self fail. ].
]
#method new
[
#shift Start.
]
#method $setBrackets
[
Line 1,162 ⟶ 1,214:
#symbol Program =>
[
#var aText := basic'String.
#loop ((io'Console >> aText) length > 0)?
[
#var aParser := Parser.
 
io'Console << "=" << ctrl'Control do: =>
[ Console << "=" << aText then: aText =>
[
ctrl'Control run &literal:aText &foreach: aParser.
^Scan::aText run:aParser real.
^ aParser numeric.
]
| << "Invalid Expression".
io'Console << "%n".
].
].</lang>
</lang>
 
=={{header|Factor}}==
Anonymous user