2019-01-20 16:22:32 -06:00
|
|
|
// The list of base types for a UCG value.
|
|
|
|
let base_types = [
|
|
|
|
"int",
|
|
|
|
"float",
|
|
|
|
"str",
|
|
|
|
"bool",
|
|
|
|
"null",
|
|
|
|
"tuple",
|
|
|
|
"list",
|
2019-01-24 20:04:40 -06:00
|
|
|
"func",
|
2019-01-20 16:22:32 -06:00
|
|
|
"module",
|
|
|
|
];
|
|
|
|
|
2019-01-24 20:04:40 -06:00
|
|
|
let base_type_reducer = func (acc, f) => select (acc.val is f), f, {
|
2019-01-20 16:22:32 -06:00
|
|
|
true = acc{typ = f},
|
|
|
|
false = acc,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Computes the base type of a value.
|
2019-01-31 16:33:12 -06:00
|
|
|
let base_type_of = func (val) => reduce(base_type_reducer, {val=val, typ="null"}, base_types).typ;
|
2019-01-20 16:22:32 -06:00
|
|
|
|
|
|
|
// Turns any schema check module into a compile failure.
|
|
|
|
// The module must export the computed value as the result field.
|
2019-01-24 20:04:40 -06:00
|
|
|
let must = func (m, msg) => select m.result, fail msg, {
|
2019-01-20 16:22:32 -06:00
|
|
|
true = m.result,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Any does a boolean match against a set of allowed shapes for a type.
|
|
|
|
// This module uses the shape module below so the same rules for checking
|
|
|
|
// the types against the source value apply for each example in the list.
|
|
|
|
let any = module {
|
|
|
|
// The source value to check.
|
2019-01-20 08:32:05 -06:00
|
|
|
val=NULL,
|
2019-01-20 16:22:32 -06:00
|
|
|
// The set of allowed type shapes it can be.
|
2019-01-20 08:32:05 -06:00
|
|
|
types=[],
|
|
|
|
} => {
|
2019-01-20 16:22:32 -06:00
|
|
|
let schema = import "std/schema.ucg";
|
|
|
|
|
2019-01-24 20:04:40 -06:00
|
|
|
let reducer = func (acc, t) => acc{
|
2019-01-20 16:22:32 -06:00
|
|
|
ok = acc.ok || (schema.shaped{val=acc.val, shape=t}.result),
|
2019-01-20 08:32:05 -06:00
|
|
|
};
|
2019-01-31 16:33:12 -06:00
|
|
|
let any = func (val, types) => reduce(reducer, {ok=false, val=val}, types);
|
2019-01-20 08:32:05 -06:00
|
|
|
|
|
|
|
let result = any(mod.val, mod.types).ok;
|
|
|
|
};
|
|
|
|
|
2019-01-20 16:22:32 -06:00
|
|
|
// Compares a value against an example schema value. compares the "shape" of the
|
|
|
|
// value to see if it matches. The base type must be the same as the base type
|
|
|
|
// of the shape. For tuples any field in the shape tuple must be present in the
|
|
|
|
// source value and must be of the same base type and shape. This module will
|
|
|
|
// recurse into nested tuples.
|
|
|
|
//
|
|
|
|
// Lists are assumed to be able to contain any type and can be any length.
|
2019-01-24 20:04:40 -06:00
|
|
|
// We do not check that functions or modules have the same argument lengths or types
|
2019-01-20 16:22:32 -06:00
|
|
|
// nor we check that they output the same types.
|
|
|
|
let shaped = module {
|
|
|
|
// The source value to validate
|
|
|
|
val = NULL,
|
|
|
|
|
|
|
|
// The shape to validate it against.
|
|
|
|
shape = NULL,
|
|
|
|
|
|
|
|
// Whether partial matches are accepted.
|
|
|
|
// When set to true then the source value can have
|
|
|
|
// fields in tuples that are not present in the
|
|
|
|
// shape it is compared with.
|
|
|
|
partial = true,
|
|
|
|
} => {
|
|
|
|
let schema = import "std/schema.ucg";
|
|
|
|
|
2019-01-24 20:04:40 -06:00
|
|
|
let simple_handler = func (val, shape) => val is (schema.base_type_of(shape));
|
2019-01-20 16:22:32 -06:00
|
|
|
|
2019-01-24 20:04:40 -06:00
|
|
|
let tuple_handler = func (acc, name, value) => acc{
|
2019-01-20 16:22:32 -06:00
|
|
|
ok = select (name) in acc.shape, mod.partial, {
|
|
|
|
true = schema.shaped{val=value, shape=acc.shape.(name)}.result,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let result =select schema.base_type_of(mod.val), false, {
|
|
|
|
str = simple_handler(mod.val, mod.shape),
|
|
|
|
int = simple_handler(mod.val, mod.shape),
|
|
|
|
float = simple_handler(mod.val, mod.shape),
|
|
|
|
bool = simple_handler(mod.val, mod.shape),
|
|
|
|
null = simple_handler(mod.val, mod.shape),
|
2019-01-31 16:33:12 -06:00
|
|
|
tuple = reduce(tuple_handler, {shape=mod.shape, ok=false}, (mod.val)).ok,
|
2019-01-20 16:22:32 -06:00
|
|
|
list = simple_handler(mod.val, mod.shape),
|
2019-01-24 20:04:40 -06:00
|
|
|
func = simple_handler(mod.val, mod.shape),
|
2019-01-20 16:22:32 -06:00
|
|
|
module = simple_handler(mod.val, mod.shape),
|
|
|
|
};
|
|
|
|
};
|