Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

feat: add debug option to cli #6

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
118 changes: 117 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.1.0"
lalrpop = "0.19.7"

[dependencies]
clap = { version = "4.1.8", features = ["derive"] }
im = "15.1.0"
lalrpop-util = {version = "0.19.7", features = ["lexer"]}
libc = "0.2.139"
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use clap::Parser;

#[derive(Parser, Debug, Default)]
#[command(author, version, about, long_about = None)]
pub struct Options {
#[arg(short, long)]
pub debug: bool,
}
20 changes: 16 additions & 4 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use llvm_sys::{
LLVMDiagnosticSeverity::{LLVMDSError, LLVMDSNote, LLVMDSRemark, LLVMDSWarning},
};

use crate::macros::{cstr, llvm_wrapper};
use crate::{
cli::Options,
macros::{cstr, llvm_wrapper},
};

pub use llvm_sys::{core::*, execution_engine::*, prelude::*};

Expand All @@ -30,6 +33,7 @@ pub struct Codegen {
pub environment: compile::Environment,
pub global_environment: *mut GlobalEnvironment,
pub global_sym: LLVMValueRef,
pub options: Box<crate::cli::Options>,
}

impl Drop for Codegen {
Expand Down Expand Up @@ -60,14 +64,16 @@ impl Codegen {
global_environment,
current_fn: std::ptr::null_mut(),
global_sym: std::ptr::null_mut(),
options: Box::default(),
}
}
}

pub fn install_error_handling(self) -> Self {
pub fn install_error_handling(mut self) -> Self {
unsafe {
// enable diagnostic messages
let diagnostic_context = LLVMContextGetDiagnosticContext(self.context);
let diagnostic_context = self.options.as_mut() as *mut _ as *mut c_void;
let handle_diagnostic = handle_diagnostic as extern "C" fn(_, *mut c_void);
let handle_fn: Option<extern "C" fn(_, _)> = Some(handle_diagnostic);
LLVMContextSetDiagnosticHandler(self.context, handle_fn, diagnostic_context);

Expand Down Expand Up @@ -112,8 +118,14 @@ impl Codegen {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn handle_diagnostic(info: LLVMDiagnosticInfoRef, _context: *mut c_void) {
pub extern "C" fn handle_diagnostic(info: LLVMDiagnosticInfoRef, context: *mut libc::c_void) {
let options = context as *mut Options;

unsafe {
if !(*options).debug {
return;
}

let kind = match LLVMGetDiagInfoSeverity(info) {
LLVMDSError => "error",
LLVMDSWarning => "warning",
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(box_syntax, box_patterns)]
#![feature(mem_copy_fn)]
pub mod cli;
pub mod closure;
pub mod codegen;
pub mod macros;
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use clap::Parser;
use soft::cli::Options;

fn main() {
soft::repl::run()
let options = Options::parse();

soft::repl::run(&options);
}
14 changes: 9 additions & 5 deletions src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ use crate::{
runtime::ValueRef,
};

pub fn run() {
pub fn run(options: &crate::cli::Options) {
Codegen::install_execution_targets();

let mut rl = DefaultEditor::new().expect("cannot create a repl");
let mut editor = DefaultEditor::new().expect("cannot create a repl");

let global_environment = Box::leak(Box::default());

loop {
let readline = rl.readline("> ");
let readline = editor.readline("> ");

match readline {
Ok(line) => {
rl.add_history_entry(line.as_str())
editor
.add_history_entry(line.as_str())
.expect("cannot add to the history");

let mut codegen = Codegen::new(global_environment)
.install_error_handling()
.install_primitives()
.install_global_environment();

if options.debug {
codegen.dump_module();
}

let engine = ExecutionEngine::try_new(codegen.module)
.unwrap()
.install_primitive_symbols(&codegen.environment)
Expand Down Expand Up @@ -79,7 +84,6 @@ fn eval_line(
.compile_main(converted)
.map_err(|err| format!("[error] Could not compile expression: {err:?}"))?;

codegen.dump_module();
codegen.verify_module().unwrap_or_else(|error| {
for line in error.split('\n') {
println!("[error*] {}", line);
Expand Down