From: Mart Lubbers Date: Fri, 12 Feb 2016 17:13:29 +0000 (+0100) Subject: working lexer and printer X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=98144e50ff2ca8e8d89dd1a39d4f2a41c2c6dab6;p=cc1516.git working lexer and printer --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..56d9b95 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +CPM:=cpm + +all: + $(CPM) make + +clean: + $(RM) -rv Clean\ System\ Files sapl lex diff --git a/example.spl b/example.spl new file mode 100644 index 0000000..f7ce562 --- /dev/null +++ b/example.spl @@ -0,0 +1,11 @@ +/* + Three ways to implement the f acto rial function in SPL. + First the recursive version . +*/ +facR(n) :: Int -> Int { + if (n < 2) { + return 1; + } else { + return n * facR(n-1); + } +} diff --git a/lex.dcl b/lex.dcl index 46a3ab8..d6c421c 100644 --- a/lex.dcl +++ b/lex.dcl @@ -9,18 +9,29 @@ import Data.Either | CharToken Char // Character literal //Keyword tokens | VarToken // var keyword - | VoidToken // void keyword | ReturnToken // return keyword | IfToken // if keyword | ElseToken // else keyword | WhileToken // while keyword | TrueToken // True keyword | FalseToken // False keyword + | VoidToken // Void keyword + | IntTypeToken // Int keyword + | CharTypeToken // Char keyword + | BoolTypeToken // Bool keyword + //Two character tokens + | DoubleColonToken // :: + | LesserEqToken // <= + | GreaterEqToken // >= + | EqualsToken // == + | AmpersandsToken // && + | PipesToken // || + | ArrowToken // -> //One character tokens | BraceOpenToken // ( | BraceCloseToken // ) | CBraceOpenToken // { - | CBraceCloseToken // { + | CBraceCloseToken // } | SquareOpenToken // [ | SquareCloseToken // ] | CommaToken // , @@ -36,16 +47,11 @@ import Data.Either | LesserToken // < | BiggerToken // > | ExclamationToken // ! - //Two character tokens - | DoubleColonToken // :: - | LesserEqToken // <= - | GreaterEqToken // >= - | EqualsToken // == - | AmpersandsToken // && - | PipesToken // || - | ArrowToken // -> + //Special tokens | EndOfFileToken // EOF :: LexerOutput :== Either String [Token] +instance toString LexerOutput + lexer :: [Char] -> LexerOutput diff --git a/lex.icl b/lex.icl index a1912bf..aa50e72 100644 --- a/lex.icl +++ b/lex.icl @@ -1,38 +1,29 @@ implementation module lex +from Text import class Text, instance Text String +import Control.Monad import Data.Either +import Data.Functor import Data.List import StdString -import System.CommandLine -import StdFile -import StdMisc import StdBool -from StdFunc import id, const -import Data.Maybe -import Control.Applicative -import Control.Monad -import Control.Monad.State -import Data.Functor -from Data.Func import $ -from Text import class Text, instance Text String +import StdFile +import System.CommandLine import qualified Text Start :: *World -> *World Start w -# (args, w) = getCommandLine w // We lezen nu nog standaard van stdin # (out, w) = stdio w # (toparse, out) = readEntireFile out -# out = out <<< print (lexer toparse) -# (b, w) = fclose out w -| not b = setReturnCode 1 w +# (_, w) = fclose (out <<< "\n" <<< toString (lexer toparse)) w = w - where - readEntireFile :: *File -> *([Char], *File) - readEntireFile f - # (b, c, f) = freadc f - | not b = ([], f) - # (cs, f) = readEntireFile f - = ([c:cs], f) + +readEntireFile :: *File -> *([Char], *File) +readEntireFile f +# (b, c, f) = freadc f +| not b = ([], f) +# (cs, f) = readEntireFile f += ([c:cs], f) lexer :: [Char] -> LexerOutput lexer [] = Right [] @@ -40,60 +31,17 @@ lexer x = case lex x of (Right t, rest) = lexer rest >>= \ts.Right [t:ts] (Left e, _) = Left e -print :: LexerOutput -> String -print (Left l) = "Error: " +++ l -print (Right x) = 'Text'.concat (print` 0 x) - 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:print` i 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:CBraceCloseToken:rest] = - [";\n":tab (i-1):"}\n":print` (i-1) 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] - print` i [EndOfFileToken:rest] = ["\n":print` i rest] - -tab :: Int -> String -tab 0 = "" -tab i = "\t" +++ tab (i-1) - +untilNext :: [Char] -> [Char] +untilNext [] = [] +untilNext ['*':'/':rest] = rest +untilNext [x:rest] = untilNext rest lex :: [Char] -> (Either String Token, [Char]) lex [] = (Right EndOfFileToken, []) +//Comments +lex ['/':'/':rest] = lex (tl (dropWhile ((<>) '\n') rest)) +lex ['/':'*':rest] = lex (untilNext rest) +//Keyword tokens lex ['v':'a':'r':rest] = (Right VarToken, rest) lex ['V':'o':'i':'d':rest] = (Right VoidToken, rest) lex ['r':'e':'t':'u':'r':'n':rest] = (Right ReturnToken, rest) @@ -102,16 +50,10 @@ lex ['e':'l':'s':'e':rest] = (Right ElseToken, rest) lex ['w':'h':'i':'l':'e':rest] = (Right WhileToken, rest) lex ['T':'r':'u':'e':rest] = (Right TrueToken, rest) lex ['F':'a':'l':'s':'e':rest] = (Right FalseToken, rest) -lex ['\'':'\\':x:'\'':rest] = case x of - 'a' = (Right (CharToken (toChar 7)), rest) //Alarm - 'b' = (Right (CharToken '\b'), rest) //Backspace - 'f' = (Right (CharToken '\f'), rest) //Formfeed - 'n' = (Right (CharToken '\n'), rest) //Newline - 'r' = (Right (CharToken '\r'), rest) //Carriage Return - 't' = (Right (CharToken '\t'), rest) //Horizontal tab - 'v' = (Right (CharToken '\v'), rest) //Vertical tab - _ = (Left ("Unknown escape: \\" +++ toString x), rest) -lex ['\'':x:'\'':rest] = (Right (CharToken x), rest) +lex ['I':'n':'t':rest] = (Right IntTypeToken, rest) +lex ['C':'h':'a':'r':rest] = (Right CharTypeToken, rest) +lex ['B':'o':'o':'l':rest] = (Right BoolTypeToken, rest) +//Two character tokens lex [':':':':rest] = (Right DoubleColonToken, rest) lex ['<':'=':rest] = (Right LesserEqToken, rest) lex ['>':'=':rest] = (Right GreaterEqToken, rest) @@ -119,6 +61,7 @@ lex ['=':'=':rest] = (Right EqualsToken, rest) lex ['&':'&':rest] = (Right AmpersandsToken, rest) lex ['|':'|':rest] = (Right PipesToken, rest) lex ['-':'>':rest] = (Right ArrowToken, rest) +//One character tokens lex ['(':rest] = (Right BraceOpenToken, rest) lex [')':rest] = (Right BraceCloseToken, rest) lex ['{':rest] = (Right CBraceOpenToken, rest) @@ -138,12 +81,76 @@ lex ['=':rest] = (Right AssignmentToken, rest) lex ['<':rest] = (Right LesserToken, rest) lex ['>':rest] = (Right BiggerToken, rest) lex ['!':rest] = (Right ExclamationToken, rest) +//Value tokens +lex ['\'':x:'\'':rest] = (Right (CharToken x), rest) +lex ['\'':'\\':x:'\'':rest] = case x of + 'a' = (Right (CharToken (toChar 7)), rest) //Alarm + 'b' = (Right (CharToken '\b'), rest) //Backspace + 'f' = (Right (CharToken '\f'), rest) //Formfeed + 'n' = (Right (CharToken '\n'), rest) //Newline + 'r' = (Right (CharToken '\r'), rest) //Carriage Return + 't' = (Right (CharToken '\t'), rest) //Horizontal tab + 'v' = (Right (CharToken '\v'), rest) //Vertical tab + _ = (Left ("Unknown escape: \\" +++ toString x), []) lex [x:xs] | isSpace x = lex xs -| isDigit x - # (is, rest) = span isDigit xs - = (Right (NumberToken [x:is]), rest) -| isAlpha x - # (is, rest) = span (\c.isAlphanum c || c == '_') xs - = (Right (IdentToken [x:is]), rest) +| isDigit x = let (is, rest) = span isDigit xs in + (Right (NumberToken [x:is]), rest) +| isAlpha x = let (is, rest) = span (\c.isAlphanum c || c == '_') xs in + (Right (IdentToken [x:is]), rest) | otherwise = (Left ("Unexpected character: " +++ toString x), []) + +instance toString LexerOutput where + toString (Left l) = "Error: " +++ l + toString (Right x) = 'Text'.concat (print 0 x) + +tab :: Int -> [String] +tab i = replicate i "\t" + +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 [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 [VoidToken:rest] = ["Void":print i rest] +print i [IntTypeToken:rest] = ["Int":print i rest] +print i [CharTypeToken:rest] = ["Char":print i rest] +print i [BoolTypeToken:rest] = ["Bool":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] +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] = case rest of + [CBraceCloseToken:_] = ["}\n":tab (i-2)] ++ print (i-1) 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] = case rest of + [CBraceCloseToken:_] = [";\n":tab (i-1)] ++ print i 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 [EndOfFileToken:rest] = ["\n":print i rest] diff --git a/lex.prj b/lex.prj index 35240fc..199a01e 100644 --- a/lex.prj +++ b/lex.prj @@ -349,34 +349,6 @@ OtherModules ReadableABC: False ReuseUniqueNodes: True Fusion: False - Module - Name: Control.Monad.State - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False - Module - Name: Control.Monad.Trans - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False Module Name: Data.Either Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent @@ -419,20 +391,6 @@ OtherModules ReadableABC: False ReuseUniqueNodes: True Fusion: False - Module - Name: Data.Functor.Identity - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent - Compiler - NeverMemoryProfile: False - NeverTimeProfile: False - StrictnessAnalysis: True - ListTypes: StrictExportTypes - ListAttributes: True - Warnings: True - Verbose: True - ReadableABC: False - ReuseUniqueNodes: True - Fusion: False Module Name: Data.List Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent