From b472bd6c47d9983bd0a6383e362d9ebdba73145f Mon Sep 17 00:00:00 2001 From: aarne Date: Tue, 4 Sep 2007 07:57:50 +0000 Subject: [PATCH] div --- examples/tutorial/calculator/Calculator.gf | 2 +- examples/tutorial/calculator/CalculatorC.gf | 32 ++++++++++++++------- examples/tutorial/calculator/CalculatorJ.gf | 1 + 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/examples/tutorial/calculator/Calculator.gf b/examples/tutorial/calculator/Calculator.gf index 0d67c1e21..c2fbcd1bc 100644 --- a/examples/tutorial/calculator/Calculator.gf +++ b/examples/tutorial/calculator/Calculator.gf @@ -9,7 +9,7 @@ abstract Calculator = { PDecl : Exp -> (Var -> Prog) -> Prog ; PAss : Var -> Exp -> Prog -> Prog ; - EPlus, EMinus, ETimes : Exp -> Exp -> Exp ; + EPlus, EMinus, ETimes, EDiv : Exp -> Exp -> Exp ; EInt : Int -> Exp ; EVar : Var -> Exp ; 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 < : Prec * Prec> of { - <1,1> | <1,0> | <0,0> => x.s ; - <1,_> | <0,_> => "(" ++ x.s ++ ")" ; - _ => x.s + lessPrec : Prec -> Prec -> Bool = \p,q -> + case < : 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

of { 2 => 2 ; - n => Predef.plus n 1 + n => plus n 1 } ; } diff --git a/examples/tutorial/calculator/CalculatorJ.gf b/examples/tutorial/calculator/CalculatorJ.gf index 15b2caffd..0df7b73fe 100644 --- a/examples/tutorial/calculator/CalculatorJ.gf +++ b/examples/tutorial/calculator/CalculatorJ.gf @@ -15,6 +15,7 @@ concrete CalculatorJ of Calculator = open Prelude in { EPlus = postfix "iadd" ; EMinus = postfix "isub" ; ETimes = postfix "imul" ; + EDiv = postfix "imul" ; EInt = prefixSS "iconst" ; EVar = prefixSS "iload" ;