diff options
Diffstat (limited to 'src/compiler/GF/Compile/PGFtoJava.hs')
| -rw-r--r-- | src/compiler/GF/Compile/PGFtoJava.hs | 44 |
1 files changed, 44 insertions, 0 deletions
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 = + [ + "", + "}" + ] |
