diff options
author | Freya Murphy <freya@freyacat.org> | 2024-02-23 11:32:47 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-02-23 11:32:47 -0500 |
commit | a888c09bd54de77fb2004754a0e14ce14a906232 (patch) | |
tree | c5b20b4be32feec7a3430f1191e1f735ea51ca57 /matrix-bin | |
parent | indexing and field access (diff) | |
download | matrix-a888c09bd54de77fb2004754a0e14ce14a906232.tar.gz matrix-a888c09bd54de77fb2004754a0e14ce14a906232.tar.bz2 matrix-a888c09bd54de77fb2004754a0e14ce14a906232.zip |
more changes
Diffstat (limited to 'matrix-bin')
-rw-r--r-- | matrix-bin/Cargo.toml | 2 | ||||
-rw-r--r-- | matrix-bin/src/main.rs | 14 | ||||
-rw-r--r-- | matrix-bin/src/repl.rs | 26 |
3 files changed, 32 insertions, 10 deletions
diff --git a/matrix-bin/Cargo.toml b/matrix-bin/Cargo.toml index 029923b..c4b4d5a 100644 --- a/matrix-bin/Cargo.toml +++ b/matrix-bin/Cargo.toml @@ -9,5 +9,7 @@ path = "src/main.rs" [dependencies] clap = { version = "4", features = [ "derive" ] } +ctrlc = "3" matrix = { path = "../matrix" } +matrix-stdlib = { path = "../matrix-stdlib" } rustyline = "13" diff --git a/matrix-bin/src/main.rs b/matrix-bin/src/main.rs index 72972c6..909ee77 100644 --- a/matrix-bin/src/main.rs +++ b/matrix-bin/src/main.rs @@ -1,6 +1,6 @@ use std::{path::PathBuf, io::{self, Read, IsTerminal}, fs}; use clap::Parser as ArgParser; -use matrix::{compiler::{Compiler, CompilerBuilder}, vm::Vm, parse::{Parser, ParserBuilder}}; +use matrix::{compiler::{Compiler, CompilerBuilder}, vm::Vm, parse::{Parser, ParserBuilder}, value::Value}; use repl::Repl; mod repl; @@ -50,24 +50,24 @@ impl<'a> State<'a> { let parser = ParserBuilder::new() .optimize(!args.disable_optimizations) .build(); - let vm = Vm::new(); + let mut vm = Vm::new(); let compiler = CompilerBuilder::new() .repl(repl) .debug(args.debug) .names(vm.names()) + .globals(vm.global_names()) .build(); + matrix_stdlib::load(&mut vm); + (Self { parser, vm, compiler, repl }, file) } - pub fn execute(&mut self, code: String) -> matrix::Result<()> { + pub fn execute(&mut self, code: String) -> matrix::Result<Value> { let ast = self.parser.parse(code)?; let fun = self.compiler.compile(&ast)?; let val = self.vm.run(fun)?; - if self.repl { - println!("{val}"); - } - Ok(()) + Ok(val) } } diff --git a/matrix-bin/src/repl.rs b/matrix-bin/src/repl.rs index 81fd289..1b5addc 100644 --- a/matrix-bin/src/repl.rs +++ b/matrix-bin/src/repl.rs @@ -1,3 +1,8 @@ +use std::{io::Write, sync::atomic::Ordering}; + +use matrix::{value::Value, vm::Interupt}; +use rustyline::Config; + use crate::State; pub struct Repl<'a> { @@ -11,14 +16,29 @@ impl<'a> Repl<'a> { } pub fn run(&mut self) { - let mut rl = rustyline::DefaultEditor::new().unwrap(); + + let interupt = self.state.vm.interupt(); + ctrlc::set_handler(move || { + interupt.store(Interupt::KeyboardInterupt as usize, Ordering::SeqCst); + }).unwrap(); + + let config = Config::builder() + .check_cursor_position(true) + .build(); + let mut rl = rustyline::DefaultEditor::with_config(config).unwrap(); loop { let Ok(line) = rl.readline(">> ") else { break; }; - if let Err(err) = self.state.execute(line) { - crate::error(err); + match self.state.execute(line) { + Err(err) => crate::error(err), + Ok(val) => { + if val != Value::Nil { + println!("{val}"); + } + } } + let _ = std::io::stdout().flush(); } } |