mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-20 08:32:50 -06:00
new precedence lib
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
--# -path=.:prelude
|
--# -path=.:prelude
|
||||||
|
|
||||||
concrete CalculatorC of Calculator = open Predef,Prelude in {
|
concrete CalculatorC of Calculator = open Prelude, Formal in {
|
||||||
|
|
||||||
flags lexer=codevars ; unlexer=code ;
|
flags lexer=codevars ; unlexer=code ;
|
||||||
|
|
||||||
@@ -21,39 +21,4 @@ concrete CalculatorC of Calculator = open Predef,Prelude in {
|
|||||||
EInt i = constant i.s ;
|
EInt i = constant i.s ;
|
||||||
EVar x = constant x.s ;
|
EVar x = constant x.s ;
|
||||||
|
|
||||||
oper
|
|
||||||
Prec : PType = Ints 2 ;
|
|
||||||
TermPrec : Type = {s : Str ; p : Prec} ;
|
|
||||||
|
|
||||||
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} ;
|
|
||||||
|
|
||||||
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 => plus n 1
|
|
||||||
} ;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user