lexer :: [Char] -> LexerOutput [Token]
lexer _ = Left "Not Implemented"
+//lexer functions
+identT = alpha >>= \a -> many (char '_' <|> alphaNum) >>= \as -> return $ IdentToken [a:as]
+numberT = optional (char '-') >>= \sign -> (some digit) >>= \n -> case sign of
+ Nothing -> return $ NumberToken $ 5 //fromString n
+ _ -> return $ NumberToken $ -5 //(fromString n) * -1
+charLT = liftM CharToken item
+char2T = item >>= \c1 -> case c1 of
+ ':' = char ':' >>| return DoubleColonToken
+ '<' = char '=' >>| return LesserEqToken
+ '>' = char '=' >>| return GreaterEqToken
+ '=' = char '=' >>| return EqualsToken
+ '&' = char '&' >>| return AmpersandsToken
+ '|' = char '|' >>| return PipesToken
+ '-' = char '>' >>| return ArrowToken
+char1T = item >>= \c1 -> findT c1 charTokenMap
+varT = string (fromString "var") >>| return VarToken
+voidT = string (fromString "Void") >>| return VoidToken
+returnT = string (fromString "return") >>| return ReturnToken
+ifT = string (fromString "if") >>| return IfToken
+elseT = string (fromString "else") >>| return ElseToken
+whileT = string (fromString "while") >>| return WhileToken
+trueT = string (fromString "True") >>| return TrueToken
+falseT = string (fromString "False") >>| return FalseToken
+
Start :: *World -> *World
Start w
# (args, w) = getCommandLine w // We lezen nu nog standaard van stdin
+charTokenMap = [('(', BraceOpenToken)
+ ,(')', BraceCloseToken)
+ ,('{', CBraceOpenToken)
+ ,('}', CBraceCloseToken)
+ ,('[', SquareOpenToken)
+ ,(']', SquareCloseToken)
+ ,(',', CommaToken)
+ ,(':', ColonToken)
+ ,(';', SColonToken)
+ ,('.', DotToken)
+ ,('+', PlusToken)
+ ,('-', DashToken)
+ ,('*', StarToken)
+ ,('/', SlashToken)
+ ,('%', PercentToken)
+ ,('=', AssignmentToken)
+ ,('<', LesserToken)
+ ,('>', BiggerToken)
+ ,('!', ExclamationToken)]
+findT c [] = fail "Unrecognized character"
+findT c [(k,v):xs] = if (c==k) (return v) (findT c xs)
+
+
// Clean adaption of Yard, a parsec like parser combinator
:: Parser a = Parser ([Char] -> (LexerOutput a, [Char]))
//gives us some, many and optional
instance Alternative Parser where
empty = zero
- (<|>) p1 p2 = parserAlternative p1 p2
-parserAlternative p1 p2 = Parser $ \s -> let (out, rest) = runParser p1 s in case out of
- Left e = runParser p2 s
- Right t = (Right t, rest)
+ (<|>) p1 p2 = Parser $ \s -> let (out, rest) = runParser p1 s in case out of
+ Left e = runParser p2 s
+ Right t = (Right t, rest)
//parser that fails with error
fail :: String -> Parser a
//tries a parser, if it fails returns a default value
optionalDef :: a (Parser a) -> Parser a
-optionalDef def p = parserAlternative p (return def)
+optionalDef def p = p <|> return def
//matched given char
char :: Char -> Parser Char
digit :: Parser Char
digit = satisfy isDigit
+alphaNum :: Parser Char
+alphaNum = alpha <|> digit
+
//matches a given String
string :: [Char] -> Parser [Char]
string s = mapM_ char s >>| return s
\ No newline at end of file