Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler] Generalize arithmetic instructions for all number values #3761

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions bbq/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,27 +1051,27 @@ func (c *Compiler[_]) VisitBinaryExpression(expression *ast.BinaryExpression) (_

switch expression.Operation {
case ast.OperationPlus:
c.codeGen.Emit(opcode.InstructionIntAdd{})
c.codeGen.Emit(opcode.InstructionAdd{})
case ast.OperationMinus:
c.codeGen.Emit(opcode.InstructionIntSubtract{})
c.codeGen.Emit(opcode.InstructionSubtract{})
case ast.OperationMul:
c.codeGen.Emit(opcode.InstructionIntMultiply{})
c.codeGen.Emit(opcode.InstructionMultiply{})
case ast.OperationDiv:
c.codeGen.Emit(opcode.InstructionIntDivide{})
c.codeGen.Emit(opcode.InstructionDivide{})
case ast.OperationMod:
c.codeGen.Emit(opcode.InstructionIntMod{})
c.codeGen.Emit(opcode.InstructionMod{})
case ast.OperationEqual:
c.codeGen.Emit(opcode.InstructionEqual{})
case ast.OperationNotEqual:
c.codeGen.Emit(opcode.InstructionNotEqual{})
case ast.OperationLess:
c.codeGen.Emit(opcode.InstructionIntLess{})
c.codeGen.Emit(opcode.InstructionLess{})
case ast.OperationLessEqual:
c.codeGen.Emit(opcode.InstructionIntLessOrEqual{})
c.codeGen.Emit(opcode.InstructionLessOrEqual{})
case ast.OperationGreater:
c.codeGen.Emit(opcode.InstructionIntGreater{})
c.codeGen.Emit(opcode.InstructionGreater{})
case ast.OperationGreaterEqual:
c.codeGen.Emit(opcode.InstructionIntGreaterOrEqual{})
c.codeGen.Emit(opcode.InstructionGreaterOrEqual{})
default:
panic(errors.NewUnreachableError())
}
Expand Down
22 changes: 11 additions & 11 deletions bbq/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ func TestCompileRecursionFib(t *testing.T) {
// if n < 2
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 6},
// then return n
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionReturnValue{},
// fib(n - 1)
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntSubtract{},
opcode.InstructionSubtract{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionGetGlobal{GlobalIndex: 0x0},
opcode.InstructionInvoke{},
// fib(n - 2)
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntSubtract{},
opcode.InstructionSubtract{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionGetGlobal{GlobalIndex: 0x0},
opcode.InstructionInvoke{},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
// assign to temp $result
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x1},
Expand Down Expand Up @@ -144,12 +144,12 @@ func TestCompileImperativeFib(t *testing.T) {
// while i < n
opcode.InstructionGetLocal{LocalIndex: 0x4},
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 33},
// fibonacci = fib1 + fib2
opcode.InstructionGetLocal{LocalIndex: 0x1},
opcode.InstructionGetLocal{LocalIndex: 0x2},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x3},
// fib1 = fib2
Expand All @@ -163,7 +163,7 @@ func TestCompileImperativeFib(t *testing.T) {
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x4},
opcode.InstructionGetConstant{ConstantIndex: 0x0},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x4},
// continue loop
Expand Down Expand Up @@ -228,14 +228,14 @@ func TestCompileBreak(t *testing.T) {
// if i > 3
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntGreater{},
opcode.InstructionGreater{},
opcode.InstructionJumpIfFalse{Target: 10},
// break
opcode.InstructionJump{Target: 16},
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x2},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x0},
// repeat
Expand Down Expand Up @@ -305,13 +305,13 @@ func TestCompileContinue(t *testing.T) {
// i = i + 1
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x1},
opcode.InstructionIntAdd{},
opcode.InstructionAdd{},
opcode.InstructionTransfer{TypeIndex: 0x0},
opcode.InstructionSetLocal{LocalIndex: 0x0},
// if i < 3
opcode.InstructionGetLocal{LocalIndex: 0x0},
opcode.InstructionGetConstant{ConstantIndex: 0x2},
opcode.InstructionIntLess{},
opcode.InstructionLess{},
opcode.InstructionJumpIfFalse{Target: 15},
// continue
opcode.InstructionJump{Target: 3},
Expand Down
2 changes: 2 additions & 0 deletions bbq/opcode/gen/instructions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"value",
"string",
"array",
"dictionary",
"integer",
"number",
"bool",
"path",
"int",
Expand Down
Loading
Loading