From bc645ea3ab333bedf5c4cbc453ee3a34c23d7193 Mon Sep 17 00:00:00 2001 From: Tim Vieira Date: Fri, 28 Jun 2013 17:00:29 -0400 Subject: [PATCH] Removed existing calls to pycall. stdlib functions must be declared manually as a case in the `constants` function `Python/Backend.hs`. Otherwise, `pycall` is still available. --- src/Dyna/Backend/Python/Backend.hs | 24 ++++++++++++------------ src/Dyna/Backend/Python/stdlib.py | 14 ++++++++------ test/repl/load.dyna | 8 ++++---- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Dyna/Backend/Python/Backend.hs b/src/Dyna/Backend/Python/Backend.hs index 0b297b5..e155443 100644 --- a/src/Dyna/Backend/Python/Backend.hs +++ b/src/Dyna/Backend/Python/Backend.hs @@ -140,18 +140,17 @@ constants = go go ("&",2) = Just $ PDBS $ infixOp "&" go ("%",2) = Just $ PDBS $ infixOp "%" go ("+",2) = Just $ PDBS $ infixOp "+" - - go ("mod",2) = Just $ PDBS $ infixOp "%" - go ("abs",1) = Just $ PDBS $ call "abs" - go ("log",1) = Just $ PDBS $ call "log" - go ("exp",1) = Just $ PDBS $ call "exp" - - - - go ("pycall", _) = Just $ PDBS $ call "pycall" - go ("getattr", 2) = Just $ PDBS $ call "getattr" - - + go ("mod",2) = Just $ PDBS $ infixOp "%" + go ("abs",1) = Just $ PDBS $ call "abs" + + go ("log",_) = Just $ PDBS $ call "log" + go ("exp",1) = Just $ PDBS $ call "exp" + go ("sqrt",1) = Just $ PDBS $ call "sqrt" + go ("split", _) = Just $ PDBS $ call "split" + go ("float", _) = Just $ PDBS $ call "float" + go ("int", _) = Just $ PDBS $ call "int" + go ("pycall", _) = Just $ PDBS $ call "pycall" + go ("getattr", _) = Just $ PDBS $ call "getattr" go ("uniform", _) = Just $ PDBS $ call "uniform" go ("<=",2) = Just $ PDBS $ infixOp "<=" @@ -383,6 +382,7 @@ driver :: BackendDriver PyDopeBS driver am um is bc qp pr fh = do hPutStrLn fh "from __future__ import division" + hPutStrLn fh "from stdlib import *" -- Parser resume state hPutStrLn fh "parser_state = \"\"\"" diff --git a/src/Dyna/Backend/Python/stdlib.py b/src/Dyna/Backend/Python/stdlib.py index 1da99ad..a96ffd5 100644 --- a/src/Dyna/Backend/Python/stdlib.py +++ b/src/Dyna/Backend/Python/stdlib.py @@ -1,3 +1,4 @@ +import re from term import Term, Cons, Nil try: @@ -9,18 +10,19 @@ except ImportError: # XXX: should probably issue a warning def uniform(a=0, b=1): return _random() * (b - a) + a -import re def split(s, delim='\s+'): - return re.split(delim, s) + return _todynalist(re.split(delim, s)) -# used as a work around to bring arbitrary python functions into dyna def pycall(name, *args): + """ + Temporary foreign function interface - call Python functions from dyna! + """ x = eval(name)(*args) if isinstance(x, list): - return todynalist(x) + return _todynalist(x) return x -def todynalist(x): +def _todynalist(x): if not x: return Nil - return Cons(x[0], todynalist(x[1:])) + return Cons(x[0], _todynalist(x[1:])) diff --git a/test/repl/load.dyna b/test/repl/load.dyna index b933930..860a3dd 100644 --- a/test/repl/load.dyna +++ b/test/repl/load.dyna @@ -6,14 +6,14 @@ % binary rules rewrite(X, Y, Z) := rules_tsv(Linenum, Cost, X, R), - [Y, Z] is pycall("split", R, "\\s+"), - pycall("float", Cost). + [Y, Z] is split(R, "\\s+"), + float(Cost). % load unary rules rewrite(X, Y) := rules_tsv(Linenum, Cost, X, R), - [Y] is pycall("split", R, "\\s+"), - pycall("float", Cost). + [Y] is split(R, "\\s+"), + float(Cost). phrase(S,X,I,K) += phrase(S,Y,I,K) + rewrite(X,Y). phrase(S,X,I,K) += phrase(S,Y,I,J) + phrase(S,Z,J,K) + rewrite(X,Y,Z). -- 2.50.1