Anonymous user
Arithmetic evaluation: Difference between revisions
→{{header|Elena}}
(added Scheme example) |
|||
Line 1,061:
=={{header|Elena}}==
ELENA 3.x :
<lang elena>#import system'routines.
#import extensions.
#import extensions'text.
{
[
theValue := StringWriter new.
Line 1,077:
]
[
theValue << aChar.
]
}
{
[
theLevel := aLevel.
]
}
{
<=
}
{
<=
}
{
<=
}
{
<=
}
{
[
theLevel := aLevel.
]
}
[
ch =>
#40
self new &bracket goto &start.
];
! [
self new &token append:ch goto &token.
].
].
[
ch =>
#41
self close &bracket goto &token.
];
#42
self new &product goto &operator.
];
#43
self new &summary goto &operator.
];
#45
self new &difference goto &operator.
];
#47
self new &fraction goto &operator.
];
! [
self append:ch.
Line 1,199 ⟶ 1,180:
].
[
ch =>
#40
self new &bracket goto &start.
];
#45
self new &token append &literal:"0" new &difference goto &operator.
];
! [
self new &token append:ch goto &token.
Line 1,213 ⟶ 1,194:
].
{
[
theState := startState.
Line 1,229 ⟶ 1,210:
]
[
theToken := theParser
]
[
theToken := nil.
theParser
]
[
theToken := nil.
theParser
]
[
theToken := nil.
theParser
]
[
theToken := nil.
theParser
]
[
theToken := nil.
Line 1,268 ⟶ 1,249:
theLevel := theLevel + 10.
theParser
]
[
if (theLevel < 10)
theLevel := theLevel - 10.
]
[
if((ch >= #48)
]
[
aLiteral run &each: ch [ self append:ch. ].
]
[
theState := startState.
]
[
theState := tokenState.
]
[
theState := operatorState.
]
}
{
[
anExpression set &top:($self append &last:(anExpression top) &new:aToken).
Line 1,322 ⟶ 1,303:
]
[
anExpression set &top:($self append &last:(anExpression top) &new:(SummaryNode new &level:aLevel)).
]
[
anExpression set &top:($self append &last:(anExpression top) &new:(DifferenceNode new &level:aLevel)).
]
[
anExpression set &top:($self append &last:(anExpression top) &new:(ProductNode new &level:aLevel)).
]
[
anExpression set &top:($self append &last:(anExpression top) &new:(FractionNode new &level:aLevel)).
]
[
anExpression set &top:($self append &last:(anExpression top) &new:(Expression new &level:aLevel)).
]
[
($nil == aLastNode)
? [ ^ aNewNode. ].
if (aNewNode level <= aLastNode level)
[ aParent := aCurrent. aCurrent := aCurrent right. ].
if ($nil == aCurrent)
^ aLastNode.
]
[
aText run &each: ch [ aScope eval:ch. ].
Line 1,377 ⟶ 1,358:
}
[
[ console readLine save &to:aText length > 0] doWhile:
[
try(console writeLine:"=" :(aParser run:aText))
! e
▲ ]
aText clear.
|