Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WIP] Add proper Repl #488

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/bin/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use saltwater::{
PreProcessorBuilder, PureAnalyzer, SyntaxError, Type, JIT,
};
use std::{collections::HashMap, path::PathBuf};
use types::ArrayType;

mod commands;
mod helper;
Expand All @@ -21,6 +22,15 @@ const PREFIX: char = ':';
const VERSION: &str = env!("CARGO_PKG_VERSION");
const PROMPT: &str = ">> ";

macro_rules! execute {
($fun:ident, $ty:path, $action:expr) => {
$action(unsafe {
let execute: unsafe extern "C" fn() -> $ty = std::mem::transmute($fun);
execute()
});
};
}
Comment on lines +25 to +32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used like

execute!(fun, bool, |b| println!("=> {}", b)),

I think it would be easier and simpler to do println!("=> {}", execute!(fun, bool)), which means you don't need $action or to create a new closure.


pub struct Repl {
editor: Editor<ReplHelper>,
commands: HashMap<&'static str, fn(&mut Repl, &str)>,
Expand Down Expand Up @@ -114,26 +124,37 @@ impl Repl {
let expr = analyze_expr(code)?;
let expr_ty = expr.ctype.clone();
let decl = wrap_expr(expr);
// FIXME: `ir` module is currently private.
let module = ir::compile(module, vec![decl], false).0?;

let mut jit = JIT::from(module);
jit.finalize();
let execute_fun = jit
let fun = jit
.get_compiled_function("execute")
.expect("this is not good.");

match expr_ty {
Type::Long(signed) => {
let result = unsafe {
let execute: unsafe extern "C" fn() -> u64 = std::mem::transmute(execute_fun);
execute()
};
match signed {
true => println!("=> {}", result as i64),
false => println!("=> {}", result),
}
}
Type::Short(signed) => execute!(fun, i16, |x| match signed {
true => println!("=> {}", x),
false => println!("=> {}", x as u16),
}),
Type::Int(signed) => execute!(fun, i32, |x| match signed {
true => println!("=> {}", x),
false => println!("=> {}", x as u32),
}),
Type::Long(signed) => execute!(fun, i64, |x| match signed {
true => println!("=> {}", x),
false => println!("=> {}", x as u64),
}),
Type::Float => execute!(fun, f32, |f| println!("=> {}", f)),
Type::Double => execute!(fun, f64, |f| println!("=> {}", f)),

Type::Char(_) => execute!(fun, char, |c| println!("=> {}", c)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look quite right ... in Rust, char is a unicode scalar character which is represented as 4 bytes. In C, it's an arbitrary byte, which could be invalid utf8 (0xff).

Suggested change
Type::Char(_) => execute!(fun, char, |c| println!("=> {}", c)),
Type::Char(_) => execute!(fun, u8, |c| println!("=> {}", if c.is_ascii() { c as char } else { `hex::encode(&[c]) })),

https://docs.rs/hex/0.4.2/hex/fn.encode.html

Type::Bool => execute!(fun, bool, |b| println!("=> {}", b)),
Type::Void => unsafe {
let execute: unsafe extern "C" fn() = std::mem::transmute(fun);
execute()
},
Comment on lines +153 to +156
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Type::Void => unsafe {
let execute: unsafe extern "C" fn() = std::mem::transmute(fun);
execute()
},
Type::Void => execute!(fun, (), |_| {}),


// TODO: Implement execution for more types
ty => println!("error: expression returns unsupported type: {:?}", ty),
};
Expand Down
2 changes: 1 addition & 1 deletion src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct Compiler<T: Backend> {
}

/// Compile a program from a high level IR to a Cranelift Module
pub(crate) fn compile<B: Backend>(
pub fn compile<B: Backend>(
module: Module<B>,
program: Vec<Locatable<Declaration>>,
debug: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub mod data;
mod fold;
pub mod intern;
#[cfg(feature = "codegen")]
mod ir;
pub mod ir;
mod lex;
mod parse;

Expand Down