diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:34 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:45 -0800 |
| commit | 8a3c32408733a2f5e14f8a2dbafa3f980b2f73a1 (patch) | |
| tree | 68f1e8a8b956c33ed24cc7a6a369fd97b8849321 /lib | |
| parent | 359152da1772ce501609edd8f84b4985ed3e42f2 (diff) | |
Update to new syntax.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/base64.nom | 44 | ||||
| -rw-r--r-- | lib/consolecolor.nom | 20 | ||||
| -rw-r--r-- | lib/file_hash.nom | 28 | ||||
| -rw-r--r-- | lib/object.nom | 64 | ||||
| -rw-r--r-- | lib/os.nom | 31 | ||||
| -rw-r--r-- | lib/things.nom | 128 |
6 files changed, 171 insertions, 144 deletions
diff --git a/lib/base64.nom b/lib/base64.nom index 6f22afc..44d68cb 100644 --- a/lib/base64.nom +++ b/lib/base64.nom @@ -1,17 +1,17 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file defines actions for encoding/decoding base 64, as specified in: https://tools.ietf.org/html/rfc4648 $b64_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" -$b64_chars = [: for $ in 1 to (size of $b64_str): add ($b64_str|character $)] +$b64_chars = [: for $ in 1 to (size of $b64_str): add ($b64_str, character $)] $reverse_b64 = {: for $c in $b64_chars at $i: add $c = ($i - 1)} $reverse_b64."=" = 64 -set $reverse_b64's metatable to {__index: -> 0} +set $reverse_b64's metatable to {.__index = (-> 0)} test: $cases = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"] for $len = $encoded in $cases: - $plain = ("foobar"|from 1 to ($len - 1)) + $plain = ("foobar", from 1 to ($len - 1)) assume (base64 $plain) == $encoded assume (base64 decode $encoded) == $plain @@ -19,32 +19,32 @@ externally [base64 $str, base64 encode $str, $str base64] all mean: $chars = [] for $i in 1 to (size of $str) via 3: $bytes = [=lua "\$str:byte(\$i, \($i + 2))"] - $chars|add $b64_chars.((($bytes.1 & 252) >> 2) + 1) + $chars, add $b64_chars.((($bytes.1 & 252) >> 2) + 1) if (size of $bytes) is: 3: - $chars|add $b64_chars.((($bytes.1 & 3) << 4) + (($bytes.2 & 240) >> 4) + 1) - $chars|add $b64_chars.((($bytes.2 & 15) << 2) + (($bytes.3 & 192) >> 6) + 1) - $chars|add $b64_chars.(($bytes.3 & 63) + 1) + $chars, add $b64_chars.((($bytes.1 & 3) << 4) + (($bytes.2 & 240) >> 4) + 1) + $chars, add $b64_chars.((($bytes.2 & 15) << 2) + (($bytes.3 & 192) >> 6) + 1) + $chars, add $b64_chars.(($bytes.3 & 63) + 1) 2: - $chars|add $b64_chars.((($bytes.1 & 3) << 4) + (($bytes.2 & 240) >> 4) + 1) - $chars|add $b64_chars.((($bytes.2 & 15) << 2) + 1) - $chars|add "=" + $chars, add $b64_chars.((($bytes.1 & 3) << 4) + (($bytes.2 & 240) >> 4) + 1) + $chars, add $b64_chars.((($bytes.2 & 15) << 2) + 1) + $chars, add "=" 1: - $chars|add $b64_chars.((($bytes.1 & 3) << 4) + 1) - $chars|add "=" - $chars|add "=" - return ($chars|joined) + $chars, add $b64_chars.((($bytes.1 & 3) << 4) + 1) + $chars, add "=" + $chars, add "=" + return ($chars, joined) externally (chr $) means (=lua "string.char(\$)") externally [decode base64 $str, $str base64 decoded, base64 decode $str] all mean: $chars = [] for $i in 1 to (size of $str) via 4: - $indices = [: for $j in $i to ($i + 3): add $reverse_b64.($str|character $j)] - $chars|add (chr (($indices.1 << 2) + (($indices.2 & 48) >> 4))) - if (($str|character ($i + 2)) == "="): stop - $chars|add (chr ((($indices.2 & 15) << 4) + (($indices.3 & 60) >> 2))) - if (($str|character ($i + 3)) == "="): stop - $chars|add (chr ((($indices.3 & 3) << 6) + $indices.4)) - return ($chars|joined) + $indices = [: for $j in $i to ($i + 3): add $reverse_b64.($str, character $j)] + $chars, add (chr (($indices.1 << 2) + (($indices.2 & 48) >> 4))) + if (($str, character ($i + 2)) == "="): stop + $chars, add (chr ((($indices.2 & 15) << 4) + (($indices.3 & 60) >> 2))) + if (($str, character ($i + 3)) == "="): stop + $chars, add (chr ((($indices.3 & 3) << 6) + $indices.4)) + return ($chars, joined) diff --git a/lib/consolecolor.nom b/lib/consolecolor.nom index 55feade..1f5e926 100644 --- a/lib/consolecolor.nom +++ b/lib/consolecolor.nom @@ -1,22 +1,24 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file defines actions for ANSI console color escape codes. test: $ = (bright "\(green)Color test passed.") -$colors = {..} - normal: 0, "reset color": 0, bright: 1, bold: 1, dim: 2, italic: 3, underscore: 4 - "slow blink": 5, "fast blink": 6, reverse: 7, inverse: 7, inverted: 7 - hidden: 8 +$colors = { + .normal = 0, ."reset color" = 0, .bright = 1, .bold = 1, .dim = 2, .italic = 3 + .underscore = 4, ."slow blink" = 5, ."fast blink" = 6, .reverse = 7, .inverse = 7 + .inverted = 7, .hidden = 8 # There's some other codes, but they're not currently implemented - black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36 - white: 37, "on black": 40, "on red": 41, "on green": 42, "on yellow": 43 - "on blue": 44, "on magenta": 45, "on cyan": 46, "on white": 47 + .black = 30, .red = 31, .green = 32, .yellow = 33, .blue = 34, .magenta = 35 + .cyan = 36, .white = 37, ."on black" = 40, ."on red" = 41, ."on green" = 42 + ."on yellow" = 43, ."on blue" = 44, ."on magenta" = 45, ."on cyan" = 46 + ."on white" = 47 +} for $name = $colornum in $colors: $colornum = "\$colornum" - $compile.action.$name = (..) + $compile.action.$name = for ($compile $text): if $text: return (Lua "('\\027[\($colornum)m'..\($text as lua expr)..'\\027[0m')") diff --git a/lib/file_hash.nom b/lib/file_hash.nom index dd3d520..a45b684 100644 --- a/lib/file_hash.nom +++ b/lib/file_hash.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file defines some actions for hashing files and looking up files by hash. @@ -10,19 +10,21 @@ use "lib/base64.nom" lua> "local \$use_sha1, \$hashlib = pcall(require, 'openssl.digest')" test: assume (hash "hello world") == (hash "hello world") - assume ((hash "hello world") != (hash "goodbye")) or barf " + assume ((hash "hello world") != (hash "goodbye")) or barf (" Hash collision: (hash "hello world") = \(hash "hello world") - (hash "goodbye") = \(hash "goodbye")" + (hash "goodbye") = \(hash "goodbye") + ") - assume (..) - (..) - hash " + assume + ( + hash (" This is a really long string meant to stress test the hashing function and - ensure that it's not overflowing with long inputs." - ..!= "inf" + ensure that it's not overflowing with long inputs. + ") + ) != "inf" - assume ((hash "\000") != (hash "\000\000\000\000\000")) or barf \ + assume ((hash "\000") != (hash "\000\000\000\000\000")) or barf .."Incorrect hashing of null strings" if $use_sha1: @@ -34,9 +36,13 @@ if $use_sha1: return (base64 $hash) ..else: # TODO: remove warning? - say "\027[31;1mWARNING: OpenSSL module not found. Defaulting to a non-cryptographically secure hash function.\027[0m" + say (" + \027[31;1mWARNING: OpenSSL module not found. Defaulting to a non-cryptographically secure \ + ..hash function.\027[0m + ") + externally (hash $) means: - $bytes = ($|bytes) + $bytes = ($, bytes) $hash = ($bytes.1 << 7) for $i in 2 to (size of $bytes): $hash = ((1000003 * $hash) ~ $bytes.$i) diff --git a/lib/object.nom b/lib/object.nom index 6f16410..72e1588 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -1,14 +1,16 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file contains the implementation of an Object-Oriented programming system. -$globals.METAMETHOD_MAP = {..} - "as text": "__tostring", "clean up": "__gc", "+ 1": "__add", "- 1": "__sub" - "* 1": "__mul", "/ 1": "__div", "-": "__unm", "// 1": "__idiv", "mod 1": "__mod" - "^ 1": "__pow", "& 1": "__band", "| 1": "__bor", "~ 1": "__bxor", "~": "__bnot" - "<< 1": "__bshl", ">> 1": "__bshr", "== 1": "__eq", "< 1": "__lt", "<= 1": "__le" - "set 1 = 2": "__newindex", size: "__len", iterate: "__ipairs" - "iterate all": "__pairs" +$globals.METAMETHOD_MAP = { + ."as text" = "__tostring", ."clean up" = "__gc", ."+ 1" = "__add" + ."- 1" = "__sub", ."* 1" = "__mul", ."/ 1" = "__div", ."-" = "__unm" + ."// 1" = "__idiv", ."mod 1" = "__mod", ."^ 1" = "__pow", ."& 1" = "__band" + ."| 1" = "__bor", ."~ 1" = "__bxor", ."~" = "__bnot", ."<< 1" = "__bshl" + .">> 1" = "__bshr", ."== 1" = "__eq", ."< 1" = "__lt", ."<= 1" = "__le" + ."set 1 = 2" = "__newindex", .size = "__len", .iterate = "__ipairs" + ."iterate all" = "__pairs" +} test: object (Dog): @@ -18,45 +20,45 @@ test: my action [bark, woof]: $barks = [: for $ in 1 to $me.barks: add "Bark!"] - return ($barks|joined with " ") + return ($barks, joined with " ") my action [get pissed off]: $me.barks += 1 - $d = (Dog {barks: 2}) + $d = (Dog {.barks = 2}) assume (type of $d) == "Dog" assume ($d is a "Dog") assume $d.barks == 2 - assume (($d|bark) == "Bark! Bark!") - assume (($d|woof) == "Bark! Bark!") - $d|get pissed off + assume (($d, bark) == "Bark! Bark!") + assume (($d, woof) == "Bark! Bark!") + $d, get pissed off assume ($d.barks == 3) - assume (($d|bark) == "Bark! Bark! Bark!") + assume (($d, bark) == "Bark! Bark! Bark!") assume ($d.genus == "Canus") assume ("\($d.class)" == "Dog") assume ($d.genus == "Canus") assume ($d.barks == 3) $d2 = (Dog {}) assume ($d2.barks == 0) or barf "Default initializer failed" - with {$d: Dog {barks: 1}}: - assume (($d|bark) == "Bark!") + with [$d = (Dog {.barks = 1})]: + assume (($d, bark) == "Bark!") object (Corgi) extends (Dog): my action [sploot] "splooted" my action [bark, woof]: $barks = [: for $ in 1 to $me.barks: add "Yip!"] - return ($barks|joined with " ") + return ($barks, joined with " ") $corg = (Corgi {}) assume ($corg.barks == 0) - with {$d: Corgi {barks: 1}}: - assume (($d|sploot) == "splooted") or barf "subclass method failed" - assume (($d|bark) == "Yip!") or barf "inheritance failed" - assume (($d|woof) == "Yip!") + with [$d = (Corgi {.barks = 1})]: + assume (($d, sploot) == "splooted") or barf "subclass method failed" + assume (($d, bark) == "Yip!") or barf "inheritance failed" + assume (($d, woof) == "Yip!") - with {$d: Dog {barks: 2}}: - assume (($d|bark) == "Bark! Bark!") + with [$d = (Dog {.barks = 2})]: + assume (($d, bark) == "Bark! Bark!") (my action $actions $body) compiles to: - lua> " + lua> (" local fn_name = \$actions[1].stub:as_lua_id() local \$args = List(\$actions[1]:get_args()) table.insert(\$args, 1, \(\$me)) @@ -73,19 +75,20 @@ test: lua:add(\(\($alias_args -> $actions.1) as lua)) end end - return lua" + return lua + ") (object $classname extends $parent $class_body) compiles to: unless ($classname.type == "Action"): - compile error at $classname \ + compile error at $classname .."Expected this to be an action, not a \$classname.type" for $ in $classname: unless ($ is text): compile error at $ "Class names should not have arguments." - return (..) - Lua " + return + Lua (" do local class = {name=\(quote $classname.stub)} class.__type = class.name @@ -109,7 +112,8 @@ test: for stub,metamethod in pairs(globals.METAMETHOD_MAP) do class[metamethod] = class[stub:as_lua_id()] end - end" + end + ") -(object $classname $class_body) parses as (..) +(object $classname $class_body) parses as object $classname extends (nil) $class_body @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file defines some actions that interact with the operating system and filesystem. @@ -12,39 +12,44 @@ externally (files for $path) means: return $files externally (nomsu files for $path) means: - for $nomsupath in ($package.nomsupath|all matches of "[^;]+"): + for $nomsupath in ($package.nomsupath, all matches of "[^;]+"): $files = (files for "\($nomsupath)/\$path") if $files: return $files externally (sh> $cmd) means: - lua> " + lua> (" local result = io.popen(\$cmd) local contents = result:read("*a") result:close() - return contents" + return contents + ") test: read file "lib/os.nom" externally (read file $filename) means (=lua "Files.read(\$filename)") -externally [..] +externally [ write to file $filename $text, to file $filename write $text write $text to file $filename -..all mean: +] all mean: assume ($filename != "stdin") or barf "Cannot write to stdin" - lua> "local file = io.open(\$filename, 'w')\nfile:write(\$text)\nfile:close()" + lua> (" + local file = io.open(\$filename, 'w') + file:write(\$text) + file:close() + ") externally (source lines of $tree) means: $source = ($tree.source if ($tree is syntax tree) else $tree) $file = (read file $source.filename) - return (..) - [..] + return + [ : - for $ in ($file|line number at $source.start) to (..) - $file|line number at $source.stop - ..: add ($file|line $) - ..|joined with "\n" + for $ in ($file, line number at $source.start) to + $file, line number at $source.stop + ..: add ($file, line $) + ], joined with "\n" externally (spoof file $text) means ($Files.spoof $text) externally (spoof file $filename = $text) means ($Files.spoof $filename $text) diff --git a/lib/things.nom b/lib/things.nom index de51683..e10b9eb 100644 --- a/lib/things.nom +++ b/lib/things.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # A library for simple object oriented programming. @@ -6,86 +6,91 @@ test: an (Empty) is a thing a (Dog) is a thing: [$it, $its] = [Dog, Dog] - ($its|set up) means: + ($its, set up) means: $its.barks or= 0 - [$its|bark, $its|woof] all mean: + [($its, bark), ($its, woof)] all mean: $barks = [: for $ in 1 to $its.barks: add "Bark!"] - return ($barks|joined with " ") + return ($barks, joined with " ") - ($it|gets pissed off) means: $it.barks += 1 + ($it, gets pissed off) means: $it.barks += 1 (Dog).genus = "Canus" - $d = (a Dog with {barks: 2}) + $d = (a Dog with {.barks = 2}) assume "\$d" == "Dog {barks: 2}" assume (type of $d) == "Dog" assume ($d is a "Dog") assume $d.barks == 2 - assume (($d|bark) == "Bark! Bark!") - assume (($d|woof) == "Bark! Bark!") - $d|gets pissed off + assume (($d, bark) == "Bark! Bark!") + assume (($d, woof) == "Bark! Bark!") + $d, gets pissed off assume ($d.barks == 3) - assume (($d|bark) == "Bark! Bark! Bark!") + assume (($d, bark) == "Bark! Bark! Bark!") assume ($d.genus == "Canus") assume ("\($d.class)" == "Dog") assume ($d.genus == "Canus") assume ($d.barks == 3) $d2 = (a Dog) assume ($d2.barks == 0) or barf "Default initializer failed" - with {$d: a Dog with {barks: 1}}: - assume (($d|bark) == "Bark!") + with [$d = (a Dog with {.barks = 1})]: + assume (($d, bark) == "Bark!") a (Corgi) is a thing: [$it, $its] = [Corgi, Corgi] $it [set up, gets pissed off] like a (Dog) - ($it|as text) means "Dogloaf \{: for $k = $v in $it: add $k = $v}" - ($its|sploot) means "sploooot" - [$its|bark, $its|woof] all mean: + ($it, as text) means "Dogloaf \{: for $k = $v in $it: add $k = $v}" + ($its, sploot) means "sploooot" + [($its, bark), ($its, woof)] all mean: $barks = [: for $ in 1 to $its.barks: add "Yip!"] - return ($barks|joined with " ") + return ($barks, joined with " ") $corg = (a Corgi) assume ($corg.barks == 0) assume "\$corg" == "Dogloaf {barks: 0}" - with {$d: a Corgi with {barks: 1}}: - assume (($d|sploot) == "sploooot") or barf "subclass method failed" - assume (($d|bark) == "Yip!") or barf "inheritance failed" - assume (($d|woof) == "Yip!") + with [$d = (a Corgi with {.barks = 1})]: + assume (($d, sploot) == "sploooot") or barf "subclass method failed" + assume (($d, bark) == "Yip!") or barf "inheritance failed" + assume (($d, woof) == "Yip!") - with {$d: a Dog with {barks: 2}}: - assume (($d|bark) == "Bark! Bark!") + with [$d = (a Dog with {.barks = 2})]: + assume (($d, bark) == "Bark! Bark!") - a (Vec) is a thing with {x, y}: + a (Vec) is a thing with {.x, .y}: $its = (Vec) - ($its|+ $other) means (Vec {x: $its.x + $other.x, y: $its.y + $other.y}) + ($its, + $other) means (Vec {.x = ($its.x + $other.x), .y = ($its.y + $other.y)}) - assume ((Vec {x: 1, y: 2}) + (Vec {x: 10, y: 10})) == (Vec {x: 11, y: 12}) - assume (((Vec {x: 1, y: 2}) + (Vec {x: 10, y: 10})) != (Vec {x: 0, y: 0})) + assume ((Vec {.x = 1, .y = 2}) + (Vec {.x = 10, .y = 10})) == + Vec {.x = 11, .y = 12} + + assume + ((Vec {.x = 1, .y = 2}) + (Vec {.x = 10, .y = 10})) != (Vec {.x = 0, .y = 0}) -[..] +[ $it can $actions like a $class, $it can $actions like an $class $it has $actions like a $class, $it has $actions like an $class $it $actions like a $class, $it $actions like an $class -..all compile to: +] all compile to: $lua = (Lua "") $class_expr = ($class as lua expr) $lines = [] for $a in $actions: - $lines| - add "\($it as lua expr).\($a.stub|as lua id) = \$class_expr.\($a.stub|as lua id)" - $lua|add $lines joined with "\n" + $lines, + add "\($it as lua expr).\($a.stub, as lua id) = \$class_expr.\($a.stub, as lua id)" + $lua, add $lines joined with "\n" return $lua -$METAMETHOD_MAP = {..} - "as text": "__tostring", "clean up": "__gc", "+": "__add", "-": "__sub" - "*": "__mul", "/": "__div", negative: "__unm", "//": "__idiv", mod: "__mod" - "^": "__pow", "&": "__band", "|": "__bor", "~": "__bxor", "~": "__bnot" - "<<": "__bshl", ">>": "__bshr", "==": "__eq", "<": "__lt", "<=": "__le" - "set 1 =": "__newindex", size: "__len", iterate: "__ipairs", "iterate all": "__pairs" +$METAMETHOD_MAP = { + ."as text" = "__tostring", ."clean up" = "__gc", ."+" = "__add", ."-" = "__sub" + ."*" = "__mul", ."/" = "__div", .negative = "__unm", ."//" = "__idiv" + .mod = "__mod", ."^" = "__pow", ."&" = "__band", ."|" = "__bor", ."~" = "__bxor" + ."~" = "__bnot", ."<<" = "__bshl", .">>" = "__bshr", ."==" = "__eq" + ."<" = "__lt", ."<=" = "__le", ."set 1 =" = "__newindex" + .size = "__len", .iterate = "__ipairs", ."iterate all" = "__pairs" +} (($ as text like a dict)'s meaning) = ({}'s metatable).__tostring -externally (a class named $classname with $members ((initialize $it)'s meaning)) \ +externally (a class named $classname with $members ((initialize $it)'s meaning)) ..means: - $class = {__type: $classname} + $class = {.__type = $classname} $class.__index = $class $class.class = $class $class.__tostring = ($ -> "\($.__type) \($ as text like a dict)") @@ -93,45 +98,50 @@ externally (a class named $classname with $members ((initialize $it)'s meaning)) $class.__len = ({}'s metatable).__len if $members: $class.__members = $members - $class.__newindex = (..) + $class.__newindex = for ($its $key = $value): if $members.$key: rawset $its $key $value ..else: barf "Cannot set \$key, it's not one of the allowed member fields." - set $class's metatable to {..} - __tostring: $class -> $class.__type, __call: for ($class with $initial_values): - if ($initial_values == (nil)): return $class - set $initial_values's metatable to $class - if $initial_values.set_up: - $initial_values|set up - return $initial_values + set $class's metatable to { + .__tostring = ($class -> $class.__type), .__call = ( + for ($class with $initial_values): + if ($initial_values == (nil)): return $class + set $initial_values's metatable to $class + if $initial_values.set_up: + $initial_values, set up + return $initial_values + ) + } if ((initialize)'s meaning): initialize $class for $stub = $metamethod in $METAMETHOD_MAP: - if $class.($stub|as lua id): - $class.$metamethod = $class.($stub|as lua id) + if $class.($stub, as lua id): + $class.$metamethod = $class.($stub, as lua id) return $class -[..] +[ a $classname is a thing with $members $class_body an $classname is a thing with $members $class_body -..all compile to: - $class_id = ($classname.stub|as lua id) +] all compile to: + $class_id = ($classname.stub, as lua id) if $class_body: $body_lua = ($class_body as lua) - $body_lua|remove free vars [$class_id] - $body_lua|declare locals + $body_lua, remove free vars [$class_id] + $body_lua, declare locals - return (..) - Lua " + return + Lua (" \$class_id = a_class_named_1_with(\(quote $classname.stub), \($members as lua)\((Lua ", function(\$class_id)\n \$body_lua\nend") if $class_body else "")\ ..) - a_\$class_id = function(initial_values) return \($classname.stub|as lua id)(initial_values or {}) end - an_\$class_id, a_\($class_id)_with, an_\($class_id)_with = a_\$class_id, a_\$class_id, a_\$class_id" + a_\$class_id = function(initial_values) return \($classname.stub, as lua id)(initial_values \ + ..or {}) end + an_\$class_id, a_\($class_id)_with, an_\($class_id)_with = a_\$class_id, a_\$class_id, a_\$class_id + ") -[a $classname is a thing $class_body, an $classname is a thing] all parse as (..) +[a $classname is a thing $class_body, an $classname is a thing] all parse as a $classname is a thing with (nil) $class_body |
