From: Mart Lubbers Date: Thu, 7 Apr 2016 12:45:53 +0000 (+0200) Subject: BAM X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=8c4ec1c3287d6c6ddefd22ef608a4d97693e7e85;p=cc1516.git BAM --- diff --git a/AST.dcl b/AST.dcl index 1e241dd..ac1cf4e 100644 --- a/AST.dcl +++ b/AST.dcl @@ -41,3 +41,5 @@ from StdOverloaded import class toString | ReturnStmt (Maybe Expr) instance toString AST +instance toString Type +instance toString Pos diff --git a/AST.icl b/AST.icl index 21f27d5..b9b9417 100644 --- a/AST.icl +++ b/AST.icl @@ -7,6 +7,9 @@ from Data.Func import $ from Text import class Text(concat), instance Text String from Data.Maybe import :: Maybe, maybe +instance toString Pos where + toString {line,col} = concat [toString line, ":", toString col, " "] + instance toString AST where toString (AST v f) = concat ( ["\n":printersperse "\n" v] ++ @@ -50,6 +53,9 @@ printStatements [s:ss] i = (case s of instance print VarDecl where print (VarDecl _ t i e) = print t ++ [" ":i:"=":print e] ++ [";"] +instance toString Type where + toString t = concat $ print t + instance print Type where print (TupleType (t1, t2)) = ["(":print t1] ++ [",":print t2] ++ [")"] print (ListType t) = ["[":print t] ++ ["]"] diff --git a/parse.dcl b/parse.dcl index 111540c..ad7edea 100644 --- a/parse.dcl +++ b/parse.dcl @@ -9,4 +9,4 @@ from AST import :: AST :: ParserOutput :== Either Error AST -parser :: LexerOutput -> ParserOutput +parser :: [Token] -> ParserOutput diff --git a/parse.icl b/parse.icl index bdb6a77..a507ab3 100644 --- a/parse.icl +++ b/parse.icl @@ -18,9 +18,8 @@ import yard import lex import AST -parser :: LexerOutput -> ParserOutput -parser (Left e) = Left e -parser (Right r) = case runParser parseProgram r of +parser :: [Token] -> ParserOutput +parser ts = case runParser parseProgram ts of (Right ast, [(p, t):xs]) = Left $ PositionalError p.line p.col ( "Unable to parse from: " +++ printToString t) x = fst x diff --git a/sem.dcl b/sem.dcl index be207dd..5c01ba0 100644 --- a/sem.dcl +++ b/sem.dcl @@ -1,10 +1,15 @@ definition module sem from Data.Either import :: Either -from AST import :: AST -from lex import :: Error -from parse import :: ParserOutput +from AST import :: AST, :: Pos, :: Type -:: SemOutput :== Either Error AST +:: SemError + = ParseError Pos String + | UnifyError Pos Type Type + | Error String +:: SemOutput :== Either [SemError] AST -sem :: ParserOutput -> SemOutput + +instance toString SemError + +sem :: AST -> SemOutput diff --git a/sem.icl b/sem.icl index 02d0117..99aa44a 100644 --- a/sem.icl +++ b/sem.icl @@ -2,26 +2,109 @@ implementation module sem import qualified Data.Map as Map from Data.Func import $ +import Data.Maybe import Data.Either +import Data.Functor +import Control.Applicative +import Control.Monad import Control.Monad.State +import Control.Monad.Identity import StdMisc +from StdFunc import id, const +import StdString +import StdList + +from Text import class Text(concat), instance Text String import AST -from parse import :: ParserOutput -from yard import :: Error +from parse import :: ParserOutput, :: Error :: Gamma :== 'Map'.Map String Type -:: Env a :== State Gamma a +:: Env a :== (State Gamma (Either SemError a)) -sem :: ParserOutput -> SemOutput -sem (Left p) = Left p -sem (Right (AST vd fd)) = Right $ AST vd fd -// foldM semVarDecl vd -// >>= \gamma ->foldM typecheck gamma fd +instance toString SemError where + toString (ParseError p e) = concat [ + toString p,"SemError: ParseError: ", e] + toString (Error e) = "SemError: " +++ e + toString (UnifyError p t1 t2) = concat [ + toString p, + "SemError: Cannot unify types. Expected: ", + toString t1, ". Given: ", toString t2] -semVarDecl :: VarDecl -> Env VarDecl -semVarDecl (Vardecl pos type ident expr) = undef - +sem :: AST -> SemOutput +sem (AST vd fd) +# (eithervds, gamma) = runState (mapM semVarDecl vd) 'Map'.newMap +# (eitherfds, gamma) = runState (mapM semFunDecl fd) gamma += case splitEithers eithervds of + (Left errs) = Left $ errs ++ [x\\(Left x)<-eitherfds] + (Right vds) = case splitEithers eitherfds of + (Left errs) = Left errs + (Right fds) = Right $ AST vds fds +splitEithers :: [Either a b] -> Either [a] [b] +splitEithers [] = Right [] +splitEithers [Right x:xs] = splitEithers xs >>= \rest->Right [x:rest] +splitEithers xs = Left $ [x\\(Left x)<-xs] - +semFunDecl :: FunDecl -> Env FunDecl +semFunDecl f = pure $ Right f + +semVarDecl :: VarDecl -> Env VarDecl +semVarDecl v = pure $ Right v +//Right v +//semVarDecl vd=:(VarDecl pos type ident expr) = case unify type expr of +// Left e = Left e +// //TODO ident in de environment +// Right e = Right $ pure vd +// +//typeExpr :: Expr -> Env Type +//typeExpr (IntExpr _ _) = Right $ pure IntType +//typeExpr (CharExpr _ _) = Right $ pure CharType +//typeExpr (BoolExpr _ _) = Right $ pure BoolType +//typeExpr (Op1Expr p UnNegation expr) = undef//typeExpr expr +//// >>= \exprtype->case exprtype of +//// Right BoolType = Right $ pure BoolType +//// t = Left $ UnifyError p BoolType exprtype +//typeExpr (Op1Expr p UnMinus expr) = undef// typeExpr expr +//// >>= \exprtype->case exprtype of +//// IntType = Right $ pure IntType +//// t = Left $ UnifyError p IntType exprtype +//// typeExpr (Op2Expr Pos Expr Op2 Expr) = undef +////typeExpr (FunExpr Pos FunCall +////typeExpr (EmptyListExpr Pos +////typeExpr (TupleExpr Pos (Expr, Expr) +////typeExpr (VarExpr Pos VarDef) = undef +//// +//class unify a :: Type a -> Env a +// +//instance unify Type where +// unify IntType IntType = Right $ pure IntType +// unify BoolType BoolType = Right $ pure BoolType +// unify CharType CharType = Right $ pure CharType +// unify _ _ = undef +// +//instance unify Expr where +// unify type expr = case type of +// _ ->> _ = Left $ ParseError (extrPos expr) +// "Expression cannot be a higher order function. Yet..." +// VoidType = Left $ ParseError (extrPos expr) +// "Expression cannot be a Void type." +// IdType _ = Left $ ParseError (extrPos expr) +// "Expression cannot be an polymorf type." +// TupleType (_, _) = undef +// ListType _ = undef +// IntType = undef +// BoolType = undef +// CharType = undef +// VarType = undef +// +//extrPos :: Expr -> Pos +//extrPos (VarExpr p _) = p +//extrPos (Op2Expr p _ _ _) = p +//extrPos (Op1Expr p _ _) = p +//extrPos (IntExpr p _) = p +//extrPos (CharExpr p _) = p +//extrPos (BoolExpr p _) = p +//extrPos (FunExpr p _) = p +//extrPos (EmptyListExpr p) = p +//extrPos (TupleExpr p _) = p diff --git a/spl.icl b/spl.icl index 93374a5..240de63 100644 --- a/spl.icl +++ b/spl.icl @@ -12,7 +12,7 @@ import Data.Maybe import Data.Func import System.CommandLine import GenPrint -from Text import class Text(concat), instance Text String +from Text import class Text(concat,join), instance Text String import parse import lex @@ -25,6 +25,7 @@ from yard import :: Error, instance toString Error program :: String, lex :: Bool, parse :: Bool, + sem :: Bool, fp :: Maybe String, help :: Bool} @@ -54,23 +55,23 @@ Start w # (contents, stdin, w) = readFileOrStdin stdin args.fp w = case contents of (Left cs) = snd $ fclose (stdin <<< cs) w - (Right cs) - # lexOut = lexer cs - # stdin = if (not args.lex) stdin (case lexOut of - (Right toks) = - stdin <<< "//LEXER\n" <<< printTokens toks <<< "//LEXER\n" - _ = stdin) - # parseOut = parser lexOut - # stdin = if (not args.parse) stdin (case parseOut of - (Right ast) = - stdin <<< "//PARSER\n" <<< toString ast <<< "//PARSER\n" - (Left parse) = stdin <<< toString parse) - # semOut = sem parseOut - # stdin = case semOut of - (Right ast) = - stdin <<< "//TYPE\n" <<< toString ast <<< "//TYPE\n" - (Left parse) = stdin <<< toString parse - = snd $ fclose stdin w + (Right cs) = case lexer cs of + (Left e) = snd $ fclose (stdin <<< toString e) w + (Right lexOut) + # stdin = if (not args.lex) stdin ( + stdin <<< "//LEXER\n" <<< printTokens lexOut <<< "//LEXER\n") + = case parser lexOut of + (Left e) = snd $ fclose (stdin <<< toString e) w + (Right parseOut) + # stdin = if (not args.parse) stdin ( + stdin <<< "//PARSER\n" <<< toString parseOut <<< "//PARSER\n") + = case sem parseOut of + (Left e) = snd $ fclose (stdin <<< "SEMERRORS: " <<< + join "\n" (map toString e)) w + (Right semOut) + # stdin = if (not args.sem) stdin ( + stdin <<< "//SEM\n" <<< toString semOut <<< "//SEM\n") + = snd $ fclose stdin w where printTokens :: [Token] -> String printTokens ts = concat $ flatten $ map pt ts @@ -85,7 +86,8 @@ parseArgs w program=p, version=False, lex=False, - parse=True, + parse=False, + sem=True, fp=Nothing, help=False}, w) where @@ -97,6 +99,8 @@ where pa ["--no-lex":r] o = pa r {o & lex=False} pa ["--parse":r] o = pa r {o & parse=True} pa ["--no-parse":r] o = pa r {o & parse=False} + pa ["--sem":r] o = pa r {o & sem=True} + pa ["--no-sem":r] o = pa r {o & sem=False} pa [x:r] o = pa r {o & fp=Just x} readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World)