Some cleanups.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
use codespan_reporting::diagnostic::Diagnostic;
|
||||
use codespan_reporting::files::SimpleFiles;
|
||||
use codespan_reporting::term::{self, Config};
|
||||
use cranelift_jit::JITModule;
|
||||
use cranelift_module::ModuleError;
|
||||
use ngr::backend::{Backend, BackendError};
|
||||
use ngr::ir::Program as IR;
|
||||
use ngr::jit::{JITEngine, JITError};
|
||||
use ngr::syntax::{Location, ParserError, Statement};
|
||||
use pretty::termcolor::{ColorChoice, StandardStream, WriteColor};
|
||||
use rustyline::error::ReadlineError;
|
||||
@@ -11,7 +13,7 @@ use std::collections::HashMap;
|
||||
|
||||
pub struct RunLoop<'a> {
|
||||
file_database: SimpleFiles<&'a str, String>,
|
||||
jitter: JITEngine,
|
||||
jitter: Backend<JITModule>,
|
||||
variable_binding_sites: HashMap<String, Location>,
|
||||
gensym_index: usize,
|
||||
writer: &'a mut dyn WriteColor,
|
||||
@@ -24,7 +26,9 @@ enum REPLError {
|
||||
#[error("Error parsing statement: {0}")]
|
||||
Parser(#[from] ParserError),
|
||||
#[error("JIT error: {0}")]
|
||||
JIT(#[from] JITError),
|
||||
JIT(#[from] BackendError),
|
||||
#[error("Internal cranelift error: {0}")]
|
||||
Cranelift(#[from] ModuleError),
|
||||
#[error(transparent)]
|
||||
Reporting(#[from] codespan_reporting::files::Error),
|
||||
}
|
||||
@@ -34,16 +38,17 @@ impl From<REPLError> for Diagnostic<usize> {
|
||||
match value {
|
||||
REPLError::Parser(err) => Diagnostic::from(&err),
|
||||
REPLError::JIT(err) => Diagnostic::from(err),
|
||||
REPLError::Cranelift(err) => Diagnostic::bug().with_message(format!("{}", err)),
|
||||
REPLError::Reporting(err) => Diagnostic::bug().with_message(format!("{}", err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> RunLoop<'a> {
|
||||
pub fn new(writer: &'a mut dyn WriteColor, config: Config) -> Result<Self, JITError> {
|
||||
pub fn new(writer: &'a mut dyn WriteColor, config: Config) -> Result<Self, BackendError> {
|
||||
Ok(RunLoop {
|
||||
file_database: SimpleFiles::new(),
|
||||
jitter: JITEngine::new()?,
|
||||
jitter: Backend::jit()?,
|
||||
variable_binding_sites: HashMap::new(),
|
||||
gensym_index: 1,
|
||||
writer,
|
||||
@@ -83,7 +88,7 @@ impl<'a> RunLoop<'a> {
|
||||
// then we won't use this definition until someone tries again.
|
||||
if let Statement::Binding(_, ref name, _) = syntax {
|
||||
if !self.variable_binding_sites.contains_key(name.as_str()) {
|
||||
self.jitter.define_string(name.clone())?;
|
||||
self.jitter.define_string(name)?;
|
||||
self.jitter.define_variable(name.clone())?;
|
||||
}
|
||||
};
|
||||
@@ -104,13 +109,17 @@ impl<'a> RunLoop<'a> {
|
||||
}
|
||||
|
||||
let ir = IR::from(syntax.simplify(&mut self.gensym_index));
|
||||
let compiled = self.jitter.compile(line_no, ir)?;
|
||||
compiled();
|
||||
let name = format!("line{}", line_no);
|
||||
let function_id = self.jitter.compile_function(&name, ir)?;
|
||||
self.jitter.module.finalize_definitions()?;
|
||||
let compiled_bytes = self.jitter.bytes(function_id);
|
||||
let compiled_function = unsafe { std::mem::transmute::<_, fn() -> ()>(compiled_bytes) };
|
||||
compiled_function();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), JITError> {
|
||||
fn main() -> Result<(), BackendError> {
|
||||
let mut editor = DefaultEditor::new().expect("rustyline works");
|
||||
let mut line_no = 0;
|
||||
let mut writer = StandardStream::stdout(ColorChoice::Auto);
|
||||
|
||||
Reference in New Issue
Block a user