add option -r for forced repl

This commit is contained in:
Freya Murphy 2024-09-04 12:03:25 -04:00
parent 4124346ba1
commit ab042c7956
Signed by: freya
GPG key ID: 744AB800E383AE52
2 changed files with 16 additions and 11 deletions

View file

@ -1,4 +1,4 @@
use std::{path::PathBuf, io::{self, Read, IsTerminal}, cell::RefCell, rc::Rc}; use std::{cell::RefCell, io::{self, IsTerminal, Read}, path::PathBuf, rc::Rc};
use clap::{Parser as ArgParser, ColorChoice}; use clap::{Parser as ArgParser, ColorChoice};
use matrix_lang::prelude::*; use matrix_lang::prelude::*;
use repl::Repl; use repl::Repl;
@ -24,6 +24,10 @@ pub struct Args {
#[arg(short, long)] #[arg(short, long)]
debug: bool, debug: bool,
/// Force repl after running a file
#[arg(short, long)]
repl: bool,
/// Choses color /// Choses color
#[arg(long)] #[arg(long)]
color: Option<ColorChoice>, color: Option<ColorChoice>,
@ -44,6 +48,7 @@ pub struct State<'a> {
compiler: Compiler<'a>, compiler: Compiler<'a>,
vm: Rc<RefCell<Vm>>, vm: Rc<RefCell<Vm>>,
color: bool, color: bool,
repl: bool,
} }
pub fn error(err: Exception, state: &State) { pub fn error(err: Exception, state: &State) {
@ -83,10 +88,10 @@ impl<'a> State<'a> {
} }
}; };
mode = Mode::Compile(buffer, path); mode = Mode::Compile(buffer, path);
repl = false; repl = args.repl;
} else if buffer.len() > 0 { } else if buffer.len() > 0 {
mode = Mode::Execute(buffer); mode = Mode::Execute(buffer);
repl = false; repl = args.repl;
} else { } else {
mode = Mode::Repl; mode = Mode::Repl;
repl = true; repl = true;
@ -118,6 +123,7 @@ impl<'a> State<'a> {
vm: Rc::new(RefCell::new(vm)), vm: Rc::new(RefCell::new(vm)),
compiler, compiler,
color, color,
repl,
}, mode)) }, mode))
} }
@ -156,6 +162,7 @@ fn handle_mode(state: &mut State, mode: Mode) -> Result<()> {
Mode::Repl => { Mode::Repl => {
let mut repl = Repl::new(state); let mut repl = Repl::new(state);
repl.run()?; repl.run()?;
state.repl = false;
}, },
Mode::Execute(buffer) => { Mode::Execute(buffer) => {
let fun = state.load_program(buffer)?; let fun = state.load_program(buffer)?;
@ -170,6 +177,12 @@ fn handle_mode(state: &mut State, mode: Mode) -> Result<()> {
Program::save(fun, &mut file)?; Program::save(fun, &mut file)?;
} }
}; };
if state.repl {
let mut repl = Repl::new(state);
repl.run()?;
}
Ok(()) Ok(())
} }

View file

@ -1,8 +0,0 @@
let a = 3
{
a = 4;
const bees = \a => a*2;
let j = bees(3);
a = j;
}
let g = a + 'aaaa';