@@ -10,29 +10,29 @@ import (
1010
1111var jumpReturns []* int
1212
13- func (c * Compiler ) Compile (node ast.Node ) error {
13+ func (c * Compiler ) Compile (node ast.Node , root , identifier , previous string ) error {
1414 switch node := node .(type ) {
1515 case * ast.Program :
1616 for _ , stmt := range node .Statements {
17- err := c .Compile (stmt )
17+ err := c .Compile (stmt , root , identifier , previous )
1818 if err != nil {
1919 return err
2020 }
2121 }
2222 case * ast.ExpressionStatement :
23- err := c .Compile (node .Expression )
23+ err := c .Compile (node .Expression , root , "" , previous )
2424 if err != nil {
2525 return err
2626 }
2727 c .emit (code .OpPop )
2828 case * ast.SuffixExpression :
2929 if node .Operator == "<" {
30- err := c .Compile (node .Right )
30+ err := c .Compile (node .Right , root , "" , previous )
3131 if err != nil {
3232 return err
3333 }
3434
35- err = c .Compile (node .Left )
35+ err = c .Compile (node .Left , root , "" , previous )
3636 if err != nil {
3737 return err
3838 }
@@ -41,12 +41,12 @@ func (c *Compiler) Compile(node ast.Node) error {
4141 return nil
4242 }
4343
44- err := c .Compile (node .Left )
44+ err := c .Compile (node .Left , root , "" , previous )
4545 if err != nil {
4646 return err
4747 }
4848
49- err = c .Compile (node .Right )
49+ err = c .Compile (node .Right , root , "" , previous )
5050 if err != nil {
5151 return err
5252 }
@@ -84,14 +84,14 @@ func (c *Compiler) Compile(node ast.Node) error {
8484 }
8585 case * ast.While :
8686 startPos := len (c .currentInstructions ())
87- err := c .Compile (node .Condition )
87+ err := c .Compile (node .Condition , root , "" , previous )
8888 if err != nil {
8989 return err
9090 }
9191
9292 jumpPos := c .emit (code .OpJumpIfNotTrue , 9999 )
9393
94- err = c .Compile (node .Block )
94+ err = c .Compile (node .Block , root , "" , previous )
9595 if err != nil {
9696 return err
9797 }
@@ -113,14 +113,14 @@ func (c *Compiler) Compile(node ast.Node) error {
113113 jumpReturns = []* int {}
114114 }
115115 case * ast.ConditionalStatement :
116- err := c .Compile (node .Condition )
116+ err := c .Compile (node .Condition , root , "" , previous )
117117 if err != nil {
118118 return err
119119 }
120120
121121 jumpPos := c .emit (code .OpJumpIfNotTrue , 9999 )
122122
123- err = c .Compile (node .Body )
123+ err = c .Compile (node .Body , root , "" , previous )
124124 if err != nil {
125125 return err
126126 }
@@ -137,7 +137,7 @@ func (c *Compiler) Compile(node ast.Node) error {
137137 if node .ElseCondition == nil && node .ElseStatement == nil {
138138 c .emit (code .OpNull )
139139 } else if node .ElseCondition != nil {
140- err := c .Compile (node .ElseCondition )
140+ err := c .Compile (node .ElseCondition , root , "" , previous )
141141 if err != nil {
142142 return nil
143143 }
@@ -146,7 +146,7 @@ func (c *Compiler) Compile(node ast.Node) error {
146146 c .removeLastPop ()
147147 }
148148 } else if node .ElseStatement != nil {
149- err := c .Compile (node .ElseStatement )
149+ err := c .Compile (node .ElseStatement , root , "" , previous )
150150 if err != nil {
151151 return nil
152152 }
@@ -166,7 +166,7 @@ func (c *Compiler) Compile(node ast.Node) error {
166166 case * ast.BlockStatement :
167167 c .currentScope = c .deeperScope ()
168168 for _ , s := range node .Statements {
169- err := c .Compile (s )
169+ err := c .Compile (s , root , "" , previous )
170170 if err != nil {
171171 return err
172172 }
@@ -176,35 +176,41 @@ func (c *Compiler) Compile(node ast.Node) error {
176176 case * ast.VariableDeclaration :
177177 index := c .variables
178178
179+ name := node .Identifier .Value
180+
181+ if root != "" {
182+ name = "_INTERNAL_" + root + "" + name
183+ }
184+
179185 c .currentScope .Variables [index ] = Variable {
180- Name : node . Identifier . Value ,
186+ Name : name ,
181187 Index : index ,
182188 Object : & object.Null {},
183189 }
184190
185191 c .variables ++
186192
187- err := c .Compile (node .Value )
193+ err := c .Compile (node .Value , root , "" , previous )
188194 if err != nil {
189195 return err
190196 }
191197
192198 c .emit (code .OpSetVar , index )
193199 case * ast.Assign :
194- variable := c .currentScope .FindByName (node .Identifier .Value )
200+ variable := c .currentScope .FindByName (node .Identifier .Value , root )
195201
196202 if variable == nil {
197203 return fmt .Errorf ("undefined variable %s" , node .Identifier .Value )
198204 }
199205
200- err := c .Compile (node .Value )
206+ err := c .Compile (node .Value , root , "" , previous )
201207 if err != nil {
202208 return err
203209 }
204210
205211 c .emit (code .OpSetVar , variable .Index )
206212 case * ast.Identifier :
207- variable := c .currentScope .FindByName (node .Value )
213+ variable := c .currentScope .FindByName (node .Value , root )
208214
209215 if variable != nil {
210216 c .emit (code .OpGetVar , variable .Index )
@@ -217,20 +223,20 @@ func (c *Compiler) Compile(node ast.Node) error {
217223 }
218224 case * ast.Array :
219225 for _ , element := range node .Elements {
220- err := c .Compile (element )
226+ err := c .Compile (element , root , "" , previous )
221227 if err != nil {
222228 return err
223229 }
224230 }
225231
226232 c .emit (code .OpArray , len (node .Elements ))
227233 case * ast.IndexExpression :
228- err := c .Compile (node .Value )
234+ err := c .Compile (node .Value , root , "" , previous )
229235 if err != nil {
230236 return err
231237 }
232238
233- err = c .Compile (node .Index )
239+ err = c .Compile (node .Index , root , "" , previous )
234240 if err != nil {
235241 return err
236242 }
@@ -248,11 +254,11 @@ func (c *Compiler) Compile(node ast.Node) error {
248254 })
249255
250256 for _ , k := range keys {
251- err := c .Compile (k )
257+ err := c .Compile (k , root , "" , previous )
252258 if err != nil {
253259 return err
254260 }
255- err = c .Compile (node .Values [k ])
261+ err = c .Compile (node .Values [k ], root , "" , previous )
256262 if err != nil {
257263 return err
258264 }
@@ -266,7 +272,7 @@ func (c *Compiler) Compile(node ast.Node) error {
266272 c .symbolTable .Define (p .Value )
267273 }
268274
269- err := c .Compile (node .Body )
275+ err := c .Compile (node .Body , root , "" , previous )
270276 if err != nil {
271277 return err
272278 }
@@ -299,7 +305,7 @@ func (c *Compiler) Compile(node ast.Node) error {
299305 return fmt .Errorf ("cannot have return statement in root scope" )
300306 }
301307
302- err := c .Compile (node .Value )
308+ err := c .Compile (node .Value , root , "" , previous )
303309 if err != nil {
304310 return err
305311 }
@@ -311,19 +317,42 @@ func (c *Compiler) Compile(node ast.Node) error {
311317 jumpReturns = append (jumpReturns , & val )
312318 }
313319 case * ast.CallExpression :
314- err := c .Compile (node .Function )
320+ err := c .Compile (node .Function , root , "" , previous )
315321 if err != nil {
316322 return err
317323 }
318324
319325 for _ , arg := range node .Parameters {
320- err := c .Compile (arg )
326+ err := c .Compile (arg , root , "" , previous )
321327 if err != nil {
322328 return err
323329 }
324330 }
325331
326332 c .emit (code .OpCall , len (node .Parameters ))
333+ case * ast.Import :
334+ err := c .importPackage (root , node )
335+
336+ if err != nil {
337+ return err
338+ }
339+ case * ast.Export :
340+ index := c .variables
341+
342+ c .currentScope .Variables [index ] = Variable {
343+ Name : identifier ,
344+ Index : index ,
345+ Object : & object.Null {},
346+ }
347+
348+ c .variables ++
349+
350+ err := c .Compile (node .Expression , root , identifier , previous )
351+ if err != nil {
352+ return err
353+ }
354+
355+ c .emit (code .OpSetVar , index )
327356 }
328357
329358 return nil
0 commit comments