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] ++
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] ++ ["]"]
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
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
program :: String,
lex :: Bool,
parse :: Bool,
+ sem :: Bool,
fp :: Maybe String,
help :: Bool}
# (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
program=p,
version=False,
lex=False,
- parse=True,
+ parse=False,
+ sem=True,
fp=Nothing,
help=False}, w)
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)