differentmatt / filbert

JavaScript parser of Python
Other
133 stars 27 forks source link

Get imports working. #2

Open Xavion3 opened 10 years ago

Xavion3 commented 10 years ago

So here for discussion about imports, my current idea is to have a modules container where we store them and move them into scope as they get imported. I'll put up an example snippet when I get home.

differentmatt commented 10 years ago

Cool, looking forward to an example.

Xavion3 commented 10 years ago

Alright code stuff, here is what you'd have in pythonRuntime

modules : {
  random : new (function() {
    var self = this;
    self.random = function () { return Math.random(); };
    self.randint = function (a, b) { return self.randrange(a, b+1); };
    etc. etc.
  })(),
  math : new (function() {
    var self = this;
    this.sin = Math.sin;
    etc. etc.
  })()
}

Then the code they'd enter would be something like the following

import math, random as rand
from math import sin as sine

This would be transformed into the following code.

var math = pythonRuntime.modules.math;
var rand = pythonRuntime.modules.random;
var sin = pythonRuntime.modules.math.sine;

Of course you'd modify the vars and so as normal so you only declare once but that's the model I thought of.

differentmatt commented 10 years ago

I like it! This should work nicely.

Why would you use self instead of this directly in your modules objects?

parseSubscripts has examples of generating AST nodes to reference runtime objects.

createVarDeclFromId will help you create an AST node of the form var math = something;.

from and import keywords are currently skipped over in parseStatement.

We also need to make sure new parsing work is duplicated in filbert_loose.js. There is currently more duplication than necessary between filbert.js and filbert_loose.js.

Xavion3 commented 10 years ago

The self instead of this is a habit to make sure scoping doesn't suddenly screw up by giving me the inner functions scope with this so I also get the one I was trying for. I'll look into those and try and get something done.

Also I have been forgetting to update filbert_loose.js, I'll try and remember to do that. I'm going to need to start having multiple branches here or the different goals will start polluting each other.