making parsing ImperC work

This commit is contained in:
aarne
2004-09-22 20:42:21 +00:00
parent a0116fd288
commit d5b4230d6d
6 changed files with 51 additions and 31 deletions

View File

@@ -2,6 +2,7 @@ abstract Imper = PredefAbs ** {
cat cat
Program ; Program ;
Rec ListTyp ;
Typ ; Typ ;
NumTyp ; NumTyp ;
ListTyp ; ListTyp ;
@@ -15,12 +16,13 @@ abstract Imper = PredefAbs ** {
fun fun
Empty : Program ; Empty : Program ;
Funct : (AS : ListTyp) -> (V : Typ) -> Funct : (AS : ListTyp) -> (V : Typ) ->
Body AS -> (Fun AS V -> Program) -> Program ; (Fun AS V -> Rec AS) -> Program ;
FunctNil : (V : Typ) ->
Stm -> (Fun NilTyp V -> Program) -> Program ;
BodyNil : Stm -> Body NilTyp ; RecOne : (A : Typ) -> (Var A -> Stm) -> Program -> Rec (ConsTyp A NilTyp) ;
BodyOne : (A : Typ) -> (Var A -> Stm) -> Body (ConsTyp A NilTyp) ; RecCons : (A : Typ) -> (AS : ListTyp) ->
BodyCons : (A : Typ) -> (AS : ListTyp) -> (Var A -> Rec AS) -> Program -> Rec (ConsTyp A AS) ;
(Var A -> Body AS) -> Body (ConsTyp A AS) ;
Decl : (A : Typ) -> (Var A -> Stm) -> Stm ; Decl : (A : Typ) -> (Var A -> Stm) -> Stm ;
Assign : (A : Typ) -> Var A -> Exp A -> Stm -> Stm ; Assign : (A : Typ) -> Var A -> Exp A -> Stm -> Stm ;

View File

@@ -4,21 +4,25 @@ concrete ImperC of Imper = open ResImper in {
lincat lincat
Exp = PrecExp ; Exp = PrecExp ;
Body = {s,s2 : Str} ; Rec = {s,s2,s3 : Str} ;
lin lin
Empty = ss [] ; Empty = ss [] ;
Funct args val body cont = ss ( FunctNil val stm cont = ss (
val.s ++ cont.$0 ++ paren body.s2 ++ "{" ++ val.s ++ cont.$0 ++ paren [] ++ "{" ++
body.s ++ "}" ++ ";" ++ cont.s) ; stm.s ++ "}" ++ ";" ++ cont.s) ;
Funct args val rec = ss (
val.s ++ rec.$0 ++ paren rec.s2 ++ "{" ++
rec.s ++ "}" ++ ";" ++ rec.s3) ;
BodyNil stm = stm ** {s2 = []} ; RecOne typ stm prg = stm ** {
BodyOne typ stm = stm ** { s2 = typ.s ++ stm.$0 ;
s2 = typ.s ++ stm.$0 s3 = prg.s
} ; } ;
BodyCons typ _ body = { RecCons typ _ body prg = {
s = body.s ; s = body.s ;
s2 = typ.s ++ body.$0 ++ "," ++ body.s2 ; s2 = typ.s ++ body.$0 ++ "," ++ body.s2 ;
s3 = prg.s
} ; } ;
Decl typ cont = continues (typ.s ++ cont.$0) cont ; Decl typ cont = continues (typ.s ++ cont.$0) cont ;

View File

@@ -1,24 +1,37 @@
--# -path=.:../prelude
concrete ImperJVM of Imper = open ResImper in { concrete ImperJVM of Imper = open ResImper in {
flags lexer=codevars ; unlexer=code ; startcat=Stm ; flags lexer=codevars ; unlexer=code ; startcat=Stm ;
lincat lincat
Body = {s,s2 : Str} ; -- code, storage for locals Rec = {s,s2,s3 : Str} ; -- code, storage for locals, continuation
Stm = Instr ; Stm = Instr ;
lin lin
Empty = ss [] ; Empty = ss [] ;
Funct args val body rest = ss ( FunctNil val stm cont = ss (
".method" ++ rest.$0 ++ paren args.s ++ val.s ++ ";" ++ ".method" ++ cont.$0 ++ paren [] ++ val.s ++ ";" ++
".limit" ++ "locals" ++ body.s2 ++ ";" ++ ".limit" ++ "locals" ++ stm.s2 ++ ";" ++
".limit" ++ "stack" ++ "1000" ++ ";" ++ ".limit" ++ "stack" ++ "1000" ++ ";" ++
body.s ++ stm.s ++
".end" ++ "method" ++ ";" ++ ".end" ++ "method" ++ ";" ++
rest.s cont.s
) ; ) ;
BodyNil stm = stm ; Funct args val rec = ss (
BodyCons a as body = instrb a.s ( ".method" ++ rec.$0 ++ paren args.s ++ val.s ++ ";" ++
"alloc" ++ a.s ++ body.$0 ++ body.s2) (body ** {s3 = []}); ".limit" ++ "locals" ++ rec.s2 ++ ";" ++
".limit" ++ "stack" ++ "1000" ++ ";" ++
rec.s ++
".end" ++ "method" ++ ";" ++
rec.s3
) ;
RecOne typ stm prg = instrb typ.s (
"alloc" ++ typ.s ++ stm.$0 ++ stm.s2) {s = stm.s ; s2 = stm.s2 ; s3 = prg.s};
RecCons typ _ body prg = instrb typ.s (
"alloc" ++ typ.s ++ body.$0 ++ body.s2)
{s = body.s ; s2 = body.s2 ; s3 = prg.s};
Decl typ cont = instrb typ.s ( Decl typ cont = instrb typ.s (
"alloc" ++ typ.s ++ cont.$0 "alloc" ++ typ.s ++ cont.$0
@@ -61,19 +74,16 @@ flags lexer=codevars ; unlexer=code ; startcat=Stm ;
EVar t x = instr (t.s ++ "_load" ++ x.s) ; EVar t x = instr (t.s ++ "_load" ++ x.s) ;
EInt n = instr ("ipush" ++ n.s) ; EInt n = instr ("ipush" ++ n.s) ;
EFloat a b = instr ("fpush" ++ a.s ++ "." ++ b.s) ; EFloat a b = instr ("fpush" ++ a.s ++ "." ++ b.s) ;
EAddI = binop "iadd" ; EAdd = binopt "add" ;
EAddF = binop "fadd" ; ESub = binopt "sub" ;
ESubI = binop "isub" ; EMul = binopt "mul" ;
ESubF = binop "fsub" ; ELt t = binop ("invoke" ++ t.s ++ "lt" ++ paren (t.s ++ t.s) ++ "i") ;
EMulI = binop "imul" ;
EMulF = binop "fmul" ;
ELtI = binop ("call" ++ "ilt") ;
ELtF = binop ("call" ++ "flt") ;
EApp args val f exps = instr ( EApp args val f exps = instr (
exps.s ++ exps.s ++
"invoke" ++ f.s ++ paren args.s ++ val.s "invoke" ++ f.s ++ paren args.s ++ val.s
) ; ) ;
TNum t = t ;
TInt = ss "i" ; TInt = ss "i" ;
TFloat = ss "f" ; TFloat = ss "f" ;
@@ -81,5 +91,6 @@ flags lexer=codevars ; unlexer=code ; startcat=Stm ;
ConsTyp = cc2 ; ConsTyp = cc2 ;
NilExp = ss [] ; NilExp = ss [] ;
OneExp _ e = e ;
ConsExp _ _ = cc2 ; ConsExp _ _ = cc2 ;
} }

View File

@@ -67,4 +67,6 @@ resource ResImper = {
ss (s ++ ";" ++ i.s) ** {s2 = v ++ i.s2 ; s3 = i.s3} ; ss (s ++ ";" ++ i.s) ** {s2 = v ++ i.s2 ; s3 = i.s3} ;
binop : Str -> SS -> SS -> SS = \op, x, y -> binop : Str -> SS -> SS -> SS = \op, x, y ->
ss (x.s ++ y.s ++ op ++ ";") ; ss (x.s ++ y.s ++ op ++ ";") ;
binopt : Str -> SS -> SS -> SS -> SS = \op, x, y, t ->
ss (x.s ++ y.s ++ t.s ++ op ++ ";") ;
} }

View File

@@ -168,6 +168,7 @@ instance Print Val where
prt (VClos env e) = case e of prt (VClos env e) = case e of
Meta _ -> prt_ e ++ prEnv env Meta _ -> prt_ e ++ prEnv env
_ -> prt_ e ---- ++ prEnv env ---- for debugging _ -> prt_ e ---- ++ prEnv env ---- for debugging
prt VType = "Type"
prv1 v = case v of prv1 v = case v of
VApp _ _ -> prParenth $ prt v VApp _ _ -> prParenth $ prt v

View File

@@ -59,7 +59,7 @@ prToken t = case t of
PT _ (TD s) -> s PT _ (TD s) -> s
PT _ (TC s) -> s PT _ (TC s) -> s
PT _ (T_LString s) -> s PT _ (T_LString s) -> s
_ -> show t
eitherResIdent :: (String -> Tok) -> String -> Tok eitherResIdent :: (String -> Tok) -> String -> Tok
eitherResIdent tv s = if isResWord s then (TS s) else (tv s) where eitherResIdent tv s = if isResWord s then (TS s) else (tv s) where