Loading [MathJax]/jax/output/HTML-CSS/jax.js
Jikka-5.0.11.1: A transpiler from Python to C++ for competitive programming
Copyright(c) Kimiyuki Onaka 2021
LicenseApache License 2.0
Maintainerkimiyuki95@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Jikka.RestrictedPython.Evaluate

Description

 
Synopsis

Documentation

internal functions

makeGlobal :: MonadError Error m => Program -> m Global Source #

makeGlobal packs toplevel definitions into Global. This assumes doesntHaveLeakOfLoopCounters.

evalExpr :: (MonadReader Global m, MonadState Local m, MonadError Error m) => Expr' -> m Value Source #

evalExpr evaluates exprs of our restricted Python-like language.

Rules for e1boolope2

e1μfalsee1 and e2μfalse e1μtruee2μve1 and e2μv

Rules for e1binope2

e1μv1e2μv2e1+e2μ(v1+v2)

Rules for unaryope

Rules for λxτxτxτ.e

λx0τ0x1τ1xn1τn1.eμλμx0x1xn1.e

Rules for if e then e else e

Rules for [e for y in e (if e)?]

Rules for ecmpope

Rules for e(e,e,,e)

eμλμx0x1xn1.ee0μv0e1μv1en1μvn1e(x0v0;x1v1;;xn1vn1;μ)ve(e0,e1,,en1)μv

eμbe0μv0e1μv1en1μvn1e(e0,e1,,en1)μb(e0,e1,,en1)(b is a builtin function)

Rules for constant

Rules for e[e]

Rules for x

xμμ(x)

Rules for [e,e,,e]τ

Rules for e[e?:e?:e?]

evalStatement :: (MonadReader Global m, MonadState Local m, MonadError Error m) => Statement -> m (Maybe Value) Source #

evalStatement evaluates statements of our restricted Python-like language. When a statement is evaluated, it returns a value v, doesn't return anything stop, or fails err. Also it updates the environment function μ from variables to values.

Rules for return e

eμvreturn eμvμ

Rules for ybinop=e

ybinopeμvybinop=eμstop(yv;μ)

Rules for y:=e

eμvybinop=eμstop(yv;μ)

Rules for for y in e:stmt;stmt;;stmt

eμnil(for y in e: stmt)μstopμ

eμcons(v1,v2)stmt(yv1;μ)vμ(for y in e: stmt)μvμ

eμcons(v1,v2)stmt(yv1;μ)stopμ(for y in v2: stmt)μaμ(for y in e: stmt)μaμ(a{v,stop})

It assumes the program is properly alpha-converted, i.e. doesntHaveLeakOfLoopCounters. So it leaks loop counters to out of loops.

Rules for if e:stmt;stmt;;stmt;else:stmt;stmt;;stmt

eμtruestmt1μaμ(if e: stmt1 else: stmt2)μaμ(a{v,stop})

eμfalsestmt2μaμ(if e: stmt1 else: stmt2)μaμ(a{v,stop})

Rules for assert e

eμtrueassert eμstop

eμfalseassert eμerr

Rules for e

eμvx.append(e)μstop(xsnoc(μ(x),v);μ)

evalStatements :: (MonadReader Global m, MonadState Local m, MonadError Error m) => [Statement] -> m (Maybe Value) Source #

evalStatements evaluates sequences of statements of our restricted Python-like language.

stmt0μvμstmt0;stmt1;;stmtn1μvμ

stmt0μstopμstmt1;;stmtn1μaμstmt0;stmt1;;stmtn1μaμ(a{v,stop})

ϵμstopμ