diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 15:17:48 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 15:17:49 -0700 |
| commit | 0f17c5eb9ac4660f2f969bd1e67af42713e45eac (patch) | |
| tree | 279ca7da2de0efe2f363684f3c84a540635f11a8 /lib | |
| parent | acd9c2acd4688f2301b091daad910c04e402bd6a (diff) | |
| parent | dc41f30c73c9686685e3a4183c1213fb4ba55c90 (diff) | |
Merge branch 'master' into working
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/base64.nom | 8 | ||||
| -rw-r--r-- | lib/consolecolor.nom | 15 | ||||
| -rw-r--r-- | lib/file_hash.nom | 10 | ||||
| -rw-r--r-- | lib/object.nom | 73 | ||||
| -rw-r--r-- | lib/os.nom | 28 | ||||
| -rw-r--r-- | lib/training_wheels.nom | 46 | ||||
| -rw-r--r-- | lib/version.nom | 2 |
7 files changed, 95 insertions, 87 deletions
diff --git a/lib/base64.nom b/lib/base64.nom index 8fc4e6b..2091615 100644 --- a/lib/base64.nom +++ b/lib/base64.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file defines actions for encoding/decoding base 64, as specified in: https://tools.ietf.org/html/rfc4648 @@ -13,7 +13,7 @@ test: %plain = "foobar".[1, %len - 1] assume (base64 %plain) == %encoded assume (base64 decode %encoded) == %plain -action [base64 %str, base64 encode %str, %str base64]: +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))"] @@ -36,8 +36,8 @@ action [base64 %str, base64 encode %str, %str base64]: return (%chars::joined) -action [chr %] (=lua "string.char(\%)") -action [decode base64 %str, %str base64 decoded, base64 decode %str]: +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)) diff --git a/lib/consolecolor.nom b/lib/consolecolor.nom index f4c4df9..fe7da4c 100644 --- a/lib/consolecolor.nom +++ b/lib/consolecolor.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file defines actions for ANSI console color escape codes. @@ -14,9 +14,10 @@ test: for %name = %colornum in %colors: %colornum = "\%colornum" - (=lua "COMPILE_ACTIONS").%name = (..) - [%nomsu, %tree] -> (..) - Lua value "'\\027[\(%colornum)m'" - (=lua "COMPILE_ACTIONS")."\%name 1" = (..) - [%nomsu, %tree, %text] -> (..) - Lua value "('\\027[\(%colornum)m'..\(%text as lua expr)..'\\027[0m')" + #(=lua "COMPILE_ACTIONS").%name = (..) + [%nomsu, %tree] -> (Lua value "'\\027[\(%colornum)m'") + (=lua "COMPILE_ACTIONS")."\%name" = (..) + [%nomsu, %tree, %text] ->: + if %text: + return (Lua value "('\\027[\(%colornum)m'..\(%text as lua expr)..'\\027[0m')") + ..else: return (Lua value "'\\027[\(%colornum)m'") diff --git a/lib/file_hash.nom b/lib/file_hash.nom index 6fdb2f4..6c815f5 100644 --- a/lib/file_hash.nom +++ b/lib/file_hash.nom @@ -1,4 +1,4 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file defines some actions for hashing files and looking up files by hash. @@ -26,14 +26,14 @@ test: if %use_sha1: assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") if %use_sha1: - action [hash %]: + 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" - action [hash %]: + externally (hash %) means: %bytes = (%::bytes) %hash = (%bytes.1 << 7) for %i in 2 to (size of %bytes): @@ -41,10 +41,10 @@ if %use_sha1: %hash = (%hash ~ (size of %bytes)) return "\%hash" -action [file with hash %hash]: +externally (file with hash %hash) means: for file %filename in ".": %contents = (read file %filename) %file_hash = (hash %contents) if (%file_hash == %hash): return %filename -parse [hash of file %filename] as (hash (read file %filename)) +(hash of file %filename) parses as (hash (read file %filename)) diff --git a/lib/object.nom b/lib/object.nom index b49b8f2..d5555df 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -1,9 +1,16 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # 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" + test: - object "Dog": + object (Dog): (Dog).genus = "Canus" my action [set up]: %me.barks or= 0 my action [bark, woof]: @@ -12,8 +19,10 @@ test: my action [get pissed off]: %me.barks += 1 - %d = (new Dog {barks:2}) - assume (%d.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 @@ -23,38 +32,38 @@ test: assume ("\(%d.class)" == "Dog") assume (%d.genus == "Canus") assume (%d.barks == 3) - %d2 = (new Dog) + %d2 = (Dog {}) assume (%d2.barks == 0) or barf "Default initializer failed" - with {%d:new Dog {barks:1}}: + with {%d:Dog {barks:1}}: assume ((%d::bark) == "Bark!") - object "Corgi" extends (Dog): + object (Corgi) extends (Dog): my action [sploot] "splooted" my action [bark, woof]: %barks = ("Yip!" for % in 1 to %me.barks) return (%barks::joined with " ") - %corg = (new Corgi) + %corg = (Corgi {}) assume (%corg.barks == 0) - with {%d:new 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:new Dog {barks:2}}: + with {%d:Dog {barks:2}}: assume ((%d::bark) == "Bark! Bark!") -compile [my action %actions %body] to: +(my action %actions %body) compiles to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end) - table.insert(\%args, \(\%me as lua id)) + table.insert(\%args, 1, \(\%me as lua id)) local lua = LuaCode(tree.source, "class.", fn_name, " = ", \(..) - compile as (%args -> %body) + what (%args -> %body) compiles to ..) for i=2,#\%actions do local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() local \%alias_args = table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) end) - table.insert(\%alias_args, \(\%me as lua id)) + table.insert(\%alias_args, 1, \(\%me as lua id)) lua:append("\\nclass.", alias_name, " = ") if utils.equivalent(\%args, \%alias_args) then lua:append("class.", fn_name) @@ -68,45 +77,39 @@ compile [my action %actions %body] to: end return lua" -compile [object %classname extends %parent %class_body] to: +(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)" + for % in %classname: + unless (% is text): + compile error at % "Class names should not have arguments." + return (..) Lua "\ ..do - local class = {name=\(%classname as lua expr)} + local class = {name=\(quote %classname.stub)} + class.__type = class.name setmetatable(class, { __index=\(%parent as lua expr), __tostring=function(cls) return cls.name end, __call=function(cls, inst) - inst = setmetatable(inst or {}, cls) - if inst.set_up then - inst:set_up() - end + if inst == nil then return cls end + inst = setmetatable(inst, cls) + if inst.set_up then inst:set_up() end return inst end, }) - nomsu.environment[("new "..class.name):as_lua_id()] = class - nomsu.environment[("new "..class.name.." 1"):as_lua_id()] = class - nomsu.environment[class.name:as_lua_id()] = function() return class end + nomsu.environment[class.name:as_lua_id()] = class class.__index = class class.class = class class.__tostring = function(inst) return inst.name..getmetatable(_Dict{}).__tostring(inst) end - \(%class_body as lua statements) - - local 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", ["length"]="__len", - ["__ipairs"]="__ipairs", ["__pairs"]="__pairs", - } - for stub,metamethod in pairs(metamethod_map) do + for stub,metamethod in pairs(globals.METAMETHOD_MAP) do class[metamethod] = class[stub:as_lua_id()] end end" -parse [object %classname %class_body] as (..) +(object %classname %class_body) parses as (..) object %classname extends (nil) %class_body @@ -1,14 +1,14 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file defines some actions that interact with the operating system and filesystem. test: path of Nomsu file "lib/os.nom" -action [path of Nomsu file %filename]: +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" -action [sh> %cmd]: +externally (sh> %cmd) means: lua> "\ ..local result = io.popen(\%cmd) local contents = result:read("*a") @@ -17,19 +17,19 @@ action [sh> %cmd]: test: read file "lib/os.nom" -action [read file %filename] (=lua "Files.read(\%filename)") +externally (read file %filename) means (=lua "Files.read(\%filename)") test: for file %f in "core": do nothing -compile [for file %f in %path %body] to (..) +(for file %f in %path %body) compiles to (..) Lua "\ ..for i,\(%f as lua expr) in Files.walk(\(%path as lua expr)) do \(%body as lua statements) - \(compile as (===next %f ===)) + \(what (===next %f ===) compiles to) end - \(compile as (===stop %f ===))" + \(what (===stop %f ===) compiles to)" -compile [%expr for file %f in %path] to (..) +(%expr for file %f in %path) compiles to (..) Lua value "\ ..(function() local ret = _List{} @@ -39,10 +39,10 @@ compile [%expr for file %f in %path] to (..) return ret end)()" -action [..] +externally [..] write to file %filename %text, to file %filename write %text write %text to file %filename -..: +..all mean: assume (%filename != "stdin") or barf "Cannot write to stdin" lua> "\ ..local file = io.open(\%filename, 'w') @@ -51,15 +51,17 @@ action [..] test: assume (line number of 3 in "x\ny") == 2 -action [line number of %pos in %str] (=lua "Files.get_line_number(\%str, \%pos)") +externally (line number of %pos in %str) means (..) + =lua "Files.get_line_number(\%str, \%pos)" test: assume (line 2 in "one\ntwo\nthree") == "two" -action [line %line_num in %str] (=lua "Files.get_line(\%str, \%line_num)") +externally (line %line_num in %str) means (..) + =lua "Files.get_line(\%str, \%line_num)" test: assume (source lines of \(this)) -action [source lines of %tree]: +externally (source lines of %tree) means: %source = (%tree.source if (%tree is syntax tree) else %tree) %file = (read file %source.filename) return (..) diff --git a/lib/training_wheels.nom b/lib/training_wheels.nom index 6155069..f16b700 100644 --- a/lib/training_wheels.nom +++ b/lib/training_wheels.nom @@ -1,26 +1,28 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file contains a set of definitions that bring some familiar language features from other languages into nomsu (e.g. "||" and "continue") -parse [%a === %b] as ((%a 's id) is (%b 's id)) -parse [%a !== %b] as ((%a 's id) is not (%b 's id)) -parse [function %names %body, def %names %body] as (action %names %body) -parse [switch %branch_value %body] as (if %branch_value is %body) -parse [None, Null] as (nil) -parse [True, true] as (yes) -parse [False, false] as (no) -parse [pass] as (do nothing) -parse [%a || %b] as (%a or %b) -parse [%a && %b] as (%a and %b) -parse [continue] as (do next) -parse [break] as (stop) -parse [let %thing = %value in %action] as (with local {%thing:%value}) -parse [print %, println %] as (say %) -parse [error!, panic!, fail!, abort!] as (barf!) -parse [error %, panic %, fail %, abort %] as (barf %) -parse [assert %condition] as (assume %condition) -parse [assert %condition %message] as (assume %condition or barf %message) -parse [%cond ? %if_true %if_false] as (%if_true if %cond else %if_false) -parse [lambda %args %body] as (%args -> %body) -parse [function %name %args %body] as (%name = (%args -> %body)) +(%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) +[False, false] all parse as (no) +(pass) parses as (do nothing) +(%a || %b) parses as (%a or %b) +(%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}) +[print %, println %] all parse as (say %) +[error!, panic!, fail!, abort!] all parse as (barf!) +[error %, panic %, fail %, abort %] all parse as (barf %) +(assert %condition) parses as (assume %condition) +(assert %condition %message) parses as (assume %condition or barf %message) +(%cond ? %if_true %if_false) parses as (%if_true if %cond else %if_false) +(lambda %args %body) parses as (%args -> %body) +(function %name %args %body) parses as (%name = (%args -> %body)) diff --git a/lib/version.nom b/lib/version.nom index afb9e14..2f4003f 100644 --- a/lib/version.nom +++ b/lib/version.nom @@ -1,3 +1,3 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file sets the current library version. lua> "NOMSU_LIB_VERSION = 6" |
