From 43e59fec7e8396fa63885ead987afea606a24a1c Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Tue, 1 Oct 2024 20:09:56 -0400 Subject: [PATCH] Removed redundant operand --- pkg/compiler/visitor.go | 23 ++++++++++------------- pkg/runtime/opcodes.go | 1 - pkg/runtime/vm.go | 3 --- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/pkg/compiler/visitor.go b/pkg/compiler/visitor.go index 27da2d21..12359849 100644 --- a/pkg/compiler/visitor.go +++ b/pkg/compiler/visitor.go @@ -350,7 +350,7 @@ func (v *visitor) VisitFunctionCallExpression(ctx *fql.FunctionCallExpressionCon isNonOptional := ctx.ErrorOperator() == nil - v.emit(runtime.OpConstant, v.addConstant(values.String(name))) + v.emitConstant(values.String(name)) var size int @@ -526,7 +526,7 @@ func (v *visitor) VisitObjectLiteral(ctx *fql.ObjectLiteralContext) interface{} comProp.Accept(v) pac.Expression().Accept(v) } else if variable := pac.Variable(); variable != nil { - v.emitConstant(runtime.OpConstant, values.NewString(variable.GetText())) + v.emitConstant(values.NewString(variable.GetText())) variable.Accept(v) } } @@ -538,13 +538,13 @@ func (v *visitor) VisitObjectLiteral(ctx *fql.ObjectLiteralContext) interface{} func (v *visitor) VisitPropertyName(ctx *fql.PropertyNameContext) interface{} { if id := ctx.Identifier(); id != nil { - v.emitConstant(runtime.OpConstant, values.NewString(ctx.GetText())) + v.emitConstant(values.NewString(ctx.GetText())) } else if str := ctx.StringLiteral(); str != nil { str.Accept(v) } else if word := ctx.SafeReservedWord(); word != nil { - v.emitConstant(runtime.OpConstant, values.NewString(ctx.GetText())) + v.emitConstant(values.NewString(ctx.GetText())) } else if word := ctx.UnsafeReservedWord(); word != nil { - v.emitConstant(runtime.OpConstant, values.NewString(ctx.GetText())) + v.emitConstant(values.NewString(ctx.GetText())) } return nil @@ -602,7 +602,7 @@ func (v *visitor) VisitStringLiteral(ctx *fql.StringLiteralContext) interface{} } } - v.emitConstant(runtime.OpConstant, values.NewString(b.String())) + v.emitConstant(values.NewString(b.String())) return nil } @@ -614,7 +614,7 @@ func (v *visitor) VisitIntegerLiteral(ctx *fql.IntegerLiteralContext) interface{ panic(err) } - v.emitConstant(runtime.OpConstant, values.NewInt(val)) + v.emitConstant(values.NewInt(val)) return nil } @@ -626,7 +626,7 @@ func (v *visitor) VisitFloatLiteral(ctx *fql.FloatLiteralContext) interface{} { panic(err) } - v.emitConstant(runtime.OpConstant, values.NewFloat(val)) + v.emitConstant(values.NewFloat(val)) return nil } @@ -1004,8 +1004,8 @@ func (v *visitor) defineVariable(index int) { } // emitConstant emits an opcode with a constant argument. -func (v *visitor) emitConstant(op runtime.Opcode, constant core.Value) { - v.emit(op, v.addConstant(constant)) +func (v *visitor) emitConstant(constant core.Value) { + v.emit(runtime.OpPush, v.addConstant(constant)) } // emitLoop emits a loop instruction. @@ -1083,9 +1083,6 @@ func (v *visitor) updateStackTracker(op runtime.Opcode, arg int) { case runtime.OpNone: v.operandsStackTracker++ - case runtime.OpConstant: - v.operandsStackTracker++ - case runtime.OpCastBool: break diff --git a/pkg/runtime/opcodes.go b/pkg/runtime/opcodes.go index 3cdaa1fd..ae309626 100644 --- a/pkg/runtime/opcodes.go +++ b/pkg/runtime/opcodes.go @@ -4,7 +4,6 @@ type Opcode byte const ( OpNone Opcode = iota - OpConstant OpCastBool OpTrue OpFalse diff --git a/pkg/runtime/vm.go b/pkg/runtime/vm.go index 20e604cf..75bba4e3 100644 --- a/pkg/runtime/vm.go +++ b/pkg/runtime/vm.go @@ -78,9 +78,6 @@ loop: case OpNone: stack.Push(values.None) - case OpConstant: - stack.Push(program.Constants[arg]) - case OpCastBool: stack.Push(values.ToBoolean(stack.Pop()))