pub mod dirs;
pub mod geo2d;
pub mod geo3d;
pub mod ops;
mod color;
mod debug;
mod import;
mod log;
mod math;
mod print;
pub use microcad_lang::builtin::{
Exporter, ExporterAccess, ExporterRegistry, Importer, ImporterRegistry, ModuleBuilder, Symbol,
};
use microcad_lang::{diag::*, eval::*, ty::Ty, value::*};
fn type_of() -> Symbol {
Symbol::new_builtin_fn(
"type_of",
[].into_iter(),
&|_, args, _| {
if let Ok(arg) = args.get_single() {
let ty = arg.1.ty();
return Ok(Value::String(ty.to_string()));
}
Ok(Value::None)
},
None,
)
}
fn count() -> Symbol {
Symbol::new_builtin_fn(
"count",
[].into_iter(),
&|_params, args, ctx| {
let arg = args.get_single()?;
Ok(match &arg.1.value {
Value::String(s) => Value::Integer(s.chars().count() as i64),
Value::Array(a) => Value::Integer(a.len() as i64),
_ => {
ctx.error(arg.1, EvalError::BuiltinError("Value has no count.".into()))?;
Value::None
}
})
},
None,
)
}
fn to_string() -> Symbol {
Symbol::new_builtin_fn(
"to_string",
[].into_iter(),
&|_, args, _| {
let (_, arg) = args.get_single()?;
Ok(Value::String(arg.value.to_string()))
},
None,
)
}
pub fn builtin_module() -> Symbol {
ModuleBuilder::new("__builtin")
.symbol(debug::debug())
.symbol(log::log())
.symbol(count())
.symbol(type_of())
.symbol(to_string())
.symbol(print::print())
.symbol(ops::ops())
.symbol(math::math())
.symbol(color::color())
.symbol(import::import())
.symbol(geo2d::geo2d())
.symbol(geo3d::geo3d())
.build()
}
pub fn builtin_importers() -> ImporterRegistry {
ImporterRegistry::default().insert(microcad_import::toml::TomlImporter)
}
pub fn builtin_exporters() -> ExporterRegistry {
use microcad_export::*;
ExporterRegistry::new()
.insert(svg::SvgExporter)
.insert(stl::StlExporter)
.insert(json::JsonExporter)
.insert(wkt::WktExporter)
}