In order to learn OCaml (and more of Python), I chose this as my project.
Still WIP.
Lexes, parses, and evaluates a subset of Python including arithmetic, variables, lists, dicts, functions, and basic stdlib.
It also supports higher-order functions and lazily evaluates expressions.
Currently pass-by-copy, without references for data structures.
To get a feel of what it can interpret, have a look at example.py.
To build, run:
dune buildTo run a Python file, run:
dune exec python-interpreter test.pyIf no arguments are provided, the interpreter will read from stdin:
cat test.py | dune exec python-interpreterYou can also directly invoke the generated binary in _build/install/default/bin/python-interpreter.
To run tests, run:
dune runtestTests use ppx_expect inline expect tests. Test files live in test/.
The interpreter pipeline: Source → Lexer (ocamllex) → Parser (menhir) → AST → Evaluator.
The lexer and parser are generated by ocamllex and menhir respectively.
int,float,str,bool,Nonelist,dict
- assignment, function definition, sole expression
if,elif,elsefor,whilepass,return
print,inputrange,lenint,float,bool,str,list- binary operators:
+,-,*,/,%,==,!=,>,<,>=,<=,and,or,in,notnot in - list and dictionary access:
x[i],x[i]=y - list and string slicing:
x[i:j],x[i:j:k], etc. - math utils:
abs,pow,sqrt,min,max,sum