diff --git a/examples/tutorial/calculator/CalculatorC.gf b/examples/tutorial/calculator/CalculatorC.gf index cfef0e717..e36c28e3f 100644 --- a/examples/tutorial/calculator/CalculatorC.gf +++ b/examples/tutorial/calculator/CalculatorC.gf @@ -1,6 +1,6 @@ --# -path=.:prelude -concrete CalculatorC of Calculator = open Predef,Prelude in { +concrete CalculatorC of Calculator = open Prelude, Formal in { flags lexer=codevars ; unlexer=code ; @@ -21,39 +21,4 @@ concrete CalculatorC of Calculator = open Predef,Prelude in { EInt i = constant i.s ; EVar x = constant x.s ; - oper - Prec : PType = Ints 2 ; - TermPrec : Type = {s : Str ; p : Prec} ; - - 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} ; - - 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

of { - 2 => 2 ; - n => plus n 1 - } ; - } diff --git a/lib/prelude/Formal.gf b/lib/prelude/Formal.gf new file mode 100644 index 000000000..2aa33d9ef --- /dev/null +++ b/lib/prelude/Formal.gf @@ -0,0 +1,54 @@ +resource Formal = open Prelude in { + +-- to replace the old library Precedence + + oper + Prec : PType ; + TermPrec : Type = {s : Str ; p : Prec} ; + + mkPrec : Prec -> Str -> TermPrec = \p,s -> + {s = s ; p = p} ; + + top : TermPrec -> Str = usePrec 0 ; + + constant : Str -> TermPrec = mkPrec highest ; + + infixl : Prec -> Str -> (_,_ : TermPrec) -> TermPrec = \p,f,x,y -> + mkPrec p (usePrec p x ++ f ++ usePrec (nextPrec p) y) ; + infixr : Prec -> Str -> (_,_ : TermPrec) -> TermPrec = \p,f,x,y -> + mkPrec p (usePrec (nextPrec p) x ++ f ++ usePrec p y) ; + infixn : Prec -> Str -> (_,_ : TermPrec) -> TermPrec = \p,f,x,y -> + mkPrec p (usePrec (nextPrec p) x ++ f ++ usePrec (nextPrec p) y) ; + +-- auxiliaries, should not be needed so much + + usePrec : Prec -> TermPrec -> Str = \p,x -> + case lessPrec x.p p of { + True => parenth x.s ; + False => parenthOpt x.s + } ; + + parenth : Str -> Str = \s -> "(" ++ s ++ ")" ; + parenthOpt : Str -> Str = \s -> variants {s ; "(" ++ s ++ ")"} ; + +--. +-- low-level things: don't use + + Prec : PType = Predef.Ints 4 ; + + highest = 4 ; + + lessPrec : Prec -> Prec -> Bool = \p,q -> + case < : Prec * Prec> of { + <3,4> | <2,3> | <2,4> => True ; + <1,1> | <1,0> | <0,0> => False ; + <1,_> | <0,_> => True ; + _ => False + } ; + + nextPrec : Prec -> Prec = \p -> case

of { + 4 => 4 ; + n => Predef.plus n 1 + } ; + +}