diff options
| author | aarne <aarne@cs.chalmers.se> | 2007-09-03 19:21:50 +0000 |
|---|---|---|
| committer | aarne <aarne@cs.chalmers.se> | 2007-09-03 19:21:50 +0000 |
| commit | 14f185d326590f6656cf7b8c734fbfc87cf0605b (patch) | |
| tree | 11b0a0c51c4226675f6cc53669d46affde7ad531 /examples/tutorial/calculator/CalculatorC.gf | |
| parent | ee9416d5232c86c7fa084f1a384d7424ca2d8fee (diff) | |
calculator ex
Diffstat (limited to 'examples/tutorial/calculator/CalculatorC.gf')
| -rw-r--r-- | examples/tutorial/calculator/CalculatorC.gf | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/tutorial/calculator/CalculatorC.gf b/examples/tutorial/calculator/CalculatorC.gf new file mode 100644 index 000000000..f78d4f38b --- /dev/null +++ b/examples/tutorial/calculator/CalculatorC.gf @@ -0,0 +1,47 @@ +--# -path=.:prelude + +concrete CalculatorC of Calculator = open Prelude in { + + flags lexer=codevars ; unlexer=code ; + + lincat + Prog, Var = SS ; + Exp = TermPrec ; + + lin + PEmpty = ss [] ; + PDecl exp prog = ss ("int" ++ prog.$0 ++ "=" ++ exp.s ++ ";" ++ prog.s) ; + PAss vr exp prog = ss (vr.s ++ "=" ++ exp.s ++ ";" ++ prog.s) ; + + EPlus = infixl 0 "+" ; + EMinus = infixl 0 "-" ; + ETimes = infixl 1 "*" ; + + EInt i = constant i.s ; + EVar x = constant x.s ; + + oper + Prec : PType = Predef.Ints 2 ; + TermPrec : Type = {s : Str ; p : Prec} ; + + usePrec : TermPrec -> Prec -> Str = \x,p -> + case <<x.p,p> : Prec * Prec> of { + <1,1> | <1,0> | <0,0> => x.s ; + <1,_> | <0,_> => "(" ++ x.s ++ ")" ; + _ => x.s + } ; + + mkPrec : Prec -> Str -> TermPrec = \p,s -> + {s = s ; p = p} ; + + constant : Str -> TermPrec = mkPrec 2 ; + + infixl : Prec -> Str -> (_,_ : TermPrec) -> TermPrec = \p,f,x,y -> + mkPrec p (usePrec x p ++ f ++ usePrec y (nextPrec p)) ; + + nextPrec : Prec -> Prec = \p -> case <p : Prec> of { + 2 => 2 ; + n => Predef.plus n 1 + } ; + +} |
