create cells
This commit is contained in:
parent
4ac0bf317b
commit
f0a492fc20
144
src/main.rs
144
src/main.rs
|
@ -16,12 +16,16 @@ use nac3core::typecheck::{type_inferencer, typedef};
|
||||||
mod basic_symbol_resolver;
|
mod basic_symbol_resolver;
|
||||||
use basic_symbol_resolver::{Resolver, ResolverInternal};
|
use basic_symbol_resolver::{Resolver, ResolverInternal};
|
||||||
|
|
||||||
fn run(code: &String) {
|
type RunFn = unsafe extern "C" fn() -> i32;
|
||||||
|
|
||||||
|
fn compile<'a>(
|
||||||
|
context: &'a inkwell::context::Context,
|
||||||
|
code: &String,
|
||||||
|
) -> Result<inkwell::execution_engine::JitFunction<'a, RunFn>, String> {
|
||||||
let llvm_options = codegen::CodeGenLLVMOptions {
|
let llvm_options = codegen::CodeGenLLVMOptions {
|
||||||
opt_level: inkwell::OptimizationLevel::Default,
|
opt_level: inkwell::OptimizationLevel::Default,
|
||||||
target: codegen::CodeGenTargetMachineOptions::from_host(),
|
target: codegen::CodeGenTargetMachineOptions::from_host(),
|
||||||
};
|
};
|
||||||
let context = inkwell::context::Context::create();
|
|
||||||
let size_t = context
|
let size_t = context
|
||||||
.ptr_sized_int_type(
|
.ptr_sized_int_type(
|
||||||
&llvm_options
|
&llvm_options
|
||||||
|
@ -55,8 +59,7 @@ fn run(code: &String) {
|
||||||
match nac3parser::parser::parse_program(code.as_str(), String::from("cell1").into()) {
|
match nac3parser::parser::parse_program(code.as_str(), String::from("cell1").into()) {
|
||||||
Ok(parser_result) => parser_result,
|
Ok(parser_result) => parser_result,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("parse error: {}", err);
|
return Err(format!("parse error: {}", err));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for stmt in parser_result {
|
for stmt in parser_result {
|
||||||
|
@ -68,8 +71,7 @@ fn run(code: &String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("composer error: {}", err);
|
return Err(format!("composer error: {}", err));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,15 +88,15 @@ fn run(code: &String) {
|
||||||
|
|
||||||
if let Err(errors) = composer.start_analysis(true) {
|
if let Err(errors) = composer.start_analysis(true) {
|
||||||
let error_count = errors.len();
|
let error_count = errors.len();
|
||||||
eprintln!("{error_count} error(s) occurred during top level analysis.");
|
let mut msg = format!("{error_count} error(s) occurred during top level analysis.");
|
||||||
|
|
||||||
for (error_i, error) in errors.iter().enumerate() {
|
for (error_i, error) in errors.iter().enumerate() {
|
||||||
let error_num = error_i + 1;
|
let error_num = error_i + 1;
|
||||||
eprintln!("=========== ERROR {error_num}/{error_count} ============");
|
msg.push_str(&format!(
|
||||||
eprintln!("{error}");
|
"\n=========== ERROR {error_num}/{error_count} ============\n{error}"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
eprintln!("==================================");
|
return Err(msg);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let top_level = Arc::new(composer.make_top_level_context());
|
let top_level = Arc::new(composer.make_top_level_context());
|
||||||
|
@ -102,8 +104,7 @@ fn run(code: &String) {
|
||||||
let run_id_def = match resolver.get_identifier_def("run".into()) {
|
let run_id_def = match resolver.get_identifier_def("run".into()) {
|
||||||
Ok(run_id_def) => run_id_def,
|
Ok(run_id_def) => run_id_def,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("no run() entry point");
|
return Err(format!("no run() entry point"));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let instance = {
|
let instance = {
|
||||||
|
@ -182,29 +183,54 @@ fn run(code: &String) {
|
||||||
let execution_engine = main
|
let execution_engine = main
|
||||||
.create_jit_execution_engine(llvm_options.opt_level)
|
.create_jit_execution_engine(llvm_options.opt_level)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
type Run = unsafe extern "C" fn() -> i32;
|
return Ok(unsafe { execution_engine.get_function("run").unwrap() });
|
||||||
let run: inkwell::execution_engine::JitFunction<Run> =
|
|
||||||
unsafe { execution_engine.get_function("run").unwrap() };
|
|
||||||
println!("{}", unsafe { run.call() });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> eframe::Result {
|
struct Cell<'a> {
|
||||||
inkwell::targets::Target::initialize_all(&inkwell::targets::InitializationConfig::default());
|
code: String,
|
||||||
|
context: inkwell::context::Context,
|
||||||
|
result: Result<inkwell::execution_engine::JitFunction<'a, RunFn>, String>,
|
||||||
|
}
|
||||||
|
|
||||||
let options = eframe::NativeOptions {
|
impl<'a> Cell<'a> {
|
||||||
viewport: egui::ViewportBuilder::default().with_inner_size([1024.0, 768.0]),
|
fn new() -> Self {
|
||||||
..Default::default()
|
Self {
|
||||||
};
|
code: "".to_string(),
|
||||||
|
context: inkwell::context::Context::create(),
|
||||||
let mut code = String::new();
|
result: Err("".to_string()),
|
||||||
|
|
||||||
eframe::run_simple_native("Cells", options, move |ctx, _frame| {
|
|
||||||
let submit_key = egui::KeyboardShortcut::new(egui::Modifiers::CTRL, egui::Key::Enter);
|
|
||||||
if ctx.input_mut(|i| i.consume_shortcut(&submit_key)) {
|
|
||||||
run(&code);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
fn update(&mut self) {
|
||||||
|
self.result = Err("borrow checker problems".to_string()) //compile(&self.context, &self.code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cells<'a> {
|
||||||
|
cells: Vec<Cell<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cells<'_> {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
cells: vec![Cell::new()],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self) {
|
||||||
|
for cell in self.cells.iter_mut() {
|
||||||
|
cell.update()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_last(&mut self) {
|
||||||
|
if self.cells.last().unwrap().result.is_ok() {
|
||||||
|
self.cells.push(Cell::new());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||||
|
for (cell_i, cell) in self.cells.iter_mut().enumerate() {
|
||||||
let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx());
|
let theme = egui_extras::syntax_highlighting::CodeTheme::from_memory(ui.ctx());
|
||||||
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
|
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
|
||||||
let mut layout_job =
|
let mut layout_job =
|
||||||
|
@ -221,15 +247,53 @@ fn main() -> eframe::Result {
|
||||||
.collect();
|
.collect();
|
||||||
ui.fonts(|f| f.layout_job(layout_job))
|
ui.fonts(|f| f.layout_job(layout_job))
|
||||||
};
|
};
|
||||||
ui.add(
|
ui.group(|ui| {
|
||||||
egui::TextEdit::multiline(&mut code)
|
ui.horizontal(|ui| {
|
||||||
.code_editor()
|
ui.label(format!("[{}]", cell_i));
|
||||||
.desired_rows(4)
|
ui.add(
|
||||||
.lock_focus(true)
|
egui::TextEdit::multiline(&mut cell.code)
|
||||||
.desired_width(f32::INFINITY)
|
.code_editor()
|
||||||
.layouter(&mut layouter)
|
.desired_rows(4)
|
||||||
.font(egui::FontId::monospace(16.0)),
|
.lock_focus(true)
|
||||||
);
|
.desired_width(f32::INFINITY)
|
||||||
|
.layouter(&mut layouter)
|
||||||
|
.font(egui::FontId::monospace(16.0)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
match &cell.result {
|
||||||
|
Ok(run_fn) => {
|
||||||
|
ui.label(format!("{}", unsafe { run_fn.call() }));
|
||||||
|
}
|
||||||
|
Err(msg) => {
|
||||||
|
if !msg.is_empty() {
|
||||||
|
ui.colored_label(egui::Color32::from_rgb(255, 0, 0), msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> eframe::Result {
|
||||||
|
inkwell::targets::Target::initialize_all(&inkwell::targets::InitializationConfig::default());
|
||||||
|
|
||||||
|
let options = eframe::NativeOptions {
|
||||||
|
viewport: egui::ViewportBuilder::default().with_inner_size([1024.0, 768.0]),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut cells = Cells::new();
|
||||||
|
|
||||||
|
eframe::run_simple_native("Cells", options, move |ctx, _frame| {
|
||||||
|
let submit_key = egui::KeyboardShortcut::new(egui::Modifiers::CTRL, egui::Key::Enter);
|
||||||
|
if ctx.input_mut(|input| input.consume_shortcut(&submit_key)) {
|
||||||
|
cells.update();
|
||||||
|
cells.ensure_last();
|
||||||
|
}
|
||||||
|
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
cells.ui(ui);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue