Filbert provides the basics of Python, suitable for playing CodeCombat. Additionally, it provides a strong foundation for adding more advanced Python language features in the future.

Python

I chose Python because it is the most useful language to add to CodeCombat. It is very common in both industry and as an introductory programming language.

And, I could provide a basic implementation suitable for beginners in the time allowed.

Implementation

I selected Acorn as a starting point for a Python parser because: it was tiny, fast, well written, in Javascript, outputted Mozilla AST, and already used by CodeCombat.

Leveraging a mature parser allowed me to focus more on Python language features, and less on tokenizing and AST generation. It also got us Python parseDammit at low cost, and a strong framework for adding additional Python language features in the future.

The outputted AST depends on a python runtime library being available for execution in order to support Python language features not native to JavaScript.

Language Support

My goal was to implement the most useful parts of Python for the purpose of playing CodeCombat levels. For example, I punted regular expressions and advanced string operations in favor of classes, tuples, and more built-in function support.

Keywords:

False None True and break class continue def elif else for if in is not or pass return while

Built-ins:

abs() all() any() bool() chr() dict() enumerate() filter() float() hex() int() len() list() map() max() min() oct() ord() pow() print() range() repr() reversed() round() sorted() str() sum() tuple()

Operator precedence and basic scoping rules are also working.

Future work:

  • Remaining keywords and built-ins
  • More advanced features, such as slices and list comprehensions

Ranges

The original source node range info is preserved in the AST nodes, via the ranges or locations properties. Ranges provides a ‘range’ array property, and ‘locations’ provides a ‘loc’ line/column object on the nodes.

For AST nodes that are generated as a bridge between Python and JavaScript, a reasonable range based on the original source is used.

CodeCombat Integration

Filbert has been successfully integrated into CodeCombat, and players can use Python to play through the levels.

Thanks!

This challenge has been a lot of fun, and it’s exciting to see so many languages being added to CodeCombat.

-Matt

Share this project:
×

Updates