Skip to content

Commit

Permalink
Remove debug output, make help() take identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed May 2, 2022
1 parent 91050f8 commit 730673b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ categories = ["parser-implementations", "development-tools", "command-line-utili
homepage = "https://rscarson.github.io/Lavendeux/"
repository = "https://github.com/rscarson/lavendeux-parser"
readme = "readme.md"
version = "0.6.0"
version = "0.6.1"
edition = "2021"

[features]
Expand Down
3 changes: 0 additions & 3 deletions src/functions/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,16 @@ mod test_builtin_table {

for _ in 0..30 {
result = (RAND.handler)(&RAND, &[]).unwrap();
println!("{}", result);
assert_eq!(true, result.as_float().unwrap() >= 0.0 && result.as_float().unwrap() <= 1.0);
}

for _ in 0..30 {
result = (RAND.handler)(&RAND, &[Value::Integer(5)]).unwrap();
println!("{}", result);
assert_eq!(true, result.as_int().unwrap() >= 0 && result.as_int().unwrap() <= 5);
}

for _ in 0..30 {
result = (RAND.handler)(&RAND, &[Value::Integer(5), Value::Integer(10)]).unwrap();
println!("{}", result);
assert_eq!(true, result.as_int().unwrap() >= 5 && result.as_int().unwrap() <= 10);
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/handlers/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ pub fn call_expression_handler(token: &mut Token, state: &mut ParserState) -> Op
if token.rule() == Rule::call_expression {
// Get function name and arguments
let name = token.child(0).unwrap().text();
let mut arg_tokens = Vec::<&Token>::new();

let mut args : Vec<Value> = Vec::new();
match token.child(2).unwrap().rule() {
Rule::rparen => { },
Rule::expression_list => {
let mut i = 0;
while i < token.child(2).unwrap().children().len() {
args.push(token.child(2).unwrap().child(i).unwrap().value());
let t = token.child(2).unwrap().child(i).unwrap();
args.push(t.value());
arg_tokens.push(t);
i += 2;
}
},
_ => args.push(token.child(2).unwrap().value())
_ => {
let t = token.child(2).unwrap();
args.push(t.value());
arg_tokens.push(t);
}
}

// Help
if name == "help" {
if args.len() == 1 {
let target_name = args[0].to_string();
let target_name = if arg_tokens[0].value().is_none() { arg_tokens[0].text().to_string() } else { arg_tokens[0].value().to_string() };

// Builtin functions
if let Some(f) = state.functions.get(&target_name) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
//! }
//! ```
//! Extensions give a more flexible way of adding functionality at runtime. Extensions are written in javascript.
#![doc(html_root_url = "https://docs.rs/lavendeux-parser/0.6.0")]
#![doc(html_root_url = "https://docs.rs/lavendeux-parser/0.6.1")]
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]

Expand Down
7 changes: 5 additions & 2 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ impl Token {
children: Vec::new()
};

println!("{}", token.text);

if token.rule == Rule::ternary_expression && children.len() > 1 {
// Ternary expression handler - enables short-circuit interpretation
let condition = Self::from_pair(children[0].clone(), handler, state)?;
Expand Down Expand Up @@ -523,9 +521,14 @@ mod test_token {

assert_eq!(true, t.text.contains("User-defined Functions"));
token_does_text_equal("help('strlen')", "strlen(s): Returns the length of the string s", &mut state);
token_does_text_equal("help(strlen)", "strlen(s): Returns the length of the string s", &mut state);
token_does_text_equal("help('fn')", "fn(x, y)", &mut state);
token_does_text_equal("help(fn)", "fn(x, y)", &mut state);

#[cfg(feature = "extensions")]
token_does_text_equal("help('test2')", "test2(...)", &mut state);

#[cfg(feature = "extensions")]
token_does_text_equal("help(test2)", "test2(...)", &mut state);
}
}
5 changes: 5 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ impl Value {
pub fn is_string(&self) -> bool {
matches!(self, Value::String(_))
}

/// Determine if the value is empty
pub fn is_none(&self) -> bool {
matches!(self, Value::None)
}
}

impl Clone for Value {
Expand Down

0 comments on commit 730673b

Please sign in to comment.