Skip to content

Commit

Permalink
Implemented range operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Oct 29, 2024
1 parent 52f9263 commit 0048d7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
29 changes: 16 additions & 13 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,26 +436,29 @@ func (v *visitor) VisitMemberExpression(ctx *fql.MemberExpressionContext) interf
}

func (v *visitor) VisitRangeOperator(ctx *fql.RangeOperatorContext) interface{} {
//ctx.GetLeft().Accept(v)
//ctx.GetRight().Accept(v)
//
//v.emitter.EmitABC(runtime.OpRange)
dst := v.registers.AllocateTempVar()
start := v.toRegister(ctx.GetLeft().Accept(v).(runtime.Operand))
end := v.toRegister(ctx.GetRight().Accept(v).(runtime.Operand))

return nil
v.emitter.EmitABC(runtime.OpRange, dst, start, end)

return dst
}

func (v *visitor) VisitRangeOperand(ctx *fql.RangeOperandContext) interface{} {
if c := ctx.IntegerLiteral(); c != nil {
c.Accept(v)
} else if c := ctx.Variable(); c != nil {
c.Accept(v)
} else if c := ctx.Param(); c != nil {
c.Accept(v)
} else {
panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
return c.Accept(v)
}

return nil
if c := ctx.Variable(); c != nil {
return c.Accept(v)
}

if c := ctx.Param(); c != nil {
return c.Accept(v)
}

panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}

func (v *visitor) VisitVariableDeclaration(ctx *fql.VariableDeclarationContext) interface{} {
Expand Down
22 changes: 12 additions & 10 deletions pkg/runtime/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const (
OpLoadGlobal // Load a global variable to a register A
OpStoreGlobal // Store a value from register A to a global variable

OpJump
OpJumpIfFalse
OpJumpIfTrue
OpJumpBackward

OpAdd
OpSub
OpMulti
Expand All @@ -16,12 +21,6 @@ const (
OpIncr
OpDecr

OpArray // Create an array from a list of registers (ARR R2, R3 R5 - creates an array in R2 with elements from R3 to R5)
OpObject // Create an object from a list of registers (OBJ R2, R3 R5 - creates an object in R2 with elements from R3 to R5)

OpLoadProperty
OpLoadPropertyOptional

OpNegate
OpFlipPositive
OpFlipNegative
Expand All @@ -37,7 +36,13 @@ const (
OpLike
OpNotLike

OpArray // Create an array from a list of registers (ARR R2, R3 R5 - creates an array in R2 with elements from R3 to R5)
OpObject // Create an object from a list of registers (OBJ R2, R3 R5 - creates an object in R2 with elements from R3 to R5)
OpRange

OpLoadProperty
OpLoadPropertyOptional

OpRegexpPositive
OpRegexpNegative
OpCall
Expand All @@ -52,10 +57,7 @@ const (
OpCall4Safe
OpCallN
OpCallNSafe
OpJumpIfFalse
OpJumpIfTrue
OpJump
OpJumpBackward

OpLoopInitOutput
OpLoopUnwrapOutput
OpForLoopInitInput
Expand Down
16 changes: 7 additions & 9 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,13 @@ loop:
// return nil, err
//}
case OpRange:
//right := stack.Pop()
//left := stack.Pop()
//res, err := operators.Range(left, right)
//
//if err == nil {
// stack.Push(res)
//} else {
// return nil, err
//}
res, err := operators.Range(reg[src1], reg[src2])

if err == nil {
reg[dst] = res
} else {
return nil, err
}
case OpLoopInitOutput:
//stack.Push(NewDataSet(arg == 1))

Expand Down

0 comments on commit 0048d7b

Please sign in to comment.