evaluator: add environment, make definitions work, add tests
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
as yet unclassified
|
||||
eval: expression
|
||||
^ self eval: expression in: nil
|
||||
eval: expr
|
||||
^ self eval: expr in: env
|
@@ -1,3 +1,9 @@
|
||||
as yet unclassified
|
||||
eval: expression in: anEnvironment
|
||||
^ expression
|
||||
eval: expr in: environ
|
||||
expr = #() ifTrue: [ ^ expr ].
|
||||
expr isSymbol ifTrue: [ ^ environ at: expr ]. "returns the variable value"
|
||||
expr isArray ifFalse: [ ^ expr ] "returns literals boolean, string, number"
|
||||
ifTrue: [
|
||||
expr first = #define
|
||||
ifTrue: [ ^ self evalDefine: expr in: environ ]
|
||||
]
|
@@ -0,0 +1,5 @@
|
||||
as yet unclassified
|
||||
evalDefine: expr in: environ
|
||||
environ at: expr second
|
||||
put: (self eval: expr third in: environ).
|
||||
^ #undefined
|
4
src/Phsyche.package/Phsyche.class/instance/initialize.st
Normal file
4
src/Phsyche.package/Phsyche.class/instance/initialize.st
Normal file
@@ -0,0 +1,4 @@
|
||||
initialization
|
||||
initialize
|
||||
super initialize.
|
||||
env := Dictionary new
|
@@ -1,4 +1,4 @@
|
||||
as yet unclassified
|
||||
parse: aProgramString
|
||||
aProgramString ifEmpty: [ ^ #() ].
|
||||
^ (Scanner new scanTokens: aProgramString) first
|
||||
parse: prog
|
||||
prog ifEmpty: [ ^ #() ].
|
||||
^ (Scanner new scanTokens: prog) first
|
@@ -1,3 +1,3 @@
|
||||
as yet unclassified
|
||||
parseAndEval: anExpression
|
||||
^ self eval: (self parse: anExpression)
|
||||
parseAndEval: expr
|
||||
^ self eval: (self parse: expr)
|
@@ -0,0 +1,6 @@
|
||||
tests
|
||||
testDefineExpression
|
||||
ph parseAndEval: '(define pi 3.14)'.
|
||||
self
|
||||
assert: (ph parseAndEval: 'pi')
|
||||
equals: 3.14
|
@@ -0,0 +1,6 @@
|
||||
tests
|
||||
testEvalExpressionReassignment
|
||||
ph parseAndEval: '(define pi 3.14)'.
|
||||
ph parseAndEval: '(define pi2 pi)'.
|
||||
ph parseAndEval: '(define pi 6.28)'.
|
||||
self assert: (ph parseAndEval: 'pi2') equals: 3.14
|
Reference in New Issue
Block a user