diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-03-10 18:02:16 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-03-10 18:02:16 +0100 |
| commit | 62147f625e2a51a405c1c7ea5ca2ef4d96b8e7f6 (patch) | |
| tree | 7098a1f54e44603705c376fd24ec5c186738c4fe /src/lib.rs | |
| parent | f671ecc40626e9fbac452981a87636b6f00d7e15 (diff) | |
| download | sylt-62147f625e2a51a405c1c7ea5ca2ef4d96b8e7f6.tar.gz | |
rework run_file
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 78 |
1 files changed, 44 insertions, 34 deletions
@@ -19,41 +19,31 @@ mod compiler; mod sectionizer; mod tokenizer; -/// Compiles a file and links the supplied functions as callable external -/// functions. Use this if you want your programs to be able to yield. -pub fn compile_file( - path: &Path, - print: bool, - functions: Vec<(String, RustFunction)> -) -> Result<vm::VM, Vec<Error>> { - let sections = sectionizer::sectionize(path); - match compiler::Compiler::new(sections).compile("main", path, &functions) { - Ok(prog) => { - let mut vm = vm::VM::new(); - vm.print_blocks = print; - vm.print_ops = print; - vm.typecheck(&prog)?; - vm.init(&prog); - Ok(vm) - } - Err(errors) => Err(errors), - } -} - /// Compiles, links and runs the given file. Supplied functions are callable /// external functions. If you want your program to be able to yield, use /// [compile_file]. -pub fn run_file(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec<Error>> { - run(path, print, functions) +pub fn run_file(args: Args, functions: Vec<(String, RustFunction)>) -> Result<(), Vec<Error>> { + run(args, functions) } -fn run(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec<Error>> { - let sections = sectionizer::sectionize(path); - match compiler::Compiler::new(sections).compile("main", path, &functions) { +fn run(args: Args, functions: Vec<(String, RustFunction)>) -> Result<(), Vec<Error>> { + let path = match args.file { + Some(file) => file, + None => { + return Err(vec![Error { + kind: ErrorKind::NoFileGiven, + file: PathBuf::from(""), + line: 0, + message: None, + }]); + } + }; + let sections = sectionizer::sectionize(&path); + match compiler::Compiler::new(sections).compile("/preamble", &path, &functions) { Ok(prog) => { let mut vm = vm::VM::new(); - vm.print_blocks = print; - vm.print_ops = print; + vm.print_bytecode = args.print_bytecode; + vm.print_exec = args.print_exec; vm.typecheck(&prog)?; vm.init(&prog); if let Err(e) = vm.run() { @@ -66,6 +56,22 @@ fn run(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Resu } } +pub struct Args { + pub file: Option<PathBuf>, + pub print_exec: bool, + pub print_bytecode: bool, +} + +impl Default for Args { + fn default() -> Self { + Self { + file: None, + print_exec: false, + print_bytecode: false, + } + } +} + /// A linkable external function. Created either manually or using /// [sylt_macro::extern_function]. pub type RustFunction = fn(&[Value], bool) -> Result<Value, ErrorKind>; @@ -994,19 +1000,23 @@ mod tests { ($fn:ident, $path:literal, $print:expr) => { #[test] fn $fn() { - let file = std::path::Path::new($path); - crate::run_file(&file, $print, Vec::new()).unwrap(); + let mut args = $crate::Args::default(); + args.file = Some(std::path::PathBuf::from($path)); + args.print_bytecode = $print; + $crate::run_file(args, Vec::new()).unwrap(); } }; ($fn:ident, $path:literal, $print:expr, $errs:tt) => { #[test] fn $fn() { - use crate::error::ErrorKind; + use $crate::error::ErrorKind; #[allow(unused_imports)] - use crate::Type; + use $crate::Type; - let file = std::path::Path::new($path); - let res = crate::run_file(&file, $print, Vec::new()); + let mut args = $crate::Args::default(); + args.file = Some(std::path::PathBuf::from($path)); + args.print_bytecode = $print; + let res = $crate::run_file(args, Vec::new()); $crate::assert_errs!(res, $errs); } }; |
