From: Mart Lubbers Date: Wed, 17 Feb 2016 14:22:30 +0000 (+0100) Subject: made lex a module X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=e3f049be4fb2ed9b7f675f06a9a525c81af9946c;p=cc1516.git made lex a module --- diff --git a/Makefile b/Makefile index 56d9b95..f0036fa 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ all: $(CPM) make clean: - $(RM) -rv Clean\ System\ Files sapl lex + $(RM) -rv Clean\ System\ Files sapl main diff --git a/lex.dcl b/lex.dcl index d1daf7c..7e41bc3 100644 --- a/lex.dcl +++ b/lex.dcl @@ -53,6 +53,4 @@ import Data.Either :: LexerOutput :== Either String [Token] -instance toString LexerOutput - lexer :: [Char] -> LexerOutput diff --git a/lex.icl b/lex.icl index 2eeb600..0b6272c 100644 --- a/lex.icl +++ b/lex.icl @@ -1,29 +1,30 @@ implementation module lex -from Text import class Text, instance Text String import Control.Monad import Data.Either -import Data.Functor -import Data.List +import Data.Maybe +import Data.Map import StdString import StdBool +import StdList +import StdChar import StdFile import System.CommandLine import qualified Text -Start :: *World -> *World -Start w -# (out, w) = stdio w -# (toparse, out) = readEntireFile out -# (_, w) = fclose (out <<< "\n" <<< toString (lexer toparse)) w -= w +SingleCharTokens :: Map Char Token +SingleCharTokens = fromList [ + ('(', BraceOpenToken), (')', BraceCloseToken), ('{', CBraceOpenToken), + ('}', CBraceCloseToken), ('[', SquareOpenToken), (']', SquareCloseToken), + (',', CommaToken), (':', ColonToken), (';', SColonToken), + ('.', DotToken), ('+', PlusToken), ('-', DashToken), ('*', StarToken), + ('/', SlashToken), ('%', PercentToken), ('=', AssignmentToken), + ('<', LesserToken), ('>', BiggerToken), ('!', ExclamationToken)] -readEntireFile :: *File -> *([Char], *File) -readEntireFile f -# (b, c, f) = freadc f -| not b = ([], f) -# (cs, f) = readEntireFile f -= ([c:cs], f) +EscapeMap :: Map Char Char +EscapeMap = fromList [ + ('a', toChar 7), ('b', '\b'), ('f', '\f'), ('n', '\n'), ('r', '\r'), + ('t', '\t'), ('v', '\v')] lexer :: [Char] -> LexerOutput lexer [] = Right [] @@ -31,16 +32,12 @@ lexer x = case lex x of (Right t, rest) = lexer rest >>= \ts.Right [t:ts] (Left e, _) = Left e -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) +lex ['/':'/':x:rest] = if (x == '\n') (lex rest) (lex ['/':'/':rest]) +lex ['/':'*':x1:x2:rest] = if (x1 == '*' && x2 == '/') + (lex rest) (lex ['/':'*':rest]) //Keyword tokens lex ['v':'a':'r':rest] = (Right VarToken, rest) lex ['V':'o':'i':'d':rest] = (Right VoidToken, rest) @@ -63,96 +60,17 @@ 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) -lex ['}':rest] = (Right CBraceCloseToken, rest) -lex ['[':rest] = (Right SquareOpenToken, rest) -lex [']':rest] = (Right SquareCloseToken, rest) -lex [',':rest] = (Right CommaToken, rest) -lex [':':rest] = (Right ColonToken, rest) -lex [';':rest] = (Right SColonToken, rest) -lex ['.':rest] = (Right DotToken, rest) -lex ['+':rest] = (Right PlusToken, rest) -lex ['-':rest] = (Right DashToken, rest) -lex ['*':rest] = (Right StarToken, rest) -lex ['/':rest] = (Right SlashToken, rest) -lex ['%':rest] = (Right PercentToken, rest) -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 +lex ['\'':'\\':x:'\'':rest] = case get x EscapeMap of + Just t = (Right (CharToken t), rest) _ = (Left ("Unknown escape: \\" +++ toString x), []) -lex [x:xs] -| isSpace x = lex xs -| 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) +lex [x:rest] +# t = get x SingleCharTokens +| isJust t = (Right (fromJust t), rest) +| isSpace x = lex rest +| isDigit x = let (is, rest`) = span isDigit rest in + (Right (NumberToken [x:is]), rest`) +| isAlpha x = let (is, rest`) = span (\c.isAlphanum c || c == '_') rest 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 [NotEqualToken: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/main.icl b/main.icl new file mode 100644 index 0000000..90624e8 --- /dev/null +++ b/main.icl @@ -0,0 +1,20 @@ +module main + +import StdFile +import StdBool + +import lex + +Start :: *World -> (LexerOutput, *World) +Start w +# (out, w) = stdio w +# (toparse, out) = readEntireFile out +# (_, w) = fclose out w += (lexer toparse, w) + +readEntireFile :: *File -> *([Char], *File) +readEntireFile f +# (b, c, f) = freadc f +| not b = ([], f) +# (cs, f) = readEntireFile f += ([c:cs], f) diff --git a/lex.prj b/main.prj similarity index 69% rename from lex.prj rename to main.prj index 199a01e..5ed68bd 100644 --- a/lex.prj +++ b/main.prj @@ -2,7 +2,7 @@ Version: 1.4 Global ProjectRoot: . Target: iTasks - Exec: {Project}/lex + Exec: {Project}/main CodeGen CheckStacks: False CheckIndexes: True @@ -31,6 +31,7 @@ Global Link LinkMethod: Static GenerateRelocations: False + GenerateSymbolTable: False GenerateLinkMap: False LinkResources: False ResourceSource: @@ -41,7 +42,7 @@ Global Precompile: Postlink: MainModule - Name: lex + Name: main Dir: {Project} Compiler NeverMemoryProfile: False @@ -55,6 +56,20 @@ MainModule ReuseUniqueNodes: True Fusion: False OtherModules + Module + Name: lex + Dir: {Project} + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False Module Name: StdArray Dir: {Application}/lib/StdEnv @@ -251,6 +266,20 @@ OtherModules ReadableABC: False ReuseUniqueNodes: True Fusion: False + Module + Name: StdOverloadedList + Dir: {Application}/lib/StdEnv + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False Module Name: StdReal Dir: {Application}/lib/StdEnv @@ -265,6 +294,20 @@ OtherModules ReadableABC: False ReuseUniqueNodes: True Fusion: False + Module + Name: StdStrictLists + Dir: {Application}/lib/StdEnv + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False Module Name: StdString Dir: {Application}/lib/StdEnv @@ -321,9 +364,37 @@ OtherModules ReadableABC: False ReuseUniqueNodes: True Fusion: False + Module + Name: _SystemEnumStrict + Dir: {Application}/lib/StdEnv + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False + Module + Name: _SystemStrictLists + Dir: {Application}/lib/StdEnv + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False Module Name: Control.Applicative - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -337,7 +408,7 @@ OtherModules Fusion: False Module Name: Control.Monad - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -351,7 +422,21 @@ OtherModules Fusion: False Module Name: Data.Either - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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.Foldable + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -365,7 +450,7 @@ OtherModules Fusion: False Module Name: Data.Func - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -379,7 +464,7 @@ OtherModules Fusion: False Module Name: Data.Functor - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -393,7 +478,21 @@ OtherModules Fusion: False Module Name: Data.List - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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.Map + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -407,7 +506,7 @@ OtherModules Fusion: False Module Name: Data.Maybe - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -421,7 +520,35 @@ OtherModules Fusion: False Module Name: Data.Monoid - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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.Set + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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.Traversable + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -435,7 +562,7 @@ OtherModules Fusion: False Module Name: Data.Void - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -449,7 +576,7 @@ OtherModules Fusion: False Module Name: System.CommandLine - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -463,7 +590,7 @@ OtherModules Fusion: False Module Name: System.IO - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -477,7 +604,7 @@ OtherModules Fusion: False Module Name: System._Pointer - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -491,7 +618,35 @@ OtherModules Fusion: False Module Name: Text - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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: Text.JSON + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/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: Text.PPrint + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -505,7 +660,7 @@ OtherModules Fusion: False Module Name: System.OS - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Linux-64 Compiler NeverMemoryProfile: False NeverTimeProfile: False