fixed a few bugs

This commit is contained in:
Snorre Ettrup Altschul 2025-02-24 03:09:04 +01:00
parent 2c38fb391b
commit bb1b051598
3 changed files with 23 additions and 2 deletions

View file

@ -9,8 +9,10 @@ use libopenbirch::node::closure::Closure;
use libopenbirch::node::constant::Constant;
use libopenbirch::node::empty::Empty;
use libopenbirch::node::set::Set;
use libopenbirch::node::string_node::StringNode;
use libopenbirch::node::{Node, NodeEnum};
use libopenbirch::parser::{Lexer, LexerError, Parser, ParserError};
use raylib::ease::elastic_in;
#[cfg(feature = "async")]
use termion::AsyncReader;
use termion::color;
@ -362,6 +364,24 @@ fn length(args: &Vec<Rc<NodeEnum>>, env: &mut Environment) -> Result<Rc<NodeEnum
))
}
fn benchmark(args: &Vec<Rc<NodeEnum>>, env: &mut Environment) -> Result<Rc<NodeEnum>, String> {
if args.len() != 1 {
Err(format!("Expected 1 argument but got {}", args.len()))?
}
use std::time::Instant;
let now = Instant::now();
let result = Call::new(args.first().unwrap().clone(), vec![]).evaluate(env)?;
let elapsed = now.elapsed();
let time_str = StringNode::new(elapsed.as_secs_f64().to_string()).into();
let time_const = Rc::new(Constant::new_from_float(elapsed.as_secs_f64(), &env).into());
Ok(Set::new(vec![time_str, time_const, result]))
}
fn main() -> Result<(), io::Error> {
let mut input = Input::new();
@ -376,6 +396,7 @@ fn main() -> Result<(), io::Error> {
env.define_native_function("map", map);
env.define_native_function("get", get);
env.define_native_function("length", length);
env.define_native_function("benchmark", benchmark);
env.define(
"pi",

View file

@ -24,7 +24,7 @@ impl Node for Constant {
fn as_string(&self, _env: Option<&Environment>) -> String {
if self.value.is_zero() {
"0".to_string();
return "0".to_string();
}
self.value

View file

@ -102,7 +102,7 @@ impl Node for IfElse {
.reduce(|a, b| a + "\n" + &b)
.unwrap()
+ "end",
ElseBranchEnum::None => "end".to_owned(),
ElseBranchEnum::None => " end".to_owned(),
}
.as_str()
)