Use reserved bang sym for env result
This commit is contained in:
parent
f71f88dce3
commit
79317bf4e3
14
src/Eval.hs
14
src/Eval.hs
@ -17,19 +17,19 @@ evalSingle env term
|
||||
"Error: Identifier '" ++ name ++ "' is already defined."
|
||||
| otherwise ->
|
||||
let res = evalAST env body
|
||||
in Map.insert "__result" res (Map.insert name res env)
|
||||
in Map.insert "!result" res (Map.insert name res env)
|
||||
| SApp func arg <- term =
|
||||
let res = apply (evalAST env func) (evalAST env arg)
|
||||
in Map.insert "__result" res env
|
||||
in Map.insert "!result" res env
|
||||
| SVar name <- term =
|
||||
case Map.lookup name env of
|
||||
Just v ->
|
||||
Map.insert "__result" v env
|
||||
Map.insert "!result" v env
|
||||
Nothing ->
|
||||
errorWithoutStackTrace $ "Variable `" ++ name ++ "` not defined\n\
|
||||
\This error should never occur here. Please report this as an issue."
|
||||
| otherwise =
|
||||
Map.insert "__result" (evalAST env term) env
|
||||
Map.insert "!result" (evalAST env term) env
|
||||
|
||||
evalTricu :: Env -> [TricuAST] -> Env
|
||||
evalTricu env x = go env (reorderDefs env x)
|
||||
@ -37,7 +37,7 @@ evalTricu env x = go env (reorderDefs env x)
|
||||
go env [] = env
|
||||
go env [x] =
|
||||
let updatedEnv = evalSingle env x
|
||||
in Map.insert "__result" (result updatedEnv) updatedEnv
|
||||
in Map.insert "!result" (result updatedEnv) updatedEnv
|
||||
go env (x:xs) =
|
||||
evalTricu (evalSingle env x) xs
|
||||
|
||||
@ -180,9 +180,9 @@ depends topDefs (SDef _ _ body) =
|
||||
depends _ _ = Set.empty
|
||||
|
||||
result :: Env -> T
|
||||
result r = case Map.lookup "__result" r of
|
||||
result r = case Map.lookup "!result" r of
|
||||
Just a -> a
|
||||
Nothing -> errorWithoutStackTrace "No __result field found in provided env"
|
||||
Nothing -> errorWithoutStackTrace "No !result field found in provided env"
|
||||
|
||||
mainResult :: Env -> T
|
||||
mainResult r = case Map.lookup "main" r of
|
||||
|
@ -23,8 +23,8 @@ identifier = do
|
||||
<|> char '_' <|> char '-' <|> char '?' <|> char '!'
|
||||
<|> char '$' <|> char '#' <|> char '@' <|> char '%'
|
||||
let name = first : rest
|
||||
if (name == "t" || name == "__result")
|
||||
then fail "Keywords (`t`, `__result`) cannot be used as an identifier"
|
||||
if (name == "t" || name == "!result")
|
||||
then fail "Keywords (`t`, `!result`) cannot be used as an identifier"
|
||||
else return (LIdentifier name)
|
||||
|
||||
integerLiteral :: Lexer LToken
|
||||
|
@ -61,7 +61,7 @@ main = do
|
||||
putStrLn "Welcome to the tricu REPL"
|
||||
putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`"
|
||||
library <- liftIO $ evaluateFile "./lib/base.tri"
|
||||
repl $ Map.delete "__result" library
|
||||
repl $ Map.delete "!result" library
|
||||
Evaluate { file = filePaths, form = form } -> do
|
||||
result <- case filePaths of
|
||||
[] -> do
|
||||
|
@ -244,7 +244,7 @@ parseVarM :: ParserM TricuAST
|
||||
parseVarM = do
|
||||
satisfyM (\case LIdentifier _ -> True; _ -> False) >>= \case
|
||||
LIdentifier name
|
||||
| name == "t" || name == "__result" ->
|
||||
| name == "t" || name == "!result" ->
|
||||
fail ("Reserved keyword: " ++ name ++ " cannot be assigned.")
|
||||
| otherwise ->
|
||||
pure (SVar name)
|
||||
|
@ -34,7 +34,7 @@ repl env = runInputT defaultSettings (loop env)
|
||||
loop env
|
||||
| Just p <- path -> do
|
||||
loadedEnv <- liftIO $ evaluateFileWithContext env (strip p) `catch` \e -> errorHandler env e
|
||||
loop $ Map.delete "__result" (Map.union loadedEnv env)
|
||||
loop $ Map.delete "!result" (Map.union loadedEnv env)
|
||||
| Just s <- minput -> do
|
||||
if
|
||||
| take 2 s == "--" -> loop env
|
||||
@ -47,7 +47,7 @@ repl env = runInputT defaultSettings (loop env)
|
||||
let asts = parseTricu input
|
||||
newEnv = evalTricu env asts
|
||||
if
|
||||
| Just r <- Map.lookup "__result" newEnv -> do
|
||||
| Just r <- Map.lookup "!result" newEnv -> do
|
||||
putStrLn $ "tricu > " ++ decodeResult r
|
||||
| otherwise -> return ()
|
||||
return newEnv
|
||||
|
@ -70,9 +70,9 @@ lexer = testGroup "Lexer Tests"
|
||||
Right i -> i @?= expect
|
||||
|
||||
, testCase "Error when using invalid characters in identifiers" $ do
|
||||
case (runParser tricuLexer "" "__result = 5") of
|
||||
case (runParser tricuLexer "" "!result = 5") of
|
||||
Left _ -> return ()
|
||||
Right _ -> assertFailure "Expected failure when trying to assign the value of __result"
|
||||
Right _ -> assertFailure "Expected failure when trying to assign the value of !result"
|
||||
]
|
||||
|
||||
parser :: TestTree
|
||||
|
Loading…
x
Reference in New Issue
Block a user