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) - r-strings, 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,whileloops (butelse:block is not yet available)try/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(),hex(),oct(),bin(),pow(),sum(),ord(),chr(),isinstance() - list, dict and set comprehensions
- list, string and tuple slices - read only
- classes (no class attributes, no multiple inheritance)
globalandnonlocalvariables- limited file I/O: files can be created using
f=open(filename,mode).modecan be w,r,a,w+,r+,a+. No binary files. Available methods:f.read(n),f.readline(n),f.readlines(n),f.write(text),f.writelines(list),f.seek(position),f.close()File contents are kept in memory in a dictionary as long as the Interpreter object exists. To make them persistent, you can access them from dart byinterpreter.vfs[filename]. import(only modules from virtual file system or standard modules, no packages yet). Available standard modules:json(functions:dump, dumps, load, loads)math(constants:e, nan, inf, pi, tau; functions:exp, log, log2, log10, pow, sqrt, cbrt,sin, cos, tan, asin, acos, atan, atan2, degrees, radians)random(functions:choice, gauss, randint, random, randrange, seed, uniform)re(functions:findall, match, search, split, sub. Optional parametersflags, count, maxsplitare not supported yet)time(functions:sleep(blocking the execution),time, time_ns)
Some more features might be added soon, but this will never become a full python interpreter.
- Variable types:
-
Missing python features:
dict.key(),dict.values(),dict.items()return list copies, not dynamic views as in Python- set operators
| & - ^ <= >=(but available as set1.union(set2)` etc.) - ternary conditional expressions
:=operator- slice assignments (like
x[5:10] = [1,2,3])) - 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)
6.0*7
""";
final tokens = Lexer(py).scanTokens();
final stmts = Parser(tokens).parse();
var result = Interpreter().interpret(stmts);
print("result = $result (${result.runtimeType})");
}
Additional information
Please report any errors (except for missing features, that are not mentioned as available) at the issue tracker.