forked from M-Labs/nac3
1
0
Fork 0
This commit is contained in:
David Mak 2024-08-09 11:57:17 +08:00
parent 3cce07fafc
commit 27beee17e7
2 changed files with 42 additions and 13 deletions

View File

@ -24,6 +24,7 @@ use pyo3::{
use crate::{symbol_resolver::InnerResolver, timeline::TimeFns};
use inkwell::values::PointerValue;
use itertools::Itertools;
use nac3core::codegen::classes::{ListValue, NDArrayValue, RangeValue, UntypedArrayLikeAccessor};
use nac3core::codegen::expr::destructure_range;
@ -763,7 +764,11 @@ fn polymorphic_print<'ctx>(
let fmt = ctx.gen_string(generator, fmt);
ctx.builder
.build_call(print_fn, &once(fmt).chain(args).map(|arg| arg.into()).collect_vec(), "")
.build_call(
print_fn,
&once(fmt).chain(args).map(BasicValueEnum::into).collect_vec(),
"",
)
.unwrap();
};
@ -1081,7 +1086,7 @@ pub fn call_core_log_impl<'ctx>(
) -> Result<(), String> {
let (arg_ty, arg_val) = arg;
polymorphic_print(ctx, generator, &[(arg_ty, arg_val.into())], "", None, false, false);
polymorphic_print(ctx, generator, &[(arg_ty, arg_val.into())], "", None, false, false)?;
Ok(())
}
@ -1090,15 +1095,26 @@ pub fn call_core_log_impl<'ctx>(
pub fn call_rtio_log_impl<'ctx>(
ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator,
channel: PointerValue<'ctx>,
arg: (Type, BasicValueEnum<'ctx>),
) -> Result<(), String> {
let (arg_ty, arg_val) = arg;
polymorphic_print(ctx, generator, &[(arg_ty, arg_val.into())], "", None, false, true);
polymorphic_print(
ctx,
generator,
&[(ctx.primitives.str, channel.into())],
" ",
Some("\x1E"),
false,
true,
)?;
polymorphic_print(ctx, generator, &[(arg_ty, arg_val.into())], " ", Some("\x1D"), false, true)?;
Ok(())
}
/// Generates a call to `core_log`.
pub fn gen_core_log<'ctx>(
ctx: &mut CodeGenContext<'ctx, '_>,
obj: &Option<(Type, ValueEnum<'ctx>)>,
@ -1115,6 +1131,7 @@ pub fn gen_core_log<'ctx>(
call_core_log_impl(ctx, generator, (value_ty, value_arg))
}
/// Generates a call to `rtio_log`.
pub fn gen_rtio_log<'ctx>(
ctx: &mut CodeGenContext<'ctx, '_>,
obj: &Option<(Type, ValueEnum<'ctx>)>,
@ -1125,8 +1142,12 @@ pub fn gen_rtio_log<'ctx>(
assert!(obj.is_none());
assert_eq!(args.len(), 1);
let value_ty = fun.0.args[0].ty;
let value_arg = args[0].1.clone().to_basic_value_enum(ctx, generator, value_ty)?;
let channel_ty = fun.0.args[0].ty;
assert!(ctx.unifier.unioned(channel_ty, ctx.primitives.str));
let channel_arg =
args[0].1.clone().to_basic_value_enum(ctx, generator, channel_ty)?.into_pointer_value();
let value_ty = fun.0.args[1].ty;
let value_arg = args[1].1.clone().to_basic_value_enum(ctx, generator, value_ty)?;
call_rtio_log_impl(ctx, generator, (value_ty, value_arg))
call_rtio_log_impl(ctx, generator, channel_arg, (value_ty, value_arg))
}

View File

@ -909,7 +909,7 @@ impl Nac3 {
"core_log".into(),
FunSignature {
args: vec![FuncArg {
name: "obj".into(),
name: "arg".into(),
ty: arg_ty.ty,
default_value: None,
is_vararg: false,
@ -930,12 +930,20 @@ impl Nac3 {
(
"rtio_log".into(),
FunSignature {
args: vec![FuncArg {
name: "obj".into(),
ty: arg_ty.ty,
default_value: None,
is_vararg: false,
}],
args: vec![
FuncArg {
name: "channel".into(),
ty: primitive.str,
default_value: None,
is_vararg: false,
},
FuncArg {
name: "arg".into(),
ty: arg_ty.ty,
default_value: None,
is_vararg: false,
},
],
ret: primitive.none,
vars: into_var_map([arg_ty]),
},