Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Elena}}
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Combine into a single file for ease of testing, fix return type) |
|||
Line 1,061:
=={{header|Elena}}==
ELENA 3.
<lang elena>import system'routines.
import extensions.
Line 1,068:
class Token
{
object
constructor new(int
[
theValue := StringWriter new.
Line 1,083:
]
number = theValue
}
Line 1,090:
object prop left :: theLeft.
object prop right :: theRight.
constructor new(int
[
theLevel := aLevel.
Line 1,100:
class SummaryNode :: Node
{
constructor new(int
<= new
number = theLeft number + theRight number.
Line 1,108:
class DifferenceNode :: Node
{
constructor new(int
<= new
number = theLeft number - theRight number.
Line 1,116:
class ProductNode :: Node
{
constructor new(int
<= new
number = theLeft number * theRight number.
Line 1,124:
class FractionNode :: Node
{
constructor new(int
<= new
number = theLeft number / theRight number.
Line 1,132:
class Expression
{
object prop top :: theTop.
constructor new(int
[
theLevel := aLevel
Line 1,151:
ch =>
$40 [ // (
^
];
! [
^
].
]
Line 1,162:
ch =>
$41 [ // )
^
];
$42 [ // *
^
];
$43 [ // +
^
];
$45 [ // -
^
];
$47 [ // /
^
];
! [
^
].
]
Line 1,185:
ch =>
$40 [ // (
^
];
$45 [ // -
^
];
! [
^
].
]
Line 1,198:
{
object theState.
object theParser.
object theToken.
object theExpression.
constructor new
[
theState := startState.
theLevel := 0.
theExpression := Expression new
theParser := aParser.
]
Line 1,213:
newToken
[
theToken := theParser appendToken
]
Line 1,220:
theToken := nil.
theParser appendSummary
]
Line 1,227:
theToken := nil.
theParser appendDifference
]
Line 1,234:
theToken := nil.
theParser appendProduct
]
Line 1,241:
theToken := nil.
theParser appendFraction
]
Line 1,250:
theLevel := theLevel + 10.
theParser appendSubexpression
]
Line 1,268:
]
append
[
aLiteral forEach(:ch)[ self append:ch ]
Line 1,295:
class Parser
{
appendToken(object
[
var aToken := Token new
anExpression
^ aToken
]
appendSummary(object
[
anExpression
]
appendDifference(object
[
anExpression
]
appendProduct(object
[
anExpression
]
appendFraction(object
[
anExpression
]
appendSubexpression(object
[
anExpression
]
append(object
[
if(
[ ^ aNewNode ].
if (aNewNode level <= aLastNode level)
[ aNewNode
var aParent := aLastNode.
var aCurrent := aLastNode right.
while ((
[ aParent := aCurrent. aCurrent := aCurrent right. ].
if (
[ aParent
[ aNewNode
^ aLastNode
Line 1,351:
run : aText
[
var aScope := Scope new
aText forEach(:ch)[ aScope eval:ch ].
Line 1,359:
}
public program =
[
var aText :=
var aParser := Parser new.
|