1
0
forked from GitHub/gf-core
This commit is contained in:
aarne
2007-09-04 07:57:50 +00:00
parent 1608ac9f6d
commit b472bd6c47
3 changed files with 24 additions and 11 deletions

View File

@@ -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 ;

View File

@@ -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
} ;
}

View File

@@ -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" ;