-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzylogo_coda.coffee
182 lines (136 loc) · 5.27 KB
/
fizzylogo_coda.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#flContexts = []
mainTurtle = null
initContext = (context) ->
keywordsAndTheirInit = [
"WorkSpace", FLWorkspace # todo probably not needed?
"Class", FLClass
"List", FLList
"String", FLString
"Exception", FLException
"Number", FLNumber
"Boolean", FLBoolean
"Console", FLConsole
"Turtle", FLTurtle
"not", FLNot.createNew()
"true", FLBoolean.createNew true
"false", FLBoolean.createNew false
"for", FLFor.createNew()
"repeat1", FLRepeat1.createNew()
"done", FLDone.createNew()
"break", FLBreak.createNew()
"return", FLReturn.createNew()
"if", FLIfThen.createNew()
"forever", FLForever.createNew()
"repeat", FLRepeat2.createNew()
"try", FLTry.createNew()
"throw", FLThrow.createNew()
"to", FLTo.createNew()
"in", FLIn.createNew()
"accessUpperContext", FLAccessUpperContext.createNew()
"evaluationsCounter", FLEvaluationsCounter.createNew()
"nil", FLNil.createNew()
"console", FLConsole.createNew()
"turtle", (mainTurtle = FLTurtle.createNew())
"pause", FLPause.createNew()
"'", FLQuote.createNew()
":", FLQuote.createNew()
"-", FLUnaryMinus.createNew()
"[", FLListLiteralArrayNotationStarter.createNew()
]
for keywords in [0...keywordsAndTheirInit.length] by 2
[keyword, itsInitialisation] = keywordsAndTheirInit[keywords .. keywords + 1]
context.tempVariablesDict[ValidIDfromString keyword] = itsInitialisation
quickReset = ->
#flContexts = []
rWorkspace = FLWorkspace.createNew()
rWorkspace.environmentPrintout = ""
console.log "resetting rWorkspace.environmentErrors"
rWorkspace.environmentErrors = ""
outerMostContext = new FLContext null, rWorkspace
#flContexts.jsArrayPush outerMostContext
initContext outerMostContext
mainTurtle.sendHome()
# instead of clearing and re-initiating all the classes,
# let's just get rid of all the classes created after
# booting, also so we don't create a memory leak.
allClasses = bootClasses.slice()
for eachClass in allClasses
eachClass.resetInstanceVariables()
reset = ->
# resetting the classes and initing them
# adds quite a bit more time to the tests
# but it's worth checking once in a while
# that the tests behave well when the state
# is reset more deeply.
clearClasses()
initBootClasses()
#flContexts = []
rWorkspace = FLWorkspace.createNew()
rWorkspace.environmentPrintout = ""
console.log "resetting rWorkspace.environmentErrors"
rWorkspace.environmentErrors = ""
outerMostContext = new FLContext null, rWorkspace
#flContexts.jsArrayPush outerMostContext
initContext outerMostContext
mainTurtle.sendHome()
textOutputElement = null
canvasOutputElement = null
devicePixelRatio = 1
# this is called from the browser
Fizzylogo.init = (textOutElem, canvasOutElem, devPixRatio) ->
if textOutElem?
# don't do any logging when used in
# the browser
log = ->
dir = ->
textOutputElement = textOutElem
canvasOutputElement = canvasOutElem
devicePixelRatio = devPixRatio
# scale all drawing operations by the
# devicePixelRatio value, otherwise, say,
# in Retina Displays 2x we'd get drawings that
# are much smaller.
canvasOutputElement?.getContext('2d').scale devicePixelRatio, devicePixelRatio
reset()
# this one is for the browser, and the yielding is always
# enabled for the browser build, so this is always used
# as a generator.
Fizzylogo.runOneStep = (code) ->
quickReset()
parsed = flTokenize code
log "evaluation " + indentation() + "messaging workspace with " + parsed.flToString()
# yield from
returned = parsed.eval outerMostContext, parsed
outerMostContext.returned = returned
console.log "evaluation " + indentation() + "end of workspace evaluation"
console.log "rWorkspace.environmentPrintout " + rWorkspace.environmentPrintout
console.log "rWorkspace.environmentErrors " + rWorkspace.environmentErrors
if outerMostContext.throwing and outerMostContext.returned.flClass == FLException
log "evaluation " + indentation() + "exception: " + outerMostContext.returned.value
rWorkspace.environmentErrors += "! exception: " + outerMostContext.returned.value
log "rWorkspace.environmentErrors " + rWorkspace.environmentErrors
if textOutputElement?
textOutputElement.value += rWorkspace.environmentErrors + "\n"
return null
run = (code) ->
quickReset()
parsed = flTokenize code
log "evaluation " + indentation() + "messaging workspace with " + parsed.flToString()
yieldMode = false
#yieldMode = true
if yieldMode
gen = parsed.eval outerMostContext, parsed
until (ret = gen.next()).done
if ret.value?
log "obtained: " + ret.value
log "obtained: yieldingfromtoplevel"
outerMostContext.returned = ret.value
else
outerMostContext.returned = parsed.eval outerMostContext, parsed
log "evaluation " + indentation() + "end of workspace evaluation"
console.log "rWorkspace.environmentErrors " + rWorkspace.environmentErrors
if outerMostContext.throwing and outerMostContext.returned.flClass == FLException
log "evaluation " + indentation() + "exception: " + outerMostContext.returned.value
rWorkspace.environmentErrors += "! exception: " + outerMostContext.returned.value
log "rWorkspace.environmentErrors " + rWorkspace.environmentErrors
return null