Fixes identifier lexing; support REPL file loading

This commit is contained in:
James Eversole 2025-01-01 18:05:21 -06:00
parent bf58c9afbd
commit 39be66a4d1
5 changed files with 23 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,10 +1,7 @@
bin/ bin/
data/Purr.sqlite
data/encryptionKey
/result /result
/config.dhall /config.dhall
/Dockerfile /Dockerfile
/docker-stack.yml
.stack-work/ .stack-work/
*.swp *.swp
dist* dist*

View File

@ -17,3 +17,9 @@ evaluateFile filePath = do
case Map.lookup "__result" finalEnv of case Map.lookup "__result" finalEnv of
Just finalResult -> return finalResult Just finalResult -> return finalResult
Nothing -> error "No result found in final environment" 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

View File

@ -17,7 +17,9 @@ keywordT = string "t" *> notFollowedBy alphaNumChar *> pure LKeywordT
identifier :: Lexer LToken identifier :: Lexer LToken
identifier = do 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") if (name == "t" || name == "__result")
then fail "Keywords (`t`, `__result`) cannot be used as an identifier" then fail "Keywords (`t`, `__result`) cannot be used as an identifier"
else return (LIdentifier name) else return (LIdentifier name)
@ -31,11 +33,8 @@ stringLiteral :: Lexer LToken
stringLiteral = do stringLiteral = do
char '"' char '"'
content <- many (noneOf ['"']) content <- many (noneOf ['"'])
if null content char '"' --"
then fail "Empty string literals are not allowed" return (LStringLiteral content)
else do
char '"' --"
return (LStringLiteral content)
assign :: Lexer LToken assign :: Lexer LToken
assign = char '=' *> pure LAssign assign = char '=' *> pure LAssign

View File

@ -38,10 +38,10 @@ evaluateMode = Evaluate
&= name "o" &= name "o"
, form = FSL &= typ "FORM" , form = FSL &= typ "FORM"
&= help "Optional output form: (fsl|tree|ast|ternary|ascii).\n \ &= help "Optional output form: (fsl|tree|ast|ternary|ascii).\n \
\ Defaults to fsl." \ Defaults to fsl \"Fork Stem Leaf\" form."
&= name "t" &= 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 &= explicit
&= name "eval" &= name "eval"

View File

@ -1,6 +1,7 @@
module REPL where module REPL where
import Eval import Eval
import FileEval
import Lexer import Lexer
import Parser import Parser
import Research import Research
@ -21,6 +22,15 @@ repl env = runInputT defaultSettings (loop env)
case minput of case minput of
Nothing -> outputStrLn "Goodbye!" Nothing -> outputStrLn "Goodbye!"
Just ":_exit" -> 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 Just "" -> do
outputStrLn "" outputStrLn ""
loop env loop env