From 39be66a4d146a7ad4afd7b97550cb4c6b21f644e Mon Sep 17 00:00:00 2001 From: James Eversole Date: Wed, 1 Jan 2025 18:05:21 -0600 Subject: [PATCH] Fixes identifier lexing; support REPL file loading --- .gitignore | 3 --- src/FileEval.hs | 6 ++++++ src/Lexer.hs | 11 +++++------ src/Main.hs | 4 ++-- src/REPL.hs | 10 ++++++++++ 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index c0f6aca..d8cf097 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,7 @@ bin/ -data/Purr.sqlite -data/encryptionKey /result /config.dhall /Dockerfile -/docker-stack.yml .stack-work/ *.swp dist* diff --git a/src/FileEval.hs b/src/FileEval.hs index 5b2a727..b33e3e5 100644 --- a/src/FileEval.hs +++ b/src/FileEval.hs @@ -17,3 +17,9 @@ evaluateFile filePath = do case Map.lookup "__result" finalEnv of Just finalResult -> return finalResult Nothing -> error "No result found in final environment" + +evaluateFileEnv :: FilePath -> IO Env +evaluateFileEnv filePath = do + contents <- readFile filePath + let asts = parseTricu contents + pure $ evalTricu library asts diff --git a/src/Lexer.hs b/src/Lexer.hs index 6e7b1b0..1f9dc9e 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -17,7 +17,9 @@ keywordT = string "t" *> notFollowedBy alphaNumChar *> pure LKeywordT identifier :: Lexer LToken identifier = do - name <- some (letterChar <|> char '_' <|> char '-') + first <- letterChar <|> char '_' + rest <- many (letterChar <|> char '_' <|> char '-' <|> digitChar) + let name = first : rest if (name == "t" || name == "__result") then fail "Keywords (`t`, `__result`) cannot be used as an identifier" else return (LIdentifier name) @@ -31,11 +33,8 @@ stringLiteral :: Lexer LToken stringLiteral = do char '"' content <- many (noneOf ['"']) - if null content - then fail "Empty string literals are not allowed" - else do - char '"' --" - return (LStringLiteral content) + char '"' --" + return (LStringLiteral content) assign :: Lexer LToken assign = char '=' *> pure LAssign diff --git a/src/Main.hs b/src/Main.hs index 336c873..1ae9a84 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -38,10 +38,10 @@ evaluateMode = Evaluate &= name "o" , form = FSL &= typ "FORM" &= help "Optional output form: (fsl|tree|ast|ternary|ascii).\n \ - \ Defaults to fsl." + \ Defaults to fsl \"Fork Stem Leaf\" form." &= name "t" } - &= help "Evaluate a file and return the result of the expression in the final line" + &= help "Evaluate tricu and return the result of the expression in the final line" &= explicit &= name "eval" diff --git a/src/REPL.hs b/src/REPL.hs index d407e86..655dc64 100644 --- a/src/REPL.hs +++ b/src/REPL.hs @@ -1,6 +1,7 @@ module REPL where import Eval +import FileEval import Lexer import Parser import Research @@ -21,6 +22,15 @@ repl env = runInputT defaultSettings (loop env) case minput of Nothing -> outputStrLn "Goodbye!" Just ":_exit" -> outputStrLn "Goodbye!" + Just ":_load" -> do + path <- getInputLine "File path to load < " + case path of + Nothing -> do + outputStrLn "No input received; stopping import." + loop env + Just path -> do + loadedEnv <- liftIO $ evaluateFileEnv path + loop $ Map.union loadedEnv env Just "" -> do outputStrLn "" loop env