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/
data/Purr.sqlite
data/encryptionKey
/result
/config.dhall
/Dockerfile
/docker-stack.yml
.stack-work/
*.swp
dist*

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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