forked from M-Labs/nac3
1
0
Fork 0

nac3core: fixes #60

This commit is contained in:
pca006132 2021-10-10 15:01:06 +08:00
parent a3ce5be10b
commit 77542170fd
1 changed files with 40 additions and 21 deletions

View File

@ -41,7 +41,8 @@ impl TopLevelComposer {
let int32 = primitives.0.int32; let int32 = primitives.0.int32;
let int64 = primitives.0.int64; let int64 = primitives.0.int64;
let float = primitives.0.float; let float = primitives.0.float;
let num_ty = primitives.1.get_fresh_var_with_range(&[int32, int64, float]); let boolean = primitives.0.bool;
let num_ty = primitives.1.get_fresh_var_with_range(&[int32, int64, float, boolean]);
let var_map: HashMap<_, _> = vec![(num_ty.1, num_ty.0)].into_iter().collect(); let var_map: HashMap<_, _> = vec![(num_ty.1, num_ty.0)].into_iter().collect();
let mut definition_ast_list = { let mut definition_ast_list = {
@ -83,9 +84,20 @@ impl TopLevelComposer {
let int32 = ctx.primitives.int32; let int32 = ctx.primitives.int32;
let int64 = ctx.primitives.int64; let int64 = ctx.primitives.int64;
let float = ctx.primitives.float; let float = ctx.primitives.float;
let boolean = ctx.primitives.bool;
let arg_ty = fun.0.args[0].ty; let arg_ty = fun.0.args[0].ty;
let arg = args[0].1; let arg = args[0].1;
if ctx.unifier.unioned(arg_ty, int32) { if ctx.unifier.unioned(arg_ty, boolean) {
Some(
ctx.builder
.build_int_s_extend(
arg.into_int_value(),
ctx.ctx.i32_type(),
"sext",
)
.into(),
)
} else if ctx.unifier.unioned(arg_ty, int32) {
Some(arg) Some(arg)
} else if ctx.unifier.unioned(arg_ty, int64) { } else if ctx.unifier.unioned(arg_ty, int64) {
Some( Some(
@ -130,9 +142,12 @@ impl TopLevelComposer {
let int32 = ctx.primitives.int32; let int32 = ctx.primitives.int32;
let int64 = ctx.primitives.int64; let int64 = ctx.primitives.int64;
let float = ctx.primitives.float; let float = ctx.primitives.float;
let boolean = ctx.primitives.bool;
let arg_ty = fun.0.args[0].ty; let arg_ty = fun.0.args[0].ty;
let arg = args[0].1; let arg = args[0].1;
if ctx.unifier.unioned(arg_ty, int32) { if ctx.unifier.unioned(arg_ty, boolean)
|| ctx.unifier.unioned(arg_ty, int32)
{
Some( Some(
ctx.builder ctx.builder
.build_int_s_extend( .build_int_s_extend(
@ -176,10 +191,12 @@ impl TopLevelComposer {
|ctx, _, fun, args| { |ctx, _, fun, args| {
let int32 = ctx.primitives.int32; let int32 = ctx.primitives.int32;
let int64 = ctx.primitives.int64; let int64 = ctx.primitives.int64;
let boolean = ctx.primitives.bool;
let float = ctx.primitives.float; let float = ctx.primitives.float;
let arg_ty = fun.0.args[0].ty; let arg_ty = fun.0.args[0].ty;
let arg = args[0].1; let arg = args[0].1;
if ctx.unifier.unioned(arg_ty, int32) if ctx.unifier.unioned(arg_ty, boolean)
|| ctx.unifier.unioned(arg_ty, int32)
|| ctx.unifier.unioned(arg_ty, int64) || ctx.unifier.unioned(arg_ty, int64)
{ {
let arg = args[0].1.into_int_value(); let arg = args[0].1.into_int_value();
@ -489,11 +506,7 @@ impl TopLevelComposer {
self.definition_ast_list.push((def, Some(ast))); self.definition_ast_list.push((def, Some(ast)));
} }
let result_ty = if contains_constructor { let result_ty = if contains_constructor { Some(constructor_ty) } else { None };
Some(constructor_ty)
} else {
None
};
Ok((class_name, DefinitionId(class_def_id), result_ty)) Ok((class_name, DefinitionId(class_def_id), result_ty))
} }
@ -904,11 +917,12 @@ impl TopLevelComposer {
.node .node
.annotation .annotation
.as_ref() .as_ref()
.ok_or_else(|| format!( .ok_or_else(|| {
"function parameter `{}` at {} need type annotation", format!(
x.node.arg, "function parameter `{}` at {} need type annotation",
x.location x.node.arg, x.location
))? )
})?
.as_ref(); .as_ref();
let type_annotation = parse_ast_to_type_annotation_kinds( let type_annotation = parse_ast_to_type_annotation_kinds(
@ -1110,11 +1124,12 @@ impl TopLevelComposer {
.node .node
.annotation .annotation
.as_ref() .as_ref()
.ok_or_else(|| format!( .ok_or_else(|| {
"type annotation for `{}` at {} needed", format!(
x.node.arg, "type annotation for `{}` at {} needed",
x.location x.node.arg, x.location
))? )
})?
.as_ref(); .as_ref();
parse_ast_to_type_annotation_kinds( parse_ast_to_type_annotation_kinds(
class_resolver.as_ref(), class_resolver.as_ref(),
@ -1629,7 +1644,9 @@ impl TopLevelComposer {
let ret_str = self.unifier.stringify( let ret_str = self.unifier.stringify(
inst_ret, inst_ret,
&mut |id| { &mut |id| {
if let TopLevelDef::Class { name, .. } = &*def_ast_list[id].0.read() { if let TopLevelDef::Class { name, .. } =
&*def_ast_list[id].0.read()
{
name.to_string() name.to_string()
} else { } else {
unreachable!("must be class id here") unreachable!("must be class id here")
@ -1639,7 +1656,9 @@ impl TopLevelComposer {
); );
return Err(format!( return Err(format!(
"expected return type of `{}` in function `{}` at {}", "expected return type of `{}` in function `{}` at {}",
ret_str, name, ast.as_ref().unwrap().location ret_str,
name,
ast.as_ref().unwrap().location
)); ));
} }