This commit is contained in:
aarne
2004-09-23 14:41:42 +00:00
parent d5b4230d6d
commit 2c60a2d82a
31 changed files with 434 additions and 211 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,12 @@
int abs (int x){
if (x < 0){
return 0 - x ;
}
else return x ;
} ;
int main () {
int i ;
i = abs (16);
} ;

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,8 @@
n Program
open gft.tmp
'
c solve
'
c reindex
'
save ImperJVM jvm.tmp