Skip to content

Commit

Permalink
Add args() builtin for getting argv[]
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Dec 23, 2019
1 parent 861c9df commit 828dfd1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ These are the right primitives, but we can build much more sophisticated systems

### System interfaces

- `args() => list`: argv of the currently running process
- `in(callback<string> => boolean)`: Read from stdin. The callback function returns a boolean that determines whether to continue reading from input.
- `out(string)`: Print to stdout.
- `dir(string, callback<list>)`: List the contents of a directory. The callback gets a list of values of the form `{name: string, len: number, dir: boolean}`. Effectively `stat()` for all files in the directory.
Expand Down
9 changes: 9 additions & 0 deletions pkg/ink/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (ctx *Context) LoadEnvironment() {
ctx.LoadFunc("load", inkLoad)

// system interfaces
ctx.LoadFunc("args", inkArgs)
ctx.LoadFunc("in", inkIn)
ctx.LoadFunc("out", inkOut)
ctx.LoadFunc("dir", inkDir)
Expand Down Expand Up @@ -143,6 +144,14 @@ func inkLoad(ctx *Context, in []Value) (Value, error) {
}
}

func inkArgs(ctx *Context, in []Value) (Value, error) {
comp := CompositeValue{}
for i, v := range os.Args {
comp[nToS(float64(i))] = StringValue(v)
}
return comp, nil
}

func inkIn(ctx *Context, in []Value) (Value, error) {
if len(in) != 1 {
return nil, Err{
Expand Down
13 changes: 13 additions & 0 deletions samples/test.ink
Original file line number Diff line number Diff line change
Expand Up @@ -1071,5 +1071,18 @@ m('load() import semantics')
A = B, false)
)

m('args() list')
(
hasSuffix? := str.hasSuffix?

as := args()
t('args() returns a list'
type(as), 'composite')
t('first item in args() is the Ink executable'
hasSuffix?(as.0, 'ink'), true)
t('second item in args() is the test script'
hasSuffix?(as.1, 'test.ink'), true)
)

` end test suite, print result `
(s.end)()
1 change: 1 addition & 0 deletions utils/ink.vim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ highlight link inkIdentifier Identifier
syntax match builtinFunctionCall "\v[A-Za-z@!?][A-Za-z0-9@!?]*\(" contains=inkIdentifier,inkBuiltin,inkDelim
syntax keyword inkBuiltin load contained

syntax keyword inkBuiltin args contained
syntax keyword inkBuiltin in contained
syntax keyword inkBuiltin out contained
syntax keyword inkBuiltin dir contained
Expand Down

0 comments on commit 828dfd1

Please sign in to comment.