diff options
| author | seh <henesy.dev@gmail.com> | 2019-03-04 00:52:38 -0600 |
|---|---|---|
| committer | seh <henesy.dev@gmail.com> | 2019-03-04 00:52:38 -0600 |
| commit | 78873542e74f63d289a1a44ead0dd143a9b11fcf (patch) | |
| tree | 2b3b1cf2a1810555e1a82305a80d504c6d93e779 /Arrays | |
| parent | 6a49bdd014794981f99ed32969b3dac2db78e981 (diff) | |
add example and frame for arrays ex
Diffstat (limited to 'Arrays')
| -rw-r--r-- | Arrays/README.md | 69 | ||||
| -rw-r--r-- | Arrays/arrays.b | 125 |
2 files changed, 194 insertions, 0 deletions
diff --git a/Arrays/README.md b/Arrays/README.md new file mode 100644 index 0000000..6e559b9 --- /dev/null +++ b/Arrays/README.md @@ -0,0 +1,69 @@ +# Arrays + +Arrays in Limbo are dynamic and indexed by 0. They can be initialized providing a size, or declared and initialized in two separate statements if no size is specified. + +Disclaimer: I don't like this example very much, but I'm not sure how to shore it up. The less on here is to use lists. + +## Source + +### arrays.b:20,30 + + + +### arrays.b:32,44 + + + +### arrays.b:46,57 + + + +### arrays.b:59,67 + + + +### arrays.b:69,84 + + + +### arrays.b:86,94 + + + +### arrays.b:96,122 + + + +## Demo + + ; limbo arrays.b + ; arrays + Len nums: 0 + Len nums: 6 + [ 0 2 4 6 8 10] + + Len arr: 12 + [ b a b y d u c k s ! !] + + Len dbl: 24 + [ b b a a b b y y d d u u c c k k s s ! ! ! !] + + Len chars: 6 + [ a b c d e f] + + Len nest: 0 + Len nest: 4 + Lens: [ 2 4 8 16] + + Len buf: 10 + [ 3 3 3 3 3 3 3 3 3 3] + + Len two: 4 + Lens: [ 1 0 1 0] + [ (ducks quack) (nil, nil) (inferno os) (nil, nil)] + ; + +## Exercises + +- Play with the widths of different arrays, what happens? +- What can you initialize with the `* =>` operator, what can't you? diff --git a/Arrays/arrays.b b/Arrays/arrays.b new file mode 100644 index 0000000..671efff --- /dev/null +++ b/Arrays/arrays.b @@ -0,0 +1,125 @@ +implement Arrays; + +include "sys.m"; +include "draw.m"; + +sys: Sys; +print: import sys; + +Arrays: module { + init: fn(nil: ref Draw->Context, nil: list of string); +}; + +nums: array of int; + +width: int = 6; + +init(nil: ref Draw->Context, nil: list of string) { + sys = load Sys Sys->PATH; + + # Nums + print("Len nums: %d\n", len nums); + + nums = array[width] of int; + + print("Len nums: %d\n", len nums); + + print("["); + for(i := 0; i < width; i++) + print(" %d", nums[i] = i << 1); + print("]\n\n"); + + # Arr + arr := array[12] of byte; + arrstr := "baby ducks!!"; + + for(i = 0; i < len arrstr; i++) + arr[i] = byte arrstr[i]; + + print("Len arr: %d\n", len arr); + + print("["); + for(i = 0; i < len arr; i++) + print(" %c", int arr[i]); + print("]\n\n"); + + # Dbl + dbl := array[len arr *2] of byte; + + for(i = 0; i < len arr; i++) + dbl[i*2] = dbl[i*2+1] = arr[i]; + + print("Len dbl: %d\n", len dbl); + + print("["); + for(i = 0; i < len dbl; i++) + print(" %c", int dbl[i]); + print("]\n\n"); + + # Chars + chars := array[] of {"a", "b", "c", "d", "e", "f"}; + + print("Len chars: %d\n", len chars); + + print("["); + for(i = 0; i < len chars; i++) + print(" %s", chars[i]); + print("]\n\n"); + + # Nest + nest: array of array of string; + + print("Len nest: %d\n", len nest); + + nest = array[4] of array of string; + + print("Len nest: %d\n", len nest); + + for(i = 0; i < len nest; i++) + nest[i] = array[2 << i] of string; + + print("Lens: ["); + for(i = 0; i < len nest; i++) + print(" %d", len nest[i]); + print("]\n\n"); + + # Buf + buf := array[10] of {* => byte 3}; + + print("Len buf: %d\n", len buf); + + print("["); + for(i = 0; i < len buf; i++) + print(" %d", int buf[i]); + print("]\n\n"); + + # Two + two := array[4] of list of (string, string); + + #two = array[4] of list of (string, string); + + two[0] = ("ducks", "quack") :: two[0]; + + two[2] = ("inferno", "os") :: two[2]; + + print("Len two: %d\n", len two); + + print("Lens: ["); + for(i = 0; i < len two; i++) + print(" %d", len two[i]); + print("]\n"); + + print("["); + for(i = 0; i < len two; i++) { + if(two[i] == nil) { + print(" (nil, nil)"); + continue; + } + + (s0, s1) := hd two[i]; + print(" (%s %s)", s0, s1); + } + print("]\n"); + + exit; +} |
