evaluator: add environment, make definitions work, add tests

This commit is contained in:
2018-04-30 16:48:27 +02:00
parent 584f9b24c7
commit 92a2ecbd8e
8 changed files with 36 additions and 9 deletions

View File

@@ -1,3 +1,3 @@
as yet unclassified as yet unclassified
eval: expression eval: expr
^ self eval: expression in: nil ^ self eval: expr in: env

View File

@@ -1,3 +1,9 @@
as yet unclassified as yet unclassified
eval: expression in: anEnvironment eval: expr in: environ
^ expression 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 ]
]

View File

@@ -0,0 +1,5 @@
as yet unclassified
evalDefine: expr in: environ
environ at: expr second
put: (self eval: expr third in: environ).
^ #undefined

View File

@@ -0,0 +1,4 @@
initialization
initialize
super initialize.
env := Dictionary new

View File

@@ -1,4 +1,4 @@
as yet unclassified as yet unclassified
parse: aProgramString parse: prog
aProgramString ifEmpty: [ ^ #() ]. prog ifEmpty: [ ^ #() ].
^ (Scanner new scanTokens: aProgramString) first ^ (Scanner new scanTokens: prog) first

View File

@@ -1,3 +1,3 @@
as yet unclassified as yet unclassified
parseAndEval: anExpression parseAndEval: expr
^ self eval: (self parse: anExpression) ^ self eval: (self parse: expr)

View File

@@ -0,0 +1,6 @@
tests
testDefineExpression
ph parseAndEval: '(define pi 3.14)'.
self
assert: (ph parseAndEval: 'pi')
equals: 3.14

View File

@@ -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