From 44522ffad79c2448602d8036974c574875be0e3d Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 16 Feb 2024 17:30:15 +0100 Subject: [PATCH] fixed binary op send2 + tests for send BC --- som-interpreter-bc/src/compiler.rs | 20 ++++++------- som-interpreter-bc/tests/specialized_bc.rs | 33 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/som-interpreter-bc/src/compiler.rs b/som-interpreter-bc/src/compiler.rs index b25d63df..0692ef86 100644 --- a/som-interpreter-bc/src/compiler.rs +++ b/som-interpreter-bc/src/compiler.rs @@ -272,16 +272,14 @@ impl MethodCodegen for ast::Expression { ctxt.push_instr(Bytecode::PushArgument(up_idx, idx)) } Some(FoundVar::Field(idx)) => ctxt.push_instr(Bytecode::PushField(idx)), - None => { - match name.as_str() { - "nil" => ctxt.push_instr(Bytecode::PushNil), - _ => { - let name = ctxt.intern_symbol(name); - let idx = ctxt.push_literal(Literal::Symbol(name)); - ctxt.push_instr(Bytecode::PushGlobal(idx as u8)); - } + None => match name.as_str() { + "nil" => ctxt.push_instr(Bytecode::PushNil), + _ => { + let name = ctxt.intern_symbol(name); + let idx = ctxt.push_literal(Literal::Symbol(name)); + ctxt.push_instr(Bytecode::PushGlobal(idx as u8)); } - } + }, } Some(()) } @@ -344,9 +342,9 @@ impl MethodCodegen for ast::Expression { let sym = ctxt.intern_symbol(message.op.as_str()); let idx = ctxt.push_literal(Literal::Symbol(sym)); if super_send { - ctxt.push_instr(Bytecode::SuperSendN(idx as u8)); + ctxt.push_instr(Bytecode::SuperSend2(idx as u8)); } else { - ctxt.push_instr(Bytecode::SendN(idx as u8)); + ctxt.push_instr(Bytecode::Send2(idx as u8)); } Some(()) } diff --git a/som-interpreter-bc/tests/specialized_bc.rs b/som-interpreter-bc/tests/specialized_bc.rs index e557f73e..36ebd171 100644 --- a/som-interpreter-bc/tests/specialized_bc.rs +++ b/som-interpreter-bc/tests/specialized_bc.rs @@ -110,3 +110,36 @@ fn push_constant_bytecodes() { ], ); } + +#[test] +fn send_bytecodes() { + let class_txt = "Foo = ( + send: a three: b = ( + ^ false + ) + + send: a with: b four: c = ( + ^ false + ) + + run = ( + 1 abs. + 1 + 1. + self send: 1 three: 1. + self send: 1 with: 1 four: 1. + ) + ) + "; + + let bytecodes = get_bytecodes_from_method(class_txt, "run"); + expect_bytecode_sequence(&bytecodes, &[Push1, Send1(0)]); + + expect_bytecode_sequence(&bytecodes, &[Push1, Push1, Send2(1)]); + + expect_bytecode_sequence(&bytecodes, &[PushArgument(0, 0), Push1, Push1, Send3(2)]); + + expect_bytecode_sequence( + &bytecodes, + &[PushArgument(0, 0), Push1, Push1, Push1, SendN(3)], + ); +}