Skip to content

Commit

Permalink
core!: 1) changes var params and named params specs similar to Python…
Browse files Browse the repository at this point in the history
…. 2) changes lambda functions type def.
  • Loading branch information
moisespsena committed Nov 20, 2023
1 parent 4bfbcad commit f2d9400
Show file tree
Hide file tree
Showing 14 changed files with 2,189 additions and 1,868 deletions.
8 changes: 4 additions & 4 deletions compiler_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,10 +1270,10 @@ func (c *Compiler) compileCallExpr(nd *node.CallExpr) error {
}
}

if nd.Args.Ellipsis != nil {
if nd.Args.Var != nil {
numArgs++
flags |= OpCallFlagVarArgs
if err := c.Compile(nd.Args.Ellipsis.Value); err != nil {
if err := c.Compile(nd.Args.Var.Value); err != nil {
return err
}
}
Expand All @@ -1296,9 +1296,9 @@ func (c *Compiler) compileCallExpr(nd *node.CallExpr) error {
}
}

if nd.NamedArgs.Ellipsis != nil {
if nd.NamedArgs.Var != nil {
flags |= OpCallFlagVarNamedArgs
if err := c.Compile(nd.NamedArgs.Ellipsis.Value); err != nil {
if err := c.Compile(nd.NamedArgs.Var.Value); err != nil {
return err
}
}
Expand Down
42 changes: 21 additions & 21 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestCompiler_Compile(t *testing.T) {
),
))

expectCompile(t, `param (;a=1, ...na)`, bytecode(
expectCompile(t, `param (a=1, **na)`, bytecode(
Array{Int(1)},
compFunc(concatInsts(
makeInst(OpGetLocal, 0),
Expand All @@ -360,7 +360,7 @@ func TestCompiler_Compile(t *testing.T) {
))

// multiple declaration requires parentheses
expectCompileError(t, `param a, b`, `Parse Error: expected ';', found ','`)
expectCompileError(t, `param a, b`, `Parse Error: expected statement, found ','`)
expectCompileError(t, `global a, b`, `Parse Error: expected ';', found ','`)
expectCompileError(t, `var a, b`, `Parse Error: expected ';', found ','`)
// param declaration can only be at the top scope
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestCompiler_Compile(t *testing.T) {
withLocals(1),
),
))
expectCompile(t, `param (a, b, ...c)`, bytecode(
expectCompile(t, `param (a, b, *c)`, bytecode(
Array{},
compFunc(concatInsts(
makeInst(OpReturn, 0),
Expand All @@ -427,7 +427,7 @@ func TestCompiler_Compile(t *testing.T) {
withLocals(1),
),
))
expectCompile(t, `param (arg1, ...varg); global (a, b); var c = arg1; c = b`, bytecode(
expectCompile(t, `param (arg1, *varg); global (a, b); var c = arg1; c = b`, bytecode(
Array{String("a"), String("b")},
compFunc(concatInsts(
makeInst(OpGetLocal, 0),
Expand Down Expand Up @@ -995,7 +995,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `f1 := func(a) { return a }; f1(...[1, 2]);`, bytecode(
expectCompile(t, `f1 := func(a) { return a }; f1(*[1, 2]);`, bytecode(
Array{
compFunc(concatInsts(
makeInst(OpGetLocal, 0),
Expand All @@ -1022,8 +1022,8 @@ func TestCompiler_Compile(t *testing.T) {
))

for _, s := range []string{
`f1 := func(a) { return a }; f1(...[1, 2]);`,
`f1 := func(a) => a; f1(...[1, 2]);`} {
`f1 := func(a) { return a }; f1(*[1, 2]);`,
`f1 := (a) => a; f1(*[1, 2]);`} {
expectCompile(t, s, bytecode(
Array{
compFunc(concatInsts(
Expand Down Expand Up @@ -1051,7 +1051,7 @@ func TestCompiler_Compile(t *testing.T) {
))
}

for _, s := range []string{`func() { return 5 + 10 }`, `func() => 5 + 10`} {
for _, s := range []string{`func() { return 5 + 10 }`, `() => 5 + 10`} {
expectCompile(t, s, bytecode(
Array{
Int(5),
Expand Down Expand Up @@ -1090,7 +1090,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => 5 + 10`, bytecode(
expectCompile(t, `() => 5 + 10`, bytecode(
Array{
Int(5),
Int(10),
Expand Down Expand Up @@ -1127,7 +1127,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { 1; 2 }`, bytecode(
expectCompile(t, `() => { 1; 2 }`, bytecode(
Array{
Int(1),
Int(2),
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { 1; return 2 }`, bytecode(
expectCompile(t, `() => { 1; return 2 }`, bytecode(
Array{
Int(1),
Int(2),
Expand Down Expand Up @@ -1203,7 +1203,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { if(true) { return 1 } else { return 2 } }`, bytecode(
expectCompile(t, `() => { if(true) { return 1 } else { return 2 } }`, bytecode(
Array{
Int(1),
Int(2),
Expand Down Expand Up @@ -1253,7 +1253,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { 1; if(true) { 2 } else { 3 }; 4 }`, bytecode(
expectCompile(t, `() => { 1; if(true) { 2 } else { 3 }; 4 }`, bytecode(
Array{
Int(1),
Int(2),
Expand Down Expand Up @@ -1293,7 +1293,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { }`, bytecode(
expectCompile(t, `() => { }`, bytecode(
Array{
compFunc(concatInsts(
makeInst(OpReturn, 0),
Expand Down Expand Up @@ -1323,7 +1323,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { 24 }()`, bytecode(
expectCompile(t, `() => { 24 }()`, bytecode(
Array{
Int(24),
compFunc(concatInsts(
Expand Down Expand Up @@ -1355,7 +1355,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `(func() => 24)()`, bytecode(
expectCompile(t, `(() => 24)()`, bytecode(
Array{
Int(24),
compFunc(concatInsts(
Expand All @@ -1371,7 +1371,7 @@ func TestCompiler_Compile(t *testing.T) {
)),
))

expectCompile(t, `func() => { return 24 }()`, bytecode(
expectCompile(t, `() => { return 24 }()`, bytecode(
Array{
Int(24),
compFunc(concatInsts(
Expand Down Expand Up @@ -1429,7 +1429,7 @@ func TestCompiler_Compile(t *testing.T) {
),
))

expectCompile(t, `f := func() => 24; f();`, bytecode(
expectCompile(t, `f := () => 24; f();`, bytecode(
Array{
Int(24),
compFunc(concatInsts(
Expand Down Expand Up @@ -1557,7 +1557,7 @@ func TestCompiler_Compile(t *testing.T) {
),
))

expectCompile(t, `f := func(...a) { return a }; f(1, 2, 3);`, bytecode(
expectCompile(t, `f := func(*a) { return a }; f(1, 2, 3);`, bytecode(
Array{
compFunc(concatInsts(
makeInst(OpGetLocal, 0),
Expand Down Expand Up @@ -2220,7 +2220,7 @@ func TestCompiler_Compile(t *testing.T) {
),
)

expectCompile(t, `f := func(...a) { return a }; f(1, 2, 3);`, bytecode(
expectCompile(t, `f := func(*a) { return a }; f(1, 2, 3);`, bytecode(
Array{
compFunc(concatInsts(
makeInst(OpGetLocal, 0),
Expand Down Expand Up @@ -2733,7 +2733,7 @@ func TestCompilerScopes(t *testing.T) {
))

expectCompile(t, `
func() => {
() => {
if a := 1; a {
a = 2
b := a
Expand Down
8 changes: 4 additions & 4 deletions encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,21 @@ func TestEncDecObjects(t *testing.T) {

func TestEncDecBytecode(t *testing.T) {
testEncDecBytecode(t, `
param (arg0, arg1, ...varg; na0=100, na1=200, ...na)
param (arg0, arg1, *varg; na0=100, na1=200, **na)
return [arg0, arg1, varg, na0, na1, na.dict]`, &testopts{
args: Array{gad.Int(1), gad.Int(2), gad.Int(3)},
namedArgs: gad.Dict{"na0": gad.Int(4), "na2": gad.Int(5)},
}, gad.Array{gad.Int(1), gad.Int(2), gad.Array{gad.Int(3)}, gad.Int(4), gad.Int(200), gad.Dict{"na2": gad.Int(5)}})

testEncDecBytecode(t, `
param (arg0, arg1, ...varg; na0=100, na1=200, ...na)
param (arg0, arg1, *varg; na0=100, na1=200, **na)
return [arg0, arg1, varg, na0, na1, na.dict]`, &testopts{
args: Array{gad.Int(1), gad.Int(2), gad.Int(3)},
namedArgs: gad.Dict{"na2": gad.Int(5)},
}, gad.Array{gad.Int(1), gad.Int(2), gad.Array{gad.Int(3)}, gad.Int(100), gad.Int(200), gad.Dict{"na2": gad.Int(5)}})

testEncDecBytecode(t, `
f := func(arg0, arg1, ...varg; na0=100, ...na) {
f := func(arg0, arg1, *varg; na0=100, **na) {
return [arg0, arg1, varg, na0, na.dict]
}
return f(1,2,3,na0=4,na1=5)`, nil, gad.Array{gad.Int(1), gad.Int(2), gad.Array{gad.Int(3)}, gad.Int(4), gad.Dict{"na1": gad.Int(5)}})
Expand All @@ -424,7 +424,7 @@ func TestEncDecBytecode(t *testing.T) {
m := {a: 1, b: ["abc"], c: {x: bytes()}, builtins: [append, len]}`, nil, gad.Nil)

testEncDecBytecode(t, `
f := func(arg0, arg1, ...varg; na0=3, ...na) {
f := func(arg0, arg1, *varg; na0=3, **na) {
return [arg0, arg1, varg, na0, na.dict, nil, true, false, "", -1, 0, 1, 2u, 3.0, 123.456d, 'a', bytes(0, 1, 2)]
}
f(1,2,na0=4,na1=5)
Expand Down
14 changes: 7 additions & 7 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestEval(t *testing.T) {
name: "namedParams3",
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("b"), Int(3)}, KeyValue{String("c"), Int(4)}}),
sr: []scriptResult{
{`param (a=1,b=2,...other)`, Nil},
{`param (a=1,b=2,**other)`, Nil},
{`a`, Int(1)},
{`b`, Int(3)},
{`string(other)`, String("(;c=4)")},
Expand All @@ -129,7 +129,7 @@ func TestEval(t *testing.T) {
name: "paramsAndNamedParams1",
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("c"), Int(4)}}),
sr: []scriptResult{
{`param (a;b=1,...other)`, Nil},
{`param (a;b=1,**other)`, Nil},
{`a`, Nil},
{`b`, Int(1)},
{`string(other)`, String("(;c=4)")},
Expand All @@ -139,7 +139,7 @@ func TestEval(t *testing.T) {
name: "paramsAndNamedParams2",
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("c"), Int(4)}, KeyValue{String("d"), Int(5)}}),
sr: []scriptResult{
{`param (a;b=1,c=2,...other)`, Nil},
{`param (a;b=1,c=2,**other)`, Nil},
{`a`, Nil},
{`b`, Int(1)},
{`c`, Int(4)},
Expand All @@ -150,7 +150,7 @@ func TestEval(t *testing.T) {
name: "paramsAndNamedParams3",
args: []Object{Int(1), Int(2)},
sr: []scriptResult{
{`param (a, b, c;d=100,e=10,...other)`, Nil},
{`param (a, b, c;d=100,e=10,**other)`, Nil},
{`a`, Int(1)},
{`b`, Int(2)},
{`c`, Nil},
Expand All @@ -164,7 +164,7 @@ func TestEval(t *testing.T) {
args: []Object{Int(1), Int(2)},
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("e"), Int(6)}, KeyValue{String("f"), Int(7)}}),
sr: []scriptResult{
{`param (a, b, c;d=100,e=10,...other)`, Nil},
{`param (a, b, c;d=100,e=10,**other)`, Nil},
{`a`, Int(1)},
{`b`, Int(2)},
{`c`, Nil},
Expand All @@ -178,7 +178,7 @@ func TestEval(t *testing.T) {
args: []Object{Int(1), Int(2), Int(3)},
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("e"), Int(6)}, KeyValue{String("f"), Int(7)}}),
sr: []scriptResult{
{`param (a, ...otherArgs;...other)`, Nil},
{`param (a, *otherArgs;**other)`, Nil},
{`string(otherArgs)`, String("[2, 3]")},
},
},
Expand All @@ -187,7 +187,7 @@ func TestEval(t *testing.T) {
args: []Object{Int(1), Int(2), Int(3)},
namedArgs: NewNamedArgs(KeyValueArray{KeyValue{String("e"), Int(6)}, KeyValue{String("f"), Int(7)}}),
sr: []scriptResult{
{`param (a, ...otherArgs;d=100,e=10,...other)`, Nil},
{`param (a, *otherArgs;d=100,e=10,**other)`, Nil},
{`a`, Int(1)},
{`string(otherArgs)`, String("[2, 3]")},
{`d`, Int(100)},
Expand Down
20 changes: 10 additions & 10 deletions new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ func TestVMDestructuring(t *testing.T) {
return [x, a, b]`, nil, Array{Int(3), Nil, Nil})
expectRun(t, `
var x = 10
a, b := func(...args) {
a, b := func(*args) {
x, y := args
return [x, y]
}(1, 2)
return [x, a, b]`, nil, Array{Int(10), Int(1), Int(2)})
expectRun(t, `
var x = 10
a, b := func(...args) {
a, b := func(*args) {
var y
x, y = args
return [x, y]
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestVMDestructuring(t *testing.T) {
}(1, 2), 4`, nil, Array{Array{Int(2), Int(3)}, Int(4)})

expectRun(t, `
param ...args
param *args
mapEach := func(seq, fn) {
Expand Down Expand Up @@ -949,7 +949,7 @@ func TestVM_Invoke(t *testing.T) {
t.Run("apply", func(t *testing.T) {
scr := `
global apply
sum := func(...args) {
sum := func(*args) {
println("called f", args)
s := 0
for v in args {
Expand All @@ -969,7 +969,7 @@ return apply(sum, 1, 2, 3)
t.Run("apply indirect", func(t *testing.T) {
scr := `
global apply
sum := func(...args) {
sum := func(*args) {
println("sum args", args)
s := 0
for v in args {
Expand All @@ -978,8 +978,8 @@ sum := func(...args) {
}
return s
}
f := func(fn, ...args) {
return fn(...args)
f := func(fn, *args) {
return fn(*args)
}
return apply(f, sum, 1, 2, 3)
`
Expand All @@ -992,7 +992,7 @@ return apply(f, sum, 1, 2, 3)
t.Run("apply indirect 2", func(t *testing.T) {
scr := `
global apply
sum := func(...args) {
sum := func(*args) {
println("sum args", args)
s := 0
for v in args {
Expand All @@ -1001,8 +1001,8 @@ sum := func(...args) {
}
return s
}
f := func(fn, ...args) {
return apply(fn, ...args)
f := func(fn, *args) {
return apply(fn, *args)
}
return apply(f, sum, 1, 2, 3)
`
Expand Down
Loading

0 comments on commit f2d9400

Please sign in to comment.