Skip to content

Commit

Permalink
core!: 1) optimize args validation; 2) fix pipe call
Browse files Browse the repository at this point in the history
  • Loading branch information
moisespsena committed Dec 14, 2023
1 parent 9dbacdc commit 2625082
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 366 deletions.
263 changes: 133 additions & 130 deletions builtins_funcs.go

Large diffs are not rendered by default.

39 changes: 18 additions & 21 deletions builtins_zfuncs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 23 additions & 84 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,26 +351,15 @@ func (o Args) ShiftArg(shifts *int, dst *Arg) (ok bool, err error) {

*shifts++

if len(dst.AcceptTypes) == 0 {
return
}

for _, t := range dst.AcceptTypes {
if dst.Value.Type().Equal(t) {
return
}
}

var s = make([]string, len(dst.AcceptTypes))
for i, acceptType := range dst.AcceptTypes {
s[i] = acceptType.ToString()
if expectedTypes := dst.Accept(dst.Value); expectedTypes != "" {
ok = false
err = NewArgumentTypeError(
strconv.Itoa(*shifts)+"st",
expectedTypes,
dst.Value.Type().Name(),
)
}

return false, NewArgumentTypeError(
strconv.Itoa(*shifts)+"st",
strings.Join(s, "|"),
dst.Value.Type().Name(),
)
return
}

// Destructure shifts argument and set value to dst.
Expand All @@ -381,41 +370,17 @@ func (o Args) Destructure(dst ...*Arg) (err error) {
return
}

args:
for i, d := range dst {
d.Value = o.Shift()

if d.Accept != nil {
if tname := d.Accept(d.Value); tname != "" {
pos := strconv.Itoa(i) + "st"
if d.Name != "" {
pos += " (" + d.Name + ")"
}
return NewArgumentTypeError(
pos,
tname,
d.Value.Type().Name(),
)
}
} else if len(d.AcceptTypes) > 0 {
for _, t := range d.AcceptTypes {
if t.Equal(d.Value.Type()) {
continue args
}
}

pos := strconv.Itoa(i+1) + "st"
if expected := d.Accept(d.Value); expected != "" {
pos := strconv.Itoa(i) + "st"
if d.Name != "" {
pos += " (" + d.Name + ")"
}

var s = make([]string, len(d.AcceptTypes))
for i, acceptType := range d.AcceptTypes {
s[i] = acceptType.ToString()
}
return NewArgumentTypeError(
pos,
strings.Join(s, "|"),
expected,
d.Value.Type().Name(),
)
}
Expand All @@ -426,29 +391,16 @@ args:
// DestructureValue shifts argument and set value to dst.
// If type check of arg is fails, returns ArgumentTypeError.
func (o Args) DestructureValue(dst ...*Arg) (err error) {
args:
for i, d := range dst {
d.Value = o.Shift()

if len(d.AcceptTypes) == 0 {
continue
}

for _, t := range d.AcceptTypes {
if t.Equal(d.Value.Type()) {
continue args
}
}

var s = make([]string, len(d.AcceptTypes))
for i, acceptType := range d.AcceptTypes {
s[i] = acceptType.ToString()
if expectedTypes := d.Accept(d.Value); expectedTypes != "" {
return NewArgumentTypeError(
strconv.Itoa(i)+"st",
expectedTypes,
d.Value.Type().Name(),
)
}
return NewArgumentTypeError(
strconv.Itoa(i)+"st",
strings.Join(s, "|"),
d.Value.Type().Name(),
)
}
return
}
Expand All @@ -461,29 +413,16 @@ func (o Args) DestructureVar(dst ...*Arg) (other Array, err error) {
return
}

args:
for i, d := range dst {
d.Value = o.Shift()

if len(d.AcceptTypes) == 0 {
continue
}

for _, t := range d.AcceptTypes {
if t.Equal(d.Value.Type()) {
continue args
}
}

var s = make([]string, len(d.AcceptTypes))
for i, acceptType := range d.AcceptTypes {
s[i] = acceptType.ToString()
if expectedTypes := d.Accept(d.Value); expectedTypes != "" {
return nil, NewArgumentTypeError(
strconv.Itoa(i)+"st",
expectedTypes,
d.Value.Type().Name(),
)
}
return nil, NewArgumentTypeError(
strconv.Itoa(i)+"st",
strings.Join(s, "|"),
d.Value.Type().Name(),
)
}
other = o.Values()
return
Expand Down
13 changes: 6 additions & 7 deletions cmd/mkcallable/mkcallable.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,12 @@ func (f *Fn) HelperCheckNamedArgs() string {
%s %[4]s
%[1]s_ = &NamedArgVar{
Name: %[1]q,
Accept: func(v Object) error {
var ok bool
if %[1]s, ok = %s; !ok {
return %sNewArgumentTypeError(%[1]q, %[4]q, v.Type().Name())
}
return nil
},
TypeAssertion: %[3]sNewTypeAssertion(TypeAssertionHandlers{
%[4]q: func(v Object) (ok bool) {
%[1]s, ok = %s
return
},
}),
}`, p.Name, conv, gaddot(), p.gadTypeName()))
}
}
Expand Down
2 changes: 0 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ func (c *Compiler) Compile(nd ast.Node) error {
return c.compileReturnStmt(nt)
case *node.CallExpr:
return c.compileCallExpr(nt)
case *node.PipeExpr:
return c.compilePipeExpr(nt)
case *node.ImportExpr:
return c.compileImportExpr(nt)
case *node.CondExpr:
Expand Down
28 changes: 14 additions & 14 deletions compiler_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,20 @@ func (c *Compiler) compileLogical(nd *node.BinaryExpr) error {
}

func (c *Compiler) compileBinaryExpr(nd *node.BinaryExpr) error {
if nd.Token == token.Pipe {
var call node.CallExpr
switch t := nd.RHS.(type) {
case *node.CallExpr:
call = *t
default:
call = node.CallExpr{
Func: t,
}
}
call.CallArgs.Args.Values = append([]node.Expr{nd.LHS}, call.CallArgs.Args.Values...)
return c.Compile(&call)
}

if err := c.Compile(nd.LHS); err != nil {
return err
}
Expand Down Expand Up @@ -1319,20 +1333,6 @@ func (c *Compiler) compileSliceExpr(nd *node.SliceExpr) error {
return nil
}

func (c *Compiler) compilePipeExpr(nd *node.PipeExpr) error {
var call node.CallExpr
switch t := nd.Dst.(type) {
case *node.CallExpr:
call = *t
default:
call = node.CallExpr{
Func: t,
}
}
call.CallArgs.Args.Values = append([]node.Expr{nd.Src}, call.CallArgs.Args.Values...)
return c.Compile(&call)
}

func (c *Compiler) compileCallExpr(nd *node.CallExpr) error {
var (
selExpr *node.SelectorExpr
Expand Down
27 changes: 26 additions & 1 deletion objects_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type CanFilterabler interface {

type Mapabler interface {
Object
Map(vm *VM, args Array, handler VMCaller) (Object, error)
Map(c Call, update bool, keyValue Array, handler VMCaller) (Object, error)
}

type CanMapeabler interface {
Expand Down Expand Up @@ -388,6 +388,31 @@ func Reducable(obj Object) bool {
return false
}

func IsType(obj Object) (ok bool) {
_, ok = obj.(ObjectType)
return
}

func IsObjector(obj Object) (ok bool) {
_, ok = obj.(Objector)
return
}

func IsIndexDeleter(obj Object) (ok bool) {
_, ok = obj.(IndexDeleter)
return
}

func IsIndexSetter(obj Object) (ok bool) {
_, ok = obj.(IndexSetter)
return
}

func IsIndexGetter(obj Object) (ok bool) {
_, ok = obj.(IndexGetter)
return
}

type BinaryOperatorHandler interface {
// BinaryOp handles +,-,*,/,%,<<,>>,<=,>=,<,> operators.
// Returned error stops VM execution if not handled with an error handler
Expand Down
Loading

0 comments on commit 2625082

Please sign in to comment.