Let players code in Python (or any other non-JS-based language) #72

Closed
nwinter opened this Issue Jan 4, 2014 · 5 comments

Projects

None yet

3 participants

@nwinter
Contributor
nwinter commented Jan 4, 2014

I haven't looked into this much yet. It should be kind of like #71 for adding CoffeeScript, except that we also need to:

  1. Find/adapt/write a parser from Python to the Mozilla AST format
  2. Handle the important parts of the Python standard library
  3. ??? I'm sure there's something else obvious to anyone who actually knows compilers

The same thing goes for Ruby, Haskell, and every other non-JS-based language that could conceivably be mostly transpiled to JavaScript. I know there are several projects that already parse Python in JavaScript for running it in the browser, and the same for Haskell. If anyone even knows what else we'd need to do or which projects we should start to try to adapt, please comment.

It would be awesome to have CodeCombat support a bunch of different programming languages so that players have their choice of which language to learn or to use for programming challenges.

@nwinter
Contributor
nwinter commented Jan 6, 2014

Can check out Brython and a few others for Python.

@deepak1556
Contributor

https://github.com/replit/jsrepl will this serve the purpose? they have the mentioned languages interpreters compiled to js with emscripten.

@nwinter
Contributor
nwinter commented Jan 8, 2014

I actually had this in the original prototype, but as far as I can tell, there isn't an easy way to pull out just the parsing part–they're compiling the entire interpreters with emscripten, but CodeCombat can't use a full interpreter; it needs to do its own transpiling and running after getting just the AST. I could be wrong, though; I'm no compiler whiz.

@fess89
Contributor
fess89 commented Jan 8, 2014

https://github.com/opal/opal I guess this could do the job for Ruby. It says to translate from Ruby to JS, so probably the compiler which is already in place could be used.

@nwinter
Contributor
nwinter commented Mar 4, 2014

I thought about it a little bit and wrote up this answer for someone asking what would be involved for doing C over on ChallengePost:

C would be a great language to add, although because it's so different from JavaScript, it's not exactly clear what all the tricky bits will be. Have you seen the Mozilla abstract syntax tree format that other JS tools like Esprima and Acorn generate from JavaScript code or that the CoffeeScript Redux compiler generates from CoffeeScript code? Your C parser should do two things:

  1. Using JavaScript/CoffeeScript, parse C code into this AST format.
  2. Add a JavaScript/CoffeeScript runtime library that adds functionality needed to run the C code (types, language features, basic standard libraries).

For a trivial example, imagine this code (please excuse any C mistakes, it's been a while):

int foo = 8;
float bar = 5 * sizeof foo;  // bar = 5 * 4 = 20.0
float baz = bar / foo;  // baz = 20.0 (float) / 8 (int) = 3.0  (integer division truncates)

Your code might translate it it into an AST which, when then generating concrete JavaScript code again, could end up like this:

// We'll need to store the types of all the variables we define.
var __types = {'foo': __c.types.int, 'bar': __c.types.float, 'baz': __c.types.float};
var foo, bar;

// This part is easy, at least!
foo = 8;

// We'll need to use a JS function to emulate C's sizeof operator.
bar = 5 * __c.functions.sizeof(__types['foo']);

// We'll need to use another function to emulate C's float / integer division truncation.
baz = __c.functions.divide({value: bar, type: __types['bar']}, {value: 'foo', type: __types['baz']});

So for this example, your runtime library (__c) would need to define functions.sizeof, functions.divide, and all the types.

You wouldn't need to handle every possible piece of C code–it's not like we care about writing files and doing stdio and such things, and if some of the really advanced semantics are a bit different, we can live with that. But the basic stuff like you might use to play CodeCombat levels should be there. And since CodeCombat's API is object-oriented, then there'll have to be some syntax for emulating things like this:

this.attack(this.getNearbyEnemy());
@nwinter nwinter closed this Jun 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment