summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-17 19:43:42 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-17 19:43:42 +0200
commit972de9e89b88240b7c5c798f2cd766e5f181c007 (patch)
tree870a250ca0b25b94f7b522440a18dc6ab3ab523c /modules
parent97cb53113ec7f8238e37f798b6c83ec9c2b21151 (diff)
eval
Diffstat (limited to 'modules')
-rw-r--r--modules/language/python/eval.scm85
-rw-r--r--modules/language/python/exceptions.scm3
-rw-r--r--modules/language/python/module/python.scm17
3 files changed, 98 insertions, 7 deletions
diff --git a/modules/language/python/eval.scm b/modules/language/python/eval.scm
new file mode 100644
index 0000000..999acc0
--- /dev/null
+++ b/modules/language/python/eval.scm
@@ -0,0 +1,85 @@
+(define-module (language python eval)
+ #:use-module
+ #:use-module (parser stis-parser lang python3-parser)
+ #:use-module (language python exceptions)
+ #:use-module ((ice-9 local-eval) #:select ((the-environment . locals)))
+ #:re-export (locals)
+ #:replace (eval)
+ #:export (local-eval local-compile globals compile exec))
+
+(define seval (@ (guile) eval))
+
+(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
+
+(define-syntax-rule (L x) (@@ (ice-9 local-eval) x))
+
+(define-syntax globals
+ (lambda (x)
+ (syntax-case x ()
+ ((g)
+ #'((L env-module) (locals g))))))
+
+(define* (local-eval x locals globals)
+ "Evaluate the expression @var{x} within the local environment @var{local} and
+global environment @var{global}."
+ (if locals
+ (if globals
+ (apply (seval ((L local-wrap) x locals) globals)
+ ((L env-boxes) locals))
+ (apply (seval ((L local-wrap) x locals) ((L env-module) locals))
+ ((L env-boxes) locals)))
+ (seval x (current-module))))
+
+(define* (local-compile x locals globals #:key (opts '()))
+ "Compile the expression @var{x} within the local environment @var{local} and
+global environment @var{global}."
+ (if locals
+ (if globals
+ (apply ((@ (system base compile) compile)
+ ((L local-wrap) x locals) #:env globals
+ #:from 'scheme #:opts opts)
+ ((L env-boxes) locals))
+ (apply ((@ (system base compile) compile) ((L local-wrap) x locals)
+ #:env ((L env-module) locals)
+ #:from 'scheme #:opts opts)
+ ((L env-boxes) locals)))
+ ((@ (system base compile) compile) x #:env (current-module)
+ #:from 'scheme #:opts opts)))
+
+(define-syntax eval
+ (lambda (x)
+ (syntax-case x ()
+ ((eval x)
+ #'(eval0 x (locals eval)))
+ ((eval x . l)
+ #'(eval0 x . l)))))
+
+(define* (eval0 x #:optional (locals #f) (globals #f))
+ (cond
+ ((string? x)
+ (aif xp (p x)
+ (aif cp (comp xp)
+ (local-eval cp locals globals)
+ (raise SyntaxError))
+ (raise SyntaxError)))
+ ((pair? x)
+ (local-eval x locals globals))))
+
+(define* (compile x filename mode
+ #:optional (flags 0) (dont_inherit #f) (optiomize -1))
+ (aif xp (p x)
+ (aif cp (comp xp)
+ cp
+ (raise SyntaxError))
+ (raise SyntaxError)))
+
+(define-syntax exec
+ (lambda (x)
+ (syntax-case x ()
+ ((exec x)
+ #'(eval0 x (locals exec)))
+ ((exec x . l)
+ #'(exec0 x . l)))))
+
+(define* (exec0 x #:optional (locals #f) (globals #f))
+ (local-eval x locals globals))
diff --git a/modules/language/python/exceptions.scm b/modules/language/python/exceptions.scm
index 646a0a3..413abe1 100644
--- a/modules/language/python/exceptions.scm
+++ b/modules/language/python/exceptions.scm
@@ -4,6 +4,7 @@
#:export (StopIteration GeneratorExit RuntimeError
Exception ValueError TypeError
IndexError KeyError AttributeError
+ SyntaxError
None))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
@@ -17,6 +18,8 @@
(define KeyError 'KeyError)
(define TypeError 'TypeError)
(define AttributeError 'AttributeError)
+(define SyntaxError 'SyntaxError)
+
(define-python-class Exception ()
(define __init__
(case-lambda
diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm
index 7022ff1..296a304 100644
--- a/modules/language/python/module/python.scm
+++ b/modules/language/python/module/python.scm
@@ -23,17 +23,20 @@
#:use-module (language python property )
#:use-module (language python range )
#:use-module (language python tuple )
+ #:use-module (language python eval )
#:replace (list abs min max hash round)
#:re-export (StopIteration GeneratorExit RuntimeError
- Exception ValueError TypeError
- IndexError KeyError AttributeError
- send sendException next
- GeneratorExit sendClose RuntimeError
- len dir next dict None property range
- tuple bytes bytearray
- )
+ Exception ValueError TypeError
+ IndexError KeyError AttributeError
+ send sendException next
+ GeneratorExit sendClose RuntimeError
+ SyntaxError
+ len dir next dict None property range
+ tuple bytes bytearray eval locals globals
+ compile exec
+ )
#:export (print repr complex float int
set all any bin callable reversed