mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Arity for builtins (#28)
Old-style function call Utility for running opa yaml tests Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
951bb6b14c
commit
800e594d52
@@ -12,12 +12,12 @@ use std::collections::HashMap;
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("count", count);
|
||||
m.insert("max", max);
|
||||
m.insert("min", min);
|
||||
m.insert("product", product);
|
||||
m.insert("sort", sort);
|
||||
m.insert("sum", sum);
|
||||
m.insert("count", (count, 1));
|
||||
m.insert("max", (max, 1));
|
||||
m.insert("min", (min, 1));
|
||||
m.insert("product", (product, 1));
|
||||
m.insert("sort", (sort, 1));
|
||||
m.insert("sum", (sum, 1));
|
||||
}
|
||||
|
||||
fn count(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
@@ -57,9 +57,9 @@ fn min(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
Ok(match &args[0] {
|
||||
Value::Array(a) if a.is_empty() => Value::Undefined,
|
||||
Value::Array(a) => a.iter().max().unwrap().clone(),
|
||||
Value::Array(a) => a.iter().min().unwrap().clone(),
|
||||
Value::Set(a) if a.is_empty() => Value::Undefined,
|
||||
Value::Set(a) => a.iter().max().unwrap().clone(),
|
||||
Value::Set(a) => a.iter().min().unwrap().clone(),
|
||||
a => {
|
||||
let span = params[0].span();
|
||||
bail!(span.error(format!("`min` requires array/set argument. Got `{a}`.").as_str()))
|
||||
@@ -68,7 +68,7 @@ fn min(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn product(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
ensure_args_count(span, "min", params, args, 1)?;
|
||||
ensure_args_count(span, "product", params, args, 1)?;
|
||||
|
||||
let mut v = 1 as Float;
|
||||
Ok(match &args[0] {
|
||||
|
||||
@@ -13,9 +13,9 @@ use anyhow::Result;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("array.concat", concat);
|
||||
m.insert("array.reverse", reverse);
|
||||
m.insert("array.slice", slice);
|
||||
m.insert("array.concat", (concat, 2));
|
||||
m.insert("array.reverse", (reverse, 1));
|
||||
m.insert("array.slice", (slice, 3));
|
||||
}
|
||||
|
||||
fn concat(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
@@ -13,12 +13,12 @@ use std::collections::HashMap;
|
||||
use anyhow::Result;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("bits.and", and);
|
||||
m.insert("bits.lsh", lsh);
|
||||
m.insert("bits.negate", negate);
|
||||
m.insert("bits.or", or);
|
||||
m.insert("bits.rsh", rsh);
|
||||
m.insert("bits.xor", xor);
|
||||
m.insert("bits.and", (and, 2));
|
||||
m.insert("bits.lsh", (lsh, 2));
|
||||
m.insert("bits.negate", (negate, 1));
|
||||
m.insert("bits.or", (or, 2));
|
||||
m.insert("bits.rsh", (rsh, 2));
|
||||
m.insert("bits.xor", (xor, 2));
|
||||
}
|
||||
|
||||
fn and(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
@@ -12,7 +12,7 @@ use std::collections::HashMap;
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("to_number", to_number);
|
||||
m.insert("to_number", (to_number, 1));
|
||||
}
|
||||
|
||||
fn to_number(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
@@ -8,16 +8,23 @@ use crate::value::Value;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
// TODO: Should we avoid this limit?
|
||||
const MAX_ARGS: u8 = std::u8::MAX;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("print", print);
|
||||
m.insert("print", (print, MAX_ARGS));
|
||||
}
|
||||
|
||||
// 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> {
|
||||
if args.len() > MAX_ARGS as usize {
|
||||
bail!(span.error("print supports up to 100 arguments"));
|
||||
}
|
||||
|
||||
let mut msg = String::default();
|
||||
for a in args {
|
||||
match a {
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ use std::collections::HashMap;
|
||||
use anyhow::Result;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
pub type BuiltinFcn = fn(&Span, &[Expr], &[Value]) -> Result<Value>;
|
||||
pub type BuiltinFcn = (fn(&Span, &[Expr], &[Value]) -> Result<Value>, u8);
|
||||
|
||||
#[rustfmt::skip]
|
||||
lazy_static! {
|
||||
|
||||
@@ -13,12 +13,12 @@ use anyhow::Result;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("abs", abs);
|
||||
m.insert("ceil", ceil);
|
||||
m.insert("floor", floor);
|
||||
m.insert("numbers.range", range);
|
||||
m.insert("rand.intn", intn);
|
||||
m.insert("round", round);
|
||||
m.insert("abs", (abs, 1));
|
||||
m.insert("ceil", (ceil, 1));
|
||||
m.insert("floor", (floor, 1));
|
||||
m.insert("numbers.range", (range, 2));
|
||||
m.insert("rand.intn", (intn, 2));
|
||||
m.insert("round", (round, 1));
|
||||
}
|
||||
|
||||
pub fn arithmetic_operation(
|
||||
|
||||
@@ -14,12 +14,12 @@ use std::rc::Rc;
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("json.filter", json_filter);
|
||||
// m.insert("json.patch", json_patch);
|
||||
m.insert("object.filter", filter);
|
||||
m.insert("object.get", get);
|
||||
m.insert("object.keys", keys);
|
||||
m.insert("object.remove", remove);
|
||||
m.insert("json.filter", (json_filter, 2));
|
||||
// m.insert("json.patch", (json_patch));
|
||||
m.insert("object.filter", (filter, 2));
|
||||
m.insert("object.get", (get, 3));
|
||||
m.insert("object.keys", (keys, 1));
|
||||
m.insert("object.remove", (remove, 2));
|
||||
}
|
||||
|
||||
fn json_filter_impl(v: &Value, filter: &Value) -> Value {
|
||||
|
||||
@@ -12,8 +12,8 @@ use std::collections::{BTreeSet, HashMap};
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("intersection", intersection_of_set_of_sets);
|
||||
m.insert("union", union_of_set_of_sets);
|
||||
m.insert("intersection", (intersection_of_set_of_sets, 1));
|
||||
m.insert("union", (union_of_set_of_sets, 1));
|
||||
}
|
||||
|
||||
pub fn intersection(expr1: &Expr, expr2: &Expr, v1: Value, v2: Value) -> Result<Value> {
|
||||
|
||||
+23
-23
@@ -15,29 +15,29 @@ use std::collections::HashMap;
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("concat", concat);
|
||||
m.insert("contains", contains);
|
||||
m.insert("endswith", endswith);
|
||||
m.insert("format_int", format_int);
|
||||
m.insert("indexof", indexof);
|
||||
m.insert("indexof_n", indexof_n);
|
||||
m.insert("lower", lower);
|
||||
m.insert("replace", replace);
|
||||
m.insert("split", split);
|
||||
m.insert("sprintf", sprintf);
|
||||
m.insert("startswith", startswith);
|
||||
m.insert("strings.any_prefix_match", any_prefix_match);
|
||||
m.insert("strings.any_suffix_match", any_suffix_match);
|
||||
m.insert("strings.replace_n", replace_n);
|
||||
m.insert("strings.reverse", reverse);
|
||||
m.insert("strings.substring", substring);
|
||||
m.insert("trim", trim);
|
||||
m.insert("trim_left", trim_left);
|
||||
m.insert("trim_prefix", trim_prefix);
|
||||
m.insert("trim_right", trim_right);
|
||||
m.insert("trim_space", trim_space);
|
||||
m.insert("trim_suffix", trim_suffix);
|
||||
m.insert("upper", upper);
|
||||
m.insert("concat", (concat, 2));
|
||||
m.insert("contains", (contains, 2));
|
||||
m.insert("endswith", (endswith, 2));
|
||||
m.insert("format_int", (format_int, 2));
|
||||
m.insert("indexof", (indexof, 2));
|
||||
m.insert("indexof_n", (indexof_n, 2));
|
||||
m.insert("lower", (lower, 1));
|
||||
m.insert("replace", (replace, 3));
|
||||
m.insert("split", (split, 2));
|
||||
m.insert("sprintf", (sprintf, 2));
|
||||
m.insert("startswith", (startswith, 2));
|
||||
m.insert("strings.any_prefix_match", (any_prefix_match, 2));
|
||||
m.insert("strings.any_suffix_match", (any_suffix_match, 2));
|
||||
m.insert("strings.replace_n", (replace_n, 2));
|
||||
m.insert("strings.reverse", (reverse, 1));
|
||||
m.insert("strings.substring", (substring, 3));
|
||||
m.insert("trim", (trim, 2));
|
||||
m.insert("trim_left", (trim_left, 2));
|
||||
m.insert("trim_prefix", (trim_prefix, 2));
|
||||
m.insert("trim_right", (trim_right, 2));
|
||||
m.insert("trim_space", (trim_space, 1));
|
||||
m.insert("trim_suffix", (trim_suffix, 2));
|
||||
m.insert("upper", (upper, 1));
|
||||
}
|
||||
|
||||
fn concat(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
@@ -12,7 +12,7 @@ use std::collections::HashMap;
|
||||
use anyhow::Result;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("trace", trace);
|
||||
m.insert("trace", (trace, 1));
|
||||
}
|
||||
|
||||
// Symbol analyzer must ensure that vars used by trace are defined before
|
||||
|
||||
@@ -12,14 +12,14 @@ use std::collections::HashMap;
|
||||
use anyhow::Result;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("is_array", is_array);
|
||||
m.insert("is_boolean", is_boolean);
|
||||
m.insert("is_null", is_null);
|
||||
m.insert("is_number", is_number);
|
||||
m.insert("is_object", is_object);
|
||||
m.insert("is_set", is_set);
|
||||
m.insert("is_string", is_string);
|
||||
m.insert("type_name", type_name);
|
||||
m.insert("is_array", (is_array, 1));
|
||||
m.insert("is_boolean", (is_boolean, 1));
|
||||
m.insert("is_null", (is_null, 1));
|
||||
m.insert("is_number", (is_number, 1));
|
||||
m.insert("is_object", (is_object, 1));
|
||||
m.insert("is_set", (is_set, 1));
|
||||
m.insert("is_string", (is_string, 1));
|
||||
m.insert("type_name", (type_name, 1));
|
||||
}
|
||||
|
||||
fn is_array(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
|
||||
Reference in New Issue
Block a user