From fd3bc0ef4542783846af4e2c4f986e94f96c74ff Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Fri, 10 Mar 2006 08:36:58 +0000 Subject: [PATCH] added constructor for declaration with default value --- Abssyntax.hs | 3 ++- Compile.hs | 4 ++++ Docsyntax.tex | 6 ++---- Interpret.hs | 4 ++++ Parsyntax.y | 10 ++-------- Printsyntax.hs | 3 ++- Skelsyntax.hs | 3 ++- Typecheck.hs | 11 ++++++++--- syntax.cf | 11 ++--------- 9 files changed, 28 insertions(+), 27 deletions(-) diff --git a/Abssyntax.hs b/Abssyntax.hs index ec85c8d..d9fc344 100644 --- a/Abssyntax.hs +++ b/Abssyntax.hs @@ -16,11 +16,12 @@ data Stms = data Stm = SExp Exp | SBlock [Stm] + | SDecl Type Ident Exp + | SDeclD Type Ident | SWhile Exp Stm | SIf Exp Stm Stm | SPrint Exp | SNoop - | SDecl Type Ident Exp deriving (Eq,Ord,Show) data Exp = diff --git a/Compile.hs b/Compile.hs index 69edecd..0e1cf22 100644 --- a/Compile.hs +++ b/Compile.hs @@ -36,4 +36,8 @@ compileStm (SIf b s s') = "if("++compileExp b++")"++compileStm s++" \nelse "++co compileStm (SPrint e) = "printf(\"%d\\n\","++compileExp e++");\n" compileStm (SBlock ss) = "{\n"++concat (map (("\t"++).compileStm) ss)++"\n}\n" compileStm (SWhile e s) = "while("++compileExp e++")"++compileStm s +compileStm (SDeclD t i) = compileStm (SDecl t i $ case t of + TInt -> EInt 0 + TBool -> EBool False + ) compileStm (SDecl t (Ident i) e) = "int "++i++"="++compileExp e++";\n" diff --git a/Docsyntax.tex b/Docsyntax.tex index e0d2758..d978641 100644 --- a/Docsyntax.tex +++ b/Docsyntax.tex @@ -84,10 +84,8 @@ All other symbols are terminals.\\ \begin{tabular}{lll} {\nonterminal{Stm}} & {\arrow} &{\nonterminal{Exp}} {\terminal{;}} \\ & {\delimit} &{\terminal{\{}} {\nonterminal{ListStm}} {\terminal{\}}} \\ - & {\delimit} &{\terminal{int}} {\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} {\terminal{;}} \\ - & {\delimit} &{\terminal{bool}} {\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} {\terminal{;}} \\ - & {\delimit} &{\terminal{int}} {\nonterminal{Ident}} {\terminal{;}} \\ - & {\delimit} &{\terminal{bool}} {\nonterminal{Ident}} {\terminal{;}} \\ + & {\delimit} &{\nonterminal{Type}} {\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} {\terminal{;}} \\ + & {\delimit} &{\nonterminal{Type}} {\nonterminal{Ident}} {\terminal{;}} \\ & {\delimit} &{\terminal{while}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} \\ & {\delimit} &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} {\terminal{else}} {\nonterminal{Stm}} \\ & {\delimit} &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} \\ diff --git a/Interpret.hs b/Interpret.hs index 8ac65bf..fd04dcd 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -109,6 +109,10 @@ execute (SBlock ss) = pushAndPop $ mapM execute ss execute (SWhile e s) = do (VBool b) <- eval e if b then pushAndPop (execute s) >> execute (SWhile e s) else return () +execute (SDeclD t i) = execute (SDecl t i $ case t of + TInt -> EInt 0 + TBool -> EBool False + ) execute (SDecl t i e) =do e' <- eval e (m:ms) <- get diff --git a/Parsyntax.y b/Parsyntax.y index 02e94b0..6613fa3 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -71,10 +71,8 @@ Stms : ListStm { Program (reverse $1) } Stm :: { Stm } Stm : Exp ';' { SExp $1 } | '{' ListStm '}' { SBlock (reverse $2) } - | 'int' Ident '=' Exp ';' { declIntE_ $2 $4 } - | 'bool' Ident '=' Exp ';' { declBoolE_ $2 $4 } - | 'int' Ident ';' { declInt_ $2 } - | 'bool' Ident ';' { declBool_ $2 } + | Type Ident '=' Exp ';' { SDecl $1 $2 $4 } + | Type Ident ';' { SDeclD $1 $2 } | 'while' '(' Exp ')' Stm { SWhile $3 $5 } | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 } | 'if' '(' Exp ')' Stm { if_ $3 $5 } @@ -154,10 +152,6 @@ happyError ts = Bad $ "syntax error at " ++ tokenPos ts ++ if null ts then [] else (" before " ++ unwords (map prToken (take 4 ts))) myLexer = tokens -declIntE_ x_ e_ = SDecl TInt x_ e_ -declBoolE_ x_ e_ = SDecl TBool x_ e_ -declInt_ x_ = SDecl TInt x_ (EInt 0) -declBool_ x_ = SDecl TBool x_ (EBool False) if_ e_ s_ = SIf e_ s_ SNoop compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ diff --git a/Printsyntax.hs b/Printsyntax.hs index ff0128e..d1b4c49 100644 --- a/Printsyntax.hs +++ b/Printsyntax.hs @@ -102,11 +102,12 @@ instance Print Stm where prt i e = case e of SExp exp -> prPrec i 0 (concatD [prt 0 exp , doc (showString ";")]) SBlock stms -> prPrec i 0 (concatD [doc (showString "{") , prt 0 stms , doc (showString "}")]) + SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")]) + SDeclD type' id -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString ";")]) SWhile exp stm -> prPrec i 0 (concatD [doc (showString "while") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm]) SIf exp stm0 stm -> prPrec i 0 (concatD [doc (showString "if") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm0 , doc (showString "else") , prt 0 stm]) SPrint exp -> prPrec i 0 (concatD [doc (showString "print") , prt 0 exp , doc (showString ";")]) SNoop -> prPrec i 0 (concatD []) - SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")]) prtList es = case es of [] -> (concatD []) diff --git a/Skelsyntax.hs b/Skelsyntax.hs index bc9bc0c..9f3feab 100644 --- a/Skelsyntax.hs +++ b/Skelsyntax.hs @@ -35,11 +35,12 @@ transStm :: Stm -> Result transStm x = case x of SExp exp -> failure x SBlock stms -> failure x + SDecl type' id exp -> failure x + SDeclD type' id -> failure x SWhile exp stm -> failure x SIf exp stm0 stm -> failure x SPrint exp -> failure x SNoop -> failure x - SDecl type' id exp -> failure x transExp :: Exp -> Result diff --git a/Typecheck.hs b/Typecheck.hs index cdba298..70aad70 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -75,13 +75,18 @@ typeCheckStm (SIf e s s') = do typeCheckStm (SWhile e s) = do TBool <- typeCheckExp e pushAndPop $ typeCheckStm s +typeCheckStm (SDeclD t i) = addVariable i t typeCheckStm (SDecl t i e) = do t2 <- typeCheckExp e assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t + addVariable i t +typeCheckStm (SPrint e) = do + typeCheckExp e + return () + +addVariable :: (MonadState Types m) => Ident -> Type -> m () +addVariable i t = do (m:ms) <- get case insertLookupWithKey (\k a1 a2 -> a1) i t m of (Nothing,m') -> put (m':ms) _ -> fail $ "Duplicate variable declaration: "++show i -typeCheckStm (SPrint e) = do - typeCheckExp e - return () diff --git a/syntax.cf b/syntax.cf index 2640824..0347ce5 100644 --- a/syntax.cf +++ b/syntax.cf @@ -13,14 +13,8 @@ Program. Stms ::= [Stm] ; SExp. Stm ::= Exp ";" ; SBlock. Stm ::= "{" [Stm] "}" ; -declIntE. Stm ::= "int" Ident "=" Exp ";" ; -declBoolE. Stm ::= "bool" Ident "=" Exp ";" ; -define declIntE x e = SDecl TInt x e; -define declBoolE x e = SDecl TBool x e; -declInt. Stm ::= "int" Ident ";" ; -declBool. Stm ::= "bool" Ident ";" ; -define declInt x = SDecl TInt x (EInt 0); -define declBool x = SDecl TBool x (EBool False); +SDecl. Stm ::= Type Ident "=" Exp ";" ; +SDeclD. Stm ::= Type Ident ";" ; SWhile. Stm ::= "while" "(" Exp ")" Stm ; @@ -96,7 +90,6 @@ internal BiOpExp. Exp ::= Exp Op Exp ; internal EPost. Exp ::= Ident Op1 ; internal SNoop. Stm ::= ; -internal SDecl. Stm ::= Type Ident "=" Exp ";" ; comment "/*" "*/" ; comment "//" ; -- 2.39.2