Skip to content

Commit

Permalink
core!: 1) add negative index get for slices;
Browse files Browse the repository at this point in the history
cli!: 2) pass named args to script.
  • Loading branch information
moisespsena committed Mar 25, 2024
1 parent 3954567 commit 3d48ceb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
15 changes: 13 additions & 2 deletions cmd/gad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,16 @@ func parseFlags(

flagset.Usage = func() {
_, _ = fmt.Fprint(flagset.Output(),
"Usage: gad [flags] [Gad script file]\n\n",
"If script file is not provided, REPL terminal application is started\n",
"Usage: gad [flags] [SCRIPT_FILE [ARGS...]]\n\n",
"If script file is not provided, REPL terminal application is started.\n\n",
"If script file is provided, pass named params with '--' prefix and named flags with '-' prefix.\n",
" Script example for join arguments:\n\n",
" // usages: 1) SCRIPT.gad a b c (result: a,b,c)\n",
" // 2) SCRIPT.gad a b c --sep + (result: a+b+c)\n",
" // 3) SCRIPT.gad a b c -ln (result: a,b,c\\n)\n",
" // 4) SCRIPT.gad a b c --sep + -ln (result: a+b+c\\n)\n",
" param (*args, sep=\",\", ln=no)\n",
" if !args { return }\n for i, arg in args[:-1] { print(arg, sep) }\n print(args[-1])\n if ln { println() }\n\n",
"Use - to read from stdin\n\n",
"\nFlags:\n",
)
Expand Down Expand Up @@ -609,6 +617,9 @@ func (s *Script) execute() error {
i++
continue
}
} else if strings.HasPrefix(arg, "-") && len(arg) > 1 {
namedArgs[arg[1:]] = gad.Yes
continue
}
newArgs = append(newArgs, gad.Str(arg))
}
Expand Down
26 changes: 22 additions & 4 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ func (o RawStr) IndexGet(_ *VM, index Object) (Object, error) {
switch v := index.(type) {
case Int:
idx = int(v)
if idx < 0 {
idx = len(o) + idx
}
case Uint:
idx = int(v)
case Char:
Expand Down Expand Up @@ -359,6 +362,9 @@ func (o Str) IndexGet(_ *VM, index Object) (Object, error) {
switch v := index.(type) {
case Int:
idx = int(v)
if idx < 0 {
idx = len(o) + idx
}
case Uint:
idx = int(v)
case Char:
Expand Down Expand Up @@ -478,6 +484,9 @@ func (o Bytes) IndexSet(_ *VM, index, value Object) error {
switch v := index.(type) {
case Int:
idx = int(v)
if idx < 0 {
idx = len(o) + idx
}
case Uint:
idx = int(v)
default:
Expand All @@ -504,6 +513,9 @@ func (o Bytes) IndexGet(_ *VM, index Object) (Object, error) {
switch v := index.(type) {
case Int:
idx = int(v)
if idx < 0 {
idx = len(o) + idx
}
case Uint:
idx = int(v)
default:
Expand Down Expand Up @@ -825,15 +837,18 @@ func (o Array) IndexSet(_ *VM, index, value Object) error {
switch v := index.(type) {
case Int:
idx := int(v)
if idx < 0 {
idx = len(o) + idx
}
if idx >= 0 && idx < len(o) {
o[v] = value
o[idx] = value
return nil
}
return ErrIndexOutOfBounds
case Uint:
idx := int(v)
if idx >= 0 && idx < len(o) {
o[v] = value
o[idx] = value
return nil
}
return ErrIndexOutOfBounds
Expand All @@ -846,14 +861,17 @@ func (o Array) IndexGet(_ *VM, index Object) (Object, error) {
switch v := index.(type) {
case Int:
idx := int(v)
if idx < 0 {
idx = len(o) + idx
}
if idx >= 0 && idx < len(o) {
return o[v], nil
return o[idx], nil
}
return nil, ErrIndexOutOfBounds
case Uint:
idx := int(v)
if idx >= 0 && idx < len(o) {
return o[v], nil
return o[idx], nil
}
return nil, ErrIndexOutOfBounds
}
Expand Down
5 changes: 4 additions & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestVMArray(t *testing.T) {
expectRun(t, fmt.Sprintf("idx := %d; return %s.(idx)", idx, arrStr),
nil, arr[idx])
}
expectErrIs(t, fmt.Sprintf("%s[%d]", arrStr, -1), nil, ErrIndexOutOfBounds)
expectErrIs(t, fmt.Sprintf("%s[%d]", arrStr, -10), nil, ErrIndexOutOfBounds)
expectErrIs(t, fmt.Sprintf("%s[%d]", arrStr, arrLen), nil, ErrIndexOutOfBounds)

// slice operator
Expand All @@ -95,6 +95,9 @@ func TestVMArray(t *testing.T) {

expectRun(t, fmt.Sprintf("return %s[:]", arrStr), nil, arr)
expectRun(t, fmt.Sprintf("return %s[%d:%d]", arrStr, 2, 2), nil, Array{})
expectRun(t, `return "ab"[1]`, nil, Int('b'))
expectRun(t, `return "ab"[-1]`, nil, Int('b'))
expectRun(t, `return "ab"[-2]`, nil, Int('a'))
expectErrIs(t, fmt.Sprintf("return %s[%d:\"\"]", arrStr, -1), nil, ErrType)
expectErrIs(t, fmt.Sprintf("return %s[:%d]", arrStr, arrLen+1), nil, ErrIndexOutOfBounds)
expectErrIs(t, fmt.Sprintf("%s[%d:%d]", arrStr, 2, 1), nil, ErrInvalidIndex)
Expand Down

0 comments on commit 3d48ceb

Please sign in to comment.