diff options
| author | aarne <aarne@chalmers.se> | 2010-03-02 19:10:56 +0000 |
|---|---|---|
| committer | aarne <aarne@chalmers.se> | 2010-03-02 19:10:56 +0000 |
| commit | e4748e998453b979af46983a079f2ec3d307ada4 (patch) | |
| tree | ef1c2b278850a7f6a95959b6f18469645cf1094c /examples/gfcc/JVM.hs | |
| parent | 21b10f91cbbd0a2d369dc55306bad53df1502cd7 (diff) | |
restored gfcc example (GF C compiler)
Diffstat (limited to 'examples/gfcc/JVM.hs')
| -rw-r--r-- | examples/gfcc/JVM.hs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/examples/gfcc/JVM.hs b/examples/gfcc/JVM.hs new file mode 100644 index 000000000..380570049 --- /dev/null +++ b/examples/gfcc/JVM.hs @@ -0,0 +1,20 @@ +module JVM where + +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 |
