New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let players code in Python (or any other non-JS-based language) #72
Comments
Can check out Brython and a few others for Python. |
https://github.com/replit/jsrepl will this serve the purpose? they have the mentioned languages interpreters compiled to js with emscripten. |
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. |
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. |
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:
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()); |
I haven't looked into this much yet. It should be kind of like #71 for adding CoffeeScript, except that we also need to:
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.
The text was updated successfully, but these errors were encountered: