4 Commits

Author SHA1 Message Date
f97bd84050 Adds basic tests for modules
Basic tests for modules. Fixes for parsing multiple import statements.
2025-01-27 16:03:03 -06:00
ae971ec968 Error on import cycles 2025-01-27 13:23:38 -06:00
63504ba939 Rough draft of modules
This includes a naive implementation of a module system where imported
files have their imports recursively handled, strips the module/import
AST nodes, and then evals everything into a flat environment using
namespace prefixes like "Module.function".
2025-01-27 12:24:30 -06:00
79317bf4e3 Use reserved bang sym for env result 2025-01-27 08:46:40 -06:00
3 changed files with 22 additions and 20 deletions

View File

@ -10,7 +10,7 @@ tricu is the word for "tree" in Lojban: `(x1) is a tree of species/cultivar (x2)
- Tree Calculus operator: `t` - Tree Calculus operator: `t`
- Assignments: `x = t t` - Assignments: `x = t t`
- Immutable definitions - Immutabile definitions
- Lambda abstraction syntax: `id = (\a : a)` - Lambda abstraction syntax: `id = (\a : a)`
- List, Number, and String literals: `[(2) ("Hello")]` - List, Number, and String literals: `[(2) ("Hello")]`
- Function application: `not (not false)` - Function application: `not (not false)`
@ -45,7 +45,7 @@ tricu > 12
[Releases are available for Linux.](https://git.eversole.co/James/tricu/releases) [Releases are available for Linux.](https://git.eversole.co/James/tricu/releases)
Or you can easily build and run this project using [Nix](https://nixos.org/download/). Or you can easily build and/or run this project using [Nix](https://nixos.org/download/).
- Quick Start (REPL): - Quick Start (REPL):
- `nix run git+https://git.eversole.co/James/tricu` - `nix run git+https://git.eversole.co/James/tricu`

View File

@ -10,26 +10,26 @@ import qualified Data.Set as Set
evalSingle :: Env -> TricuAST -> Env evalSingle :: Env -> TricuAST -> Env
evalSingle env term evalSingle env term
| SDef name [] body <- term | SDef name [] body <- term =
= case Map.lookup name env of if
Just existingValue | Map.member name env ->
| existingValue == evalAST env body -> env errorWithoutStackTrace $
| otherwise -> errorWithoutStackTrace $ "Error: Identifier '" ++ name ++ "' is already defined."
"Unable to rebind immutable identifier: '" ++ name | otherwise ->
Nothing -> let res = evalAST env body
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 =
| SApp func arg <- term let res = apply (evalAST env func) (evalAST env arg)
= let res = apply (evalAST env func) (evalAST env arg)
in Map.insert "!result" res env in Map.insert "!result" res env
| SVar name <- term | SVar name <- term =
= case Map.lookup name env of case Map.lookup name env of
Just v -> Map.insert "!result" v env Just v ->
Map.insert "!result" v env
Nothing -> Nothing ->
errorWithoutStackTrace $ "Variable `" ++ name ++ "` not defined\n\ errorWithoutStackTrace $ "Variable `" ++ name ++ "` not defined\n\
\This error should never occur here. Please report this as an issue." \This error should never occur here. Please report this as an issue."
| otherwise | otherwise =
= Map.insert "!result" (evalAST env term) env Map.insert "!result" (evalAST env term) env
evalTricu :: Env -> [TricuAST] -> Env evalTricu :: Env -> [TricuAST] -> Env
evalTricu env x = go env (reorderDefs env x) evalTricu env x = go env (reorderDefs env x)

View File

@ -60,7 +60,8 @@ main = do
Repl -> do Repl -> do
putStrLn "Welcome to the tricu REPL" putStrLn "Welcome to the tricu REPL"
putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`" putStrLn "You can exit with `CTRL+D` or the `:_exit` command.`"
repl Map.empty library <- liftIO $ evaluateFile "./lib/base.tri"
repl $ Map.delete "!result" library
Evaluate { file = filePaths, form = form } -> do Evaluate { file = filePaths, form = form } -> do
result <- case filePaths of result <- case filePaths of
[] -> do [] -> do
@ -76,7 +77,8 @@ main = do
value <- case filePaths of value <- case filePaths of
[] -> getContents [] -> getContents
(filePath:_) -> readFile filePath (filePath:_) -> readFile filePath
putStrLn $ decodeResult $ result $ evalTricu Map.empty $ parseTricu value library <- liftIO $ evaluateFile "./lib/base.tri"
putStrLn $ decodeResult $ result $ evalTricu library $ parseTricu value
runTricu :: String -> T runTricu :: String -> T
runTricu input = runTricu input =