Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Oct 21, 2024
1 parent 8571c48 commit 39dccbc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 176 deletions.
166 changes: 19 additions & 147 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ type (
err error
src string
//funcs core.Functions
constantsIndex map[uint64]int
locations []core.Location
bytecode []runtime.Opcode
arguments []int
constants []core.Value
scope int
loops []*loopScope
globals map[string]int
locals []variable
catchTable [][2]int
operandsStackTracker int
variablesStackTracker int
constantsIndex map[uint64]int
locations []core.Location
bytecode []runtime.Opcode
arguments []int
constants []core.Value
scope int
loops []*loopScope
globals map[string]int
locals []variable
catchTable [][2]int
}
)

Expand Down Expand Up @@ -147,7 +145,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
c.Accept(v)
}

v.emit(runtime.OpForLoopInitInput)
v.emit(runtime.OpForLoopInit)
loopJump = len(v.bytecode)
v.emit(runtime.OpForLoopHasNext)
exitJump = v.emitJump(runtime.OpJumpIfFalse)
Expand Down Expand Up @@ -200,7 +198,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
}
} else {
// Create initial value for the loop counter
v.emit(runtime.OpWhileLoopInitCounter)
v.emit(runtime.OpWhileLoopInit)

loopJump = len(v.bytecode)

Expand Down Expand Up @@ -497,7 +495,7 @@ func (v *visitor) VisitArrayLiteral(ctx *fql.ArrayLiteralContext) interface{} {
size = out.(int)
}

v.emit(runtime.OpArray, size)
v.emit(runtime.OpConstArray, size)

return nil
}
Expand Down Expand Up @@ -531,7 +529,7 @@ func (v *visitor) VisitObjectLiteral(ctx *fql.ObjectLiteralContext) interface{}
}
}

v.emit(runtime.OpObject, len(assignments))
v.emit(runtime.OpConstObject, len(assignments))

return nil
}
Expand Down Expand Up @@ -634,9 +632,9 @@ func (v *visitor) VisitFloatLiteral(ctx *fql.FloatLiteralContext) interface{} {
func (v *visitor) VisitBooleanLiteral(ctx *fql.BooleanLiteralContext) interface{} {
switch strings.ToLower(ctx.GetText()) {
case "true":
v.emit(runtime.OpTrue)
v.emit(runtime.OpConstBool, 1)
case "false":
v.emit(runtime.OpFalse)
v.emit(runtime.OpConstBool, 0)
default:
panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}
Expand All @@ -645,7 +643,7 @@ func (v *visitor) VisitBooleanLiteral(ctx *fql.BooleanLiteralContext) interface{
}

func (v *visitor) VisitNoneLiteral(ctx *fql.NoneLiteralContext) interface{} {
v.emit(runtime.OpNone)
v.emit(runtime.OpConstNone)

return nil
}
Expand Down Expand Up @@ -881,8 +879,7 @@ func (v *visitor) beginLoopScope(passThrough, distinct bool) {
arg = 1
}

resultPos = v.operandsStackTracker
v.emit(runtime.OpLoopInit, arg)
v.emit(runtime.OpLoopBegin, arg)
} else {
resultPos = prevResult
}
Expand Down Expand Up @@ -917,7 +914,7 @@ func (v *visitor) endLoopScope() {
}

if unwrap {
v.emit(runtime.OpLoopFin)
v.emit(runtime.OpLoopEnd)
}
}

Expand Down Expand Up @@ -1050,131 +1047,6 @@ func (v *visitor) emit(op runtime.Opcode, args ...int) {
}

v.arguments = append(v.arguments, arg)
v.updateStackTracker(op, arg)
}

func (v *visitor) updateStackTracker(op runtime.Opcode, arg int) {
switch op {
case runtime.OpPush:
v.operandsStackTracker++

case runtime.OpPop:
v.operandsStackTracker--

case runtime.OpPopClose:
v.operandsStackTracker--

case runtime.OpStoreGlobal:
v.operandsStackTracker--

case runtime.OpLoadGlobal:
v.operandsStackTracker++

case runtime.OpStoreLocal:
v.operandsStackTracker--
v.variablesStackTracker++

case runtime.OpPopLocal:
v.variablesStackTracker--

case runtime.OpLoadLocal:
v.operandsStackTracker++

case runtime.OpNone:
v.operandsStackTracker++

case runtime.OpCastBool:
break

case runtime.OpTrue:
v.operandsStackTracker++

case runtime.OpFalse:
v.operandsStackTracker++

case runtime.OpArray:
v.operandsStackTracker++
v.operandsStackTracker -= arg

case runtime.OpObject:
v.operandsStackTracker++
v.operandsStackTracker -= arg * 2

case runtime.OpLoadProperty, runtime.OpLoadPropertyOptional:
v.operandsStackTracker--

case runtime.OpNegate, runtime.OpFlipPositive, runtime.OpFlipNegative, runtime.OpNot:
break

case runtime.OpEq, runtime.OpNeq:
v.operandsStackTracker--

case runtime.OpGt, runtime.OpLt, runtime.OpGte, runtime.OpLte:
v.operandsStackTracker--

case runtime.OpIn, runtime.OpNotIn:
v.operandsStackTracker--

case runtime.OpLike, runtime.OpNotLike:
v.operandsStackTracker--

case runtime.OpAdd, runtime.OpSub, runtime.OpMulti, runtime.OpDiv, runtime.OpMod:
v.operandsStackTracker--

case runtime.OpIncr, runtime.OpDecr:
break

case runtime.OpRegexpPositive, runtime.OpRegexpNegative:
break

case runtime.OpCall, runtime.OpCallSafe:
break

case runtime.OpCall1, runtime.OpCall1Safe:
v.operandsStackTracker--

case runtime.OpCall2, runtime.OpCall2Safe:
v.operandsStackTracker -= 2

case runtime.OpCall3, runtime.OpCall3Safe:
v.operandsStackTracker -= 3

case runtime.OpCall4, runtime.OpCall4Safe:
v.operandsStackTracker -= 4

case runtime.OpCallN, runtime.OpCallNSafe:
v.operandsStackTracker -= arg

case runtime.OpRange:
v.operandsStackTracker--

case runtime.OpLoopInit:
v.operandsStackTracker++

case runtime.OpLoopFin, runtime.OpForLoopInitInput:
break

case runtime.OpForLoopHasNext:
v.operandsStackTracker++

case runtime.OpForLoopNext:
v.operandsStackTracker += 2

case runtime.OpForLoopNextValue, runtime.OpForLoopNextCounter:
v.operandsStackTracker++

case runtime.OpWhileLoopInitCounter:
v.operandsStackTracker++

case runtime.OpWhileLoopNext:
v.operandsStackTracker += 2

case runtime.OpJump, runtime.OpJumpBackward, runtime.OpJumpIfFalse, runtime.OpJumpIfTrue:
break

case runtime.OpLoopReturn, runtime.OpReturn:
break
}
}

// addConstant adds a constant to the constants pool and returns its index.
Expand Down
28 changes: 14 additions & 14 deletions pkg/runtime/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ const (
OpPush Opcode = iota
OpPop
OpPopClose
OpJumpIfFalse
OpJumpIfTrue
OpJump
OpJumpBackward
OpNone
OpCastBool
OpTrue
OpFalse
OpArray
OpObject
OpSwap
OpLoadGlobal
OpStoreGlobal
OpLoadLocal
OpStoreLocal
OpPopLocal
OpJumpIfFalse
OpJumpIfTrue
OpJump
OpJumpBackward
OpConstNone
OpConstBool
OpConstArray
OpConstObject
OpLoadProperty
OpLoadPropertyOptional
OpCastBool
OpNegate
OpFlipPositive
OpFlipNegative
Expand Down Expand Up @@ -59,14 +59,14 @@ const (
OpCall4Safe
OpCallN
OpCallNSafe
OpLoopInit
OpLoopFin
OpForLoopInitInput
OpLoopBegin
OpLoopEnd
OpForLoopInit
OpForLoopHasNext
OpForLoopNext
OpForLoopNextValue
OpForLoopNextCounter
OpWhileLoopInitCounter
OpWhileLoopInit
OpWhileLoopNext
OpLoopReturn
OpReturn
Expand Down
10 changes: 10 additions & 0 deletions pkg/runtime/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func (s *Stack) Pop() core.Value {
return value
}

func (s *Stack) Swap() {
l := len(s.values)

if l < 2 {
return
}

s.values[l-1], s.values[l-2] = s.values[l-2], s.values[l-1]
}

func (s *Stack) Get(index int) core.Value {
return s.values[index]
}
Expand Down
33 changes: 18 additions & 15 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ loop:
if ok {
closable.Close()
}
case OpSwap:
stack.Swap()

case OpStoreGlobal:
vm.globals[program.Constants[arg].String()] = stack.Pop()
Expand All @@ -81,19 +83,20 @@ loop:
case OpLoadLocal:
stack.Push(variables.Get(arg))

case OpNone:
case OpConstNone:
stack.Push(values.None)

case OpCastBool:
stack.Push(values.ToBoolean(stack.Pop()))

case OpTrue:
stack.Push(values.True)

case OpFalse:
stack.Push(values.False)
case OpConstBool:
if arg > 0 {
stack.Push(values.True)
} else {
stack.Push(values.False)
}

case OpArray:
case OpConstArray:
size := arg
arr := values.NewSizedArray(size)

Expand All @@ -105,7 +108,7 @@ loop:

stack.Push(arr)

case OpObject:
case OpConstObject:
obj := values.NewObject()
propertyCount := arg

Expand Down Expand Up @@ -398,14 +401,14 @@ loop:
} else {
return nil, err
}
case OpLoopInit:
case OpLoopBegin:
state.Push(NewDataSet(arg == 1))

case OpLoopFin:
case OpLoopEnd:
ds := state.Pop().(*DataSet)
stack.Push(ds.ToArray())

case OpForLoopInitInput:
case OpForLoopInit:
// start a new iteration
// get the data source
input := stack.Pop()
Expand Down Expand Up @@ -455,13 +458,13 @@ loop:
stack.Push(val)
}

case OpWhileLoopInitCounter:
stack.Push(values.ZeroInt)
case OpWhileLoopInit:
state.Push(values.ZeroInt)

case OpWhileLoopNext:
counter := stack.Pop().(values.Int)
counter := state.Pop().(values.Int)
// increment the counter for the next iteration
stack.Push(counter + 1)
state.Push(counter + 1)
// put the current counter value
stack.Push(counter)

Expand Down

0 comments on commit 39dccbc

Please sign in to comment.