Implement int and float subtraction

This commit is contained in:
Richard Feldman 2020-03-11 21:10:41 -04:00
parent 9fcfa90bff
commit df78068e81
5 changed files with 39 additions and 2 deletions

View file

@ -559,13 +559,20 @@ fn call_by_name<'a, B: Backend>(
builder.ins().fadd(a, b) builder.ins().fadd(a, b)
} }
Symbol::NUM_SUB => { Symbol::INT_SUB | Symbol::NUM_SUB => {
debug_assert!(args.len() == 2); debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs); let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs); let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().isub(a, b) builder.ins().isub(a, b)
} }
Symbol::FLOAT_SUB => {
debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs);
let b = build_arg(&args[1], env, scope, module, builder, procs);
builder.ins().fsub(a, b)
}
Symbol::NUM_MUL => { Symbol::NUM_MUL => {
debug_assert!(args.len() == 2); debug_assert!(args.len() == 2);
let a = build_arg(&args[0], env, scope, module, builder, procs); let a = build_arg(&args[0], env, scope, module, builder, procs);

View file

@ -558,7 +558,7 @@ fn call_with_args<'a, 'ctx, 'env>(
BasicValueEnum::FloatValue(float_val) BasicValueEnum::FloatValue(float_val)
} }
Symbol::NUM_SUB => { Symbol::INT_SUB | Symbol::NUM_SUB => {
debug_assert!(args.len() == 2); debug_assert!(args.len() == 2);
let int_val = env.builder.build_int_sub( let int_val = env.builder.build_int_sub(
@ -569,6 +569,17 @@ fn call_with_args<'a, 'ctx, 'env>(
BasicValueEnum::IntValue(int_val) BasicValueEnum::IntValue(int_val)
} }
Symbol::FLOAT_SUB => {
debug_assert!(args.len() == 2);
let float_val = env.builder.build_float_sub(
args[0].into_float_value(),
args[1].into_float_value(),
"sub_f64",
);
BasicValueEnum::FloatValue(float_val)
}
Symbol::NUM_MUL => { Symbol::NUM_MUL => {
debug_assert!(args.len() == 2); debug_assert!(args.len() == 2);

View file

@ -826,6 +826,19 @@ mod test_gen {
); );
} }
#[test]
fn gen_sub_f64() {
assert_evals_to!(
indoc!(
r#"
1.5 - 2.4 - 3
"#
),
-3.9,
f64
);
}
#[test] #[test]
fn gen_sub_i64() { fn gen_sub_i64() {
assert_evals_to!( assert_evals_to!(

View file

@ -576,6 +576,7 @@ define_builtins! {
5 INT_HIGHEST: "highest" 5 INT_HIGHEST: "highest"
6 INT_LOWEST: "lowest" 6 INT_LOWEST: "lowest"
7 INT_ADD: "#add" 7 INT_ADD: "#add"
8 INT_SUB: "#sub"
} }
3 FLOAT: "Float" => { 3 FLOAT: "Float" => {
0 FLOAT_FLOAT: "Float" imported // the Float.Float type alias 0 FLOAT_FLOAT: "Float" imported // the Float.Float type alias
@ -587,6 +588,7 @@ define_builtins! {
6 FLOAT_HIGHEST: "highest" 6 FLOAT_HIGHEST: "highest"
7 FLOAT_LOWEST: "lowest" 7 FLOAT_LOWEST: "lowest"
8 FLOAT_ADD: "#add" 8 FLOAT_ADD: "#add"
9 FLOAT_SUB: "#sub"
} }
4 BOOL: "Bool" => { 4 BOOL: "Bool" => {
0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias 0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias

View file

@ -345,6 +345,10 @@ fn from_can<'a>(
FloatType => Symbol::FLOAT_ADD, FloatType => Symbol::FLOAT_ADD,
IntType => Symbol::INT_ADD, IntType => Symbol::INT_ADD,
}, },
Symbol::NUM_SUB => match to_int_or_float(subs, ret_var) {
FloatType => Symbol::FLOAT_SUB,
IntType => Symbol::INT_SUB,
},
_ => symbol, _ => symbol,
} }
}; };