simplepy
An interpreter for a subset of the Python language, written purely in Dart. It is intended for adding scripting capabilities to Dart projects and for educational purposes.
Features
-
Available python features:
- Variable types:
int,float,bool,str,list,dict,set,tuple,NoneType
(somedictmethods with limitations) - f-strings:
{value:[[fill]align][sign][0][width][.precision][dfs]};#,and typesb/o/x/e/g/%are not yet supported. - arithmetic operators:
+ - * / // ** %and bitwise operators - assignments:
= += -= *= /= **= //= %= - comparisons:
== > >= < <= != - boolean operators:
and,or,not if,else,elifforloops,whileloopstry/except/else/finally- functions (default arguments, *args, **kwargs are partially implemented),
lambdafunctions - some built-in functions:
print(),range(),len(),str(),repr(),int(),float(),bool(),type(),list(),dict(),set(),tuple(),abs(),round(),min(),max(),sum(),isinstance() - list, dict and set comprehensions
- list, string and tuple slices - read only
- classes (no class attributes, no multiple inheritance)
globalandnonlocalvariables
Some more features might be added soon, but this will never become a full python interpreter.
- Variable types:
-
Missing python features:
intlimited to dart int: ±1<<63 native ; ±1<<53 in webdict.key(),dict.values(),dict.items()return list copies, not dynamic views as in Python- set operators
| & - ^ <= >=(but available as set1.union(set2)` etc.) :=operatorimport- slice assignments (like
x[5:10] = [1,2,3])) - file I/O
- decorators
- async functions, generators
input()and some other built-in functions- complex numbers
- multiple statements in one line separated by
; - dunder methods (
__xxx__) except for__init__ - anything else not mentioned as available
Usage
import 'package:simplepy/simplepy.dart';
void main() {
String py = "print(3**4)";
final tokens = Lexer(py).scanTokens();
final stmts = Parser(tokens).parse();
Interpreter().interpret(stmts);
}
Additional information
Please report any errors (except for missing features, that are not mentioned as available) at the issue tracker.