summaryrefslogtreecommitdiff
path: root/matrix-bin/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'matrix-bin/src/main.rs')
-rw-r--r--matrix-bin/src/main.rs38
1 files changed, 29 insertions, 9 deletions
diff --git a/matrix-bin/src/main.rs b/matrix-bin/src/main.rs
index 909ee77..225b353 100644
--- a/matrix-bin/src/main.rs
+++ b/matrix-bin/src/main.rs
@@ -1,9 +1,10 @@
-use std::{path::PathBuf, io::{self, Read, IsTerminal}, fs};
-use clap::Parser as ArgParser;
+use std::{path::PathBuf, io::{self, Read, IsTerminal}, fs, cell::RefCell, rc::Rc};
+use clap::{Parser as ArgParser, ColorChoice};
use matrix::{compiler::{Compiler, CompilerBuilder}, vm::Vm, parse::{Parser, ParserBuilder}, value::Value};
use repl::Repl;
mod repl;
+mod helper;
#[derive(Debug, ArgParser)]
#[command(version, long_about = None)]
@@ -19,6 +20,10 @@ pub struct Args {
#[arg(short, long)]
debug: bool,
+ /// Choses color
+ #[arg(short, long)]
+ color: Option<ColorChoice>,
+
/// Disables optimizations
#[arg(long)]
disable_optimizations: bool,
@@ -27,8 +32,11 @@ pub struct Args {
pub struct State<'a> {
parser: Parser,
compiler: Compiler<'a>,
- vm: Vm,
- repl: bool
+ vm: Rc<RefCell<Vm>>,
+ repl: bool,
+ color: bool,
+ #[allow(unused)]
+ debug: bool,
}
impl<'a> State<'a> {
@@ -60,20 +68,32 @@ impl<'a> State<'a> {
matrix_stdlib::load(&mut vm);
- (Self { parser, vm, compiler, repl }, file)
+ let color = match args.color {
+ Some(ColorChoice::Auto) | None => {
+ io::stdout().is_terminal()
+ },
+ Some(ColorChoice::Always) => true,
+ Some(ColorChoice::Never) => false,
+ };
+
+ (Self { parser, vm: Rc::new(RefCell::new(vm)), compiler, repl, debug: args.debug, color }, file)
}
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)?;
+ let val = self.vm.try_borrow_mut().unwrap().run(fun)?;
Ok(val)
}
}
-pub fn error(err: matrix::Error) {
- println!("\x1b[31m\x1b[1mError:\x1b[0m {err}");
+pub fn error(err: matrix::Error, state: &State) {
+ if state.color {
+ println!("\x1b[31m\x1b[1mError:\x1b[0m {err}");
+ } else {
+ println!("Error: {err}");
+ }
}
fn read_stdin() -> String {
@@ -93,7 +113,7 @@ fn main() {
if let Some(file) = file {
if let Err(err) = state.execute(file) {
- error(err);
+ error(err, &state);
}
}