From 6103c0359b95557566aa9154efa5bcd4d9c996f6 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Mon, 27 Feb 2006 10:25:29 +0000 Subject: [PATCH] streamlined the binary operators --- Abssyntax.hs | 12 ++++-------- Docsyntax.tex | 5 +++-- Parsyntax.y | 12 +++++++----- Printsyntax.hs | 9 ++------- Skelsyntax.hs | 11 +++-------- Typecheck.hs | 24 +++++++++++++++--------- syntax.cf | 23 +++++++++++++---------- 7 files changed, 47 insertions(+), 49 deletions(-) diff --git a/Abssyntax.hs b/Abssyntax.hs index 7c95d53..4cc8d19 100644 --- a/Abssyntax.hs +++ b/Abssyntax.hs @@ -15,9 +15,8 @@ data Stm = deriving (Eq,Ord,Show) data Exp = - BExp Exp BOp Exp + EAss Ident Exp | EVar Ident - | EAss Ident Exp | EInt Integer | ENeg Exp | EBool Bool @@ -25,7 +24,7 @@ data Exp = | EReadB | ExpT Type Exp | EDefault - | OpExp Exp Op Exp + | BiOpExp Exp Op Exp | EPost Ident Op deriving (Eq,Ord,Show) @@ -33,17 +32,14 @@ data Stms = Program [Stm] deriving (Eq,Ord,Show) -data BOp = +data Op = Lt | ELt | Gt | EGt | Eq | NEq - deriving (Eq,Ord,Show) - -data Op = - Plus + | Plus | Minus | Times | Div diff --git a/Docsyntax.tex b/Docsyntax.tex index 76d4a27..89c1a0f 100644 --- a/Docsyntax.tex +++ b/Docsyntax.tex @@ -84,7 +84,7 @@ All other symbols are terminals.\\ \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}\\ @@ -120,7 +120,7 @@ All other symbols are terminals.\\ \end{tabular}\\ \begin{tabular}{lll} -{\nonterminal{BOp}} & {\arrow} &{\terminal{{$<$}}} \\ +{\nonterminal{Op0}} & {\arrow} &{\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}} \\ + & {\delimit} &{\nonterminal{Op0}} \\ \end{tabular}\\ \begin{tabular}{lll} diff --git a/Parsyntax.y b/Parsyntax.y index f75fff6..dbd7767 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -71,7 +71,7 @@ Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 } Exp :: { Exp } Exp : Ident '=' Exp { EAss $1 $3 } - | Exp1 BOp Exp1 { BExp $1 $2 $3 } + | Exp1 Op0 Exp1 { compExp_ $1 $2 $3 } | Exp1 { $1 } @@ -106,8 +106,8 @@ Stms :: { Stms } Stms : ListStm { Program (reverse $1) } -BOp :: { BOp } -BOp : '<' { Lt } +Op0 :: { Op } +Op0 : '<' { Lt } | '<=' { ELt } | '>' { Gt } | '>=' { EGt } @@ -128,6 +128,7 @@ Op2 : '*' { Times } Op :: { Op } Op : Op1 { $1 } | Op2 { $1 } + | Op0 { $1 } Type :: { Type } @@ -151,8 +152,9 @@ happyError ts = 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 } diff --git a/Printsyntax.hs b/Printsyntax.hs index 0e54951..feb0b23 100644 --- a/Printsyntax.hs +++ b/Printsyntax.hs @@ -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]) - 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]) @@ -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 []) - 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]) @@ -122,7 +121,7 @@ instance Print Stms where 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 "<=")]) @@ -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 "!=")]) - - -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 "*")]) diff --git a/Skelsyntax.hs b/Skelsyntax.hs index 8a36053..5f49a5b 100644 --- a/Skelsyntax.hs +++ b/Skelsyntax.hs @@ -34,7 +34,6 @@ transStm x = case x of 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 @@ -43,7 +42,7 @@ transExp x = case x of 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 @@ -52,18 +51,14 @@ transStms x = case x of 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 - - -transOp :: Op -> Result -transOp x = case x of Plus -> failure x Minus -> failure x Times -> failure x diff --git a/Typecheck.hs b/Typecheck.hs index 9222640..6e845ba 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -1,4 +1,4 @@ -module Typecheck where +module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where import Abssyntax import Control.Monad.State @@ -8,15 +8,21 @@ import Prelude hiding (lookup) 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 (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 diff --git a/syntax.cf b/syntax.cf index 8bb3621..313fa55 100644 --- a/syntax.cf +++ b/syntax.cf @@ -17,11 +17,12 @@ SWhile. Stm ::= "while" "(" Exp ")" Stm ; 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 ; -define op1 e1 o e2 = OpExp e1 o e2 ; +define op1 e1 o e2 = BiOpExp e1 o e2 ; 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 "--" ; @@ -29,6 +30,7 @@ define postDecr i = EPost i Minus ; EVar. Exp3 ::= Ident ; EInt. Exp3 ::= Integer ; ENeg. Exp3 ::= "-" Exp3 ; +-- ENot. Exp3 ::= "!" Exp3 ; EBool. Exp3 ::= Bool ; EReadI. Exp3 ::= "readInt" ; EReadB. Exp3 ::= "readBool" ; @@ -43,12 +45,12 @@ terminator 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 ::= "-" ; @@ -57,6 +59,7 @@ Div. 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 OpExp. Exp ::= Exp Op Exp ; +internal BiOpExp. Exp ::= Exp Op Exp ; internal NoType. Type ::= ; internal EPost. Exp ::= Ident Op1 ; -- 2.39.2