]> ruin.nu Git - proglang.git/commitdiff
removed NoType and EDefault and added assert
authorMichael Andreen <harv@ruin.nu>
Thu, 2 Mar 2006 19:29:32 +0000 (19:29 +0000)
committerMichael Andreen <harv@ruin.nu>
Thu, 2 Mar 2006 19:29:32 +0000 (19:29 +0000)
Abssyntax.hs
Compile.hs
Docsyntax.tex
Interpret.hs
Lexsyntax.x
Parsyntax.y
Printsyntax.hs
Skelsyntax.hs
Typecheck.hs
syntax.cf

index d8ef904d06766fd3ccab71ee41d1ed9e9ceb319b..ec85c8d619730b34e85d5e00a39f4a562e995d06 100644 (file)
@@ -4,35 +4,38 @@ module Abssyntax where
 
 newtype Ident = Ident String deriving (Eq,Ord,Show)
 
+data Type =
+   TInt
+ | TBool
+  deriving (Eq,Ord,Show)
+
+data Stms =
+   Program [Stm]
+  deriving (Eq,Ord,Show)
+
 data Stm =
-   SDecl Type Ident Exp
- | SExp Exp
+   SExp Exp
  | SBlock [Stm]
- | SIf Exp Stm Stm
  | SWhile Exp Stm
+ | SIf Exp Stm Stm
  | SPrint Exp
  | SNoop
+ | SDecl Type Ident Exp
   deriving (Eq,Ord,Show)
 
 data Exp =
    EAss Ident Exp
  | EVar Ident
  | EInt Integer
+ | EBool Bool
  | ENeg Exp
  | ENot Exp
- | EBool Bool
  | EReadI
  | EReadB
- | ExpT Type Exp
- | EDefault
  | BiOpExp Exp Op Exp
  | EPost Ident Op
   deriving (Eq,Ord,Show)
 
-data Stms =
-   Program [Stm]
-  deriving (Eq,Ord,Show)
-
 data Op =
    Lt
  | ELt
@@ -46,9 +49,3 @@ data Op =
  | Div
   deriving (Eq,Ord,Show)
 
-data Type =
-   TInt
- | TBool
- | NoType
-  deriving (Eq,Ord,Show)
-
index c3e93f94850a720839196f010a8b6fe6294b60ce..69edecd879be433bab4f32259f8f0f79d2af0d41 100644 (file)
@@ -9,7 +9,6 @@ compileExp (EBool False) = "0";
 compileExp (EInt n) = show n
 compileExp (EVar (Ident i)) = i
 compileExp (EAss (Ident i) e) = i++"="++compileExp e
-compileExp EDefault = error "EDefault called from an illegal place"
 compileExp (BiOpExp e o e') = "("++compileExp e++")"++op o++"("++compileExp e'++")"
 compileExp (ENeg e) = "-("++compileExp e++")"
 compileExp (ENot e) ="!("++compileExp e++")" 
@@ -37,5 +36,4 @@ 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 (SDecl t (Ident i) EDefault) = "int "++i++"=0;\n"
 compileStm (SDecl t (Ident i) e) = "int "++i++"="++compileExp e++";\n"
index 09e48987172d66864ffffae73e3e784dc9506f2b..e0d2758fa311ea43031e94b4f8e449d033227bba 100644 (file)
@@ -49,8 +49,8 @@ The reserved words used in syntax are the following: \\
 The symbols used in syntax are the following: \\
 
 \begin{tabular}{lll}
-{\symb{{$=$}}} &{\symb{;}} &{\symb{\{}} \\
-{\symb{\}}} &{\symb{(}} &{\symb{)}} \\
+{\symb{;}} &{\symb{\{}} &{\symb{\}}} \\
+{\symb{{$=$}}} &{\symb{(}} &{\symb{)}} \\
 {\symb{{$+$}{$+$}}} &{\symb{{$-$}{$-$}}} &{\symb{{$-$}}} \\
 {\symb{!}} &{\symb{{$<$}}} &{\symb{{$<$}{$=$}}} \\
 {\symb{{$>$}}} &{\symb{{$>$}{$=$}}} &{\symb{{$=$}{$=$}}} \\
@@ -73,13 +73,24 @@ All other symbols are terminals.\\
 \end{tabular}\\
 
 \begin{tabular}{lll}
-{\nonterminal{Stm}} & {\arrow}  &{\nonterminal{Type}} {\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} {\terminal{;}}  \\
- & {\delimit}  &{\nonterminal{Type}} {\nonterminal{Ident}} {\terminal{;}}  \\
- & {\delimit}  &{\nonterminal{Exp}} {\terminal{;}}  \\
+{\nonterminal{Type}} & {\arrow}  &{\terminal{int}}  \\
+ & {\delimit}  &{\terminal{bool}}  \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{Stms}} & {\arrow}  &{\nonterminal{ListStm}}  \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{Stm}} & {\arrow}  &{\nonterminal{Exp}} {\terminal{;}}  \\
  & {\delimit}  &{\terminal{\{}} {\nonterminal{ListStm}} {\terminal{\}}}  \\
- & {\delimit}  &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}}  \\
- & {\delimit}  &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} {\terminal{else}} {\nonterminal{Stm}}  \\
+ & {\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}  &{\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}}  \\
  & {\delimit}  &{\terminal{print}} {\nonterminal{Exp}} {\terminal{;}}  \\
 \end{tabular}\\
 
@@ -104,9 +115,9 @@ All other symbols are terminals.\\
  & {\delimit}  &{\nonterminal{Ident}} {\terminal{{$-$}{$-$}}}  \\
  & {\delimit}  &{\nonterminal{Ident}}  \\
  & {\delimit}  &{\nonterminal{Integer}}  \\
+ & {\delimit}  &{\nonterminal{Bool}}  \\
  & {\delimit}  &{\terminal{{$-$}}} {\nonterminal{Exp3}}  \\
  & {\delimit}  &{\terminal{!}} {\nonterminal{Exp3}}  \\
- & {\delimit}  &{\nonterminal{Bool}}  \\
  & {\delimit}  &{\terminal{readInt}}  \\
  & {\delimit}  &{\terminal{readBool}}  \\
  & {\delimit}  &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}}  \\
@@ -117,10 +128,6 @@ All other symbols are terminals.\\
  & {\delimit}  &{\nonterminal{Stm}} {\nonterminal{ListStm}}  \\
 \end{tabular}\\
 
-\begin{tabular}{lll}
-{\nonterminal{Stms}} & {\arrow}  &{\nonterminal{ListStm}}  \\
-\end{tabular}\\
-
 \begin{tabular}{lll}
 {\nonterminal{Op0}} & {\arrow}  &{\terminal{{$<$}}}  \\
  & {\delimit}  &{\terminal{{$<$}{$=$}}}  \\
@@ -146,11 +153,6 @@ All other symbols are terminals.\\
  & {\delimit}  &{\nonterminal{Op0}}  \\
 \end{tabular}\\
 
-\begin{tabular}{lll}
-{\nonterminal{Type}} & {\arrow}  &{\terminal{int}}  \\
- & {\delimit}  &{\terminal{bool}}  \\
-\end{tabular}\\
-
 
 
 \end{document}
index 9e4dcff34541b1f180b73b3c12dc15cbcfb3e642..5a792b9b2bbbe27be026d99096d17df1ef5cffa2 100644 (file)
@@ -23,7 +23,6 @@ eval (EBool b) = return (VBool b)
 eval (EInt n) = return (VInt n)
 eval (EVar i) = getVariableValue i
 eval (EAss i e) = setVariableValue i e 
-eval EDefault = error "EDefault called from an illegal place"
 eval (BiOpExp e o e') = do
        v <- eval e
        v'<- eval e'
@@ -78,11 +77,6 @@ findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
 
 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value 
 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value 
-addVariable :: Ident -> Exp -> StateT Variables IO ()
-addVariable i e = do
-       e' <- eval e
-       (m:ms) <- get
-       put $ (insert i e' m):ms
 
 setVariableValue :: Ident -> Exp -> StateT Variables IO Value
 setVariableValue i e = do
@@ -113,9 +107,7 @@ execute (SBlock ss) = do
 execute (SWhile e s) = do
        (VBool b) <- eval e
        if b then execute s >> execute (SWhile e s) else return ()
-execute (SDecl t i EDefault) = do
-       case t of
-               TInt -> addVariable i (EInt 0)
-               TBool -> addVariable i (EBool False)
-       return ()
-execute (SDecl t i e) = addVariable i e >> return ()
+execute (SDecl t i e) =do
+       e' <- eval e
+       (m:ms) <- get
+       put $ (insert i e' m):ms
index 2d4ef67758974929d70389241cb78382e0e1a8cf..88f0e2d714f9a304daae1337c4ff2897ccece5a8 100644 (file)
@@ -16,7 +16,7 @@ $i = [$l $d _ ']          -- identifier character
 $u = [\0-\255]          -- universal: any character
 
 @rsyms =    -- symbols and non-identifier-like reserved words
-   \= | \; | \{ | \} | \( | \) | \+ \+ | \- \- | \- | \! | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
+   \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
 
 :-
 "//" [.]* ; -- Toss single line comments
index 461c967b366f8bf473c2b90877643b884aac1490..02e94b0bc4803b1a1ac5ca5f525de11cc3e348f1 100644 (file)
@@ -14,10 +14,10 @@ import ErrM
 %tokentype { Token }
 
 %token 
- '=' { PT _ (TS "=") }
  ';' { PT _ (TS ";") }
  '{' { PT _ (TS "{") }
  '}' { PT _ (TS "}") }
+ '=' { PT _ (TS "=") }
  '(' { PT _ (TS "(") }
  ')' { PT _ (TS ")") }
  '++' { PT _ (TS "++") }
@@ -59,14 +59,25 @@ Bool : 'true' { True }
   | 'false' { False }
 
 
+Type :: { Type }
+Type : 'int' { TInt } 
+  | 'bool' { TBool }
+
+
+Stms :: { Stms }
+Stms : ListStm { Program (reverse $1) } 
+
+
 Stm :: { Stm }
-Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 } 
-  | Type Ident ';' { decl_ $1 $2 }
-  | Exp ';' { SExp $1 }
+Stm : Exp ';' { SExp $1 } 
   | '{' ListStm '}' { SBlock (reverse $2) }
-  | 'if' '(' Exp ')' Stm { if_ $3 $5 }
-  | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
+  | 'int' Ident '=' Exp ';' { declIntE_ $2 $4 }
+  | 'bool' Ident '=' Exp ';' { declBoolE_ $2 $4 }
+  | 'int' Ident ';' { declInt_ $2 }
+  | 'bool' Ident ';' { declBool_ $2 }
   | 'while' '(' Exp ')' Stm { SWhile $3 $5 }
+  | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
+  | 'if' '(' Exp ')' Stm { if_ $3 $5 }
   | 'print' Exp ';' { SPrint $2 }
 
 
@@ -91,9 +102,9 @@ Exp3 : Ident '++' { postIncr_ $1 }
   | Ident '--' { postDecr_ $1 }
   | Ident { EVar $1 }
   | Integer { EInt $1 }
+  | Bool { EBool $1 }
   | '-' Exp3 { ENeg $2 }
   | '!' Exp3 { ENot $2 }
-  | Bool { EBool $1 }
   | 'readInt' { EReadI }
   | 'readBool' { EReadB }
   | '(' Exp ')' { $2 }
@@ -104,10 +115,6 @@ ListStm : {- empty -} { [] }
   | ListStm Stm { flip (:) $1 $2 }
 
 
-Stms :: { Stms }
-Stms : ListStm { Program (reverse $1) } 
-
-
 Op0 :: { Op }
 Op0 : '<' { Lt } 
   | '<=' { ELt }
@@ -133,11 +140,6 @@ Op : Op1 { $1 }
   | Op0 { $1 }
 
 
-Type :: { Type }
-Type : 'int' { TInt } 
-  | 'bool' { TBool }
-
-
 
 {
 
@@ -152,7 +154,10 @@ happyError ts =
   Bad $ "syntax error at " ++ tokenPos ts ++ if null ts then [] else (" before " ++ unwords (map prToken (take 4 ts)))
 
 myLexer = tokens
-decl_ t_ v_ = SDecl t_ v_ EDefault
+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_
index e84a1d333e8c46d73773924fa4869e035a7d191e..ff0128e8d8f68a0398d6b154dc5478968c19a4c6 100644 (file)
@@ -87,15 +87,26 @@ instance Print Bool where
    False  -> prPrec i 0 (concatD [doc (showString "false")])
 
 
+instance Print Type where
+  prt i e = case e of
+   TInt  -> prPrec i 0 (concatD [doc (showString "int")])
+   TBool  -> prPrec i 0 (concatD [doc (showString "bool")])
+
+
+instance Print Stms where
+  prt i e = case e of
+   Program stms -> prPrec i 0 (concatD [prt 0 stms])
+
+
 instance Print Stm where
   prt i e = case e of
-   SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")])
    SExp exp -> prPrec i 0 (concatD [prt 0 exp , doc (showString ";")])
    SBlock stms -> prPrec i 0 (concatD [doc (showString "{") , prt 0 stms , doc (showString "}")])
-   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])
    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 [])
@@ -106,22 +117,15 @@ instance Print Exp where
    EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
    EVar id -> prPrec i 3 (concatD [prt 0 id])
    EInt n -> prPrec i 3 (concatD [prt 0 n])
+   EBool bool -> prPrec i 3 (concatD [prt 0 bool])
    ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
    ENot exp -> prPrec i 3 (concatD [doc (showString "!") , prt 3 exp])
-   EBool bool -> prPrec i 3 (concatD [prt 0 bool])
    EReadI  -> prPrec i 3 (concatD [doc (showString "readInt")])
    EReadB  -> prPrec i 3 (concatD [doc (showString "readBool")])
-   ExpT type' exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 exp])
-   EDefault  -> prPrec i 0 (concatD [])
    BiOpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
    EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
 
 
-instance Print Stms where
-  prt i e = case e of
-   Program stms -> prPrec i 0 (concatD [prt 0 stms])
-
-
 instance Print Op where
   prt i e = case e of
    Lt  -> prPrec i 0 (concatD [doc (showString "<")])
@@ -136,11 +140,4 @@ instance Print Op where
    Div  -> prPrec i 2 (concatD [doc (showString "/")])
 
 
-instance Print Type where
-  prt i e = case e of
-   TInt  -> prPrec i 0 (concatD [doc (showString "int")])
-   TBool  -> prPrec i 0 (concatD [doc (showString "bool")])
-   NoType  -> prPrec i 0 (concatD [])
-
-
 
index 07898ef6d2aa24e4e2ff417f75af2b21268368cb..bc9bc0cd636ac74859db584ea12a06693837fbec 100644 (file)
@@ -20,15 +20,26 @@ transBool x = case x of
   False  -> failure x
 
 
+transType :: Type -> Result
+transType x = case x of
+  TInt  -> failure x
+  TBool  -> failure x
+
+
+transStms :: Stms -> Result
+transStms x = case x of
+  Program stms  -> failure x
+
+
 transStm :: Stm -> Result
 transStm x = case x of
-  SDecl type' id exp  -> failure x
   SExp exp  -> failure x
   SBlock stms  -> failure x
-  SIf exp stm0 stm  -> 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
@@ -36,22 +47,15 @@ transExp x = case x of
   EAss id exp  -> failure x
   EVar id  -> failure x
   EInt n  -> failure x
+  EBool bool  -> failure x
   ENeg exp  -> failure x
   ENot exp  -> failure x
-  EBool bool  -> failure x
   EReadI  -> failure x
   EReadB  -> failure x
-  ExpT type' exp  -> failure x
-  EDefault  -> failure x
   BiOpExp exp0 op exp  -> failure x
   EPost id op  -> failure x
 
 
-transStms :: Stms -> Result
-transStms x = case x of
-  Program stms  -> failure x
-
-
 transOp :: Op -> Result
 transOp x = case x of
   Lt  -> failure x
@@ -66,11 +70,4 @@ transOp x = case x of
   Div  -> failure x
 
 
-transType :: Type -> Result
-transType x = case x of
-  TInt  -> failure x
-  TBool  -> failure x
-  NoType  -> failure x
-
-
 
index 6f65d6e2f538d18d554787225c4aa138918a586f..a78f8f02c6a51d5d6d643d01ee8a4232f4223dab 100644 (file)
@@ -12,29 +12,31 @@ inList :: Eq a => a -> [a] -> Bool
 inList _ [] = False
 inList a (x:xs) = if a == x then True else inList a xs
 
+assert :: Monad m => Bool -> String -> m ()
+assert True _ = return ()
+assert False s = fail s
+
 typeCheckExp :: (MonadState Types m) => Exp -> m Type
 typeCheckExp (BiOpExp e o e') = do
        t1 <- typeCheckExp e
        t2 <- typeCheckExp e'
-       if not(t1 == t2) then fail "The parameters for the binary operator aren't equal" 
-               else if inList o [Eq,NEq] then return TBool
-                       else if not(t1 == TInt) then fail "The parameters need to be of type int" 
-                               else if inList o [Plus,Minus,Times,Div]
-                                       then return TInt
-                                       else return TBool
+       assert (t1 == t2) "The parameters for the binary operator aren't equal"
+       if inList o [Eq,NEq] then return TBool
+               else do 
+                       assert (t1 == TInt) "The parameters need to be of type int" 
+                       if inList o [Plus,Minus,Times,Div]
+                               then return TInt
+                               else return TBool
 typeCheckExp (EVar i) = typeCheckVar i
 typeCheckExp (EAss i e) = do
        t <- typeCheckVar i
        t2 <- typeCheckExp e
-       if t == t2 then return t else fail $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t 
+       assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t 
+       return t
 typeCheckExp (EInt i) = return TInt
 typeCheckExp (EBool b) = return TBool
 typeCheckExp EReadI = return TInt
 typeCheckExp EReadB = return TBool
-typeCheckExp (ExpT t e) = do
-       t2 <- typeCheckExp e
-       if t == t2 then return t else fail "Something went wrong ExpT is created with the wrong type"
-typeCheckExp EDefault = return NoType
 typeCheckExp (EPost i op) = do
        TInt <- typeCheckVar i
        return TInt
@@ -54,34 +56,34 @@ findVariable :: (MonadState Types m) => Ident -> Types -> m Type
 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
 
-typeCheckStm :: (MonadState Types m) => Stm -> m Type 
-typeCheckStm SNoop = return NoType
+typeCheckStm :: (MonadState Types m) => Stm -> m ()
+typeCheckStm SNoop = return ()
 typeCheckStm (SExp e) = do 
        typeCheckExp e
-       return NoType
+       return ()
 typeCheckStm (SBlock ss) = do 
        modify (empty:)
        mapM typeCheckStm ss
        modify tail
-       return NoType
 typeCheckStm (SIf e s s') = do
        TBool <- typeCheckExp e
-       NoType <- typeCheckStm s
-       NoType <- typeCheckStm s
-       return NoType
+       modify (empty:)
+       typeCheckStm s
+       modify (\s -> empty:tail s)
+       typeCheckStm s'
+       modify tail
 typeCheckStm (SWhile e s) = do
        TBool <- typeCheckExp e
-       NoType <- typeCheckStm s
-       return NoType
+       modify (empty:)
+       typeCheckStm s
+       modify tail
 typeCheckStm (SDecl t i e) = do
        t2 <- typeCheckExp e
-       if t == t2 || t2 == NoType then 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
-               return NoType
-               else fail $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t
+       assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t
+       (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 NoType
+       return ()
index 7b84054391014ef81d22f0f03d8e8866af3049ce..26408246305de004f448085d22bb717adca8736b 100644 (file)
--- a/syntax.cf
+++ b/syntax.cf
@@ -1,14 +1,27 @@
 
 -- ordinary rules
 
+
+True.    Bool ::= "true" ;
+False.   Bool ::= "false" ;
+
+TInt.   Type  ::= "int" ;
+TBool.  Type  ::= "bool" ;
+
 Program.  Stms ::= [Stm] ;
 
 
-SExp.     Stm      ::= Exp ";" ;
-SBlock.   Stm      ::= "{" [Stm] "}" ;
-SDecl.   Stm      ::= Type Ident "=" Exp ";" ;
-decl.    Stm      ::= Type Ident ";" ;
-define decl t v = SDecl t  v EDefault ;
+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);
+
 
 SWhile.   Stm      ::= "while" "(" Exp ")" Stm ;
 SIf.     Stm      ::= "if" "(" Exp ")" Stm "else" Stm ;
@@ -74,21 +87,17 @@ _. Op ::= Op2;
 _. Op ::= Op0;
 
 
-True.    Bool ::= "true" ;
-False.   Bool ::= "false" ;
-
-TInt.   Type  ::= "int" ;
-TBool.  Type  ::= "bool" ;
-
 -- pragmas
 
-internal ExpT. Exp ::= Type Exp ;
-internal SNoop. Stm ::= ;
-internal EDefault. Exp ::= ;
+-- internal ExpT. Exp ::= Type Exp ;
+-- internal EDefault. Exp ::= ;
 internal BiOpExp. Exp ::= Exp Op Exp ;
-internal NoType. Type ::= ;
+-- internal NoType. Type ::= ;
 internal EPost. Exp ::= Ident Op1 ;
 
+internal SNoop. Stm ::= ;
+internal SDecl. Stm ::= Type Ident "=" Exp ";" ;
+
 comment "/*" "*/" ;
 comment "//" ;