from Text import class Text, instance Text String
import qualified Text
-lexer :: [Char] -> LexerOutput [Token]
+lexer :: [Char] -> LexerOutput
lexer _ = undef
-// Misschien moeten we hier continuation style van maken
-instance toString (LexerOutput [Token]) where
+instance toString LexerOutput where
toString (Left l) = "Error: " +++ l
toString (Right x) = 'Text'.concat (print 0 x)
-
-print :: Int [Token] -> [String]
-print i [(IdentToken l):rest] = [tab i:toString l:print i rest]
-print i [(NumberToken j):rest] = [tab i:toString j:print i rest]
-print i [(CharToken c):rest] = [tab i:"'":toString c:"'":print i rest]
-print i [VarToken:rest] = [tab i:"var":print i rest]
-print i [VoidToken:rest] = [tab i:"Void":print i rest]
-print i [ReturnToken:rest] = [tab i:"return":print i rest]
-print i [IfToken:rest] = [tab i:"if":print i rest]
-print i [ElseToken:rest] = [tab i:"else":print i rest]
-print i [WhileToken:rest] = [tab i:"while":print i rest]
-print i [TrueToken:rest] = [tab i:"True":print i rest]
-print i [FalseToken:rest] = [tab i:"False":print i rest]
-print i [BraceOpenToken:rest] = [tab i:"(":print i rest]
-print i [BraceCloseToken:rest] = [tab i:")":print i rest]
-print i [CBraceOpenToken:rest] = [tab i:"{\n":print (i+1) rest]
-print i [CBraceCloseToken:rest] = [tab i:"}\n":print (i-1) rest]
-print i [SquareOpenToken:rest] = [tab i:"[":print i rest]
-print i [SquareCloseToken:rest] = [tab i:"]":print i rest]
-print i [CommaToken:rest] = [tab i:", ":print i rest]
-print i [ColonToken:rest] = [tab i:":":print i rest]
-print i [SColonToken:rest] = [tab i:";\n":print i rest]
-print i [DotToken:rest] = [tab i:".":print i rest]
-print i [PlusToken:rest] = [tab i:" + ":print i rest]
-print i [DashToken:rest] = [tab i:" - ":print i rest]
-print i [StarToken:rest] = [tab i:" * ":print i rest]
-print i [SlashToken:rest] = [tab i:" / ":print i rest]
-print i [PercentToken:rest] = [tab i:" % ":print i rest]
-print i [AssignmentToken:rest] = [tab i:" = ":print i rest]
-print i [LesserToken:rest] = [tab i:" < ":print i rest]
-print i [BiggerToken:rest] = [tab i:" > ":print i rest]
-print i [ExclamationToken:rest] = [tab i:"!":print i rest]
-print i [DoubleColonToken:rest] = [tab i:" :: ":print i rest]
-print i [LesserEqToken:rest] = [tab i:" <= ":print i rest]
-print i [GreaterEqToken:rest] = [tab i:" >= ":print i rest]
-print i [EqualsToken:rest] = [tab i:" == ":print i rest]
-print i [AmpersandsToken:rest] = [tab i:" && ":print i rest]
-print i [PipesToken:rest] = [tab i:" || ":print i rest]
-print i [ArrowToken:rest] = [tab i:" -> ":print i rest]
+ where
+ print :: Int [Token] -> [String]
+ print _ [] = []
+ print i [(IdentToken l):rest] = [toString l:print i rest]
+ print i [(NumberToken j):rest] = [toString j:print i rest]
+ print i [(CharToken c):rest] = ["'":toString c:"'":print i rest]
+ print i [VarToken:rest] = ["var":print i rest]
+ print i [VoidToken:rest] = ["Void":print i rest]
+ print i [ReturnToken:rest] = ["return":print i rest]
+ print i [IfToken:rest] = ["if":print i rest]
+ print i [ElseToken:rest] = ["else":print i rest]
+ print i [WhileToken:rest] = ["while":print i rest]
+ print i [TrueToken:rest] = ["True":print i rest]
+ print i [FalseToken:rest] = ["False":print i rest]
+ print i [BraceOpenToken:rest] = ["(":print i rest]
+ print i [BraceCloseToken:rest] = [")":print i rest]
+ print i [CBraceOpenToken:rest] = ["{\n":tab (i+1):print (i+1) rest]
+ print i [CBraceCloseToken:rest] = ["}\n":tab (i-1):print (i-1) rest]
+ print i [SquareOpenToken:rest] = ["[":print i rest]
+ print i [SquareCloseToken:rest] = ["]":print i rest]
+ print i [CommaToken:rest] = [", ":print i rest]
+ print i [ColonToken:rest] = [":":print i rest]
+ print i [SColonToken:rest] = [";\n":tab i:print i rest]
+ print i [DotToken:rest] = [".":print i rest]
+ print i [PlusToken:rest] = [" + ":print i rest]
+ print i [DashToken:rest] = [" - ":print i rest]
+ print i [StarToken:rest] = [" * ":print i rest]
+ print i [SlashToken:rest] = [" / ":print i rest]
+ print i [PercentToken:rest] = [" % ":print i rest]
+ print i [AssignmentToken:rest] = [" = ":print i rest]
+ print i [LesserToken:rest] = [" < ":print i rest]
+ print i [BiggerToken:rest] = [" > ":print i rest]
+ print i [ExclamationToken:rest] = ["!":print i rest]
+ print i [DoubleColonToken:rest] = [" :: ":print i rest]
+ print i [LesserEqToken:rest] = [" <= ":print i rest]
+ print i [GreaterEqToken:rest] = [" >= ":print i rest]
+ print i [EqualsToken:rest] = [" == ":print i rest]
+ print i [AmpersandsToken:rest] = [" && ":print i rest]
+ print i [PipesToken:rest] = [" || ":print i rest]
+ print i [ArrowToken:rest] = [" -> ":print i rest]
tab :: Int -> String
tab 0 = ""
tab i = "\t" +++ tab (i-1)
-lex :: [Char] -> LexerOutput [Token]
+lex :: [Char] -> LexerOutput
lex ['v':'a':'r':rest] = undef
lex ['V':'o':'i':'d':rest] = undef
lex ['r':'e':'t':'u':'r':'n':rest] = undef
lex ['<':rest] = undef
lex ['>':rest] = undef
lex ['!':rest] = undef
-lex ['\'':'\\':x'\'':rest] = case x of
+lex ['\'':'\\':x:'\'':rest] = case x of
'a' = undef // (CharToken '\a')
'b' = undef // (CharToken '\b')
'f' = undef // (CharToken '\f')
'r' = undef // (CharToken '\r')
't' = undef // (CharToken '\t')
'v' = undef // (CharToken '\v')
- _ = Left ("Illegal escape: \\" +++ x)
+ _ = Left ("Illegal escape: \\" +++ toString x)
lex ['\'':x:'\'':rest] = undef
lex [x:xs]
| isSpace x = lex xs
| isAlpha x = undef //Parse ident
| otherwise = Left ("Unexpected character: " +++ toString x)
-Start = "Hi"
+:: LexerOutput :== Either String [Token]
+
+Start = toString sp
+ where
+ sp :: LexerOutput
+ sp = Right [
+ WhileToken,
+ BraceOpenToken,
+ TrueToken,
+ BraceCloseToken,
+ CBraceOpenToken,
+ VarToken,
+ IdentToken ['x'],
+ AssignmentToken,
+ IdentToken ['x'],
+ PlusToken,
+ NumberToken 5,
+ SColonToken,
+ CBraceCloseToken]
//:: LexerOutput a :== Either String a
//