From 0283ccf588a5243b2a992d3d726c2f1344c5438a Mon Sep 17 00:00:00 2001 From: seh Date: Mon, 18 Mar 2019 16:36:39 -0500 Subject: stdlib modules have -M suffix for dir ;; frame for lists example --- Args-M/README.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Args-M/args.b | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ Args/README.md | 62 -------------------------------------------------------- Args/args.b | 54 ------------------------------------------------ Lists/README.md | 17 ++++++++++++++++ Lists/lists.b | 21 +++++++++++++++++++ README.md | 2 +- 7 files changed, 155 insertions(+), 117 deletions(-) create mode 100644 Args-M/README.md create mode 100644 Args-M/args.b delete mode 100644 Args/README.md delete mode 100644 Args/args.b create mode 100644 Lists/README.md create mode 100644 Lists/lists.b diff --git a/Args-M/README.md b/Args-M/README.md new file mode 100644 index 0000000..2320046 --- /dev/null +++ b/Args-M/README.md @@ -0,0 +1,62 @@ +# Command-Line Arguments + +Inferno has a dedicated module for processing commandline flags and arguments, [arg(2)](http://man.cat-v.org/inferno/2/arg). + +## Source + +### args.b:17,22 + +The module `Arg` is loaded and initialized. As per the manual, the `arg->init()` function must be called before any other functions can be called from `Arg`. + +The usage message is also set pre-emptively for use later, if necessary. + +### args.b:24,37 + +There are two flag arguments, `r` and `c` which set reversal of arguments and the list item indicator mark, respectively. + +The flag `r` is a binary option toggle, the flag's presence as an argument is sufficient to change the value of `rev`. + +The flag `c` takes an argument. The `arg->earg()` function is used to pop the flag's argument out of the list. Specifically with regards to `earg()` rather than `arg()` as per the manual, `earg()` will call `arg->usage()` if the relevant argument does not exist. + +Note: After the processing of all flags, `argv` is re-set from the value contained within `arg` to remove the elements (if any) utilized by flags and their arguments. + +### args.b:41,51 + +This section utilizes the `rev` variable to (naively) reverse the list `argv`. After the `rev` check, the list `argv` is printed in order from beginning to end with each element being printed on a new line with the `mark` variable being prefixed to the list element currently at the head of the `argv` list each iteration during printout. + +## Demo + + ; limbo args.b + ; args -h + usage: args [-r] [-c mark] words... + ; args a b c d + Argc after flags: 4 + → a + → b + → c + → d + ; args -r a b c d + Argc after flags: 4 + → d + → c + → b + → a + ; args -c 'quack: ' a b c d + Argc after flags: 4 + quack: a + quack: b + quack: c + quack: d + ; args -r -c '-> ' d c b a + Argc after flags: 4 + -> a + -> b + -> c + -> d + ; + +## Exercises + +- Can you trick `earg()` into accepting a nil value? +- How would you pull an `int` out of `earg()`? +- How would you trip the `usage()` message call-able from `earg()`? diff --git a/Args-M/args.b b/Args-M/args.b new file mode 100644 index 0000000..392f371 --- /dev/null +++ b/Args-M/args.b @@ -0,0 +1,54 @@ +implement Args; + +include "sys.m"; +include "draw.m"; + +include "arg.m"; + +sys: Sys; +print: import sys; + +Args: module { + init: fn(nil: ref Draw->Context, argv: list of string); +}; + +init(nil: ref Draw->Context, argv: list of string) { + sys = load Sys Sys->PATH; + arg := load Arg Arg->PATH; + if(arg == nil) + raise "bad module load"; + + arg->init(argv); + arg->setusage(arg->progname() + " [-r] [-c mark] words..."); + + rev := 0; + mark := "→ "; + + while((opt := arg->opt()) != 0) + case opt { + 'r' => + rev = 1; + 'c' => + mark = arg->earg(); + * => + arg->usage(); + } + + argv = arg->argv(); + + print("Argc after flags: %d\n", len argv); + + if(rev) { + argl: list of string; + + for(; argv != nil; argv = tl argv) + argl = hd argv :: argl; + + argv = argl; + } + + for(; argv != nil; argv = tl argv) + sys->print("%s %s\n", mark, hd argv); + + exit; +} diff --git a/Args/README.md b/Args/README.md deleted file mode 100644 index 2320046..0000000 --- a/Args/README.md +++ /dev/null @@ -1,62 +0,0 @@ -# Command-Line Arguments - -Inferno has a dedicated module for processing commandline flags and arguments, [arg(2)](http://man.cat-v.org/inferno/2/arg). - -## Source - -### args.b:17,22 - -The module `Arg` is loaded and initialized. As per the manual, the `arg->init()` function must be called before any other functions can be called from `Arg`. - -The usage message is also set pre-emptively for use later, if necessary. - -### args.b:24,37 - -There are two flag arguments, `r` and `c` which set reversal of arguments and the list item indicator mark, respectively. - -The flag `r` is a binary option toggle, the flag's presence as an argument is sufficient to change the value of `rev`. - -The flag `c` takes an argument. The `arg->earg()` function is used to pop the flag's argument out of the list. Specifically with regards to `earg()` rather than `arg()` as per the manual, `earg()` will call `arg->usage()` if the relevant argument does not exist. - -Note: After the processing of all flags, `argv` is re-set from the value contained within `arg` to remove the elements (if any) utilized by flags and their arguments. - -### args.b:41,51 - -This section utilizes the `rev` variable to (naively) reverse the list `argv`. After the `rev` check, the list `argv` is printed in order from beginning to end with each element being printed on a new line with the `mark` variable being prefixed to the list element currently at the head of the `argv` list each iteration during printout. - -## Demo - - ; limbo args.b - ; args -h - usage: args [-r] [-c mark] words... - ; args a b c d - Argc after flags: 4 - → a - → b - → c - → d - ; args -r a b c d - Argc after flags: 4 - → d - → c - → b - → a - ; args -c 'quack: ' a b c d - Argc after flags: 4 - quack: a - quack: b - quack: c - quack: d - ; args -r -c '-> ' d c b a - Argc after flags: 4 - -> a - -> b - -> c - -> d - ; - -## Exercises - -- Can you trick `earg()` into accepting a nil value? -- How would you pull an `int` out of `earg()`? -- How would you trip the `usage()` message call-able from `earg()`? diff --git a/Args/args.b b/Args/args.b deleted file mode 100644 index 392f371..0000000 --- a/Args/args.b +++ /dev/null @@ -1,54 +0,0 @@ -implement Args; - -include "sys.m"; -include "draw.m"; - -include "arg.m"; - -sys: Sys; -print: import sys; - -Args: module { - init: fn(nil: ref Draw->Context, argv: list of string); -}; - -init(nil: ref Draw->Context, argv: list of string) { - sys = load Sys Sys->PATH; - arg := load Arg Arg->PATH; - if(arg == nil) - raise "bad module load"; - - arg->init(argv); - arg->setusage(arg->progname() + " [-r] [-c mark] words..."); - - rev := 0; - mark := "→ "; - - while((opt := arg->opt()) != 0) - case opt { - 'r' => - rev = 1; - 'c' => - mark = arg->earg(); - * => - arg->usage(); - } - - argv = arg->argv(); - - print("Argc after flags: %d\n", len argv); - - if(rev) { - argl: list of string; - - for(; argv != nil; argv = tl argv) - argl = hd argv :: argl; - - argv = argl; - } - - for(; argv != nil; argv = tl argv) - sys->print("%s %s\n", mark, hd argv); - - exit; -} diff --git a/Lists/README.md b/Lists/README.md new file mode 100644 index 0000000..3daa129 --- /dev/null +++ b/Lists/README.md @@ -0,0 +1,17 @@ +# Lists + +Limbo supports lists of a type as a basic construct. + +## Source + +### + + + +## Demo + + + +## Exercises + +- Try reversing a list. diff --git a/Lists/lists.b b/Lists/lists.b new file mode 100644 index 0000000..0837c8a --- /dev/null +++ b/Lists/lists.b @@ -0,0 +1,21 @@ +implement Lists; + +include "sys.m"; +include "draw.m"; + +include "arg.m"; + +sys: Sys; +print: import sys; + +Lists: module { + init: fn(nil: ref Draw->Context, argv: list of string); +}; + +init(nil: ref Draw->Context, argv: list of string) { + sys = load Sys Sys->PATH; + + + + exit; +} diff --git a/README.md b/README.md index f410025..5e6d28c 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Core language functionality: Standard library modules: -- [Command-Line Arguments](./Args) +- [Command-Line Arguments](./Args-M) ## References -- cgit v1.2.3