Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Elena}}
m (Liberty BASIC added) |
|||
Line 1,057:
=={{header|Elena}}==
<lang elena>#define
#define
#class Token
Line 1,069 ⟶ 1,064:
#field theValue.
#
[
theValue := String new.
]
#constructor new : aValue
[
theValue := String new:aValue.
]
#method
#method append : aChar
[
theValue += aChar.
]
#method
[
^ aNode += self.
]
#method
}
#class(role) ENoneAssigned
{
#method append : anObject
=> %setLeft.
}
#class(role) ELeftAssigned
{
#method append : anObject
=> %setRight.
}
#class(role) ERightAssigned
{
#method append : anObject
=> %appendRight.
}
#class Node
Line 1,094 ⟶ 1,111:
#field theLeft.
#field theRight.
#field theState.
#
[
theState := ERightAssigned.
]
#method
[
theState
]
#constructor new
[
theState := ENoneAssigned.
]
#method add : aNode
= (self ParseOrder > aNode ParseOrder)
? [
self += aNode.
^ self.
]
! [
aNode += self.
^ aNode.
].
#method
[
? [
theRight += aNode.
]
theRight := aNode += theRight.
].
]
#method => theState.
}
#class SummaryNode
{
#method
#method
}
#class DifferenceNode : Node
{
#method
#method
}
#class ProductNode : Node
{
#method
#method
}
#class FractionNode : Node
{
#method
#method
}
#class SubExpression
{
Line 1,172 ⟶ 1,192:
#field theCounter.
#
[
theParser := arithmeval'Parser new.
theCounter := Integer
]
#method ParseOrder = 0.
#method
[
^ aNode += self.
]
#method
[
? [ #throw Exception new:"Invalid expression". ].
]
#method
[
aChar =>
41 ? [
theCounter -= 1.
]
40 ? [ theCounter += 1 ]
! [ theParser evaluate:aChar ].
]
#method Number
= $self validate
? [ theParser Number ]
! [ #throw Exception new:"Invalid expression". ].
}
#class(role)EStartState
{
#method eval : aChar
=> %onStart.
}
#class(role)EBracketState
{
#method eval : aChar
=> %onBrackets.
}
#class(role)EOperatorState
{
#method eval : aChar
=> %onOperator.
}
#class(role)ENumberState
{
#method eval : aChar
=> %onDigit.
}
#class Parser : system'routines'BasePattern
{
#field theToken.
#field theTopNode.
#field theRole.
#method onBrackets : aChar
?
]
#method onStart :
40 ? [ // (
theTopNode := theToken.
theRole := EBracketState.
]
45 ? [ // -
theToken := DifferenceNode new add:(Token new:"0").
theRole := EOperatorState.
]
theToken := Token new.
theTopNode := theToken.
theRole := ENumberState.
].
#method onOperator : aChar
40 ? [
theTopNode += theToken.
theRole := EBracketState.
]
theTopNode += theToken.
theRole := ENumberState.
#constructor new
[
]
#method
#method
[
? [
theToken += aChar.
]
#throw Exception new:"Invalid expression".
]
]
#method
[
40 ? [ // (
theTopNode := theToken.
theRole := EBracketState.
]
42 ? [ // *
theTopNode := theTopNode + ProductNode new.
theRole := EOperatorState.
]
43 ? [ // +
theTopNode := theTopNode + SummaryNode new.
theRole := EOperatorState.
]
45 ? [ // -
theTopNode := theTopNode + DifferenceNode new.
theRole := EOperatorState.
]
47 ? // /
[
theTopNode := theTopNode + FractionNode new.
theRole := EOperatorState.
]
! [
$self appendDigit:aChar.
].
]
#method => theRole.
}
#symbol
[
#var aText := String new.
[
#var aParser := Parser new.
| ifFailed:
consoleEx writeLine:"Invalid Expression".
].
].
].</lang>
=== ELENA VM script ===
|