Arithmetic evaluation: Difference between revisions

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 = convertControlconvertor toReal:theValue.
}
 
// --- Node ---
#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,117 ⟶ 1,103:
theRight := aNode.
theState := ERightAssigned%appendRight.
]
Line 1,124 ⟶ 1,110:
theLeft := aNode.
theState := ELeftAssigned%setRight.
]
#constructor new
[
theState := ENoneAssigned%setLeft.
]
 
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(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
Line 1,257 ⟶ 1,234:
#field theToken.
#field theTopNode.
#field theRoletheState.
#method onBrackets : aChar
Line 1,265 ⟶ 1,242:
(theToken validate)
? [
theRoletheState := ENumberState%onDigit.
].
]
Line 1,276 ⟶ 1,253:
theTopNode := theToken.
theRoletheState := EBracketState%onBrackets.
]
45 ? [ // -
Line 1,283 ⟶ 1,260:
theTopNode := theToken.
theRoletheState := EOperatorState%onOperator.
]
! [
theToken := Token new.
theTopNode := theToken.
theRoletheState := ENumberState%onDigit.
$self appendDigit:aChar.
Line 1,300 ⟶ 1,277:
theToken := SubExpression new.
theTopNode += theToken.
theRoletheState := EBracketState%onBrackets.
]
! [
theToken := Token new.
theTopNode += theToken.
theRoletheState := ENumberState%onDigit.
 
$self appendDigit:aChar.
Line 1,313 ⟶ 1,290:
#constructor new
[
theRoletheState := EStartState%onStart.
]
 
Line 1,336 ⟶ 1,313:
theToken := SubExpression new.
theTopNode := theToken.
theRoletheState := EBracketState%onBrackets.
]
42 ? [ // *
theTopNode := theTopNode + ProductNode new.
 
theRoletheState := EOperatorState%onOperator.
]
43 ? [ // +
theTopNode := theTopNode + SummaryNode new.
 
theRoletheState := EOperatorState%onOperator.
]
45 ? [ // -
theTopNode := theTopNode + DifferenceNode new.
 
theRoletheState := EOperatorState%onOperator.
]
47 ? // /
Line 1,357 ⟶ 1,334:
theTopNode := theTopNode + FractionNode new.
 
theRoletheState := EOperatorState%onOperator.
]
! [
Line 1,364 ⟶ 1,341:
]
 
#method eval : aChar => theRole$self~theState eval:aChar.
}
 
Line 1,371 ⟶ 1,348:
#var aText := String new.
 
control while:(consoleEx readLine:aText Lengthlength > 0) &do:
[
#var aParser := Parser new.
Anonymous user