forked from GitHub/gf-core
bug fixes in parsing etc; improved ImperC
This commit is contained in:
@@ -26,10 +26,10 @@ concrete ImperC of Imper = open ResImper in {
|
||||
} ;
|
||||
|
||||
Decl typ cont = continues (typ.s ++ cont.$0) cont ;
|
||||
Assign _ x exp = continues (x.s ++ "=" ++ ex exp) ;
|
||||
Return _ exp = statement ("return" ++ ex exp) ;
|
||||
While exp loop = continue ("while" ++ paren (ex exp) ++ loop.s) ;
|
||||
IfElse exp t f = continue ("if" ++ paren (ex exp) ++ t.s ++ "else" ++ f.s) ;
|
||||
Assign _ x exp = continues (x.s ++ "=" ++ exp.s) ;
|
||||
Return _ exp = statement ("return" ++ exp.s) ;
|
||||
While exp loop = continue ("while" ++ paren exp.s ++ loop.s) ;
|
||||
IfElse exp t f = continue ("if" ++ paren exp.s ++ t.s ++ "else" ++ f.s) ;
|
||||
Block stm = continue ("{" ++ stm.s ++ "}") ;
|
||||
End = ss [] ;
|
||||
|
||||
@@ -50,6 +50,6 @@ concrete ImperC of Imper = open ResImper in {
|
||||
ConsTyp = cc2 ;
|
||||
|
||||
NilExp = ss [] ;
|
||||
OneExp _ e = ss (ex e) ;
|
||||
ConsExp _ _ e es = ss (ex e ++ "," ++ es.s) ;
|
||||
OneExp _ e = e ;
|
||||
ConsExp _ _ e es = ss (e.s ++ "," ++ es.s) ;
|
||||
}
|
||||
|
||||
@@ -2,27 +2,37 @@ resource ResImper = open Predef in {
|
||||
|
||||
-- precedence
|
||||
|
||||
oper
|
||||
Prec : PType = Predef.Ints 4 ;
|
||||
PrecExp : Type = {s : Prec => Str} ;
|
||||
ex : PrecExp -> Str = \exp -> exp.s ! 0 ;
|
||||
constant : Str -> PrecExp = \c -> {s = \\_ => c} ;
|
||||
infixN : Prec -> Str -> PrecExp -> PrecExp -> PrecExp = \p,f,x,y ->
|
||||
{s = mkPrec (x.s ! (nextPrec ! p) ++ f ++ y.s ! (nextPrec ! p)) ! p} ;
|
||||
infixL : Prec -> Str -> PrecExp -> PrecExp -> PrecExp = \p,f,x,y ->
|
||||
{s = mkPrec (x.s ! p ++ f ++ y.s ! (nextPrec ! p)) ! p} ;
|
||||
param PAssoc = PN | PL | PR ;
|
||||
|
||||
nextPrec : Prec => Prec = table {
|
||||
oper
|
||||
Prec : PType = Predef.Ints 4 ;
|
||||
PrecExp : Type = {s : Str ; p : Prec ; a : PAssoc} ;
|
||||
|
||||
mkPrec : Prec -> PAssoc -> Str -> PrecExp = \p,a,f ->
|
||||
{s = f ; p = p ; a = a} ;
|
||||
|
||||
usePrec : PrecExp -> Prec -> Str = \x,p ->
|
||||
case <<x.p,p> : Prec * Prec> of {
|
||||
<3,4> | <2,3> | <2,4> => paren x.s ;
|
||||
<1,1> | <1,0> | <0,0> => x.s ;
|
||||
<1,_> | <0,_> => paren x.s ;
|
||||
_ => x.s
|
||||
} ;
|
||||
|
||||
constant : Str -> PrecExp = mkPrec 4 PN ;
|
||||
|
||||
infixN : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PN (usePrec x (nextPrec p) ++ f ++ usePrec y (nextPrec p)) ;
|
||||
infixL : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PL (usePrec x p ++ f ++ usePrec y (nextPrec p)) ;
|
||||
infixR : Prec -> Str -> (_,_ : PrecExp) -> PrecExp = \p,f,x,y ->
|
||||
mkPrec p PR (usePrec x (nextPrec p) ++ f ++ usePrec y p) ;
|
||||
|
||||
nextPrec : Prec -> Prec = \p -> case <p : Prec> of {
|
||||
4 => 4 ;
|
||||
n => Predef.plus n 1
|
||||
} ;
|
||||
|
||||
mkPrec : Str -> Prec => Prec => Str = \str ->
|
||||
\\p,q => case Predef.lessInt p q of {
|
||||
Predef.PTrue => paren str ;
|
||||
_ => str
|
||||
} ;
|
||||
|
||||
-- string operations
|
||||
|
||||
SS : Type = {s : Str} ;
|
||||
|
||||
Reference in New Issue
Block a user