]> hydra-www.ietfng.org Git - dyna2/commitdiff
Removed existing calls to pycall. stdlib functions must be declared manually as
authorTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 21:00:29 +0000 (17:00 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 21:00:29 +0000 (17:00 -0400)
a case in the `constants` function `Python/Backend.hs`. Otherwise, `pycall` is
still available.

src/Dyna/Backend/Python/Backend.hs
src/Dyna/Backend/Python/stdlib.py
test/repl/load.dyna

index 0b297b5db16076a106bb60a8cf5c414e392aca01..e15544301003c842c50affee2fdf83df015b6b21 100644 (file)
@@ -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 = \"\"\""
index 1da99adda7e0b696019d43c5565f1e7003c04ab5..a96ffd548481ba4696ffcd0b366dd44329c372e7 100644 (file)
@@ -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:]))
index b933930193cc29e50bfae5968ef7030ebfa6a174..860a3ddd1cd29fdce093a4146af57e95aeadb374 100644 (file)
@@ -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).