forked from GitHub/gf-core
restored gfcc example (GF C compiler)
This commit is contained in:
59
examples/gfcc/CleanJVM.hs
Normal file
59
examples/gfcc/CleanJVM.hs
Normal file
@@ -0,0 +1,59 @@
|
||||
module Main where
|
||||
|
||||
import Char
|
||||
import System
|
||||
|
||||
--- translation from Symbolic JVM to real Jasmin code
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
jvm:src:_ <- getArgs
|
||||
s <- readFile jvm
|
||||
let cls = takeWhile (/='.') src
|
||||
let obj = cls ++ ".j"
|
||||
writeFile obj $ boilerplate cls
|
||||
appendFile obj $ mkJVM cls s
|
||||
putStrLn $ "wrote file " ++ obj
|
||||
system $ "java -jar jasmin.jar " ++ obj
|
||||
return ()
|
||||
|
||||
mkJVM :: String -> String -> String
|
||||
mkJVM cls = unlines . reverse . fst . foldl trans ([],([],0)) . liness where
|
||||
trans (code,(env,v)) s = case words s of
|
||||
".method":p:s:f:ns
|
||||
| f == "main" ->
|
||||
(".method public static main([Ljava/lang/String;)V":code,([],1))
|
||||
| otherwise ->
|
||||
(unwords [".method",p,s, f ++ glue ns] : code,([],0))
|
||||
"alloc":t:x:_ -> (("; " ++ s):code, ((x,v):env, v + size t))
|
||||
".limit":"locals":ns -> chCode (".limit locals " ++ show (length ns + 1))
|
||||
"runtime":f:ns -> chCode $ "invokestatic " ++ "runtime/" ++ f ++ glue ns
|
||||
"static":f:ns -> chCode $ "invokestatic " ++ cls ++ "/" ++ f ++ glue ns
|
||||
"alloc":ns -> chCode $ "; " ++ s
|
||||
ins:x:_ | symb ins -> chCode $ ins ++ " " ++ look x
|
||||
"goto":ns -> chCode $ "goto " ++ glue ns
|
||||
"ifeq":ns -> chCode $ "ifeq " ++ glue ns
|
||||
"label":ns -> chCode $ glue ns ++ ":"
|
||||
";":[] -> chCode ""
|
||||
_ -> chCode s
|
||||
where
|
||||
chCode c = (c:code,(env,v))
|
||||
look x = maybe (error $ x ++ show env) show $ lookup x env
|
||||
glue = concat --init . concat
|
||||
symb = flip elem ["load","store"] . tail
|
||||
size t = case t of
|
||||
"d" -> 2
|
||||
_ -> 1
|
||||
liness = lines . map (\c -> if c==';' then '\n' else c)
|
||||
|
||||
|
||||
boilerplate :: String -> String
|
||||
boilerplate cls = unlines [
|
||||
".class public " ++ cls,
|
||||
".super java/lang/Object",
|
||||
".method public <init>()V",
|
||||
"aload_0",
|
||||
"invokenonvirtual java/lang/Object/<init>()V",
|
||||
"return",
|
||||
".end method"
|
||||
]
|
||||
53
examples/gfcc/Imper.gf
Normal file
53
examples/gfcc/Imper.gf
Normal file
@@ -0,0 +1,53 @@
|
||||
abstract Imper = {
|
||||
|
||||
flags startcat = Program ;
|
||||
|
||||
cat
|
||||
Program ;
|
||||
Rec ListTyp ;
|
||||
Typ ;
|
||||
IsNum Typ ;
|
||||
ListTyp ;
|
||||
Fun ListTyp Typ ;
|
||||
Stm ;
|
||||
Exp Typ ;
|
||||
Var Typ ;
|
||||
ListExp ListTyp ;
|
||||
|
||||
fun
|
||||
Empty : Program ;
|
||||
Funct : (AS : ListTyp) -> (V : Typ) ->
|
||||
(Fun AS V -> Rec AS) -> Program ;
|
||||
FunctNil : (V : Typ) ->
|
||||
Stm -> (Fun NilTyp V -> Program) -> Program ;
|
||||
RecOne : (A : Typ) -> (Var A -> Stm) -> Program -> Rec (ConsTyp A NilTyp) ;
|
||||
RecCons : (A : Typ) -> (AS : ListTyp) ->
|
||||
(Var A -> Rec AS) -> Program -> Rec (ConsTyp A AS) ;
|
||||
|
||||
Decl : (A : Typ) -> (Var A -> Stm) -> Stm ;
|
||||
Assign : (A : Typ) -> Var A -> Exp A -> Stm -> Stm ;
|
||||
While : Exp TInt -> Stm -> Stm -> Stm ;
|
||||
IfElse : Exp TInt -> Stm -> Stm -> Stm -> Stm ;
|
||||
Block : Stm -> Stm -> Stm ;
|
||||
Printf : (A : Typ) -> Exp A -> Stm -> Stm ;
|
||||
Return : (A : Typ) -> Exp A -> Stm ;
|
||||
Returnv : Stm ;
|
||||
End : Stm ;
|
||||
|
||||
EVar : (A : Typ) -> Var A -> Exp A ;
|
||||
EInt : Int -> Exp TInt ;
|
||||
EFloat : Int -> Int -> Exp TFloat ;
|
||||
ELt : (n : Typ) -> IsNum n -> Exp n -> Exp n -> Exp TInt ;
|
||||
EAdd, EMul, ESub : (n : Typ) -> IsNum n -> Exp n -> Exp n -> Exp n ;
|
||||
EAppNil : (V : Typ) -> Fun NilTyp V -> Exp V ;
|
||||
EApp : (AS : ListTyp) -> (V : Typ) -> Fun AS V -> ListExp AS -> Exp V ;
|
||||
|
||||
TInt, TFloat : Typ ;
|
||||
isNumInt : IsNum TInt ; isNumFloat : IsNum TFloat ;
|
||||
NilTyp : ListTyp ;
|
||||
ConsTyp : Typ -> ListTyp -> ListTyp ;
|
||||
|
||||
OneExp : (A : Typ) -> Exp A -> ListExp (ConsTyp A NilTyp) ;
|
||||
ConsExp : (A : Typ) -> (AS : ListTyp) ->
|
||||
Exp A -> ListExp AS -> ListExp (ConsTyp A AS) ;
|
||||
}
|
||||
56
examples/gfcc/ImperC.gf
Normal file
56
examples/gfcc/ImperC.gf
Normal file
@@ -0,0 +1,56 @@
|
||||
--# -path=.:../../lib/prelude
|
||||
concrete ImperC of Imper = open ResImper in {
|
||||
flags lexer=codevars ; unlexer=code ; startcat=Program ;
|
||||
|
||||
lincat
|
||||
Exp = PrecExp ;
|
||||
Typ = {s,s2 : Str} ;
|
||||
Rec = {s,s2,s3 : Str} ;
|
||||
|
||||
lin
|
||||
Empty = ss [] ;
|
||||
FunctNil val stm cont = ss (
|
||||
val.s ++ cont.$0 ++ paren [] ++ "{" ++
|
||||
stm.s ++ "}" ++ ";" ++ cont.s) ;
|
||||
Funct args val rec = ss (
|
||||
val.s ++ rec.$0 ++ paren rec.s2 ++ "{" ++
|
||||
rec.s ++ "}" ++ ";" ++ rec.s3) ;
|
||||
|
||||
RecOne typ stm prg = stm ** {
|
||||
s2 = typ.s ++ stm.$0 ;
|
||||
s3 = prg.s
|
||||
} ;
|
||||
RecCons typ _ body prg = {
|
||||
s = body.s ;
|
||||
s2 = typ.s ++ body.$0 ++ "," ++ body.s2 ;
|
||||
s3 = prg.s
|
||||
} ;
|
||||
|
||||
Decl typ cont = continues (typ.s ++ cont.$0) cont ;
|
||||
Assign _ x exp = continues (x.s ++ "=" ++ exp.s) ;
|
||||
While exp loop = continue ("while" ++ paren exp.s ++ loop.s) ;
|
||||
IfElse exp t f = continue ("if" ++ paren exp.s ++ t.s ++ "else" ++ f.s) ;
|
||||
Block stm = continue ("{" ++ stm.s ++ "}") ;
|
||||
Printf t e = continues ("printf" ++ paren (t.s2 ++ "," ++ e.s)) ;
|
||||
Return _ exp = statement ("return" ++ exp.s) ;
|
||||
Returnv = statement "return" ;
|
||||
End = ss [] ;
|
||||
|
||||
EVar _ x = constant x.s ;
|
||||
EInt n = constant n.s ;
|
||||
EFloat a b = constant (a.s ++ "." ++ b.s) ;
|
||||
EMul _ _ = infixL 3 "*" ;
|
||||
EAdd _ _ = infixL 2 "+" ;
|
||||
ESub _ _ = infixL 2 "-" ;
|
||||
ELt _ _ = infixN 1 "<" ;
|
||||
|
||||
EAppNil val f = constant (f.s ++ paren []) ;
|
||||
EApp args val f exps = constant (f.s ++ paren exps.s) ;
|
||||
|
||||
TInt = {s = "int" ; s2 = "\"%d\""} ;
|
||||
TFloat = {s = "float" ; s2 = "\"%f\""} ;
|
||||
NilTyp = ss [] ;
|
||||
ConsTyp = cc2 ;
|
||||
OneExp _ e = e ;
|
||||
ConsExp _ _ e es = ss (e.s ++ "," ++ es.s) ;
|
||||
}
|
||||
71
examples/gfcc/ImperEng.gf
Normal file
71
examples/gfcc/ImperEng.gf
Normal file
@@ -0,0 +1,71 @@
|
||||
-- # -path=.:prelude
|
||||
--# -path=.:../../lib/prelude
|
||||
|
||||
-- Toy English phrasing of C programs. Intended use is with
|
||||
-- speech synthesis. Printed code should use HTML formatting.
|
||||
-- AR 5/10/2005.
|
||||
|
||||
concrete ImperEng of Imper = open Prelude, ResImperEng in {
|
||||
flags lexer=textvars ; unlexer=text ; startcat=Program ;
|
||||
|
||||
lincat
|
||||
Rec = {s,s2,s3 : Str} ;
|
||||
|
||||
lin
|
||||
Empty = ss [] ;
|
||||
FunctNil val stm cont = ss (
|
||||
["The function"] ++ cont.$0 ++
|
||||
"returns" ++ indef ++ val.s ++ "." ++
|
||||
["It is defined as follows :"] ++
|
||||
stm.s ++
|
||||
PARA ++
|
||||
cont.s) ;
|
||||
Funct args val rec = ss (
|
||||
["The function"] ++ rec.$0 ++
|
||||
"takes" ++ rec.s2 ++
|
||||
"and" ++ "returns" ++ indef ++ val.s ++ "." ++
|
||||
["It is defined as follows:"] ++
|
||||
rec.s ++
|
||||
PARA ++
|
||||
rec.s3) ;
|
||||
|
||||
RecOne typ stm prg = stm ** {
|
||||
s2 = indef ++ typ.s ++ stm.$0 ;
|
||||
s3 = prg.s
|
||||
} ;
|
||||
RecCons typ _ body prg = {
|
||||
s = body.s ;
|
||||
s2 = indef ++ typ.s ++ body.$0 ++ "and" ++ body.s2 ;
|
||||
s3 = prg.s
|
||||
} ;
|
||||
|
||||
Decl typ cont = continues ("let" ++ cont.$0 ++ "be" ++ indef ++ typ.s) cont ;
|
||||
Assign _ x exp = continues ("set" ++ x.s ++ "to" ++ exp.s) ;
|
||||
While exp loop = continues (["if"] ++ exp.s ++
|
||||
[", do the following :"] ++ loop.s ++
|
||||
["test the condition and repeat the loop if the condition holds"]) ;
|
||||
IfElse exp t f = continue ("if" ++ exp.s ++ [", then"] ++ t.s ++ "Else" ++ f.s) ;
|
||||
Block stm = continue (stm.s) ;
|
||||
Printf t e = continues ("print" ++ e.s) ;
|
||||
Return _ exp = statement ("return" ++ exp.s) ;
|
||||
Returnv = statement ["return from the function"] ;
|
||||
End = ss [] ;
|
||||
|
||||
EVar _ x = constant x.s ;
|
||||
EInt n = constant n.s ;
|
||||
EFloat a b = constant (a.s ++ "." ++ b.s) ;
|
||||
EMul _ _ = prefix "product" ;
|
||||
EAdd _ _ = prefix "sum" ;
|
||||
ESub _ _ x y = ss (["the subtraction of"] ++ y.s ++ "from" ++ x.s) ;
|
||||
ELt _ _ = comparison "smaller" ;
|
||||
|
||||
EAppNil val f = constant f.s ;
|
||||
EApp args val f exps = constant (f.s ++ ["applied to"] ++ exps.s) ;
|
||||
|
||||
TInt = {s = "integer"} ;
|
||||
TFloat = {s = "float"} ;
|
||||
NilTyp = ss [] ;
|
||||
ConsTyp = cc2 ;
|
||||
OneExp _ e = e ;
|
||||
ConsExp _ _ e es = ss (e.s ++ "and" ++ es.s) ;
|
||||
}
|
||||
93
examples/gfcc/ImperJVM.gf
Normal file
93
examples/gfcc/ImperJVM.gf
Normal file
@@ -0,0 +1,93 @@
|
||||
--# -path=.:../../lib/prelude
|
||||
concrete ImperJVM of Imper = open ResImper in {
|
||||
|
||||
flags lexer=codevars ; unlexer=code ; startcat=Stm ;
|
||||
|
||||
lincat
|
||||
Rec = {s,s2,s3 : Str} ; -- code, storage for locals, continuation
|
||||
Typ = {s : Str ; t : TypIdent} ;
|
||||
Stm = Instr ;
|
||||
|
||||
lin
|
||||
Empty = ss [] ;
|
||||
FunctNil val stm cont = ss (
|
||||
".method" ++ "public" ++ "static" ++ cont.$0 ++ paren [] ++ val.s ++ ";" ++
|
||||
".limit" ++ "locals" ++ stm.s2 ++ ";" ++
|
||||
".limit" ++ "stack" ++ "1000" ++ ";" ++
|
||||
stm.s ++
|
||||
".end" ++ "method" ++ ";" ++ ";" ++
|
||||
cont.s
|
||||
) ;
|
||||
Funct args val rec = ss (
|
||||
".method" ++ "public" ++ "static" ++ rec.$0 ++ paren args.s ++ val.s ++ ";" ++
|
||||
".limit" ++ "locals" ++ rec.s2 ++ ";" ++
|
||||
".limit" ++ "stack" ++ "1000" ++ ";" ++
|
||||
rec.s ++
|
||||
".end" ++ "method" ++ ";" ++ ";" ++
|
||||
rec.s3
|
||||
) ;
|
||||
|
||||
RecOne typ stm prg = instrb typ.s (
|
||||
["alloc"] ++ typ.s ++ stm.$0 ++ stm.s2) {s = stm.s ; s2 = stm.s2 ; s3 = prg.s};
|
||||
|
||||
RecCons typ _ body prg = instrb typ.s (
|
||||
["alloc"] ++ typ.s ++ body.$0 ++ body.s2)
|
||||
{s = body.s ; s2 = body.s2 ; s3 = prg.s};
|
||||
|
||||
Decl typ cont = instrb typ.s (
|
||||
["alloc"] ++ typ.s ++ cont.$0
|
||||
) cont ;
|
||||
Assign t x exp = instrc (exp.s ++ typInstr "store" t.t ++ x.s) ;
|
||||
While exp loop =
|
||||
let
|
||||
test = "TEST_" ++ loop.s2 ;
|
||||
end = "END_" ++ loop.s2
|
||||
in instrl (
|
||||
"label" ++ test ++ ";" ++
|
||||
exp.s ++
|
||||
"ifeq" ++ end ++ ";" ++
|
||||
loop.s ++
|
||||
"goto" ++ test ++ ";" ++
|
||||
"label" ++ end
|
||||
) ;
|
||||
IfElse exp t f =
|
||||
let
|
||||
false = "FALSE_" ++ t.s2 ++ f.s2 ;
|
||||
true = "TRUE_" ++ t.s2 ++ f.s2
|
||||
in instrl (
|
||||
exp.s ++
|
||||
"ifeq" ++ false ++ ";" ++
|
||||
t.s ++
|
||||
"goto" ++ true ++ ";" ++
|
||||
"label" ++ false ++ ";" ++
|
||||
f.s ++
|
||||
"label" ++ true
|
||||
) ;
|
||||
Block stm = instrc stm.s ;
|
||||
Printf t e = instrc (e.s ++ "runtime" ++ typInstr "printf" t.t ++ paren (t.s) ++ "V") ;
|
||||
Return t exp = instr (exp.s ++ typInstr "return" t.t) ;
|
||||
Returnv = instr "return" ;
|
||||
End = ss [] ** {s2,s3 = []} ;
|
||||
|
||||
EVar t x = instr (typInstr "load" t.t ++ x.s) ;
|
||||
EInt n = instr ("ldc" ++ n.s) ;
|
||||
EFloat a b = instr ("ldc" ++ a.s ++ "." ++ b.s) ;
|
||||
EAdd t _ = binopt "add" t.t ;
|
||||
ESub t _ = binopt "sub" t.t ;
|
||||
EMul t _ = binopt "mul" t.t ;
|
||||
ELt t _ = binop ("runtime" ++ typInstr "lt" t.t ++ paren (t.s ++ t.s) ++ "I") ;
|
||||
EAppNil val f = instr (
|
||||
"static" ++ f.s ++ paren [] ++ val.s
|
||||
) ;
|
||||
EApp args val f exps = instr (
|
||||
exps.s ++
|
||||
"static" ++ f.s ++ paren args.s ++ val.s
|
||||
) ;
|
||||
|
||||
TInt = {s = "I" ; t = TIInt} ;
|
||||
TFloat = {s = "F" ; t = TIFloat} ;
|
||||
NilTyp = ss [] ;
|
||||
ConsTyp = cc2 ;
|
||||
OneExp _ e = e ;
|
||||
ConsExp _ _ = cc2 ;
|
||||
}
|
||||
20
examples/gfcc/JVM.hs
Normal file
20
examples/gfcc/JVM.hs
Normal file
@@ -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
|
||||
12
examples/gfcc/Makefile
Normal file
12
examples/gfcc/Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
all: pgf runtime
|
||||
|
||||
pgf:
|
||||
gf -make ImperC.gf ImperJVM.gf
|
||||
|
||||
runtime:
|
||||
java -jar jasmin.jar runtime.j
|
||||
|
||||
doc:
|
||||
pdflatex complin.tex
|
||||
pdflatex complin.tex
|
||||
|
||||
85
examples/gfcc/ResImper.gf
Normal file
85
examples/gfcc/ResImper.gf
Normal file
@@ -0,0 +1,85 @@
|
||||
resource ResImper = open Predef in {
|
||||
|
||||
-- precedence
|
||||
|
||||
param PAssoc = PN | PL | PR ;
|
||||
|
||||
oper
|
||||
Prec : PType = Predef.Ints 4 ;
|
||||
PrecExp : Type = {s : Str ; p : Prec ; a : PAssoc} ;
|
||||
|
||||
mkPrec : Prec -> PAssoc -> Str -> PrecExp = \p,a,f ->
|
||||
{s = f ; p = p ; a = a} ;
|
||||
|
||||
usePrec : PrecExp -> Prec -> Str = \x,p ->
|
||||
case <<x.p,p> : Prec * Prec> of {
|
||||
<3,4> | <2,3> | <2,4> => paren x.s ;
|
||||
<1,1> | <1,0> | <0,0> => x.s ;
|
||||
<1,_> | <0,_> => paren x.s ;
|
||||
_ => x.s
|
||||
} ;
|
||||
|
||||
constant : Str -> PrecExp = mkPrec 4 PN ;
|
||||
|
||||
infixN : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PN (usePrec x (nextPrec p) ++ f ++ usePrec y (nextPrec p)) ;
|
||||
infixL : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PL (usePrec x p ++ f ++ usePrec y (nextPrec p)) ;
|
||||
infixR : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PR (usePrec x (nextPrec p) ++ f ++ usePrec y p) ;
|
||||
|
||||
nextPrec : Prec -> Prec = \p -> case <p : Prec> of {
|
||||
4 => 4 ;
|
||||
n => Predef.plus n 1
|
||||
} ;
|
||||
|
||||
-- string operations
|
||||
|
||||
SS : Type = {s : Str} ;
|
||||
ss : Str -> SS = \s -> {s = s} ;
|
||||
cc2 : (_,_ : SS) -> SS = \x,y -> ss (x.s ++ y.s) ;
|
||||
|
||||
paren : Str -> Str = \str -> "(" ++ str ++ ")" ;
|
||||
|
||||
continues : Str -> SS -> SS = \s,t -> ss (s ++ ";" ++ t.s) ;
|
||||
continue : Str -> SS -> SS = \s,t -> ss (s ++ t.s) ;
|
||||
statement : Str -> SS = \s -> ss (s ++ ";");
|
||||
|
||||
-- taking cases of list size
|
||||
|
||||
param
|
||||
Size = Zero | One | More ;
|
||||
oper
|
||||
nextSize : Size -> Size = \n -> case n of {
|
||||
Zero => One ;
|
||||
_ => More
|
||||
} ;
|
||||
separator : Str -> Size -> Str = \t,n -> case n of {
|
||||
Zero => [] ;
|
||||
_ => t
|
||||
} ;
|
||||
|
||||
-- operations for JVM
|
||||
|
||||
param TypIdent = TIInt | TIFloat ; -- to be continued
|
||||
|
||||
oper
|
||||
typInstr : Str -> TypIdent -> Str = \instr,t -> case t of {
|
||||
TIInt => "i" + instr ;
|
||||
TIFloat => "f" + instr
|
||||
} ;
|
||||
|
||||
Instr : Type = {s,s2,s3 : Str} ; -- code, variables, labels
|
||||
instr : Str -> Instr = \s ->
|
||||
statement s ** {s2,s3 = []} ;
|
||||
instrc : Str -> Instr -> Instr = \s,i ->
|
||||
ss (s ++ ";" ++ i.s) ** {s2 = i.s2 ; s3 = i.s3} ;
|
||||
instrl : Str -> Instr -> Instr = \s,i ->
|
||||
ss (s ++ ";" ++ i.s) ** {s2 = i.s2 ; s3 = "L" ++ i.s3} ;
|
||||
instrb : Str -> Str -> Instr -> Instr = \v,s,i ->
|
||||
ss (s ++ ";" ++ i.s) ** {s2 = v ++ i.s2 ; s3 = i.s3} ;
|
||||
binop : Str -> SS -> SS -> SS = \op, x, y ->
|
||||
ss (x.s ++ y.s ++ op ++ ";") ;
|
||||
binopt : Str -> TypIdent -> SS -> SS -> SS = \op, t ->
|
||||
binop (typInstr op t) ;
|
||||
}
|
||||
16
examples/gfcc/ResImperEng.gf
Normal file
16
examples/gfcc/ResImperEng.gf
Normal file
@@ -0,0 +1,16 @@
|
||||
resource ResImperEng = open Predef, Prelude in {
|
||||
|
||||
oper
|
||||
indef = pre {"a" ;
|
||||
"an" / strs {"a" ; "e" ; "i" ; "o" ; "A" ; "E" ; "I" ; "O" }} ;
|
||||
|
||||
constant : Str -> SS = ss ;
|
||||
prefix : Str -> SS -> SS -> SS = \f,x,y ->
|
||||
ss ("the" ++ f ++ "of" ++ x.s ++ "and" ++ y.s) ;
|
||||
comparison : Str -> SS -> SS -> SS = \f,x,y ->
|
||||
ss (x.s ++ "is" ++ f ++ "than" ++ y.s) ;
|
||||
continues : Str -> SS -> SS = \s,t -> ss (s ++ "." ++ t.s) ;
|
||||
continue : Str -> SS -> SS = \s,t -> ss (s ++ t.s) ;
|
||||
statement : Str -> SS = \s -> ss (s ++ ".");
|
||||
|
||||
}
|
||||
20
examples/gfcc/abs.c
Normal file
20
examples/gfcc/abs.c
Normal file
@@ -0,0 +1,20 @@
|
||||
int abs (int x){
|
||||
int y ;
|
||||
{
|
||||
if (x < 0){
|
||||
y = 0 - x ;
|
||||
}
|
||||
else {
|
||||
y = x ;
|
||||
}
|
||||
}
|
||||
return y ;
|
||||
} ;
|
||||
int main () {
|
||||
int i ;
|
||||
i = abs (16);
|
||||
printf ("%d",i) ;
|
||||
return ;
|
||||
} ;
|
||||
|
||||
|
||||
96
examples/gfcc/complin.bbl
Normal file
96
examples/gfcc/complin.bbl
Normal file
@@ -0,0 +1,96 @@
|
||||
\begin{thebibliography}{10}
|
||||
|
||||
\bibitem{aho-ullman}
|
||||
A.~Aho, R.~Sethi, and J.~Ullman.
|
||||
\newblock {\em {Compilers: Principles, Techniques, and Tools}}.
|
||||
\newblock {Addison-Wesley}, 1988.
|
||||
|
||||
\bibitem{cayenne}
|
||||
L.~Augustsson.
|
||||
\newblock {Cayenne---a language with dependent types}.
|
||||
\newblock In {\em Proc. of {ICFP'98}}. ACM Press, September 1998.
|
||||
|
||||
\bibitem{bnfc}
|
||||
M.~Forsberg and A.~Ranta.
|
||||
\newblock {\mbox{BNF Converter Homepage}}.
|
||||
\newblock \verb!http://www.cs.chalmers.se/~markus/BNFC/!, 2002--2004.
|
||||
|
||||
\bibitem{harper-honsell}
|
||||
R.~Harper, F.~Honsell, and G.~Plotkin.
|
||||
\newblock {A Framework for Defining Logics}.
|
||||
\newblock {\em {JACM}}, 40(1):143--184, 1993.
|
||||
|
||||
\bibitem{metal}
|
||||
G.~Kahn, B.~Lang, B.~Mélèse, and E.~Morcos.
|
||||
\newblock Metal: a formalism to specify formalisms.
|
||||
\newblock {\em Science of {C}omputer {P}rogramming}, 3:151--188, 1983.
|
||||
|
||||
\bibitem{khegai}
|
||||
J.\ Khegai, B.\ Nordström, and A.\ Ranta.
|
||||
\newblock {Multilingual Syntax Editing in GF}.
|
||||
\newblock In A.~Gelbukh, editor, {\em {Intelligent Text Processing and
|
||||
Computational Linguistics (CICLing-2003), Mexico City, February 2003}},
|
||||
volume 2588 of {\em LNCS}, pages 453--464. Springer-Verlag, {2003}.
|
||||
%\newblock URL \url{http://www.cs.chalmers.se/~aarne/articles/mexico.ps.gz}.
|
||||
|
||||
\bibitem{knuth-attr}
|
||||
D.~Knuth.
|
||||
\newblock Semantics of context-free languages.
|
||||
\newblock {\em Mathematical {Systems} {Theory}}, 2:127--145, 1968.
|
||||
|
||||
\bibitem{landin}
|
||||
P.~Landin.
|
||||
\newblock The next 700 programming languages.
|
||||
\newblock {\em {Communications of the ACM}}, 9:157--166, 1966.
|
||||
|
||||
\bibitem{magnusson-nordstr}
|
||||
L.~Magnusson and B.~Nordstr\"{o}m.
|
||||
\newblock The {ALF} proof editor and its proof engine.
|
||||
\newblock In {\em {Types for Proofs and Programs}}, LNCS 806, pages 213--237.
|
||||
Springer, 1994.
|
||||
|
||||
\bibitem{happy}
|
||||
S.~Marlow.
|
||||
\newblock {Happy, The Parser Generator for Haskell}, 2001.
|
||||
\newblock \verb6http://www.haskell.org/happy/6.
|
||||
|
||||
\bibitem{jasmin}
|
||||
J.~Meyer and T.~Downing.
|
||||
\newblock {\em {Java Virtual Machine}}.
|
||||
\newblock O'Reilly, 1997.
|
||||
|
||||
\bibitem{semBNF}
|
||||
{P. M\"aenp\"a\"a}.
|
||||
\newblock {Semantic BNF}.
|
||||
\newblock In E.~Gimenez and C.~Paulin-Mohring, editors, {\em Types for Proofs
|
||||
and Programs, TYPES'96}, volume 1512 of {\em LNCS}, pages 196--215.
|
||||
Springer-Verlag, 1998.
|
||||
|
||||
\bibitem{pereira-shieber}
|
||||
F.~Pereira and S.~Shieber.
|
||||
\newblock {\em {Prolog and Natural-Language Analysis}}.
|
||||
\newblock {CSLI}, Stanford, 1987.
|
||||
|
||||
\bibitem{twelf}
|
||||
F.~Pfenning.
|
||||
\newblock {The Twelf Project}.
|
||||
\newblock \verb!http://www-2.cs.cmu.edu/~twelf!, 2002.
|
||||
|
||||
\bibitem{gf-jfp}
|
||||
A.~Ranta.
|
||||
\newblock {Grammatical Framework: A Type-Theoretical Grammar Formalism}.
|
||||
\newblock {\em {The Journal of Functional Programming}}, 14(2):145--189, 2004.
|
||||
%\newblock URL \url{http://www.cs.chalmers.se/~aarne/articles/gf-jfp.ps.gz}.
|
||||
|
||||
\bibitem{gf-homepage}
|
||||
A.~Ranta, K.~Angelov, and T.~Hallgren.
|
||||
\newblock {\mbox{Grammatical Framework Homepage}}.
|
||||
\newblock \verb!http://grammaticalframework.org!, 2000--2009.
|
||||
|
||||
\bibitem{teitelbaum}
|
||||
T.~Teitelbaum and T.~Reps.
|
||||
\newblock The {Cornell} {Program} {Synthesizer}: a syntax-directed programming
|
||||
environment.
|
||||
\newblock {\em Commun. {ACM}}, 24(9):563--573, 1981.
|
||||
|
||||
\end{thebibliography}
|
||||
1477
examples/gfcc/complin.tex
Normal file
1477
examples/gfcc/complin.tex
Normal file
File diff suppressed because it is too large
Load Diff
38
examples/gfcc/factorial.c
Normal file
38
examples/gfcc/factorial.c
Normal file
@@ -0,0 +1,38 @@
|
||||
int fact (int n) {
|
||||
int f ;
|
||||
f = 1 ;
|
||||
{
|
||||
while (1 < n) {
|
||||
f = n * f ;
|
||||
n = n - 1 ;
|
||||
}
|
||||
}
|
||||
return f ;
|
||||
} ;
|
||||
|
||||
int factr (int n) {
|
||||
int f ;
|
||||
{
|
||||
if (n < 2) {
|
||||
f = 1 ;
|
||||
}
|
||||
else {
|
||||
f = n * factr (n-1) ;
|
||||
}
|
||||
}
|
||||
return f ;
|
||||
} ;
|
||||
|
||||
int main () {
|
||||
int n ;
|
||||
n = 1 ;
|
||||
{
|
||||
while (n < 11) {
|
||||
printf("%d",fact(n)) ;
|
||||
printf("%d",factr(n)) ;
|
||||
n = n+1 ;
|
||||
}
|
||||
}
|
||||
return ;
|
||||
} ;
|
||||
|
||||
18
examples/gfcc/fibonacci.c
Normal file
18
examples/gfcc/fibonacci.c
Normal file
@@ -0,0 +1,18 @@
|
||||
int mx () {
|
||||
return 5000000 ;
|
||||
} ;
|
||||
|
||||
int main () {
|
||||
int lo ; int hi ;
|
||||
lo = 1 ;
|
||||
hi = lo ;
|
||||
printf("%d",lo) ;
|
||||
{
|
||||
while (hi < mx()) {
|
||||
printf("%d",hi) ;
|
||||
hi = lo + hi ;
|
||||
lo = hi - lo ;
|
||||
}
|
||||
}
|
||||
return ;
|
||||
} ;
|
||||
55
examples/gfcc/runtime.j
Normal file
55
examples/gfcc/runtime.j
Normal file
@@ -0,0 +1,55 @@
|
||||
.class public runtime
|
||||
.super java/lang/Object
|
||||
;
|
||||
; standard initializer
|
||||
.method public <init>()V
|
||||
aload_0
|
||||
invokenonvirtual java/lang/Object/<init>()V
|
||||
return
|
||||
.end method
|
||||
|
||||
.method public static ilt(II)I
|
||||
.limit locals 2
|
||||
.limit stack 2
|
||||
iload_0
|
||||
iload_1
|
||||
if_icmpge Label0
|
||||
iconst_1
|
||||
ireturn
|
||||
Label0:
|
||||
iconst_0
|
||||
ireturn
|
||||
.end method
|
||||
|
||||
.method public static flt(FF)I
|
||||
.limit locals 2
|
||||
.limit stack 2
|
||||
fload_0
|
||||
fload_1
|
||||
fcmpl
|
||||
ifge Label0
|
||||
iconst_1
|
||||
ireturn
|
||||
Label0:
|
||||
iconst_0
|
||||
ireturn
|
||||
.end method
|
||||
|
||||
.method public static iprintf(I)V
|
||||
.limit locals 1
|
||||
.limit stack 1000
|
||||
getstatic java/lang/System/out Ljava/io/PrintStream;
|
||||
iload_0
|
||||
invokevirtual java/io/PrintStream/println(I)V
|
||||
return
|
||||
.end method
|
||||
|
||||
.method public static fprintf(F)V
|
||||
.limit locals 1
|
||||
.limit stack 1000
|
||||
getstatic java/lang/System/out Ljava/io/PrintStream;
|
||||
fload_0
|
||||
invokevirtual java/io/PrintStream/println(F)V
|
||||
return
|
||||
.end method
|
||||
|
||||
Reference in New Issue
Block a user