feat: init Rust VM
This commit is contained in:
+17
-16
@@ -3,9 +3,9 @@ use std::process::exit;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use hashbrown::HashSet;
|
||||
use fix::context::Context;
|
||||
use fix::error::Source;
|
||||
use fix::runtime::Runtime;
|
||||
use hashbrown::HashSet;
|
||||
use rustyline::DefaultEditor;
|
||||
use rustyline::error::ReadlineError;
|
||||
|
||||
@@ -40,7 +40,7 @@ struct ExprSource {
|
||||
file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn run_compile(context: &mut Context, src: ExprSource, silent: bool) -> Result<()> {
|
||||
fn run_compile(runtime: &mut Runtime, src: ExprSource, silent: bool) -> Result<()> {
|
||||
let src = if let Some(expr) = src.expr {
|
||||
Source::new_eval(expr)?
|
||||
} else if let Some(file) = src.file {
|
||||
@@ -48,10 +48,11 @@ fn run_compile(context: &mut Context, src: ExprSource, silent: bool) -> Result<(
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
match context.compile_bytecode(src) {
|
||||
todo!()
|
||||
/* match runtime.compile_bytecode(src) {
|
||||
Ok(compiled) => {
|
||||
if !silent {
|
||||
println!("{}", context.disassemble_colored(&compiled));
|
||||
println!("{}", runtime.disassemble_colored(&compiled));
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -59,10 +60,10 @@ fn run_compile(context: &mut Context, src: ExprSource, silent: bool) -> Result<(
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
Ok(()) */
|
||||
}
|
||||
|
||||
fn run_eval(context: &mut Context, src: ExprSource) -> Result<()> {
|
||||
fn run_eval(runtime: &mut Runtime, src: ExprSource) -> Result<()> {
|
||||
let src = if let Some(expr) = src.expr {
|
||||
Source::new_eval(expr)?
|
||||
} else if let Some(file) = src.file {
|
||||
@@ -70,7 +71,7 @@ fn run_eval(context: &mut Context, src: ExprSource) -> Result<()> {
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
match context.eval_deep(src) {
|
||||
match runtime.eval_deep(src) {
|
||||
Ok(value) => {
|
||||
println!("{}", value.display_compat());
|
||||
}
|
||||
@@ -82,7 +83,7 @@ fn run_eval(context: &mut Context, src: ExprSource) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_repl(context: &mut Context) -> Result<()> {
|
||||
fn run_repl(runtime: &mut Runtime) -> Result<()> {
|
||||
let mut rl = DefaultEditor::new()?;
|
||||
let mut scope = HashSet::new();
|
||||
const RE: ere::Regex<3> = ere::compile_regex!("^[ \t]*([a-zA-Z_][a-zA-Z0-9_'-]*)[ \t]*(.*)$");
|
||||
@@ -101,20 +102,20 @@ fn run_repl(context: &mut Context) -> Result<()> {
|
||||
eprintln!("Error: missing expression after '='");
|
||||
continue;
|
||||
}
|
||||
match context.add_binding(ident, expr, &mut scope) {
|
||||
match runtime.add_binding(ident, expr, &mut scope) {
|
||||
Ok(value) => println!("{} = {}", ident, value),
|
||||
Err(err) => eprintln!("{:?}", miette::Report::new(*err)),
|
||||
}
|
||||
} else {
|
||||
let src = Source::new_repl(line)?;
|
||||
match context.eval_repl(src, &scope) {
|
||||
match runtime.eval_repl(src, &scope) {
|
||||
Ok(value) => println!("{value}"),
|
||||
Err(err) => eprintln!("{:?}", miette::Report::new(*err)),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let src = Source::new_repl(line)?;
|
||||
match context.eval_shallow(src) {
|
||||
match runtime.eval_repl(src, &scope) {
|
||||
Ok(value) => println!("{value}"),
|
||||
Err(err) => eprintln!("{:?}", miette::Report::new(*err)),
|
||||
}
|
||||
@@ -141,11 +142,11 @@ fn main() -> Result<()> {
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut context = Context::new()?;
|
||||
let mut runtime = Runtime::new()?;
|
||||
|
||||
match cli.command {
|
||||
Command::Compile { source, silent } => run_compile(&mut context, source, silent),
|
||||
Command::Eval { source } => run_eval(&mut context, source),
|
||||
Command::Repl => run_repl(&mut context),
|
||||
Command::Compile { source, silent } => run_compile(&mut runtime, source, silent),
|
||||
Command::Eval { source } => run_eval(&mut runtime, source),
|
||||
Command::Repl => run_repl(&mut runtime),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user