]> ruin.nu Git - proglang.git/commitdiff
streamlined the binary operators
authorMichael Andreen <harv@ruin.nu>
Mon, 27 Feb 2006 10:25:29 +0000 (10:25 +0000)
committerMichael Andreen <harv@ruin.nu>
Mon, 27 Feb 2006 10:25:29 +0000 (10:25 +0000)
Abssyntax.hs
Docsyntax.tex
Parsyntax.y
Printsyntax.hs
Skelsyntax.hs
Typecheck.hs
syntax.cf

index 7c95d5324e1a58ef1a8f32b099d16fb0babcac42..4cc8d19eb8e0dea898b8239177d6c32e73e7fee9 100644 (file)
@@ -15,9 +15,8 @@ data Stm =
   deriving (Eq,Ord,Show)
 
 data Exp =
   deriving (Eq,Ord,Show)
 
 data Exp =
-   BExp Exp BOp Exp
+   EAss Ident Exp
  | EVar Ident
  | EVar Ident
- | EAss Ident Exp
  | EInt Integer
  | ENeg Exp
  | EBool Bool
  | EInt Integer
  | ENeg Exp
  | EBool Bool
@@ -25,7 +24,7 @@ data Exp =
  | EReadB
  | ExpT Type Exp
  | EDefault
  | EReadB
  | ExpT Type Exp
  | EDefault
- | OpExp Exp Op Exp
+ | BiOpExp Exp Op Exp
  | EPost Ident Op
   deriving (Eq,Ord,Show)
 
  | EPost Ident Op
   deriving (Eq,Ord,Show)
 
@@ -33,17 +32,14 @@ data Stms =
    Program [Stm]
   deriving (Eq,Ord,Show)
 
    Program [Stm]
   deriving (Eq,Ord,Show)
 
-data BOp =
+data Op =
    Lt
  | ELt
  | Gt
  | EGt
  | Eq
  | NEq
    Lt
  | ELt
  | Gt
  | EGt
  | Eq
  | NEq
-  deriving (Eq,Ord,Show)
-
-data Op =
-   Plus
+ | Plus
  | Minus
  | Times
  | Div
  | Minus
  | Times
  | Div
index 76d4a279cb7e9486db34d88dbffe3dddd165d14f..89c1a0f72a0fd971c506d65c77b011cc581914a4 100644 (file)
@@ -84,7 +84,7 @@ All other symbols are terminals.\\
 
 \begin{tabular}{lll}
 {\nonterminal{Exp}} & {\arrow}  &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}}  \\
 
 \begin{tabular}{lll}
 {\nonterminal{Exp}} & {\arrow}  &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}}  \\
- & {\delimit}  &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}}  \\
+ & {\delimit}  &{\nonterminal{Exp1}} {\nonterminal{Op0}} {\nonterminal{Exp1}}  \\
  & {\delimit}  &{\nonterminal{Exp1}}  \\
 \end{tabular}\\
 
  & {\delimit}  &{\nonterminal{Exp1}}  \\
 \end{tabular}\\
 
@@ -120,7 +120,7 @@ All other symbols are terminals.\\
 \end{tabular}\\
 
 \begin{tabular}{lll}
 \end{tabular}\\
 
 \begin{tabular}{lll}
-{\nonterminal{BOp}} & {\arrow}  &{\terminal{{$<$}}}  \\
+{\nonterminal{Op0}} & {\arrow}  &{\terminal{{$<$}}}  \\
  & {\delimit}  &{\terminal{{$<$}{$=$}}}  \\
  & {\delimit}  &{\terminal{{$>$}}}  \\
  & {\delimit}  &{\terminal{{$>$}{$=$}}}  \\
  & {\delimit}  &{\terminal{{$<$}{$=$}}}  \\
  & {\delimit}  &{\terminal{{$>$}}}  \\
  & {\delimit}  &{\terminal{{$>$}{$=$}}}  \\
@@ -141,6 +141,7 @@ All other symbols are terminals.\\
 \begin{tabular}{lll}
 {\nonterminal{Op}} & {\arrow}  &{\nonterminal{Op1}}  \\
  & {\delimit}  &{\nonterminal{Op2}}  \\
 \begin{tabular}{lll}
 {\nonterminal{Op}} & {\arrow}  &{\nonterminal{Op1}}  \\
  & {\delimit}  &{\nonterminal{Op2}}  \\
+ & {\delimit}  &{\nonterminal{Op0}}  \\
 \end{tabular}\\
 
 \begin{tabular}{lll}
 \end{tabular}\\
 
 \begin{tabular}{lll}
index f75fff6d2eebaa3458074b030d5c7b70b0de7f1d..dbd776705fe7fbb566e77bf45bda1e20787638ef 100644 (file)
@@ -71,7 +71,7 @@ Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 }
 
 Exp :: { Exp }
 Exp : Ident '=' Exp { EAss $1 $3 } 
 
 Exp :: { Exp }
 Exp : Ident '=' Exp { EAss $1 $3 } 
-  | Exp1 BOp Exp1 { BExp $1 $2 $3 }
+  | Exp1 Op0 Exp1 { compExp_ $1 $2 $3 }
   | Exp1 { $1 }
 
 
   | Exp1 { $1 }
 
 
@@ -106,8 +106,8 @@ Stms :: { Stms }
 Stms : ListStm { Program (reverse $1) } 
 
 
 Stms : ListStm { Program (reverse $1) } 
 
 
-BOp :: { BOp }
-BOp : '<' { Lt } 
+Op0 :: { Op }
+Op0 : '<' { Lt } 
   | '<=' { ELt }
   | '>' { Gt }
   | '>=' { EGt }
   | '<=' { ELt }
   | '>' { Gt }
   | '>=' { EGt }
@@ -128,6 +128,7 @@ Op2 : '*' { Times }
 Op :: { Op }
 Op : Op1 { $1 } 
   | Op2 { $1 }
 Op :: { Op }
 Op : Op1 { $1 } 
   | Op2 { $1 }
+  | Op0 { $1 }
 
 
 Type :: { Type }
 
 
 Type :: { Type }
@@ -151,8 +152,9 @@ happyError ts =
 myLexer = tokens
 decl_ t_ v_ = SDecl t_ v_ EDefault
 if_ e_ s_ = SIf e_ s_ SNoop
 myLexer = tokens
 decl_ t_ v_ = SDecl t_ v_ EDefault
 if_ e_ s_ = SIf e_ s_ SNoop
-op1_ e1_ o_ e2_ = OpExp e1_ o_ e2_
-op2_ e1_ o_ e2_ = OpExp e1_ o_ e2_
+compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
+op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
+op2_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
 postIncr_ i_ = EPost i_ Plus
 postDecr_ i_ = EPost i_ Minus
 }
 postIncr_ i_ = EPost i_ Plus
 postDecr_ i_ = EPost i_ Minus
 }
index 0e549511ce4275a12b895072d1a216b7f8bf45e8..feb0b2332dce7a464f6db733404e1df969d77f70 100644 (file)
@@ -104,7 +104,6 @@ instance Print Stm where
 instance Print Exp where
   prt i e = case e of
    EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
 instance Print Exp where
   prt i e = case e of
    EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
-   BExp exp0 bop exp -> prPrec i 0 (concatD [prt 1 exp0 , prt 0 bop , prt 1 exp])
    EVar id -> prPrec i 3 (concatD [prt 0 id])
    EInt n -> prPrec i 3 (concatD [prt 0 n])
    ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
    EVar id -> prPrec i 3 (concatD [prt 0 id])
    EInt n -> prPrec i 3 (concatD [prt 0 n])
    ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
@@ -113,7 +112,7 @@ instance Print Exp where
    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 [])
    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 [])
-   OpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
+   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])
 
 
    EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
 
 
@@ -122,7 +121,7 @@ instance Print Stms where
    Program stms -> prPrec i 0 (concatD [prt 0 stms])
 
 
    Program stms -> prPrec i 0 (concatD [prt 0 stms])
 
 
-instance Print BOp where
+instance Print Op where
   prt i e = case e of
    Lt  -> prPrec i 0 (concatD [doc (showString "<")])
    ELt  -> prPrec i 0 (concatD [doc (showString "<=")])
   prt i e = case e of
    Lt  -> prPrec i 0 (concatD [doc (showString "<")])
    ELt  -> prPrec i 0 (concatD [doc (showString "<=")])
@@ -130,10 +129,6 @@ instance Print BOp where
    EGt  -> prPrec i 0 (concatD [doc (showString ">=")])
    Eq  -> prPrec i 0 (concatD [doc (showString "==")])
    NEq  -> prPrec i 0 (concatD [doc (showString "!=")])
    EGt  -> prPrec i 0 (concatD [doc (showString ">=")])
    Eq  -> prPrec i 0 (concatD [doc (showString "==")])
    NEq  -> prPrec i 0 (concatD [doc (showString "!=")])
-
-
-instance Print Op where
-  prt i e = case e of
    Plus  -> prPrec i 1 (concatD [doc (showString "+")])
    Minus  -> prPrec i 1 (concatD [doc (showString "-")])
    Times  -> prPrec i 2 (concatD [doc (showString "*")])
    Plus  -> prPrec i 1 (concatD [doc (showString "+")])
    Minus  -> prPrec i 1 (concatD [doc (showString "-")])
    Times  -> prPrec i 2 (concatD [doc (showString "*")])
index 8a3605383704b6bf4c3a36e6e4936be53876cfec..5f49a5b5a4d392571d9009e04c94d63961e34ff8 100644 (file)
@@ -34,7 +34,6 @@ transStm x = case x of
 transExp :: Exp -> Result
 transExp x = case x of
   EAss id exp  -> failure x
 transExp :: Exp -> Result
 transExp x = case x of
   EAss id exp  -> failure x
-  BExp exp0 bop exp  -> failure x
   EVar id  -> failure x
   EInt n  -> failure x
   ENeg exp  -> failure x
   EVar id  -> failure x
   EInt n  -> failure x
   ENeg exp  -> failure x
@@ -43,7 +42,7 @@ transExp x = case x of
   EReadB  -> failure x
   ExpT type' exp  -> failure x
   EDefault  -> failure x
   EReadB  -> failure x
   ExpT type' exp  -> failure x
   EDefault  -> failure x
-  OpExp exp0 op exp  -> failure x
+  BiOpExp exp0 op exp  -> failure x
   EPost id op  -> failure x
 
 
   EPost id op  -> failure x
 
 
@@ -52,18 +51,14 @@ transStms x = case x of
   Program stms  -> failure x
 
 
   Program stms  -> failure x
 
 
-transBOp :: BOp -> Result
-transBOp x = case x of
+transOp :: Op -> Result
+transOp x = case x of
   Lt  -> failure x
   ELt  -> failure x
   Gt  -> failure x
   EGt  -> failure x
   Eq  -> failure x
   NEq  -> failure x
   Lt  -> failure x
   ELt  -> failure x
   Gt  -> failure x
   EGt  -> failure x
   Eq  -> failure x
   NEq  -> failure x
-
-
-transOp :: Op -> Result
-transOp x = case x of
   Plus  -> failure x
   Minus  -> failure x
   Times  -> failure x
   Plus  -> failure x
   Minus  -> failure x
   Times  -> failure x
index 922264061337362de5dab48dcbdb8163b9bee8d4..6e845ba2cb56b9625570676c65e6354da53eb4ba 100644 (file)
@@ -1,4 +1,4 @@
-module Typecheck where 
+module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where 
 
 import Abssyntax
 import Control.Monad.State
 
 import Abssyntax
 import Control.Monad.State
@@ -8,15 +8,21 @@ import Prelude hiding (lookup)
 
 type Types = Map Ident Type
 
 
 type Types = Map Ident Type
 
+inList :: Eq a => a -> [a] -> Bool
+inList _ [] = False
+inList a (x:xs) = if a == x then True else inList a xs
+
 typeCheckExp :: (MonadState Types m) => Exp -> m Type
 typeCheckExp :: (MonadState Types m) => Exp -> m Type
-typeCheckExp (BExp e o e') = do
-       TInt <- typeCheckExp e
-       TInt <- typeCheckExp e'
-       return TBool
-typeCheckExp (OpExp e o e') = do
-       TInt <- typeCheckExp e
-       TInt <- typeCheckExp e'
-       return TInt
+typeCheckExp (BiOpExp e o e') = do
+       t1 <- typeCheckExp e
+       t2 <- typeCheckExp e'
+       if not(t1 == t2) then fail "" 
+               else case inList o [Eq,NEq] of
+                       True -> return TBool
+                       False -> if not(t1 == TInt) then fail "" 
+                               else case inList o [Plus,Minus,Times,Div] of
+                                       True -> return TInt
+                                       False -> return TBool
 typeCheckExp (EVar i) = typeCheckVar i
 typeCheckExp (EAss i e) = do
        a <- typeCheckVar i
 typeCheckExp (EVar i) = typeCheckVar i
 typeCheckExp (EAss i e) = do
        a <- typeCheckVar i
index 8bb36219c11d1b7daeeeeaf9065a534d37f8345b..313fa556810e0dff5de9602959cdb74cd81073b5 100644 (file)
--- a/syntax.cf
+++ b/syntax.cf
@@ -17,11 +17,12 @@ SWhile.   Stm      ::= "while" "(" Exp ")" Stm ;
 SPrint.   Stm      ::= "print" Exp ";" ;
 
 EAss.     Exp     ::= Ident "=" Exp;
 SPrint.   Stm      ::= "print" Exp ";" ;
 
 EAss.     Exp     ::= Ident "=" Exp;
-BExp.     Exp     ::= Exp1 BOp Exp1 ;
+compExp.  Exp     ::= Exp1 Op0 Exp1 ;
+define compExp e1 o e2 = BiOpExp e1 o e2 ;
 op1.      Exp1    ::= Exp1 Op1 Exp2 ;
 op1.      Exp1    ::= Exp1 Op1 Exp2 ;
-define op1 e1 o e2 = OpExp e1 o e2 ;
+define op1 e1 o e2 = BiOpExp e1 o e2 ;
 op2.      Exp2    ::= Exp2 Op2 Exp3 ;
 op2.      Exp2    ::= Exp2 Op2 Exp3 ;
-define op2 e1 o e2 = OpExp e1 o e2 ;
+define op2 e1 o e2 = BiOpExp e1 o e2 ;
 postIncr. Exp3    ::= Ident "++" ;
 define postIncr i = EPost i Plus ;
 postDecr.    Exp3    ::= Ident "--" ;
 postIncr. Exp3    ::= Ident "++" ;
 define postIncr i = EPost i Plus ;
 postDecr.    Exp3    ::= Ident "--" ;
@@ -29,6 +30,7 @@ define postDecr i = EPost i Minus ;
 EVar.     Exp3    ::= Ident ;
 EInt.     Exp3    ::= Integer ;
 ENeg.     Exp3    ::= "-" Exp3 ;
 EVar.     Exp3    ::= Ident ;
 EInt.     Exp3    ::= Integer ;
 ENeg.     Exp3    ::= "-" Exp3 ;
+-- ENot.     Exp3    ::= "!" Exp3 ;
 EBool.    Exp3    ::= Bool ;
 EReadI.   Exp3    ::= "readInt" ;
 EReadB.   Exp3    ::= "readBool" ;
 EBool.    Exp3    ::= Bool ;
 EReadI.   Exp3    ::= "readInt" ;
 EReadB.   Exp3    ::= "readBool" ;
@@ -43,12 +45,12 @@ terminator Stm "" ;
 
 Program.  Stms ::= [Stm] ;
 
 
 Program.  Stms ::= [Stm] ;
 
-Lt.  BOp ::= "<" ;
-ELt. BOp ::= "<=" ;
-Gt.  BOp ::= ">" ;
-EGt. BOp ::= ">=" ;
-Eq.  BOp ::= "==" ;
-NEq. BOp ::= "!=" ;
+Lt.  Op0 ::= "<" ;
+ELt. Op0 ::= "<=" ;
+Gt.  Op0 ::= ">" ;
+EGt. Op0 ::= ">=" ;
+Eq.  Op0 ::= "==" ;
+NEq. Op0 ::= "!=" ;
 
 Plus.  Op1 ::= "+" ;
 Minus. Op1 ::= "-" ;
 
 Plus.  Op1 ::= "+" ;
 Minus. Op1 ::= "-" ;
@@ -57,6 +59,7 @@ Div.   Op2 ::= "/" ;
 
 _. Op ::= Op1;
 _. Op ::= Op2;
 
 _. Op ::= Op1;
 _. Op ::= Op2;
+_. Op ::= Op0;
 
 
 
 
 
 
@@ -68,7 +71,7 @@ TBool.  Type  ::= "bool" ;
 internal ExpT. Exp ::= Type Exp ;
 internal SNoop. Stm ::= ;
 internal EDefault. Exp ::= ;
 internal ExpT. Exp ::= Type Exp ;
 internal SNoop. Stm ::= ;
 internal EDefault. Exp ::= ;
-internal OpExp. Exp ::= Exp Op Exp ;
+internal BiOpExp. Exp ::= Exp Op Exp ;
 internal NoType. Type ::= ;
 internal EPost. Exp ::= Ident Op1 ;
 
 internal NoType. Type ::= ;
 internal EPost. Exp ::= Ident Op1 ;