diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/base64.nom | 12 | ||||
| -rw-r--r-- | lib/consolecolor.nom | 21 | ||||
| -rw-r--r-- | lib/file_hash.nom | 13 | ||||
| -rw-r--r-- | lib/object.nom | 42 | ||||
| -rw-r--r-- | lib/os.nom | 19 | ||||
| -rw-r--r-- | lib/things.nom | 60 | ||||
| -rw-r--r-- | lib/training_wheels.nom | 11 | ||||
| -rw-r--r-- | lib/version.nom | 2 |
8 files changed, 97 insertions, 83 deletions
diff --git a/lib/base64.nom b/lib/base64.nom index 2091615..01d4f2f 100644 --- a/lib/base64.nom +++ b/lib/base64.nom @@ -1,18 +1,18 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # This file defines actions for encoding/decoding base 64, as specified in: https://tools.ietf.org/html/rfc4648 - + %b64_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" -%reverse_b64 = (%b64_str.%i = (%i - 1) for %i in 1 to (size of %b64_str)) +%reverse_b64 = {: for %i in 1 to (size of %b64_str): add %b64_str.%i = (%i - 1)} %reverse_b64."=" = 0 - test: %cases = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"] for %len = %encoded in %cases: %plain = "foobar".[1, %len - 1] assume (base64 %plain) == %encoded assume (base64 decode %encoded) == %plain + externally [base64 %str, base64 encode %str, %str base64] all mean: %chars = [] for %i in 1 to (size of %str) via 3: @@ -33,18 +33,16 @@ externally [base64 %str, base64 encode %str, %str base64] all mean: %chars::add %b64_str.(((%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 = (%reverse_b64.(%str.%) for % in %i to (%i + 3)) + %indices = [: for % in %i to (%i + 3): add %reverse_b64.(%str.%)] %chars::add (chr ((%indices.1 << 2) + ((%indices.2 & 48) >> 4))) if (%str.(%i + 2) == "="): stop %chars::add (chr (((%indices.2 & 15) << 4) + ((%indices.3 & 60) >> 2))) if (%str.(%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 1016c0b..aabb740 100644 --- a/lib/consolecolor.nom +++ b/lib/consolecolor.nom @@ -1,23 +1,26 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # 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 - # 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 + 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 for %name = %colornum in %colors: %colornum = "\%colornum" + #(=lua "COMPILE_ACTIONS").%name = (..) [%nomsu, %tree] -> (Lua "'\\027[\(%colornum)m'") %compile.action.%name = (..) [%nomsu, %text] ->: if %text: return (Lua "('\\027[\(%colornum)m'..\(%text as lua expr)..'\\027[0m')") - ..else: return (Lua "'\\027[\(%colornum)m'") + ..else: + return (Lua "'\\027[\(%colornum)m'") diff --git a/lib/file_hash.nom b/lib/file_hash.nom index ba2f95c..547afb5 100644 --- a/lib/file_hash.nom +++ b/lib/file_hash.nom @@ -1,14 +1,13 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # This file defines some actions for hashing files and looking up files by hash. - + use "lib/os.nom" 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 "\ @@ -25,16 +24,17 @@ test: assume ((hash "\000") != (hash "\000\000\000\000\000")) or barf "\ ..Incorrect hashing of null strings" + if %use_sha1: assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") + if %use_sha1: externally (hash %) means: %hash = (=lua "\%hashlib.new('sha1'):final(\%)") 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) %hash = (%bytes.1 << 7) @@ -47,6 +47,7 @@ externally (file with hash %hash) means: for file %filename in ".": %contents = (read file %filename) %file_hash = (hash %contents) - if (%file_hash == %hash): return %filename + if (%file_hash == %hash): + return %filename (hash of file %filename) parses as (hash (read file %filename)) diff --git a/lib/object.nom b/lib/object.nom index d8c7f5e..103f2d7 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -1,25 +1,27 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # 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" + "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): (Dog).genus = "Canus" - my action [set up]: %me.barks or= 0 + my action [set up]: + %me.barks or= 0 + my action [bark, woof]: - %barks = ("Bark!" for % in 1 to %me.barks) + %barks = [: for % in 1 to %me.barks: add "Bark!"] 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 @@ -34,31 +36,31 @@ test: assume (%d.barks == 3) %d2 = (Dog {}) assume (%d2.barks == 0) or barf "Default initializer failed" - with {%d:Dog {barks:1}}: + with {%d: Dog {barks: 1}}: assume ((%d::bark) == "Bark!") + object (Corgi) extends (Dog): my action [sploot] "splooted" my action [bark, woof]: - %barks = ("Yip!" for % in 1 to %me.barks) + %barks = [: for % in 1 to %me.barks: add "Yip!"] return (%barks::joined with " ") %corg = (Corgi {}) assume (%corg.barks == 0) - with {%d:Corgi {barks:1}}: + 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}}: + with {%d: Dog {barks: 2}}: assume ((%d::bark) == "Bark! Bark!") + (my action %actions %body) compiles to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = List(\%actions[1]:get_args()) table.insert(\%args, 1, \(\%me)) - local lua = LuaCode("class.", fn_name, " = ", \(..) - what (%args -> %body) compiles to - ..) + local lua = LuaCode("class.", fn_name, " = ", \(what (%args -> %body) compiles to)) for i=2,#\%actions do local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() @@ -75,7 +77,9 @@ test: (object %classname extends %parent %class_body) compiles to: unless (%classname.type == "Action"): - compile error at %classname "Expected this to be an action, not a \(%classname.type)" + 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." @@ -1,9 +1,10 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # This file defines some actions that interact with the operating system and filesystem. - + test: path of Nomsu file "lib/os.nom" + externally (path of Nomsu file %filename) means: lua> "for i,f in Files.walk(\%filename) do return f end" barf "Could not find file: \%filename" @@ -17,10 +18,11 @@ externally (sh> %cmd) means: test: read file "lib/os.nom" -externally (read file %filename) means (=lua "Files.read(\%filename)") +externally (read file %filename) means (=lua "Files.read(\%filename)") test: for file %f in "core": do nothing + (for file %f in %path %body) compiles to "\ ..for i,\(%f as lua expr) in Files.walk(\(%path as lua expr)) do \(%body as lua) @@ -49,23 +51,28 @@ externally [..] test: assume (line number of 3 in "x\ny") == 2 + externally (line number of %pos in %str) means (..) =lua "Files.get_line_number(\%str, \%pos)" test: assume (line 2 in "one\ntwo\nthree") == "two" + externally (line %line_num in %str) means (..) =lua "Files.get_line(\%str, \%line_num)" test: assume (source lines of \(this)) + externally (source lines of %tree) means: %source = (%tree.source if (%tree is syntax tree) else %tree) %file = (read file %source.filename) return (..) - (..) - (line % in %file) for % in (line number of %source.start in %file) to (..) - line number of %source.stop in %file + [..] + : + for % in (line number of %source.start in %file) to (..) + line number of %source.stop in %file + ..: add (line % in %file) ..::joined with "\n" externally (spoof file %text) means (%Files.spoof %text) diff --git a/lib/things.nom b/lib/things.nom index 11863de..bf4ac3b 100644 --- a/lib/things.nom +++ b/lib/things.nom @@ -1,28 +1,26 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # A library for simple object oriented programming. - + %globals.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": "__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" test: a (Dog) is a thing: that can (set up) by: %its.barks or= 0 + whose [bark, woof] all mean: - %barks = ("Bark!" for % in 1 to %its.barks) + %barks = [: for % in 1 to %its.barks: add "Bark!"] return (%barks::joined with " ") - that can (get pissed off) by: - %its.barks += 1 + + that can (get pissed off) by: %its.barks += 1 (Dog).genus = "Canus" - - %d = (a Dog with {barks:2}) + %d = (a Dog with {barks: 2}) assume (type of %d) == "Dog" assume (%d is a "Dog") assume %d.barks == 2 @@ -37,36 +35,37 @@ test: assume (%d.barks == 3) %d2 = (a Dog) assume (%d2.barks == 0) or barf "Default initializer failed" - with {%d:a Dog with {barks:1}}: + with {%d: a Dog with {barks: 1}}: assume ((%d::bark) == "Bark!") + a (Corgi) is a thing: that can [set up, get pissed off] like a (Dog) whose (sploot) means "splooted" whose [bark, woof] all mean: - %barks = ("Yip!" for % in 1 to %its.barks) + %barks = [: for % in 1 to %its.barks: add "Yip!"] return (%barks::joined with " ") %corg = (a Corgi) assume (%corg.barks == 0) - with {%d:a Corgi with {barks:1}}: + with {%d: a Corgi with {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:a Dog with {barks:2}}: + with {%d: a Dog with {barks: 2}}: assume ((%d::bark) == "Bark! Bark!") [..] - that can %actions by %body, - whose %actions means %body, whose %actions all mean %body, + that can %actions by %body, whose %actions means %body + whose %actions all mean %body ..all compile to: - unless (%actions.type == "List"): %actions = [%actions] + unless (%actions.type == "List"): + %actions = [%actions] + lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = List{\(\%its), unpack(\%actions[1]:get_args())} - local lua = LuaCode("class.", fn_name, " = ", \(..) - what (%args -> %body) compiles to - ..) + local lua = LuaCode("class.", fn_name, " = ", \(what (%args -> %body) compiles to)) for i=2,#\%actions do local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() @@ -85,8 +84,8 @@ test: return lua" [..] - that can %actions like a %class, that can %actions like an %class, - that has %actions like a %class, that has %actions like an %class, + that can %actions like a %class, that can %actions like an %class + that has %actions like a %class, that has %actions like an %class ..all compile to: %lua = (Lua "") %class_expr = (%class as lua expr) @@ -96,13 +95,15 @@ test: %lua::add %lines joined with "\n" return %lua - (a %classname is a thing with %members %class_body) compiles to: unless (%classname.type == "Action"): - compile error at %classname "Expected this to be an action, not a \(%classname.type)" + 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 "\ ..do @@ -142,4 +143,5 @@ test: end end" -(a %classname is a thing %class_body) parses as (a %classname is a thing with (nil) %class_body) +(a %classname is a thing %class_body) parses as (..) + a %classname is a thing with (nil) %class_body diff --git a/lib/training_wheels.nom b/lib/training_wheels.nom index f16b700..00ad64b 100644 --- a/lib/training_wheels.nom +++ b/lib/training_wheels.nom @@ -1,13 +1,12 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # This file contains a set of definitions that bring some familiar language features from other languages into nomsu (e.g. "||" and "continue") - -(%a === %b) parses as ((%a 's id) is (%b 's id)) -(%a !== %b) parses as ((%a 's id) is not (%b 's id)) + +(%a === %b) parses as ((%a's id) is (%b's id)) +(%a !== %b) parses as ((%a's id) is not (%b's id)) [function %names %body, def %names %body] all parse as (..) externally %names means %body - (switch %branch_value %body) parses as (if %branch_value is %body) [None, Null] all parse as (nil) [True, true] all parse as (yes) @@ -17,7 +16,7 @@ (%a && %b) parses as (%a and %b) (continue) parses as (do next) (break) parses as (stop) -(let %thing = %value in %action) parses as (with local {%thing:%value}) +(let %thing = %value in %action) parses as (with local {%thing: %value}) [print %, println %] all parse as (say %) [error!, panic!, fail!, abort!] all parse as (barf!) [error %, panic %, fail %, abort %] all parse as (barf %) diff --git a/lib/version.nom b/lib/version.nom index ee9ac92..e4a29e4 100644 --- a/lib/version.nom +++ b/lib/version.nom @@ -1,3 +1,3 @@ -#!/usr/bin/env nomsu -V4.8.10 +#!/usr/bin/env nomsu -V4.10.12.7 # This file sets the current library version. lua> "NOMSU_LIB_VERSION = 7" |
