From 93b042040034d199c925248a2dd1c321773ab6d0 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 26 Feb 2006 21:26:14 +0000 Subject: [PATCH] moved modified Testsyntax to Typechecker to keep the original one --- Docsyntax.tex | 10 +++----- Makefile | 3 ++- Parsyntax.y | 10 +++----- Printsyntax.hs | 2 +- Skelsyntax.hs | 2 +- Testsyntax.hs | 26 ++++++++------------ Typechecker.hs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 Typechecker.hs diff --git a/Docsyntax.tex b/Docsyntax.tex index a4040dc..76d4a27 100644 --- a/Docsyntax.tex +++ b/Docsyntax.tex @@ -83,8 +83,8 @@ All other symbols are terminals.\\ \end{tabular}\\ \begin{tabular}{lll} -{\nonterminal{Exp}} & {\arrow} &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}} \\ - & {\delimit} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\ +{\nonterminal{Exp}} & {\arrow} &{\nonterminal{Ident}} {\terminal{{$=$}}} {\nonterminal{Exp}} \\ + & {\delimit} &{\nonterminal{Exp1}} {\nonterminal{BOp}} {\nonterminal{Exp1}} \\ & {\delimit} &{\nonterminal{Exp1}} \\ \end{tabular}\\ @@ -107,11 +107,7 @@ All other symbols are terminals.\\ & {\delimit} &{\nonterminal{Bool}} \\ & {\delimit} &{\terminal{readInt}} \\ & {\delimit} &{\terminal{readBool}} \\ - & {\delimit} &{\nonterminal{Exp4}} \\ -\end{tabular}\\ - -\begin{tabular}{lll} -{\nonterminal{Exp4}} & {\arrow} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\ + & {\delimit} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\ \end{tabular}\\ \begin{tabular}{lll} diff --git a/Makefile b/Makefile index 883c65f..fe345b4 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ all: happy -gca -idebug Parsyntax.y alex -g Lexsyntax.x latex Docsyntax.tex; dvips Docsyntax.dvi -o Docsyntax.ps - ghc -fglasgow-exts --make Testsyntax.hs -o Testsyntax + ghc --make Testsyntax.hs -o Testsyntax + ghc -fglasgow-exts --make Typechecker.hs -o Typechecker clean: -rm -f *.log *.aux *.hi *.o *.dvi -rm -f Docsyntax.ps diff --git a/Parsyntax.y b/Parsyntax.y index 8bf7c68..f75fff6 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -70,8 +70,8 @@ Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 } Exp :: { Exp } -Exp : Exp1 BOp Exp1 { BExp $1 $2 $3 } - | Ident '=' Exp { EAss $1 $3 } +Exp : Ident '=' Exp { EAss $1 $3 } + | Exp1 BOp Exp1 { BExp $1 $2 $3 } | Exp1 { $1 } @@ -94,11 +94,7 @@ Exp3 : Ident '++' { postIncr_ $1 } | Bool { EBool $1 } | 'readInt' { EReadI } | 'readBool' { EReadB } - | Exp4 { $1 } - - -Exp4 :: { Exp } -Exp4 : '(' Exp ')' { $2 } + | '(' Exp ')' { $2 } ListStm :: { [Stm] } diff --git a/Printsyntax.hs b/Printsyntax.hs index 1d10687..0e54951 100644 --- a/Printsyntax.hs +++ b/Printsyntax.hs @@ -103,9 +103,9 @@ 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]) - EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp]) EInt n -> prPrec i 3 (concatD [prt 0 n]) ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp]) EBool bool -> prPrec i 3 (concatD [prt 0 bool]) diff --git a/Skelsyntax.hs b/Skelsyntax.hs index c1af307..8a36053 100644 --- a/Skelsyntax.hs +++ b/Skelsyntax.hs @@ -33,9 +33,9 @@ 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 - EAss id exp -> failure x EInt n -> failure x ENeg exp -> failure x EBool bool -> failure x diff --git a/Testsyntax.hs b/Testsyntax.hs index fb6986c..45f35b9 100644 --- a/Testsyntax.hs +++ b/Testsyntax.hs @@ -11,10 +11,6 @@ import Skelsyntax import Printsyntax import Abssyntax -import Typecheck -import Control.Monad.State -import Data.Map as Map hiding (showTree) - @@ -29,21 +25,19 @@ type Verbosity = Int putStrV :: Verbosity -> String -> IO () putStrV v s = if v > 1 then putStrLn s else return () -runFile :: Verbosity -> ParseFun Stms -> FilePath -> IO () +runFile :: (Print a, Show a) => Verbosity -> ParseFun a -> FilePath -> IO () runFile v p f = putStrLn f >> readFile f >>= run v p -run :: Verbosity -> ParseFun Stms -> String -> IO () +run :: (Print a, Show a) => Verbosity -> ParseFun a -> String -> IO () run v p s = let ts = myLLexer s in case p ts of - Bad s -> do - putStrLn "\nParse Failed...\n" - putStrV v "Tokens:" - putStrV v $ show ts - putStrLn s - Ok (Program s) -> do - putStrLn "\nParse Successful!" - showTree v (Program s) - runStateT (mapM typeCheckStm s) empty - return () + Bad s -> do putStrLn "\nParse Failed...\n" + putStrV v "Tokens:" + putStrV v $ show ts + putStrLn s + Ok tree -> do putStrLn "\nParse Successful!" + showTree v tree + + showTree :: (Show a, Print a) => Int -> a -> IO () showTree v tree diff --git a/Typechecker.hs b/Typechecker.hs new file mode 100644 index 0000000..fb6986c --- /dev/null +++ b/Typechecker.hs @@ -0,0 +1,64 @@ +-- automatically generated by BNF Converter +module Main where + + +import IO ( stdin, hGetContents ) +import System ( getArgs, getProgName ) + +import Lexsyntax +import Parsyntax +import Skelsyntax +import Printsyntax +import Abssyntax + +import Typecheck +import Control.Monad.State +import Data.Map as Map hiding (showTree) + + + + +import ErrM + +type ParseFun a = [Token] -> Err a + +myLLexer = myLexer + +type Verbosity = Int + +putStrV :: Verbosity -> String -> IO () +putStrV v s = if v > 1 then putStrLn s else return () + +runFile :: Verbosity -> ParseFun Stms -> FilePath -> IO () +runFile v p f = putStrLn f >> readFile f >>= run v p + +run :: Verbosity -> ParseFun Stms -> String -> IO () +run v p s = let ts = myLLexer s in case p ts of + Bad s -> do + putStrLn "\nParse Failed...\n" + putStrV v "Tokens:" + putStrV v $ show ts + putStrLn s + Ok (Program s) -> do + putStrLn "\nParse Successful!" + showTree v (Program s) + runStateT (mapM typeCheckStm s) empty + return () + +showTree :: (Show a, Print a) => Int -> a -> IO () +showTree v tree + = do + putStrV v $ "\n[Abstract Syntax]\n\n" ++ show tree + putStrV v $ "\n[Linearized tree]\n\n" ++ printTree tree + +main :: IO () +main = do args <- getArgs + case args of + [] -> hGetContents stdin >>= run 2 pStms + "-s":fs -> mapM_ (runFile 0 pStms) fs + fs -> mapM_ (runFile 2 pStms) fs + + + + + -- 2.39.2