Skip to content

Commit

Permalink
feat: Add general purpose debugging status
Browse files Browse the repository at this point in the history
  • Loading branch information
oooooba committed Oct 21, 2018
1 parent 6c79166 commit 9279778
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
26 changes: 26 additions & 0 deletions base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
use self::DebugLevel::*;
Ok(match s {
"none" => None,
"low" => Low,
"high" => High,
_ => return Err("Expected on of none, low, high"),
})
}
}
11 changes: 10 additions & 1 deletion repl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Item = (), Error = Box<StdError + Send + Sync + 'static>> {
let vm = ::gluon::VmBuilder::new().build();
vm.global_env().set_debug_level(debug_level);

let mut compiler = Compiler::new();
try_future!(
Expand Down
13 changes: 13 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -170,6 +171,9 @@ pub struct GlobalVmState {
// thread
#[cfg_attr(feature = "serde_derive", serde(state))]
pub generation_0_threads: RwLock<Vec<GcPtr<Thread>>>,

#[cfg_attr(feature = "serde_derive", serde(skip))]
debug_level: RwLock<DebugLevel>,
}

impl Traverseable for GlobalVmState {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 9279778

Please sign in to comment.