diff options
| author | aarne <aarne@cs.chalmers.se> | 2007-09-04 07:57:50 +0000 |
|---|---|---|
| committer | aarne <aarne@cs.chalmers.se> | 2007-09-04 07:57:50 +0000 |
| commit | e9985271e2d03e789e77b212526810aa2055ebe0 (patch) | |
| tree | ecb4e84ec4ee8a331589da1ee48a17fead2ffe54 /examples/tutorial/calculator/CalculatorC.gf | |
| parent | 14f185d326590f6656cf7b8c734fbfc87cf0605b (diff) | |
div
Diffstat (limited to 'examples/tutorial/calculator/CalculatorC.gf')
| -rw-r--r-- | examples/tutorial/calculator/CalculatorC.gf | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/examples/tutorial/calculator/CalculatorC.gf b/examples/tutorial/calculator/CalculatorC.gf index f78d4f38b..cfef0e717 100644 --- a/examples/tutorial/calculator/CalculatorC.gf +++ b/examples/tutorial/calculator/CalculatorC.gf @@ -1,6 +1,6 @@ --# -path=.:prelude -concrete CalculatorC of Calculator = open Prelude in { +concrete CalculatorC of Calculator = open Predef,Prelude in { flags lexer=codevars ; unlexer=code ; @@ -10,27 +10,39 @@ concrete CalculatorC of Calculator = open Prelude in { 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) ; + PDecl exp prog = ss ("int" ++ prog.$0 ++ "=" ++ top exp ++ ";" ++ prog.s) ; + PAss vr exp prog = ss (vr.s ++ "=" ++ top exp ++ ";" ++ prog.s) ; EPlus = infixl 0 "+" ; EMinus = infixl 0 "-" ; ETimes = infixl 1 "*" ; + EDiv = infixl 1 "/" ; EInt i = constant i.s ; EVar x = constant x.s ; oper - Prec : PType = Predef.Ints 2 ; + Prec : PType = 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 + lessPrec : Prec -> Prec -> Bool = \p,q -> + case <<p,q> : Prec * Prec> of { + <1,1> | <1,0> | <0,0> => False ; + <1,_> | <0,_> => True ; + _ => False } ; + usePrec : TermPrec -> Prec -> Str = \x,p -> + case lessPrec x.p p of { + True => paren x.s ; + False => noparen x.s + } ; + + paren : Str -> Str = \s -> "(" ++ s ++ ")" ; + noparen : Str -> Str = \s -> variants {s ; "(" ++ s ++ ")"} ; + + top : TermPrec -> Str = \t -> usePrec t 0 ; + mkPrec : Prec -> Str -> TermPrec = \p,s -> {s = s ; p = p} ; @@ -41,7 +53,7 @@ concrete CalculatorC of Calculator = open Prelude in { nextPrec : Prec -> Prec = \p -> case <p : Prec> of { 2 => 2 ; - n => Predef.plus n 1 + n => plus n 1 } ; } |
