This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
[WIP] Add proper Repl #488
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aade798
Start to implement REPL
Stupremee ec8ec36
Command completion works
Stupremee 53ac521
Cleanup some code
Stupremee bfe1441
Fix broken r2d2 scream path
Stupremee 33619bb
Code execution in repl works now
Stupremee 72093ed
Cleanup some code
Stupremee 3f950e8
Add more number types to repl and make ir public
Stupremee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ use saltwater::{ | |||||||||||
PreProcessorBuilder, PureAnalyzer, SyntaxError, Type, JIT, | ||||||||||||
}; | ||||||||||||
use std::{collections::HashMap, path::PathBuf}; | ||||||||||||
use types::ArrayType; | ||||||||||||
|
||||||||||||
mod commands; | ||||||||||||
mod helper; | ||||||||||||
|
@@ -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() | ||||||||||||
}); | ||||||||||||
}; | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub struct Repl { | ||||||||||||
editor: Editor<ReplHelper>, | ||||||||||||
commands: HashMap<&'static str, fn(&mut Repl, &str)>, | ||||||||||||
|
@@ -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)), | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look quite right ... in Rust,
Suggested change
|
||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
// TODO: Implement execution for more types | ||||||||||||
ty => println!("error: expression returns unsupported type: {:?}", ty), | ||||||||||||
}; | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used like
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.