summaryrefslogtreecommitdiff
path: root/examples/gfcc/compiler
diff options
context:
space:
mode:
authoraarne <unknown>2004-09-23 14:41:42 +0000
committeraarne <unknown>2004-09-23 14:41:42 +0000
commit2c60a2d82a0d7b90924e7dbbcacf36afb8549d17 (patch)
tree0a04734e741c294cb9f58cfdcafa2ff2b55894dd /examples/gfcc/compiler
parentd5b4230d6dbee8c03eedf8d181dfa2badf1a954b (diff)
Ints n
Diffstat (limited to 'examples/gfcc/compiler')
-rw-r--r--examples/gfcc/compiler/CleanJVM.hs30
-rw-r--r--examples/gfcc/compiler/FILES39
-rw-r--r--examples/gfcc/compiler/abs.c12
-rw-r--r--examples/gfcc/compiler/gfcc4
-rw-r--r--examples/gfcc/compiler/makefile12
-rw-r--r--examples/gfcc/compiler/typecheck.gfs8
6 files changed, 105 insertions, 0 deletions
diff --git a/examples/gfcc/compiler/CleanJVM.hs b/examples/gfcc/compiler/CleanJVM.hs
new file mode 100644
index 000000000..02070df4c
--- /dev/null
+++ b/examples/gfcc/compiler/CleanJVM.hs
@@ -0,0 +1,30 @@
+module Main where
+
+import System
+
+main :: IO ()
+main = do
+ jvm:src:_ <- getArgs
+ s <- readFile jvm
+ let obj = takeWhile (/='.') src ++ ".j"
+ writeFile obj $ mkJVM s
+ putStrLn $ "wrote file " ++ obj
+
+mkJVM :: String -> String
+mkJVM = unlines . reverse . fst . foldl trans ([],([],0)) . lines where
+ trans (code,(env,v)) s = case words s of
+ ".method":f:ns -> ((".method " ++ f ++ concat ns):code,([],0))
+ "alloc":t:x:_ -> (code, ((x,v):env, v + size t))
+ ".limit":"locals":ns -> chCode (".limit locals " ++ show (length ns - 1))
+ t:"_load" :x:_ -> chCode (t ++ "load " ++ look x)
+ t:"_store":x:_ -> chCode (t ++ "store " ++ look x)
+ t:"_return":_ -> chCode (t ++ "return")
+ "goto":ns -> chCode ("goto " ++ concat ns)
+ "ifzero":ns -> chCode ("ifzero " ++ concat ns)
+ _ -> chCode s
+ where
+ chCode c = (c:code,(env,v))
+ look x = maybe (x ++ show env) show $ lookup x env
+ size t = case t of
+ "d" -> 2
+ _ -> 1
diff --git a/examples/gfcc/compiler/FILES b/examples/gfcc/compiler/FILES
new file mode 100644
index 000000000..306a8d133
--- /dev/null
+++ b/examples/gfcc/compiler/FILES
@@ -0,0 +1,39 @@
+GF sources:
+----------
+Imper.gf -- abstract syntax of an imperative language
+ImperC.gf -- concrete syntax for C notation
+ImperJVM.gf -- concrete syntax for JVM notation
+ResImper.gf -- resource module for concrete syntaxes
+
+Scripts:
+-------
+gfcc -- the main compiler executable reading Foo.c ; shell script
+typecheck.gfs -- the type checker and constraint solver ; GF editor script
+CleanJVM.hs -- cleans up jvm.tmp to produce Foo.j ; Haskell module
+makefile -- builds the compiler from GF source ; Unix Make file
+
+Generated files:
+---------------
+Imper.gfcm -- canonical multilingual GF grammar for C and JVM
+ImperC.cf -- LBNF grammar for C generated from Imper.gfcm
+gft.tmp -- parse result generated by the compiler front end
+jvm.tmp -- pseudo-JVM produced by GF linearization
+
+Required programs to use the compiler:
+-------------------------------------
+gf+ -- Grammatical Framework version 2.0+, >= 23/9/2004
+jasmin -- JVM assembler (to compile Foo.j to Foo.class)
+
+Required programs to build the compiler:
+---------------------------------------
+bnfc -- BNF Converter version 2.1+, >= 23/9/2004
+happy -- parser generator for Haskell, >= 1.13
+alex -- lexer generator for Haskell, >= 2.0
+Profile.hs -- BNFC source file (formats/profile), must be on your path
+Trees.hs -- BNFC source file (formats/profile), must be on your path
+
+File formats:
+------------
+Foo.c -- C source file
+Foo.j -- generated Jasmin JVM assembler file
+Foo.class -- assembled JVM bytecode file
diff --git a/examples/gfcc/compiler/abs.c b/examples/gfcc/compiler/abs.c
new file mode 100644
index 000000000..c93b703b6
--- /dev/null
+++ b/examples/gfcc/compiler/abs.c
@@ -0,0 +1,12 @@
+int abs (int x){
+ if (x < 0){
+ return 0 - x ;
+ }
+ else return x ;
+ } ;
+int main () {
+ int i ;
+ i = abs (16);
+ } ;
+
+
diff --git a/examples/gfcc/compiler/gfcc b/examples/gfcc/compiler/gfcc
new file mode 100644
index 000000000..5ad505061
--- /dev/null
+++ b/examples/gfcc/compiler/gfcc
@@ -0,0 +1,4 @@
+./TestImperC $1 | tail -1 >gft.tmp
+echo "es -file=typecheck.gfs" | gf+ -s Imper.gfcm
+runhugs CleanJVM jvm.tmp $1
+rm *.tmp
diff --git a/examples/gfcc/compiler/makefile b/examples/gfcc/compiler/makefile
new file mode 100644
index 000000000..754f77f51
--- /dev/null
+++ b/examples/gfcc/compiler/makefile
@@ -0,0 +1,12 @@
+GF=gf+
+SRC=../
+
+all: compiler
+
+compiler:
+ echo "pm | wf Imper.gfcm ;; pg -lang=ImperC -printer=lbnf | wf ImperC.tmp" | $(GF) $(SRC)ImperC.gf $(SRC)ImperJVM.gf
+ echo "entrypoints Program, Stm, Exp ;" >entry.tmp
+ cat entry.tmp ImperC.tmp >ImperC.cf
+ bnfc -m -prof ImperC.cf
+ make -f Makefile
+ rm *.tmp
diff --git a/examples/gfcc/compiler/typecheck.gfs b/examples/gfcc/compiler/typecheck.gfs
new file mode 100644
index 000000000..30bf320d4
--- /dev/null
+++ b/examples/gfcc/compiler/typecheck.gfs
@@ -0,0 +1,8 @@
+n Program
+open gft.tmp
+'
+c solve
+'
+c reindex
+'
+save ImperJVM jvm.tmp