Arithmetic evaluation: Difference between revisions

Content added Content deleted
No edit summary
Line 928: Line 928:


=={{header|Elena}}==
=={{header|Elena}}==
<lang elena>#define basic'* = std'basic'*.
<lang elena>#define std'basic'*.
#define ctrl'* = std'patterns'*.
#define std'routines'*.
#define io'* = ext'io'*.
#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 := basic'String.
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 real'get = theLeft real'get + theRight real'get.
#method numeric = theLeft numeric + theRight numeric.
}
}


#class DifferenceNode (Node)
#class DifferenceNode (Node)
{
{
#method parse_order'get = 2.
#method parse_order'get = 2.
#method real'get = theLeft real'get - theRight real'get.
#method numeric = theLeft numeric - theRight numeric.
}
}


#class ProductNode (Node)
#class ProductNode (Node)
{
{
#method parse_order'get = 1.
#method parse_order'get = 1.
#method real'get = theLeft real'get * theRight real'get.
#method numeric = theLeft numeric * theRight numeric.
}
}


#class FractionNode (Node)
#class FractionNode (Node)
{
{
#method parse_order'get = 1.
#method parse_order'get = 1.
#method real'get = theLeft real'get / theRight real'get.
#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 ctrl'Control if:(aChar == 41)
#if control if:(aCode == 41)
[
[
theCounter -= 1.
theCounter -= 1.
]
]
| if:(aChar == 40)
| if:(aCode == 40)
[
[
theCounter += 1.
theCounter += 1.
].
].
#if(theCounter == 0)?
#if(theCounter == 0)?
[ #shift EOF. ^ $self. ].
[ #shift EOF. ^ $self. ].

theParser evaluate:aChar.
theParser evaluate:$param.
]
]
#method real'get = theParser real.
#method numeric = theParser numeric.
#method new
#method new
[
[
theParser := arithmeval'Parser.
theParser := arithmeval'Parser.
theCounter := basic'Integer << 1.
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.
theToken += aChar.
#shift.
#shift.
].
].
]
]
}
}
#role Operator
#role Brackets
{
{
#method evaluate : aChar
#method evaluate : aChar
[
[
#if ctrl'Control if:(aChar > 48) if:(aChar < 58)
theToken += aChar.
#if theToken eof'is
[
[
theToken := (Token += aChar).
#shift.
theTopNode += theToken.
].
]
| [ $self fail. ].
#shift.
]
]
}
}
#role Brackets
#role Operator
{
{
#method evaluate : aChar
#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.
#shift.
].
]
| if:(aCode == 40)
[
theToken := SubExpression.
theTopNode += theToken.
#shift Brackets.
]
| [ $self fail. ].
]
]
}
}
#method real'get = theTopNode real.
#method evaluate : aChar
#method numeric = theTopNode numeric.
#method evaluate &numeric:aCode &literal:aChar
[
[
#if ctrl'Control if:(aChar > 48) if:(aChar < 58)
#if Control if:(aCode > 48) if:(aCode < 58)
[
[
theToken += aChar.
theToken += aChar.
]
]
| if:(aChar == 42) // *
| if:(aCode == 42) // *
[
[
theTopNode := theTopNode + ProductNode.
theTopNode := theTopNode + ProductNode.
#shift Operator.
#shift Operator.
]
]
| if:(aChar == 47) // /
| if:(aCode == 47) // /
[
[
theTopNode := theTopNode + FractionNode.
theTopNode := theTopNode + FractionNode.
#shift Operator.
#shift Operator.
]
]
| if:(aChar == 43) // +
| if:(aCode == 43) // +
[
[
theTopNode := theTopNode + SummaryNode.
theTopNode := theTopNode + SummaryNode.
#shift Operator.
#shift Operator.
]
]
| if:(aChar == 45) // -
| if:(aCode == 45) // -
[
[
theTopNode := theTopNode + DifferenceNode.
theTopNode := theTopNode + DifferenceNode.
#shift Operator.
#shift Operator.
]
]
| if:(aChar == 40)
| 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 := basic'String.
#var aText := String.
#loop ((io'Console >> aText) length > 0)?
#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.
^ aParser real.
Scan::aText run:aParser.
^ aParser numeric.
]
]
| << "Invalid Expression".
| << "Invalid Expression".
io'Console << "%n".
Console << "%n".
].
].
].
].</lang>
</lang>


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