Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Elena}}
m (→{{header|Sidef}}: corrected Javascript to '''JavaScript''') |
|||
Line 1,059:
<lang elena>#define system.
#define extensions.
// --- Token ---
#class Token
Line 1,086 ⟶ 1,088:
]
#method Number =
}
// --- Node ---
#method append : anObject▼
#class Node
Line 1,117 ⟶ 1,103:
theRight := aNode.
theState :=
]
Line 1,124 ⟶ 1,110:
theLeft := aNode.
theState :=
]
#constructor new
[
theState :=
]
Line 1,155 ⟶ 1,141:
].
]
▲ #method append : anObject
= $self~theState eval:anObject.
#method => theState.
}
// --- SummaryNode
#class SummaryNode : Node
Line 1,165 ⟶ 1,156:
#method Number = theLeft Number + theRight Number.
}
// --- DifferenceNode ---
#class DifferenceNode : Node
Line 1,172 ⟶ 1,165:
#method Number = theLeft Number - theRight Number.
}
// --- ProductNode ---
#class ProductNode : Node
Line 1,179 ⟶ 1,174:
#method Number = theLeft Number * theRight Number.
}
// --- FractionNode ---
#class FractionNode : Node
Line 1,186 ⟶ 1,183:
#method Number = theLeft Number / theRight Number.
}
// --- SubExpression ---
#class SubExpression
Line 1,229 ⟶ 1,228:
}
// ---- Parser ----
#class Parser : system'routines'BasePattern
Line 1,257 ⟶ 1,234:
#field theToken.
#field theTopNode.
#field
#method onBrackets : aChar
Line 1,265 ⟶ 1,242:
(theToken validate)
? [
].
]
Line 1,276 ⟶ 1,253:
theTopNode := theToken.
]
45 ? [ // -
Line 1,283 ⟶ 1,260:
theTopNode := theToken.
]
! [
theToken := Token new.
theTopNode := theToken.
$self appendDigit:aChar.
Line 1,300 ⟶ 1,277:
theToken := SubExpression new.
theTopNode += theToken.
]
! [
theToken := Token new.
theTopNode += theToken.
$self appendDigit:aChar.
Line 1,313 ⟶ 1,290:
#constructor new
[
]
Line 1,336 ⟶ 1,313:
theToken := SubExpression new.
theTopNode := theToken.
]
42 ? [ // *
theTopNode := theTopNode + ProductNode new.
]
43 ? [ // +
theTopNode := theTopNode + SummaryNode new.
]
45 ? [ // -
theTopNode := theTopNode + DifferenceNode new.
]
47 ? // /
Line 1,357 ⟶ 1,334:
theTopNode := theTopNode + FractionNode new.
]
! [
Line 1,364 ⟶ 1,341:
]
#method eval : aChar =
}
Line 1,371 ⟶ 1,348:
#var aText := String new.
control while:(consoleEx readLine:aText
[
#var aParser := Parser new.
|