summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Compile
diff options
context:
space:
mode:
authorKrasimir Angelov <kr.angelov@gmail.com>2017-08-29 20:53:47 +0200
committerKrasimir Angelov <kr.angelov@gmail.com>2017-08-29 20:53:47 +0200
commit1e4ab95e418fd704d414b85524d94ae64175b179 (patch)
tree263c5b5fd83b7e98b1a1d331b74aa62399afad76 /src/compiler/GF/Compile
parent03479648ad008966d15c7866630fef443f70f4dd (diff)
added option -output-format=java for producing code for embedded grammars in Java
Diffstat (limited to 'src/compiler/GF/Compile')
-rw-r--r--src/compiler/GF/Compile/Export.hs2
-rw-r--r--src/compiler/GF/Compile/PGFtoJava.hs44
2 files changed, 46 insertions, 0 deletions
diff --git a/src/compiler/GF/Compile/Export.hs b/src/compiler/GF/Compile/Export.hs
index 5518d2ee2..b8b8ed1ac 100644
--- a/src/compiler/GF/Compile/Export.hs
+++ b/src/compiler/GF/Compile/Export.hs
@@ -3,6 +3,7 @@ module GF.Compile.Export where
import PGF
import PGF.Internal(ppPGF)
import GF.Compile.PGFtoHaskell
+import GF.Compile.PGFtoJava
import GF.Compile.PGFtoProlog
import GF.Compile.PGFtoLProlog
import GF.Compile.PGFtoJS
@@ -37,6 +38,7 @@ exportPGF opts fmt pgf =
FmtJavaScript -> multi "js" pgf2js
FmtPython -> multi "py" pgf2python
FmtHaskell -> multi "hs" (grammar2haskell opts name)
+ FmtJava -> multi "java" (grammar2java opts name)
FmtProlog -> multi "pl" grammar2prolog
FmtLambdaProlog -> multi "mod" grammar2lambdaprolog_mod ++ multi "sig" grammar2lambdaprolog_sig
FmtBNF -> single "bnf" bnfPrinter
diff --git a/src/compiler/GF/Compile/PGFtoJava.hs b/src/compiler/GF/Compile/PGFtoJava.hs
new file mode 100644
index 000000000..9aa7412a0
--- /dev/null
+++ b/src/compiler/GF/Compile/PGFtoJava.hs
@@ -0,0 +1,44 @@
+module GF.Compile.PGFtoJava (grammar2java) where
+
+import PGF
+import Data.Maybe(maybe)
+import Data.List(intercalate)
+import GF.Infra.Option
+
+-- | the main function
+grammar2java :: Options
+ -> String -- ^ Module name.
+ -> PGF
+ -> String
+grammar2java opts name gr = unlines $
+ javaPreamble name ++ methods ++ javaEnding
+ where
+ methods = [javaMethod gr fun | fun <- functions gr]
+
+javaPreamble name =
+ [
+ "import org.grammaticalframework.pgf.*;",
+ "",
+ "public class " ++ name ++ " {",
+ ""
+ ]
+
+javaMethod gr fun =
+ " public static Expr "++name++"("++arg_decls++") { return new Expr("++show name++args++"); }"
+ where
+ name = showCId fun
+ arity = maybe 0 getArrity (functionType gr fun)
+ vars = ['e':show i | i <- [1..arity]]
+
+ arg_decls = intercalate "," ["Expr "++v | v <- vars]
+ args = if null vars then ",new Expr[] {}" else ","++intercalate "," vars
+
+ getArrity ty = length hypos
+ where
+ (hypos,_,_) = unType ty
+
+javaEnding =
+ [
+ "",
+ "}"
+ ]