From 927977849594b0daebf274df4bdaac55d93eb2d9 Mon Sep 17 00:00:00 2001 From: oooooba Date: Sun, 21 Oct 2018 14:41:06 +0900 Subject: [PATCH] feat: Add general purpose debugging status --- base/src/lib.rs | 26 ++++++++++++++++++++++++++ repl/src/main.rs | 11 ++++++++++- repl/src/repl.rs | 3 +++ vm/src/vm.rs | 13 +++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/base/src/lib.rs b/base/src/lib.rs index 56631611fe..2c0a7631ba 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -108,3 +108,29 @@ pub fn filename_to_module(filename: &str) -> String { name.replace(|c: char| c == '/' || c == '\\', ".") } + +#[derive(Debug, Clone)] +pub enum DebugLevel { + None, + Low, + High, +} + +impl Default for DebugLevel { + fn default() -> DebugLevel { + DebugLevel::None + } +} + +impl ::std::str::FromStr for DebugLevel { + type Err = &'static str; + fn from_str(s: &str) -> ::std::result::Result { + use self::DebugLevel::*; + Ok(match s { + "none" => None, + "low" => Low, + "high" => High, + _ => return Err("Expected on of none, low, high"), + }) + } +} diff --git a/repl/src/main.rs b/repl/src/main.rs index cf31aa3651..8bba5f0cae 100644 --- a/repl/src/main.rs +++ b/repl/src/main.rs @@ -160,6 +160,13 @@ pub struct Opt { )] prompt: String, + #[structopt( + long = "debug", + default_value = "none", + help = "Debug Level: none, low, high" + )] + debug_level: base::DebugLevel, + #[structopt( name = "FILE", help = "Executes each file as a gluon program" @@ -247,6 +254,7 @@ fn run( color: Color, vm: &Thread, ) -> std::result::Result<(), gluon::Error> { + vm.global_env().set_debug_level(opt.debug_level.clone()); match opt.subcommand_opt { Some(SubOpt::Fmt(ref fmt_opt)) => { if !fmt_opt.input.is_empty() { @@ -287,7 +295,8 @@ fn run( if opt.interactive { let mut runtime = Runtime::new()?; let prompt = opt.prompt.clone(); - runtime.block_on(future::lazy(move || repl::run(color, &prompt)))?; + let debug_level = opt.debug_level.clone(); + runtime.block_on(future::lazy(move || repl::run(color, &prompt, debug_level)))?; } else if !opt.input.is_empty() { run_files(compiler, &vm, &opt.input)?; } else { diff --git a/repl/src/repl.rs b/repl/src/repl.rs index 3b001c1a89..c1dde19769 100644 --- a/repl/src/repl.rs +++ b/repl/src/repl.rs @@ -19,6 +19,7 @@ use base::pos; use base::resolve; use base::symbol::{Symbol, SymbolModule}; use base::types::ArcType; +use base::DebugLevel; use parser::{parse_partial_repl_line, ReplLine}; use vm::api::de::De; use vm::api::generic::A; @@ -535,8 +536,10 @@ fn compile_repl(compiler: &mut Compiler, vm: &Thread) -> Result<(), GluonError> pub fn run( color: Color, prompt: &str, + debug_level: DebugLevel, ) -> impl Future> { let vm = ::gluon::VmBuilder::new().build(); + vm.global_env().set_debug_level(debug_level); let mut compiler = Compiler::new(); try_future!( diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 083360641e..b4899a4d81 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -13,6 +13,7 @@ use base::symbol::{Name, Symbol, SymbolRef}; use base::types::{ Alias, AliasData, AppVec, ArcType, Generic, PrimitiveEnv, Type, TypeCache, TypeEnv, }; +use base::DebugLevel; use api::{ValueRef, IO}; use compiler::{CompiledFunction, CompiledModule, CompilerEnv, Variable}; @@ -170,6 +171,9 @@ pub struct GlobalVmState { // thread #[cfg_attr(feature = "serde_derive", serde(state))] pub generation_0_threads: RwLock>>, + + #[cfg_attr(feature = "serde_derive", serde(skip))] + debug_level: RwLock, } impl Traverseable for GlobalVmState { @@ -409,6 +413,7 @@ impl GlobalVmStateBuilder { macros: MacroEnv::new(), type_cache: TypeCache::default(), generation_0_threads: RwLock::new(Vec::new()), + debug_level: RwLock::new(DebugLevel::default()), }; vm.add_types().unwrap(); vm @@ -583,4 +588,12 @@ impl GlobalVmState { pub fn get_env<'b>(&'b self) -> RwLockReadGuard<'b, VmEnv> { self.env.read().unwrap() } + + pub fn get_debug_level<'b>(&'b self) -> RwLockReadGuard<'b, DebugLevel> { + self.debug_level.read().unwrap() + } + + pub fn set_debug_level(&self, debug_level: DebugLevel) { + *self.debug_level.write().unwrap() = debug_level; + } }