evaluator: add if and primitives and tests
This commit is contained in:
3
src/Phsyche.package/Phsyche.class/instance/carBinding.st
Normal file
3
src/Phsyche.package/Phsyche.class/instance/carBinding.st
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
carBinding
|
||||||
|
^ #car -> [ :l | l first ]
|
3
src/Phsyche.package/Phsyche.class/instance/cdrBinding.st
Normal file
3
src/Phsyche.package/Phsyche.class/instance/cdrBinding.st
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
cdrBinding
|
||||||
|
^ #cdr -> [ :l | l allButFirst ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
consBinding
|
||||||
|
^ #cons -> [ :e :l | {e} , l ]
|
3
src/Phsyche.package/Phsyche.class/instance/divBinding.st
Normal file
3
src/Phsyche.package/Phsyche.class/instance/divBinding.st
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
divBinding
|
||||||
|
^ #+ -> [ :e :v | e / v asFloat ]
|
@@ -4,6 +4,12 @@ eval: expr in: environ
|
|||||||
expr isSymbol ifTrue: [ ^ environ at: expr ]. "returns the variable value"
|
expr isSymbol ifTrue: [ ^ environ at: expr ]. "returns the variable value"
|
||||||
expr isArray ifFalse: [ ^ expr ] "returns literals boolean, string, number"
|
expr isArray ifFalse: [ ^ expr ] "returns literals boolean, string, number"
|
||||||
ifTrue: [
|
ifTrue: [
|
||||||
expr first = #define
|
| fst |
|
||||||
ifTrue: [ ^ self evalDefine: expr in: environ ]
|
fst := expr first.
|
||||||
|
(prims includes: fst) ifTrue: [
|
||||||
|
^ self evalPrim: expr in: environ
|
||||||
|
]. "evaluates primitive"
|
||||||
|
fst = #define ifTrue: [ ^ self evalDefine: expr in: environ ]. "defines the value"
|
||||||
|
fst = #if ifTrue: [ ^ self evalIf: expr in: environ]. "evaluates if"
|
||||||
|
fst = #quote ifTrue: [ ^ expr second ]. "returns the value unevaluated"
|
||||||
]
|
]
|
5
src/Phsyche.package/Phsyche.class/instance/evalIf.in..st
Normal file
5
src/Phsyche.package/Phsyche.class/instance/evalIf.in..st
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
as yet unclassified
|
||||||
|
evalIf: expr in: environ
|
||||||
|
^ (self eval: expr second in: environ)
|
||||||
|
ifTrue: [ self eval: expr third in: environ ]
|
||||||
|
ifFalse: [ self eval: expr fourth in: environ ]
|
@@ -0,0 +1,5 @@
|
|||||||
|
as yet unclassified
|
||||||
|
evalPrim: expr in: environ
|
||||||
|
| method |
|
||||||
|
method := (environ at: expr first).
|
||||||
|
^ method valueWithArguments: (expr allButFirst collect: [ :x | self eval: x in: environ])
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
greaterBinding
|
||||||
|
^ #> -> [ :e :v | e > v ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
greaterOrEqualBinding
|
||||||
|
^ #>= -> [ :e :v | e >= v ]
|
@@ -1,4 +1,6 @@
|
|||||||
initialization
|
initialization
|
||||||
initialize
|
initialize
|
||||||
super initialize.
|
super initialize.
|
||||||
env := Dictionary new
|
env := Dictionary new.
|
||||||
|
prims := OrderedCollection new.
|
||||||
|
self initializeEnvBindings
|
@@ -0,0 +1,9 @@
|
|||||||
|
as yet unclassified
|
||||||
|
initializeEnvBindings
|
||||||
|
(self class selectors select: [ :each | each endsWithSubCollection: 'Binding' ])
|
||||||
|
do: [ :s |
|
||||||
|
| binding |
|
||||||
|
binding := self perform: s.
|
||||||
|
prims add: binding key.
|
||||||
|
env at: binding key put: binding value
|
||||||
|
]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
isEqualBinding
|
||||||
|
^ #equal -> [ :e :v | e = v ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
isNotBinding
|
||||||
|
^ #not -> [ :a | a not ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
isNullBinding
|
||||||
|
^ #isNull -> [ :l | l = #() ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
minusBinding
|
||||||
|
^ #+ -> [ :e :v | e - v ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
multBinding
|
||||||
|
^ #* -> [ :e :v | e * v ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
plusBinding
|
||||||
|
^ #+ -> [ :e :v | e + v ]
|
@@ -0,0 +1,3 @@
|
|||||||
|
as yet unclassified
|
||||||
|
smallerOrEqualBinding
|
||||||
|
^ #<= -> [ :e :v | e <= v ]
|
@@ -6,7 +6,8 @@
|
|||||||
],
|
],
|
||||||
"commentStamp" : "VeitHeller 4/30/2018 16:32",
|
"commentStamp" : "VeitHeller 4/30/2018 16:32",
|
||||||
"instvars" : [
|
"instvars" : [
|
||||||
"env" ],
|
"env",
|
||||||
|
"prims" ],
|
||||||
"name" : "Phsyche",
|
"name" : "Phsyche",
|
||||||
"pools" : [
|
"pools" : [
|
||||||
],
|
],
|
||||||
|
@@ -0,0 +1,5 @@
|
|||||||
|
tests
|
||||||
|
testEvalCarExpressionEvaluatesItsArgument
|
||||||
|
self
|
||||||
|
assert: (ph parseAndEval: '(car (cons (quote a) (cons (quote b) ())))')
|
||||||
|
equals: #a
|
@@ -0,0 +1,3 @@
|
|||||||
|
tests
|
||||||
|
testEvalCdrExpressionEvaluatesItsArgument
|
||||||
|
self assert: (ph parseAndEval: '(cdr (quote (quote a)))') equals: #(a)
|
@@ -0,0 +1,4 @@
|
|||||||
|
tests
|
||||||
|
testEvalExpressionArith
|
||||||
|
self assert: (ph parseAndEval: '(* (+ 2 3) 8)') equals: 40.
|
||||||
|
self assert: (ph parseAndEval: '(* 8 (+ 2 3))') equals: 40
|
@@ -0,0 +1,3 @@
|
|||||||
|
tests
|
||||||
|
testEvalExpressionMult
|
||||||
|
self assert: (ph parseAndEval: '(* 3 8)') equals: 24
|
@@ -0,0 +1,4 @@
|
|||||||
|
tests
|
||||||
|
testEvalIf
|
||||||
|
self assert: (ph parseAndEval: '(if true 4 5)') equals: 4.
|
||||||
|
self assert: (ph parseAndEval: '(if false 4 5)') equals: 5
|
@@ -0,0 +1,3 @@
|
|||||||
|
tests
|
||||||
|
testEvalListExpression
|
||||||
|
self assert: (ph parseAndEval: '(cons (quote a) ())') equals: #(a)
|
@@ -0,0 +1,8 @@
|
|||||||
|
tests
|
||||||
|
testEvalQuote
|
||||||
|
self
|
||||||
|
assert: (ph parseAndEval: '(quote (* x x))')
|
||||||
|
equals: #(#* #x #x).
|
||||||
|
self
|
||||||
|
assert: (ph parseAndEval: '(quote (quote (* x x)))')
|
||||||
|
equals: #(quote #(#* #x #x))
|
@@ -0,0 +1,5 @@
|
|||||||
|
tests
|
||||||
|
testIsNull
|
||||||
|
self assert: (ph parseAndEval: '(isNull #())').
|
||||||
|
self assert: (ph parseAndEval: '(isNull (quote ()))').
|
||||||
|
self deny: (ph parseAndEval: '(isNull (cons (quote a) #()))')
|
@@ -0,0 +1,4 @@
|
|||||||
|
tests
|
||||||
|
testNot
|
||||||
|
self assert: (ph parseAndEval: '(not false)').
|
||||||
|
self deny: (ph parseAndEval: '(not true)')
|
Reference in New Issue
Block a user