debugging::print builtin

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-03-04 04:34:10 -08:00
committed by Anand Krishnamoorthi
parent c5ea737200
commit ec695bd22e
3 changed files with 61 additions and 6 deletions

31
src/builtins/debugging.rs Normal file
View File

@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::ast::Expr;
use crate::builtins;
use crate::lexer::Span;
use crate::value::Value;
use std::collections::HashMap;
use anyhow::Result;
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
m.insert("print", print);
}
// Symbol analyzer must ensure that vars used by print are defined before
// the print statement. Scheduler must ensure the above constraint.
// Additionally interpreter must allow undefined inputs to print.
fn print(span: &Span, _params: &[Expr], args: &[Value]) -> Result<Value> {
let mut msg = String::default();
for a in args {
match a {
Value::Undefined => msg += "<undefined>",
_ => msg += format!("{a}").as_str(),
};
}
span.message("print", msg.as_str());
Ok(Value::Bool(true))
}

View File

@@ -1,14 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
pub mod aggregates;
pub mod arrays;
mod aggregates;
mod arrays;
pub mod comparison;
mod debugging;
pub mod numbers;
pub mod sets;
pub mod strings;
mod strings;
pub mod types;
pub mod utils;
mod utils;
use crate::ast::Expr;
use crate::lexer::Span;
@@ -31,8 +32,30 @@ lazy_static! {
aggregates::register(&mut m);
arrays::register(&mut m);
sets::register(&mut m);
types::register(&mut m);
//objects::register(&mut m);
strings::register(&mut m);
//regex::register(&mut m);
//glob::register(&mut m);
//bitwise::register(&mut m);
//conversions::register(&mut m);
//units::register(&mut m);
types::register(&mut m);
//encoding::register(&mut m);
//token_signing::register(&mut m);
//token_verification::register(&mut m);
//time::register(&mut m);
//cryptography::register(&mut m);
//graphs::register(&mut m);
//graphql::register(&mut m);
//http::register(&mut m);
//cryptography::register(&mut m);
//net::register(&mut m);
//uuid::register(&mut m);
//semantic_versions::register(&mut m);
//rego::register(&mut m);
//opa::register(&mut m);
debugging::register(&mut m);
//tracing::register(&mut m);
m
};

View File

@@ -1187,10 +1187,11 @@ impl<'source> Interpreter<'source> {
params: &'source Vec<Expr<'source>>,
) -> Result<Value> {
let mut args = vec![];
let allow_undefined = name == "print"; // TODO: with modifier
for p in params {
match self.eval_expr(p)? {
// If any argument is undefined, then the call is undefined.
Value::Undefined => return Ok(Value::Undefined),
Value::Undefined if !allow_undefined => return Ok(Value::Undefined),
p => args.push(p),
}
}