Skip to content

Commit

Permalink
wip bytecode param
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed May 23, 2024
1 parent 4fa3829 commit b5ce5b5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
9 changes: 5 additions & 4 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (lib *Library) expressionFromBytecode(bytecode []byte, localLib ...*LocalLi
}
if len(callPrefix) == 1 && callPrefix[0] < LastEmbeddedReserved {
// it is a parameter function call
maxParameterNumber = callPrefix[0]
maxParameterNumber = callPrefix[0] & (^BytecodeParameterFlag)
}
if len(callPrefix) == 1 && arity < 0 {
return nil, nil, 0xff, fmt.Errorf("EasyFL: short embedded with vararg is not allowed")
Expand Down Expand Up @@ -594,15 +594,16 @@ func (lib *Library) parseCallPrefix(code []byte, localLib ...*LocalLibrary) ([]b
if code[0]&FirstByteLongCallMask == 0 {
// short call
if code[0] <= LastEmbeddedReserved {
// this param reference
// this is param reference
if code[0]&BytecodeParameterFlag == 0 {
// eval param reference
evalFun = evalEvalParamFun(code[0])
sym = fmt.Sprintf("$%d", code[0])
} else {
// bytecode param reference
evalFun = evalBytecodeParamFun(code[0])
sym = fmt.Sprintf("$$%d", code[0])
paramNr := code[0] & (^BytecodeParameterFlag)
evalFun = evalBytecodeParamFun(paramNr)
sym = fmt.Sprintf("$$%d", paramNr)
}
} else {
evalFun, arity, sym, err = lib.functionByCode(uint16(code[0]))
Expand Down
12 changes: 0 additions & 12 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"reflect"

"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -333,17 +332,6 @@ func evalRepeat(par *CallParams) []byte {
return ret
}

func evalLen8(par *CallParams) []byte {
arg := par.Arg(0)
sz := len(arg)
if sz > math.MaxUint8 {
par.TracePanic("len8:: size of the data > 255: %s", Fmt(arg))
}
ret := []byte{byte(sz)}
par.Trace("len8:: %s -> %s", Fmt(arg), Fmt(ret))
return ret
}

func evalLen(par *CallParams) []byte {
data := par.Arg(0)
var ret [8]byte
Expand Down
1 change: 1 addition & 0 deletions extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var extendWithUtilityFunctions = []*ExtendFunction{
{"lessOrEqualThan", "or(lessThan($0,$1),equal($0,$1))"},
{"greaterThan", "not(lessOrEqualThan($0,$1))"},
{"greaterOrEqualThan", "not(lessThan($0,$1))"},
{"bytecode", "$$0"},
}

func (lib *Library) extendBase() {
Expand Down
10 changes: 5 additions & 5 deletions library.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
// MaxParameters maximum number of parameters in the function definition and the call.
MaxParameters = 0x08
LastEmbeddedReserved = FirstEmbeddedReserved + 2*MaxParameters - 1 // 15 reserved for parameter access 2 x 8
BytecodeParameterFlag = 0x08
BytecodeParameterFlag = byte(0x08)

// ----- embedded short

Expand Down Expand Up @@ -305,7 +305,7 @@ func evalEvalParamFun(paramNr byte) EvalFunction {

func evalBytecodeParamFun(paramNr byte) EvalFunction {
return func(par *CallParams) []byte {
panic("evalBytecodeParamFun not implemented")
return ExpressionToBytecode(par.args[paramNr])
}
}

Expand Down Expand Up @@ -453,13 +453,13 @@ func (lib *Library) functionByCode(funCode uint16, localLib ...*LocalLibrary) (E
func (fi *funInfo) callPrefix(numArgs byte) ([]byte, error) {
var ret []byte
if fi.IsShort {
Assert(fi.FunCode > 15, "internal inconsistency: fi.FunCode must be > 15")
Assert(fi.FunCode > LastEmbeddedReserved, "internal inconsistency: fi.FunCode must be > %d", LastEmbeddedReserved)
ret = []byte{byte(fi.FunCode)}
} else {
if fi.NumParams < 0 {
// vararg function
if numArgs > 15 {
return nil, fmt.Errorf("internal inconsistency: number of arguments must be <= 15")
if numArgs > MaxParameters {
return nil, fmt.Errorf("internal inconsistency: number of arguments must be <= %d", MaxParameters)
}
} else {
if int(numArgs) != fi.NumParams {
Expand Down
14 changes: 14 additions & 0 deletions library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,3 +942,17 @@ func TestLocalLibrary(t *testing.T) {
})

}

func TestBytecodeParams(t *testing.T) {
lib := NewBase()
t.Run("1", func(t *testing.T) {
const src = "bytecode(concat(1,2))"
expr, nPar, code, err := lib.CompileExpression(src)
require.NoError(t, err)
require.EqualValues(t, 0, nPar)
t.Logf("src: '%s' -> %s", src, Fmt(code))

res := EvalExpression(nil, expr)
t.Logf("Result: '%s'", Fmt(res))
})
}

0 comments on commit b5ce5b5

Please sign in to comment.