From: Mart Lubbers Date: Fri, 12 Feb 2016 10:15:25 +0000 (+0100) Subject: print awesome X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=011c22770609138b54195d330232bc2a1e88d0ae;p=cc1516.git print awesome --- diff --git a/lex.dcl b/lex.dcl index 95bd0bd..56c3765 100644 --- a/lex.dcl +++ b/lex.dcl @@ -45,6 +45,6 @@ import Data.Either | PipesToken // || | ArrowToken // -> -:: LexerOutput a :== Either String a +:: LexerOutput :== Either String [Token] -lexer :: [Char] -> LexerOutput [Token] +lexer :: [Char] -> LexerOutput diff --git a/lex.icl b/lex.icl index 75fab23..aa80f70 100644 --- a/lex.icl +++ b/lex.icl @@ -16,58 +16,58 @@ from Data.Func import $ 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 @@ -102,7 +102,7 @@ lex ['=':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') @@ -110,7 +110,7 @@ lex ['\'':'\\':x'\'':rest] = case x of '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 @@ -118,7 +118,25 @@ lex [x: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 //