Renamed (action %) -> (%'s meaning)
This commit is contained in:
parent
f43d8c58f7
commit
7a35e38d87
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <2.3 to Nomsu 2.3
|
This file defines upgrades from Nomsu <2.3 to Nomsu 2.3
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <2.4 to Nomsu 2.4
|
This file defines upgrades from Nomsu <2.4 to Nomsu 2.4
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <2.5.5.5 to Nomsu 2.5.5.5
|
This file defines upgrades from Nomsu <2.5.5.5 to Nomsu 2.5.5.5
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <2.5 to Nomsu 2.5
|
This file defines upgrades from Nomsu <2.5 to Nomsu 2.5
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu 1 to Nomsu 2
|
This file defines upgrades from Nomsu 1 to Nomsu 2
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <3.5.5.6 to Nomsu 3.5.5.6
|
This file defines upgrades from Nomsu <3.5.5.6 to Nomsu 3.5.5.6
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <3.6 to 3.6
|
This file defines upgrades from Nomsu <3.6 to 3.6
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <3.7 to 3.7
|
This file defines upgrades from Nomsu <3.7 to 3.7
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <3.8 to 3.8 (Text method changes)
|
This file defines upgrades from Nomsu <3.8 to 3.8 (Text method changes)
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
This file defines upgrades from Nomsu <=2 to Nomsu 3
|
This file defines upgrades from Nomsu <=2 to Nomsu 3
|
||||||
|
|
||||||
|
@ -56,4 +56,4 @@ upgrade action "parse 1 as 2" to "4.8.10" via (..)
|
|||||||
return \(%spec parse as %body)
|
return \(%spec parse as %body)
|
||||||
|
|
||||||
upgrade action (compile as %) to "4.8.10" as (what % compiles to)
|
upgrade action (compile as %) to "4.8.10" as (what % compiles to)
|
||||||
|
upgrade action (action %) to "4.8.10" as (%'s meaning)
|
||||||
|
@ -80,15 +80,14 @@ externally [..]
|
|||||||
%tree with % -> (..)
|
%tree with % -> (..)
|
||||||
if ((% is "Action" syntax tree) and %ACTION_UPGRADES.%ver.(%.stub)):
|
if ((% is "Action" syntax tree) and %ACTION_UPGRADES.%ver.(%.stub)):
|
||||||
%with_upgraded_args = (..)
|
%with_upgraded_args = (..)
|
||||||
%k = (%v upgraded from %start_version to %end_version) \
|
%k = (%v upgraded from %start_version to %end_version) for %k = %v in %
|
||||||
..for %k = %v in %
|
|
||||||
set %with_upgraded_args 's metatable to (% 's metatable)
|
set %with_upgraded_args 's metatable to (% 's metatable)
|
||||||
return (call %ACTION_UPGRADES.%ver.(%.stub) with [%with_upgraded_args, %end_version])
|
return (..)
|
||||||
|
call %ACTION_UPGRADES.%ver.(%.stub) with [%with_upgraded_args, %end_version]
|
||||||
|
|
||||||
if %UPGRADES.%ver:
|
if %UPGRADES.%ver:
|
||||||
%with_upgraded_args = (..)
|
%with_upgraded_args = (..)
|
||||||
%k = (%v upgraded from %start_version to %end_version) \
|
%k = (%v upgraded from %start_version to %end_version) for %k = %v in %tree
|
||||||
..for %k = %v in %tree
|
|
||||||
set %with_upgraded_args 's metatable to (%tree 's metatable)
|
set %with_upgraded_args 's metatable to (%tree 's metatable)
|
||||||
%tree = (call %UPGRADES.%ver with [%with_upgraded_args, %end_version])
|
%tree = (call %UPGRADES.%ver with [%with_upgraded_args, %end_version])
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ lua> "\
|
|||||||
|
|
||||||
test:
|
test:
|
||||||
(foo %x) means (return "outer")
|
(foo %x) means (return "outer")
|
||||||
with local [action (foo %)]:
|
with local [(foo %)'s meaning]:
|
||||||
(foo %x) means:
|
(foo %x) means:
|
||||||
%y = (%x + 1)
|
%y = (%x + 1)
|
||||||
return %y
|
return %y
|
||||||
@ -151,8 +151,8 @@ test:
|
|||||||
return lua"
|
return lua"
|
||||||
|
|
||||||
test:
|
test:
|
||||||
assume ((action (say %)) == (=lua "say_1"))
|
assume (((say %)'s meaning) == (=lua "say_1"))
|
||||||
(action %action) compiles to (Lua value (%action.stub as lua id))
|
(%action's meaning) compiles to (Lua value (%action.stub as lua id))
|
||||||
|
|
||||||
test:
|
test:
|
||||||
(swap %x and %y) parses as (..)
|
(swap %x and %y) parses as (..)
|
||||||
|
@ -15,7 +15,7 @@ test:
|
|||||||
|
|
||||||
assume (%x == "outer")
|
assume (%x == "outer")
|
||||||
externally (foo) means "outer foo"
|
externally (foo) means "outer foo"
|
||||||
with local [action (foo)]:
|
with local [(foo)'s meaning]:
|
||||||
externally (foo) means "inner foo"
|
externally (foo) means "inner foo"
|
||||||
assume ((foo) == "inner foo")
|
assume ((foo) == "inner foo")
|
||||||
|
|
||||||
|
@ -247,7 +247,7 @@ say both (my favorite number) and also "foo"
|
|||||||
|
|
||||||
# Macros:
|
# Macros:
|
||||||
# The "lua> %" and "=lua %" macros can be used to write raw lua code:
|
# The "lua> %" and "=lua %" macros can be used to write raw lua code:
|
||||||
(say the time) means (..)
|
(say the time) means:
|
||||||
lua> "io.write(\"The OS time is: \", os.time(), \"\\n\");"
|
lua> "io.write(\"The OS time is: \", os.time(), \"\\n\");"
|
||||||
|
|
||||||
say the time
|
say the time
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
use "lib/object.nom"
|
use "lib/object.nom"
|
||||||
|
|
||||||
# The types are [..]
|
# The types are [..]
|
||||||
"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry",
|
"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry",
|
||||||
"IndexChain", "Action", "FileChunks", "Error", "Comment"
|
"IndexChain", "Action", "FileChunks", "Error", "Comment"
|
||||||
|
|
||||||
object (Syntax Tree):
|
object (Syntax Tree):
|
||||||
my action [set up]:
|
my action [set up]:
|
||||||
if (%me.type == "Action"):
|
if (%me.type == "Action"):
|
||||||
@ -11,38 +11,33 @@ object (Syntax Tree):
|
|||||||
%argnum = 1
|
%argnum = 1
|
||||||
for %bit in %me:
|
for %bit in %me:
|
||||||
if:
|
if:
|
||||||
(%bit is text): %stub_bits::add %bit
|
(%bit is text):
|
||||||
|
%stub_bits::add %bit
|
||||||
(%bit.type != "Comment"):
|
(%bit.type != "Comment"):
|
||||||
%stub_bits::add "\%argnum"
|
%stub_bits::add "\%argnum"
|
||||||
%argnum += 1
|
%argnum += 1
|
||||||
|
|
||||||
%me.stub = (%stub_bits::joined with " ")
|
%me.stub = (%stub_bits::joined with " ")
|
||||||
if (%me.stub == "Lua Code 1 2"):
|
if (%me.stub == "Lua Code 1 2"):
|
||||||
lua> "require('ldt').breakpoint()"
|
lua> "require('ldt').breakpoint()"
|
||||||
|
|
||||||
(Syntax Tree).source_code_for_tree = (..)
|
(Syntax Tree).source_code_for_tree = (..)
|
||||||
{} with fallback % -> (read file %.source.filename)
|
{} with fallback % -> (read file %.source.filename)
|
||||||
|
|
||||||
my action [children]:
|
my action [children]:
|
||||||
%children = []
|
%children = []
|
||||||
for % in %me:
|
for % in %me:
|
||||||
if ((% is a "Syntax Tree") and (%.type != "Comment")):
|
if ((% is a "Syntax Tree") and (%.type != "Comment")): %children::add %
|
||||||
%children::add %
|
|
||||||
if ((%me.type == "Action") and %me.target):
|
if ((%me.type == "Action") and %me.target):
|
||||||
%children::add %me.target
|
%children::add %me.target
|
||||||
return %children
|
return %children
|
||||||
|
|
||||||
my action [as lua] (..)
|
my action [as lua] "\
|
||||||
"Syntax_Tree(\(call ({}'s metatable).as_lua with [%me]))"
|
..a_Syntax_Tree_with_1(\(call ({} 's metatable).as_lua with [%me]))"
|
||||||
|
my action [as nomsu] "\
|
||||||
my action [as nomsu] (..)
|
..(a Syntax Tree with \(call ({} 's metatable).as_nomsu with [%me]))"
|
||||||
"(Syntax Tree \(call ({}'s metatable).as_nomsu with [%me]))"
|
my action [as text] "\
|
||||||
|
..(Syntax Tree \(call ({} 's metatable).__tostring with [%me]))"
|
||||||
my action [as text] (..)
|
my action [get source code] (Syntax Tree).source_code_for_tree.%me
|
||||||
"(Syntax Tree \(call ({}'s metatable).__tostring with [%me]))"
|
|
||||||
|
|
||||||
my action [get source code] (..)
|
|
||||||
(Syntax Tree).source_code_for_tree.%me
|
|
||||||
|
|
||||||
my action [map %fn]:
|
my action [map %fn]:
|
||||||
%replacement = (call %fn with [%me])
|
%replacement = (call %fn with [%me])
|
||||||
if %replacement:
|
if %replacement:
|
||||||
@ -58,10 +53,10 @@ object (Syntax Tree):
|
|||||||
%replacement.%k = %v
|
%replacement.%k = %v
|
||||||
if (%v is a "Syntax Tree"):
|
if (%v is a "Syntax Tree"):
|
||||||
%r = (%v::map %fn)
|
%r = (%v::map %fn)
|
||||||
if ((%r == %v) or (%r == (nil))):
|
if ((%r == %v) or (%r == (nil))): do next %k
|
||||||
do next %k
|
|
||||||
%changes = (yes)
|
%changes = (yes)
|
||||||
%replacement.%k = %r
|
%replacement.%k = %r
|
||||||
|
|
||||||
unless %changes: return %me
|
unless %changes: return %me
|
||||||
return (Syntax Tree %replacement)
|
return (Syntax Tree %replacement)
|
||||||
|
|
||||||
@ -73,10 +68,8 @@ object (Syntax Tree):
|
|||||||
my action [== %other]:
|
my action [== %other]:
|
||||||
unless (..)
|
unless (..)
|
||||||
all of [..]
|
all of [..]
|
||||||
(type of %me) == (type of %other)
|
(type of %me) == (type of %other), (%me 's metatable) == (%other 's metatable)
|
||||||
(%me's metatable) == (%other's metatable)
|
(size of %me) == (size of %other), %me.type == %other.type
|
||||||
(size of %me) == (size of %other)
|
|
||||||
%me.type == %other.type
|
|
||||||
..: return (no)
|
..: return (no)
|
||||||
|
|
||||||
for %item in %me at %i:
|
for %item in %me at %i:
|
||||||
@ -86,11 +79,11 @@ object (Syntax Tree):
|
|||||||
return (yes)
|
return (yes)
|
||||||
|
|
||||||
my action [get args]:
|
my action [get args]:
|
||||||
assume (%me.type == "Action") or barf "Only actions have arguments, not \(%me.type)"
|
assume (%me.type == "Action") or barf "\
|
||||||
|
..Only actions have arguments, not \(%me.type)"
|
||||||
%args = []
|
%args = []
|
||||||
for % in %me:
|
for % in %me:
|
||||||
unless ((% is text) or (%.type == "Comment")):
|
unless ((% is text) or (%.type == "Comment")): %args::add %
|
||||||
%args::add %
|
|
||||||
return %args
|
return %args
|
||||||
|
|
||||||
(Syntax Tree).map = (Syntax Tree).map_1
|
(Syntax Tree).map = (Syntax Tree).map_1
|
||||||
|
@ -1,26 +1,20 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# This file contains objects that are used to track code positions and incrementally
|
# This file contains objects that are used to track code positions and incrementally
|
||||||
build up generated code, while keeping track of where it came from, and managing
|
build up generated code, while keeping track of where it came from, and managing
|
||||||
indentation levels.
|
indentation levels.
|
||||||
|
|
||||||
use "lib/object.nom"
|
use "lib/object.nom"
|
||||||
|
|
||||||
object (Hole):
|
object (Hole):
|
||||||
action [Hole from %lua]:
|
externally (Hole from %lua) means:
|
||||||
return (Hole {lua:%lua})
|
return (Hole {lua:%lua})
|
||||||
|
my action [as lua]: return %me.lua
|
||||||
my action [as lua]:
|
|
||||||
return %me.lua
|
|
||||||
|
|
||||||
my action [as nomsu]:
|
my action [as nomsu]:
|
||||||
return "(Hole {lua:\(%me.lua)})"
|
return "(Hole {lua:\(%me.lua)})"
|
||||||
|
|
||||||
my action [as text]:
|
my action [as text]:
|
||||||
barf "Not implemented"
|
barf "Not implemented"
|
||||||
|
|
||||||
my action [as smext]:
|
my action [as smext]:
|
||||||
barf "Must fill in holes before smexting"
|
barf "Must fill in holes before smexting"
|
||||||
|
|
||||||
|
|
||||||
object (Code):
|
object (Code):
|
||||||
my action [set up]:
|
my action [set up]:
|
||||||
assume %me.source
|
assume %me.source
|
||||||
@ -28,12 +22,10 @@ object (Code):
|
|||||||
%me.bits = []
|
%me.bits = []
|
||||||
if (%me.source is text):
|
if (%me.source is text):
|
||||||
%me.source = (Source from text %me.source)
|
%me.source = (Source from text %me.source)
|
||||||
for % in %old_bits:
|
for % in %old_bits: %me::add %
|
||||||
%me::add %
|
|
||||||
|
|
||||||
my action [as text]:
|
my action [as text]:
|
||||||
barf "Not implemented"
|
barf "Not implemented"
|
||||||
|
|
||||||
my action [as smext]:
|
my action [as smext]:
|
||||||
if (%me.__str == (nil)):
|
if (%me.__str == (nil)):
|
||||||
set {%buff:[], %indent:0}
|
set {%buff:[], %indent:0}
|
||||||
@ -45,19 +37,26 @@ object (Code):
|
|||||||
%bit = (%bit::as smext)
|
%bit = (%bit::as smext)
|
||||||
if (%indent > 0):
|
if (%indent > 0):
|
||||||
%bit = (%bit::with "\n" -> "\n\(" "::* %indent)")
|
%bit = (%bit::with "\n" -> "\n\(" "::* %indent)")
|
||||||
|
|
||||||
%buff::add %bit
|
%buff::add %bit
|
||||||
|
|
||||||
%me.__str = (%buff::joined)
|
%me.__str = (%buff::joined)
|
||||||
|
|
||||||
return %me.__str
|
return %me.__str
|
||||||
|
|
||||||
my action [as lua]:
|
my action [as lua]:
|
||||||
barf
|
barf
|
||||||
return "\(%me.class.name::as lua id)_from_1_2(\((%me.source::as lua) if %me.source else "nil"), \(%me.bits::as lua))"
|
return "\
|
||||||
|
..\(%me.class.name::as lua id)_from_1_2(\(..)
|
||||||
|
(%me.source::as lua) if %me.source else "nil"
|
||||||
|
.., \(%me.bits::as lua))"
|
||||||
|
|
||||||
my action [as nomsu] (..)
|
my action [as nomsu] "\
|
||||||
"(\(%me.class.name) \((%me.source::as nomsu) if %me.source else "(nil)") \(%me.bits::as nomsu))"
|
..(\(%me.class.name) \((%me.source::as nomsu) if %me.source else "(nil)") \(..)
|
||||||
|
%me.bits::as nomsu
|
||||||
|
..)"
|
||||||
|
|
||||||
my action [size] (size of (%me::as smext))
|
my action [size] (size of (%me::as smext))
|
||||||
|
|
||||||
my action [mark as dirty]:
|
my action [mark as dirty]:
|
||||||
%me.__str = (nil)
|
%me.__str = (nil)
|
||||||
%me._trailing_line_len = (nil)
|
%me._trailing_line_len = (nil)
|
||||||
@ -68,9 +67,11 @@ object (Code):
|
|||||||
%new_bits = [%new_bits]
|
%new_bits = [%new_bits]
|
||||||
for % in %new_bits:
|
for % in %new_bits:
|
||||||
if (% == ""): do next %
|
if (% == ""): do next %
|
||||||
|
|
||||||
#if ((% isn't text) and (% isn't a (Code))):
|
#if ((% isn't text) and (% isn't a (Code))):
|
||||||
% = (%::as lua)
|
% = (%::as lua)
|
||||||
%me.bits::add %
|
%me.bits::add %
|
||||||
|
|
||||||
%me::mark as dirty
|
%me::mark as dirty
|
||||||
|
|
||||||
my action [trailing line length]:
|
my action [trailing line length]:
|
||||||
@ -86,15 +87,15 @@ object (Code):
|
|||||||
%num_lines += (size of (%::all matches of "\n"))
|
%num_lines += (size of (%::all matches of "\n"))
|
||||||
..else:
|
..else:
|
||||||
%num_lines += ((%::number of lines) - 1)
|
%num_lines += ((%::number of lines) - 1)
|
||||||
|
|
||||||
%me._num_lines = %num_lines
|
%me._num_lines = %num_lines
|
||||||
|
|
||||||
return %me._num_lines
|
return %me._num_lines
|
||||||
|
|
||||||
my action [is multiline, is multi-line] ((%me::number of lines) > 1)
|
my action [is multiline, is multi-line] ((%me::number of lines) > 1)
|
||||||
my action [is one line, is single line] ((%me::number of lines) == 1)
|
my action [is one line, is single line] ((%me::number of lines) == 1)
|
||||||
|
|
||||||
my action [add %values joined with %joiner]:
|
my action [add %values joined with %joiner]:
|
||||||
%me::add %values joined with %joiner or %joiner
|
%me::add %values joined with %joiner or %joiner
|
||||||
|
|
||||||
my action [add %values joined with %joiner or %wrapping_joiner]:
|
my action [add %values joined with %joiner or %wrapping_joiner]:
|
||||||
%line_len = 0
|
%line_len = 0
|
||||||
%bits = %me.bits
|
%bits = %me.bits
|
||||||
@ -103,8 +104,8 @@ object (Code):
|
|||||||
if (%line_len > 80):
|
if (%line_len > 80):
|
||||||
%bits::add %wrapping_joiner
|
%bits::add %wrapping_joiner
|
||||||
%line_len = 0
|
%line_len = 0
|
||||||
..else:
|
..else: %bits::add %joiner
|
||||||
%bits::add %joiner
|
|
||||||
%bits::add %value
|
%bits::add %value
|
||||||
unless (%value is text):
|
unless (%value is text):
|
||||||
%value = (%value::as smext)
|
%value = (%value::as smext)
|
||||||
@ -113,6 +114,7 @@ object (Code):
|
|||||||
%line_len = (size of %line)
|
%line_len = (size of %line)
|
||||||
..else:
|
..else:
|
||||||
%line_len += (size of %value)
|
%line_len += (size of %value)
|
||||||
|
|
||||||
%me::mark as dirty
|
%me::mark as dirty
|
||||||
|
|
||||||
my action [prepend %]:
|
my action [prepend %]:
|
||||||
@ -126,7 +128,6 @@ object (Code):
|
|||||||
%me.bits::add ")"
|
%me.bits::add ")"
|
||||||
%me::mark as dirty
|
%me::mark as dirty
|
||||||
|
|
||||||
|
|
||||||
object (Lua Code) extends (Code):
|
object (Lua Code) extends (Code):
|
||||||
my action [add free vars %vars]:
|
my action [add free vars %vars]:
|
||||||
if ((size of %vars) == 0): return
|
if ((size of %vars) == 0): return
|
||||||
@ -136,6 +137,7 @@ object (Lua Code) extends (Code):
|
|||||||
unless %seen.%var:
|
unless %seen.%var:
|
||||||
%me.free_vars::add %var
|
%me.free_vars::add %var
|
||||||
%seen.%var = (yes)
|
%seen.%var = (yes)
|
||||||
|
|
||||||
%me::mark as dirty
|
%me::mark as dirty
|
||||||
|
|
||||||
my action [remove free vars %vars]:
|
my action [remove free vars %vars]:
|
||||||
@ -153,9 +155,11 @@ object (Lua Code) extends (Code):
|
|||||||
lua> "table.remove(\%lua.free_vars, \%i)"
|
lua> "table.remove(\%lua.free_vars, \%i)"
|
||||||
#TODO: reinstate this
|
#TODO: reinstate this
|
||||||
#%lua.free_vars::remove at index %i
|
#%lua.free_vars::remove at index %i
|
||||||
|
|
||||||
|
|
||||||
for % in %lua.bits:
|
for % in %lua.bits:
|
||||||
unless (% is text):
|
unless (% is text): %stack::add %
|
||||||
%stack::add %
|
|
||||||
%me::mark as dirty
|
%me::mark as dirty
|
||||||
|
|
||||||
my action [declare locals] (%me::declare locals (nil))
|
my action [declare locals] (%me::declare locals (nil))
|
||||||
@ -167,9 +171,10 @@ object (Lua Code) extends (Code):
|
|||||||
unless %seen.%var:
|
unless %seen.%var:
|
||||||
%seen.%var = (yes)
|
%seen.%var = (yes)
|
||||||
%to_declare::add %var
|
%to_declare::add %var
|
||||||
|
|
||||||
for % in %lua.bits:
|
for % in %lua.bits:
|
||||||
unless (% is text):
|
unless (% is text): recurse %lua on %
|
||||||
recurse %lua on %
|
|
||||||
if ((size of %to_declare) > 0):
|
if ((size of %to_declare) > 0):
|
||||||
%me::remove free vars %to_declare
|
%me::remove free vars %to_declare
|
||||||
%me::prepend "local \(%to_declare::joined with ", ");\n"
|
%me::prepend "local \(%to_declare::joined with ", ");\n"
|
||||||
@ -178,8 +183,7 @@ object (Lua Code) extends (Code):
|
|||||||
my action [as statements] (%me::as statements "" ";")
|
my action [as statements] (%me::as statements "" ";")
|
||||||
my action [as statements %prefix] (%me::as statements %prefix ";")
|
my action [as statements %prefix] (%me::as statements %prefix ";")
|
||||||
my action [as statements %prefix %suffix]:
|
my action [as statements %prefix %suffix]:
|
||||||
unless %me.is_value:
|
unless %me.is_value: return %me
|
||||||
return %me
|
|
||||||
%statements = (Lua Code from %me.source [])
|
%statements = (Lua Code from %me.source [])
|
||||||
if ((%prefix or "") != ""):
|
if ((%prefix or "") != ""):
|
||||||
%statements::add %prefix
|
%statements::add %prefix
|
||||||
@ -194,46 +198,48 @@ object (Lua Code) extends (Code):
|
|||||||
if %code.is_variable:
|
if %code.is_variable:
|
||||||
%vars::add (%code::as smext)
|
%vars::add (%code::as smext)
|
||||||
for % in %code.bits:
|
for % in %code.bits:
|
||||||
unless (% is text):
|
unless (% is text): recurse %code on %
|
||||||
recurse %code on %
|
|
||||||
return %vars
|
return %vars
|
||||||
|
|
||||||
action [Lua Code from %source %bits]:
|
externally (Lua Code from %source %bits) means:
|
||||||
assume %source
|
assume %source
|
||||||
unless (%bits is a "List"): %bits = [%bits]
|
unless (%bits is a "List"): %bits = [%bits]
|
||||||
if (%source is a "Syntax Tree"): %source = %source.source
|
if (%source is a "Syntax Tree"):
|
||||||
return (..)
|
%source = %source.source
|
||||||
Lua Code {source:%source, bits:%bits, is_value: no, free_vars:[]}
|
return (Lua Code {source:%source, bits:%bits, is_value:no, free_vars:[]})
|
||||||
action [Lua Code from %source] (Lua Code from %source [])
|
|
||||||
|
|
||||||
action [Lua Value from %source %bits]:
|
externally (Lua Code from %source) means (Lua Code from %source [])
|
||||||
|
externally (Lua Value from %source %bits) means:
|
||||||
assume %source
|
assume %source
|
||||||
unless (%bits is a "List"): %bits = [%bits]
|
unless (%bits is a "List"): %bits = [%bits]
|
||||||
if (%source is a "Syntax Tree"): %source = %source.source
|
if (%source is a "Syntax Tree"):
|
||||||
return (..)
|
%source = %source.source
|
||||||
Lua Code {source:%source, bits:%bits, is_value: yes, free_vars:[]}
|
return (Lua Code {source:%source, bits:%bits, is_value:yes, free_vars:[]})
|
||||||
action [Lua Value from %source] (Lua Value from %source [])
|
|
||||||
|
|
||||||
action [Lua Variable from %source] (Lua Variable from %source [])
|
externally (Lua Value from %source) means (Lua Value from %source [])
|
||||||
action [Lua Variable from %source %bits]:
|
externally (Lua Variable from %source) means (Lua Variable from %source [])
|
||||||
|
externally (Lua Variable from %source %bits) means:
|
||||||
assume %source
|
assume %source
|
||||||
unless (%bits is a "List"): %bits = [%bits]
|
unless (%bits is a "List"): %bits = [%bits]
|
||||||
if (%source is a "Syntax Tree"): %source = %source.source
|
if (%source is a "Syntax Tree"):
|
||||||
|
%source = %source.source
|
||||||
return (..)
|
return (..)
|
||||||
Lua Code {source:%source, bits:%bits, is_value: yes, is_variable: yes, free_vars:[]}
|
Lua Code {..}
|
||||||
|
source:%source, bits:%bits, is_value:yes, is_variable:yes, free_vars:[]
|
||||||
|
|
||||||
# TODO: remove this shim
|
# TODO: remove this shim
|
||||||
(Lua Code).add_free_vars = (Lua Code).add_free_vars_1
|
(Lua Code).add_free_vars = (Lua Code).add_free_vars_1
|
||||||
(Lua Code).remove_free_vars = (Lua Code).remove_free_vars_1
|
(Lua Code).remove_free_vars = (Lua Code).remove_free_vars_1
|
||||||
(Lua Code).declare_locals = (Lua Code).declare_locals_1
|
(Lua Code).declare_locals = (Lua Code).declare_locals_1
|
||||||
(Lua Code).as_statements = (Lua Code).as_statements_1_2
|
(Lua Code).as_statements = (Lua Code).as_statements_1_2
|
||||||
|
|
||||||
object (Nomsu Code) extends (Code):
|
object (Nomsu Code) extends (Code):
|
||||||
action [Nomsu Code from %source %bits]:
|
externally (Nomsu Code from %source %bits) means:
|
||||||
if (%bits is text): %bits = [%bits]
|
if (%bits is text): %bits = [%bits]
|
||||||
if (%source is a "Syntax Tree"): %source = %source.source
|
if (%source is a "Syntax Tree"):
|
||||||
return (..)
|
%source = %source.source
|
||||||
Nomsu Code {source:%source, bits:%bits}
|
return (Nomsu Code {source:%source, bits:%bits})
|
||||||
action [Nomsu Code from %source] (Nomsu Code from %source [])
|
|
||||||
action [Nomsu Code %bits] (Nomsu Code from (nil) %bits)
|
externally (Nomsu Code from %source) means (Nomsu Code from %source [])
|
||||||
action [Nomsu Code] (Nomsu Code from (nil) [])
|
externally (Nomsu Code %bits) means (Nomsu Code from (nil) %bits)
|
||||||
|
externally (Nomsu Code) means (Nomsu Code from (nil) [])
|
||||||
|
@ -1,24 +1,23 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# This file contains the code to convert syntax trees to Lua code
|
# This file contains the code to convert syntax trees to Lua code
|
||||||
|
|
||||||
use "nomnom/code_obj.nom"
|
use "nomnom/code_obj.nom"
|
||||||
use "nomnom/parser.nom"
|
use "nomnom/parser.nom"
|
||||||
use "nomnom/pretty_errors.nom"
|
use "nomnom/pretty_errors.nom"
|
||||||
|
|
||||||
local action [report compile error at %tree %err]:
|
externally (report compile error at %tree %err) means:
|
||||||
barf (pretty "Compile Error" error at %tree %err)
|
barf (pretty "Compile Error" error at %tree %err)
|
||||||
|
|
||||||
local action [report compile error at %pos %err hint %hint]:
|
externally (report compile error at %pos %err hint %hint) means:
|
||||||
barf (pretty "Compile Error" error at %tree %err hint %hint)
|
barf (pretty "Compile Error" error at %tree %err hint %hint)
|
||||||
|
|
||||||
action [barf any errors in %t]:
|
externally (barf any errors in %t) means:
|
||||||
assume (%t is a "Syntax Tree")
|
assume (%t is a "Syntax Tree")
|
||||||
%errs = []
|
%errs = []
|
||||||
for % in recursive %t:
|
for % in recursive %t:
|
||||||
if (%.type == "Error"):
|
if (%.type == "Error"): %errs::add %
|
||||||
%errs::add %
|
|
||||||
for %k = %v in %:
|
for %k = %v in %:
|
||||||
if (%v is a "Syntax Tree"):
|
if (%v is a "Syntax Tree"): recurse % on %v
|
||||||
recurse % on %v
|
|
||||||
sort %errs by % -> %.source
|
sort %errs by % -> %.source
|
||||||
%errs = ((% as a pretty error) for % in %errs)
|
%errs = ((% as a pretty error) for % in %errs)
|
||||||
if ((size of %errs) > 0):
|
if ((size of %errs) > 0):
|
||||||
@ -28,17 +27,19 @@ action [barf any errors in %t]:
|
|||||||
%errs::add "\027[31;1m +\%n additional errors...\027[0m\n"
|
%errs::add "\027[31;1m +\%n additional errors...\027[0m\n"
|
||||||
barf (%errs::joined with "\n\n")
|
barf (%errs::joined with "\n\n")
|
||||||
|
|
||||||
action [%tree compiled with %compile_actions]:
|
externally (%tree compiled with %compile_actions) means:
|
||||||
assume (%tree is a "Syntax Tree")
|
assume (%tree is a "Syntax Tree")
|
||||||
if all of [..]
|
if all of [..]
|
||||||
%tree.version, action (Nomsu version)
|
%tree.version, (Nomsu version)'s meaning, %tree.version != (Nomsu version)
|
||||||
%tree.version != (Nomsu version)
|
(1 upgraded from 2 to 3)'s meaning
|
||||||
action (1 upgraded from 2 to 3)
|
..then:
|
||||||
..then: %tree = (upgrade %tree from %tree.version to (Nomsu version))
|
%tree = (upgrade %tree from %tree.version to (Nomsu version))
|
||||||
|
|
||||||
if %tree.type is:
|
if %tree.type is:
|
||||||
"Action":
|
"Action":
|
||||||
%stub = %tree.stub
|
%stub = %tree.stub
|
||||||
%compile_action = %compile_actions.%stub
|
%compile_action = %compile_actions.%stub
|
||||||
|
|
||||||
# Don't apply compiler actions to methods
|
# Don't apply compiler actions to methods
|
||||||
if (%compile_action and (not %tree.target)):
|
if (%compile_action and (not %tree.target)):
|
||||||
# TODO: restore this:
|
# TODO: restore this:
|
||||||
@ -51,26 +52,35 @@ action [%tree compiled with %compile_actions]:
|
|||||||
..The compile-time action here (\(%tree.stub)) failed to return any value."
|
..The compile-time action here (\(%tree.stub)) failed to return any value."
|
||||||
..hint "\
|
..hint "\
|
||||||
..Look at the implementation of (\(%tree.stub)) and make sure it's returning something."
|
..Look at the implementation of (\(%tree.stub)) and make sure it's returning something."
|
||||||
|
|
||||||
if (%result is a "Syntax Tree"):
|
if (%result is a "Syntax Tree"):
|
||||||
if (%result == %tree):
|
if (%result == %tree):
|
||||||
report compile error at %tree "\
|
report compile error at %tree "\
|
||||||
..The compile-time action here (\(%tree.stub)) is producing an endless loop."
|
..The compile-time action here (\(%tree.stub)) is producing an endless loop."
|
||||||
..hint "\
|
..hint "\
|
||||||
..Look at the implementation of (\(%tree.stub)) and make sure it's not just returning the original tree."
|
..Look at the implementation of (\(%tree.stub)) and make sure it's not just returning the original tree."
|
||||||
|
|
||||||
return (%result compiled with %compile_actions)
|
return (%result compiled with %compile_actions)
|
||||||
|
|
||||||
return %result
|
return %result
|
||||||
|
|
||||||
%lua = (Lua Value from %tree)
|
%lua = (Lua Value from %tree)
|
||||||
if %tree.target: # Method call
|
if %tree.target:
|
||||||
|
# Method call
|
||||||
%target_lua = (%tree.target compiled with %compile_actions)
|
%target_lua = (%tree.target compiled with %compile_actions)
|
||||||
if (((%target_lua::as smext)::matches "^%(.*%)$") or ((%target_lua::as smext)::matches "^[_a-zA-Z][_a-zA-Z0-9]*$")):
|
if (..)
|
||||||
|
((%target_lua::as smext)::matches "^%(.*%)$") or (..)
|
||||||
|
(%target_lua::as smext)::matches "^[_a-zA-Z][_a-zA-Z0-9]*$"
|
||||||
|
..:
|
||||||
%lua::add [%target_lua, ":"]
|
%lua::add [%target_lua, ":"]
|
||||||
..else:
|
..else:
|
||||||
%lua::add ["(", %target_lua, "):"]
|
%lua::add ["(", %target_lua, "):"]
|
||||||
|
|
||||||
%lua::add [%stub as lua id, "("]
|
%lua::add [%stub as lua id, "("]
|
||||||
%args = []
|
%args = []
|
||||||
for %tok in %tree at %i:
|
for %tok in %tree at %i:
|
||||||
if (%tok is text): do next %tok
|
if (%tok is text): do next %tok
|
||||||
|
|
||||||
# TODO: maybe don't translate Lua comments
|
# TODO: maybe don't translate Lua comments
|
||||||
#if (%tok.type == "Comment"): do next %tok
|
#if (%tok.type == "Comment"): do next %tok
|
||||||
if (%tok.type == "Block"):
|
if (%tok.type == "Block"):
|
||||||
@ -78,6 +88,7 @@ action [%tree compiled with %compile_actions]:
|
|||||||
for %line in %tok:
|
for %line in %tok:
|
||||||
#unless (%line.type == "Comment"):
|
#unless (%line.type == "Comment"):
|
||||||
%values::add (%line compiled with %compile_actions)
|
%values::add (%line compiled with %compile_actions)
|
||||||
|
|
||||||
if all of (%.is_value for % in %values):
|
if all of (%.is_value for % in %values):
|
||||||
if ((size of %values) == 1):
|
if ((size of %values) == 1):
|
||||||
%arg_lua = %values.1
|
%arg_lua = %values.1
|
||||||
@ -91,63 +102,61 @@ action [%tree compiled with %compile_actions]:
|
|||||||
if %v.is_value:
|
if %v.is_value:
|
||||||
%v = (%v::as statements ("return " if (%i == (size of %values) else "")))
|
%v = (%v::as statements ("return " if (%i == (size of %values) else "")))
|
||||||
%arg_lua::add ["\n ", %v]
|
%arg_lua::add ["\n ", %v]
|
||||||
|
|
||||||
%arg_lua::add "\nend)())"
|
%arg_lua::add "\nend)())"
|
||||||
..else:
|
..else:
|
||||||
%arg_lua = (%tok compiled with %compile_actions)
|
%arg_lua = (%tok compiled with %compile_actions)
|
||||||
unless %arg_lua.is_value:
|
unless %arg_lua.is_value:
|
||||||
if (%tok.type == "Action"):
|
if (%tok.type == "Action"):
|
||||||
report compile error at %tok "\
|
report compile error at %tok "\
|
||||||
..Can't use this as an argument to (\%stub), since it's not \
|
..Can't use this as an argument to (\%stub), since it's not an expression, it produces: \%arg_lua"
|
||||||
..an expression, it produces: \%arg_lua"
|
|
||||||
..hint "\
|
..hint "\
|
||||||
..Check the implementation of (\(%tok.stub)) to see if it \
|
..Check the implementation of (\(%tok.stub)) to see if it is actually meant to produce an expression."
|
||||||
..is actually meant to produce an expression."
|
|
||||||
..else:
|
..else:
|
||||||
report compile error at %tok "\
|
report compile error at %tok "\
|
||||||
..Can't use this as an argument to (\%stub), since it's \
|
..Can't use this as an argument to (\%stub), since it's not an expression, it produces: \%arg_lua"
|
||||||
..not an expression, it produces: \%arg_lua"
|
|
||||||
assume (%arg_lua != %lua) or barf "Huh? \%tree .\%i = \%tok -> \%arg_lua"
|
assume (%arg_lua != %lua) or barf "Huh? \%tree .\%i = \%tok -> \%arg_lua"
|
||||||
%args::add %arg_lua
|
%args::add %arg_lua
|
||||||
|
|
||||||
%lua::add %args joined with ", "
|
%lua::add %args joined with ", "
|
||||||
%lua::add ")"
|
%lua::add ")"
|
||||||
return %lua
|
return %lua
|
||||||
|
|
||||||
"EscapedNomsu":
|
"EscapedNomsu":
|
||||||
#return (Lua Value from %tree ((%tree.1)::as lua))
|
#return (Lua Value from %tree ((%tree.1)::as lua))
|
||||||
|
%lua = (Lua Value from %tree ["a_Syntax_Tree_with_1{type=", quote %tree.(1).type])
|
||||||
%lua = (Lua Value from %tree ["Syntax_Tree_1{type=", quote %tree.1.type])
|
|
||||||
set {%needs_comma:no, %i:1}
|
set {%needs_comma:no, %i:1}
|
||||||
local action [% as shmua]:
|
(% as shmua) means:
|
||||||
if (% is a "Lua number"):
|
if (% is a "Lua number"): return "\%"
|
||||||
return "\%"
|
|
||||||
if (% is a "Syntax Tree"):
|
if (% is a "Syntax Tree"):
|
||||||
return (% compiled with %compile_actions)
|
return (% compiled with %compile_actions)
|
||||||
if (% is text):
|
if (% is text): return (quote %)
|
||||||
return (quote %)
|
|
||||||
return (%::as lua)
|
return (%::as lua)
|
||||||
|
|
||||||
for %k = %v in (((%tree.1.type == "EscapedNomsu") and %tree) or %tree.1):
|
for %k = %v in (((%tree.(1).type == "EscapedNomsu") and %tree) or %tree.1):
|
||||||
%lua::add ", "
|
%lua::add ", "
|
||||||
if:
|
if:
|
||||||
(%k == %i):
|
(%k == %i): %i += 1
|
||||||
%i += 1
|
|
||||||
((%k is text) and (%k::is a lua identifier)):
|
((%k is text) and (%k::is a lua identifier)):
|
||||||
%lua::add [%k, "= "]
|
%lua::add [%k, "= "]
|
||||||
else:
|
else:
|
||||||
%lua::add ["[", % as shmua, "]= "]
|
%lua::add ["[", % as shmua, "]= "]
|
||||||
|
|
||||||
if (%k == "source"):
|
if (%k == "source"):
|
||||||
%lua::add (quote "\%v")
|
%lua::add (quote "\%v")
|
||||||
..else:
|
..else:
|
||||||
%lua::add (%v as shmua)
|
%lua::add (%v as shmua)
|
||||||
|
|
||||||
%lua::add "}"
|
%lua::add "}"
|
||||||
return %lua
|
return %lua
|
||||||
|
|
||||||
"Block":
|
"Block":
|
||||||
%lua = (Lua Code from %tree)
|
%lua = (Lua Code from %tree)
|
||||||
%lua::add (..)
|
%lua::add (..)
|
||||||
((%line compiled with %compile_actions)::as statements)
|
((%line compiled with %compile_actions)::as statements) for %line in %tree
|
||||||
..for %line in %tree
|
|
||||||
..joined with "\n"
|
..joined with "\n"
|
||||||
|
|
||||||
return %lua
|
return %lua
|
||||||
|
|
||||||
"Text":
|
"Text":
|
||||||
@ -158,23 +167,24 @@ action [%tree compiled with %compile_actions]:
|
|||||||
if (% is text):
|
if (% is text):
|
||||||
%string_buffer = "\%string_buffer\%"
|
%string_buffer = "\%string_buffer\%"
|
||||||
do next %
|
do next %
|
||||||
|
|
||||||
if (%string_buffer != ""):
|
if (%string_buffer != ""):
|
||||||
%lua_bits::add (%string_buffer::as lua)
|
%lua_bits::add (%string_buffer::as lua)
|
||||||
%string_buffer = ""
|
%string_buffer = ""
|
||||||
|
|
||||||
%bit_lua = (% compiled with %compile_actions)
|
%bit_lua = (% compiled with %compile_actions)
|
||||||
unless %bit_lua.is_value:
|
unless %bit_lua.is_value:
|
||||||
report compile error at % "\
|
report compile error at % "\
|
||||||
..Can't use this as a string interpolation value, since it doesn't have a value."
|
..Can't use this as a string interpolation value, since it doesn't have a value."
|
||||||
|
|
||||||
if (%.type != "Text"):
|
if (%.type != "Text"):
|
||||||
%bit_lua = (Lua Value from % ["tostring(", %bit_lua, ")"])
|
%bit_lua = (Lua Value from % ["tostring(", %bit_lua, ")"])
|
||||||
%lua_bits::add %bit_lua
|
%lua_bits::add %bit_lua
|
||||||
|
|
||||||
if ((%string_buffer != "") or ((size of %lua_bits) == 0)):
|
if ((%string_buffer != "") or ((size of %lua_bits) == 0)):
|
||||||
%lua_bits::add (%string_buffer::as lua)
|
%lua_bits::add (%string_buffer::as lua)
|
||||||
|
%lua::add %lua_bits joined with ".."
|
||||||
%lua::add %lua_bits joined with ("..")
|
if ((size of %lua_bits) > 1): %lua::parenthesize
|
||||||
if ((size of %lua_bits) > 1):
|
|
||||||
%lua::parenthesize
|
|
||||||
return %lua
|
return %lua
|
||||||
|
|
||||||
"List":
|
"List":
|
||||||
@ -195,12 +205,15 @@ action [%tree compiled with %compile_actions]:
|
|||||||
unless %key_lua.is_value:
|
unless %key_lua.is_value:
|
||||||
report compile error at %tree.1 "\
|
report compile error at %tree.1 "\
|
||||||
..Can't use this as a dict key, since it's not an expression."
|
..Can't use this as a dict key, since it's not an expression."
|
||||||
|
|
||||||
%value_lua = (..)
|
%value_lua = (..)
|
||||||
(%value compiled with %compile_actions) if %value
|
(%value compiled with %compile_actions) if %value else (..)
|
||||||
..else (Lua Value from %key ["true"])
|
Lua Value from %key ["true"]
|
||||||
|
|
||||||
unless %value_lua.is_value:
|
unless %value_lua.is_value:
|
||||||
report compile error at %tree.2 "\
|
report compile error at %tree.2 "\
|
||||||
..Can't use this as a dict value, since it's not an expression."
|
..Can't use this as a dict value, since it's not an expression."
|
||||||
|
|
||||||
%key_str = ((%key_lua::as smext)::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
|
%key_str = ((%key_lua::as smext)::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
|
||||||
if:
|
if:
|
||||||
%key_str:
|
%key_str:
|
||||||
@ -210,6 +223,7 @@ action [%tree compiled with %compile_actions]:
|
|||||||
Lua's parser if the inner expression is a long string. Lua
|
Lua's parser if the inner expression is a long string. Lua
|
||||||
parses x[[[y]]] as x("[y]"), not as x["y"]
|
parses x[[[y]]] as x("[y]"), not as x["y"]
|
||||||
return (Lua Code from %tree ["[ ", %key_lua, "]=", %value_lua])
|
return (Lua Code from %tree ["[ ", %key_lua, "]=", %value_lua])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return (Lua Code from %tree ["[", %key_lua, "]=", %value_lua])
|
return (Lua Code from %tree ["[", %key_lua, "]=", %value_lua])
|
||||||
|
|
||||||
@ -218,16 +232,17 @@ action [%tree compiled with %compile_actions]:
|
|||||||
unless %lua.is_value:
|
unless %lua.is_value:
|
||||||
report compile error at %tree.1 "\
|
report compile error at %tree.1 "\
|
||||||
..Can't index into this, since it's not an expression."
|
..Can't index into this, since it's not an expression."
|
||||||
|
|
||||||
%first_char = (%lua::as smext).1
|
%first_char = (%lua::as smext).1
|
||||||
if (any of [%first_char == "{", %first_char == "\"", %first_char == "["]):
|
if (any of [%first_char == "{", %first_char == "\"", %first_char == "["]):
|
||||||
%lua::parenthesize
|
%lua::parenthesize
|
||||||
|
|
||||||
for %i in 2 to (size of %tree):
|
for %i in 2 to (size of %tree):
|
||||||
%key = %tree.%i
|
%key = %tree.%i
|
||||||
%key_lua = (%key compiled with %compile_actions)
|
%key_lua = (%key compiled with %compile_actions)
|
||||||
unless %key_lua.is_value:
|
unless %key_lua.is_value:
|
||||||
report compile error at %key "\
|
report compile error at %key "\
|
||||||
..Can't use this as an index, since it's not an expression."
|
..Can't use this as an index, since it's not an expression."
|
||||||
|
|
||||||
%key_lua_str = (%key_lua::as smext)
|
%key_lua_str = (%key_lua::as smext)
|
||||||
%lua_id = (%key_lua_str::matching "^['\"]([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
|
%lua_id = (%key_lua_str::matching "^['\"]([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
|
||||||
if:
|
if:
|
||||||
@ -238,20 +253,19 @@ action [%tree compiled with %compile_actions]:
|
|||||||
Lua's parser if the inner expression is a long string. Lua
|
Lua's parser if the inner expression is a long string. Lua
|
||||||
parses x[[[y]]] as x("[y]"), not as x["y"]
|
parses x[[[y]]] as x("[y]"), not as x["y"]
|
||||||
%lua::add ["[ ", %key_lua, " ]"]
|
%lua::add ["[ ", %key_lua, " ]"]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
%lua::add ["[", %key_lua, "]"]
|
%lua::add ["[", %key_lua, "]"]
|
||||||
|
|
||||||
return %lua
|
return %lua
|
||||||
|
|
||||||
"Number":
|
"Number":
|
||||||
return (Lua Value from %tree ["\(%tree.1)"])
|
return (Lua Value from %tree ["\(%tree.1)"])
|
||||||
|
|
||||||
"Var":
|
"Var":
|
||||||
return (Lua Variable from %tree [%tree.1::as lua id])
|
return (Lua Variable from %tree [%tree.1::as lua id])
|
||||||
|
|
||||||
"FileChunks":
|
"FileChunks":
|
||||||
barf "\
|
barf "\
|
||||||
..Can't convert FileChunks to a single block of lua, since each chunk's \
|
..Can't convert FileChunks to a single block of lua, since each chunk's compilation depends on the earlier chunks"
|
||||||
..compilation depends on the earlier chunks"
|
|
||||||
|
|
||||||
"Comment":
|
"Comment":
|
||||||
# TODO: de-implement?
|
# TODO: de-implement?
|
||||||
@ -259,7 +273,5 @@ action [%tree compiled with %compile_actions]:
|
|||||||
|
|
||||||
"Error":
|
"Error":
|
||||||
barf (%tree as a pretty error)
|
barf (%tree as a pretty error)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
barf "Unknown type: \(%tree.type)"
|
barf "Unknown type: \(%tree.type)"
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# This file contains the code to convert syntax trees to Nomsu code
|
# This file contains the code to convert syntax trees to Nomsu code
|
||||||
use "nomnom/code_obj.nom"
|
use "nomnom/code_obj.nom"
|
||||||
use "nomnom/parser.nom"
|
use "nomnom/parser.nom"
|
||||||
|
|
||||||
# TODO: maybe re-implement the fancy coroutine checker that aborts early if nomsu gets too long
|
# TODO: maybe re-implement the fancy coroutine checker that aborts early if nomsu gets too long
|
||||||
action [%tree decompiled inline]:
|
externally (%tree decompiled inline) means:
|
||||||
assume (%tree is a "Syntax Tree")
|
assume (%tree is a "Syntax Tree")
|
||||||
if %tree.type is:
|
if %tree.type is:
|
||||||
"Action":
|
"Action":
|
||||||
@ -11,18 +12,18 @@ action [%tree decompiled inline]:
|
|||||||
if %tree.target:
|
if %tree.target:
|
||||||
%target_nomsu = (%tree.target decompiled inline)
|
%target_nomsu = (%tree.target decompiled inline)
|
||||||
if %tree.target.type is:
|
if %tree.target.type is:
|
||||||
("Action", "Block"):
|
"Action" "Block":
|
||||||
%target_nomsu::parenthesize
|
%target_nomsu::parenthesize
|
||||||
|
|
||||||
%nomsu::add [%target_nomsu, "::"]
|
%nomsu::add [%target_nomsu, "::"]
|
||||||
|
|
||||||
for %bit in %tree at %i:
|
for %bit in %tree at %i:
|
||||||
if (%bit is text):
|
if (%bit is text):
|
||||||
unless (..)
|
unless (..)
|
||||||
any of [..]
|
any of [..]
|
||||||
%i == 1,
|
%i == 1, %tree.(%i - 1) isn't text, (%bit is a nomsu operator) == (%tree.(%i - 1) is a nomsu operator)
|
||||||
%tree.(%i - 1) isn't text,
|
|
||||||
(%bit is a nomsu operator) == (%tree.(%i - 1) is a nomsu operator)
|
|
||||||
..: %nomsu::add " "
|
..: %nomsu::add " "
|
||||||
|
|
||||||
%nomsu::add %bit
|
%nomsu::add %bit
|
||||||
..else:
|
..else:
|
||||||
%arg_nomsu = (%bit decompiled inline)
|
%arg_nomsu = (%bit decompiled inline)
|
||||||
@ -30,39 +31,40 @@ action [%tree decompiled inline]:
|
|||||||
%nomsu::add " "
|
%nomsu::add " "
|
||||||
if ((%bit.type == "Action") or (%bit.type == "Block")):
|
if ((%bit.type == "Action") or (%bit.type == "Block")):
|
||||||
%arg_nomsu::parenthesize
|
%arg_nomsu::parenthesize
|
||||||
|
|
||||||
%nomsu::add %arg_nomsu
|
%nomsu::add %arg_nomsu
|
||||||
|
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"EscapedNomsu":
|
"EscapedNomsu":
|
||||||
%inner_nomsu = (%tree.1 decompiled inline)
|
%inner_nomsu = (%tree.1 decompiled inline)
|
||||||
unless (..)
|
unless (..)
|
||||||
any of [..]
|
any of [..]
|
||||||
%tree.1.type == "List", %tree.1.type == "Dict",
|
%tree.(1).type == "List", %tree.(1).type == "Dict", %tree.(1).type == "Var"
|
||||||
%tree.1.type == "Var"
|
..:
|
||||||
..: %inner_nomsu::parenthesize
|
%inner_nomsu::parenthesize
|
||||||
|
|
||||||
%nomsu = (Nomsu Code from %tree ["\\", %inner_nomsu])
|
%nomsu = (Nomsu Code from %tree ["\\", %inner_nomsu])
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"Block":
|
"Block":
|
||||||
%nomsu = (Nomsu Code from %tree [":"])
|
%nomsu = (Nomsu Code from %tree [":"])
|
||||||
for %line in %tree at %i:
|
for %line in %tree at %i:
|
||||||
%nomsu::add [..]
|
%nomsu::add [" " if (%i == 1) else "; ", %line decompiled inline]
|
||||||
" " if (%i == 1) else "; "
|
|
||||||
%line decompiled inline
|
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"Text":
|
"Text":
|
||||||
%nomsu = (Nomsu Code from %tree [])
|
%nomsu = (Nomsu Code from %tree [])
|
||||||
for %text in recursive %tree:
|
for %text in recursive %tree:
|
||||||
for %bit in %text at %i:
|
for %bit in %text at %i:
|
||||||
if (%bit is text):
|
if (%bit is text): %nomsu::add %bit
|
||||||
%nomsu::add %bit
|
|
||||||
..else:
|
..else:
|
||||||
if %bit.type is:
|
if %bit.type is:
|
||||||
"Text":
|
"Text":
|
||||||
recurse %text on %bit
|
recurse %text on %bit
|
||||||
"Var":
|
"Var":
|
||||||
%interp_nomsu = (%bit decompiled inline)
|
%interp_nomsu = (%bit decompiled inline)
|
||||||
|
|
||||||
# Make sure "...\(%x)y..." isn't confused with "...\(%xy)..."
|
# Make sure "...\(%x)y..." isn't confused with "...\(%xy)..."
|
||||||
# TODO: make this more robust against "...\%x\("y").."
|
# TODO: make this more robust against "...\%x\("y").."
|
||||||
if (..)
|
if (..)
|
||||||
@ -70,23 +72,28 @@ action [%tree decompiled inline]:
|
|||||||
not (%tree.(%i + 1)::matches "^[ \n\t,.:;#(){}%[%]]")
|
not (%tree.(%i + 1)::matches "^[ \n\t,.:;#(){}%[%]]")
|
||||||
..: %interp_nomsu::parenthesize
|
..: %interp_nomsu::parenthesize
|
||||||
%nomsu::add ["\\", %interp_nomsu]
|
%nomsu::add ["\\", %interp_nomsu]
|
||||||
("List", "Dict"):
|
|
||||||
|
"List" "Dict":
|
||||||
%nomsu::add ["\\", %bit decompiled inline]
|
%nomsu::add ["\\", %bit decompiled inline]
|
||||||
else:
|
else:
|
||||||
%nomsu::add ["\\(", %bit decompiled inline, ")"]
|
%nomsu::add ["\\(", %bit decompiled inline, ")"]
|
||||||
|
|
||||||
return (Nomsu Code from %tree ["\"", %nomsu, "\""])
|
return (Nomsu Code from %tree ["\"", %nomsu, "\""])
|
||||||
|
|
||||||
("List", "Dict"):
|
"List" "Dict":
|
||||||
%nomsu = (Nomsu Code from %tree ["[" if (%tree.type == "List") else "{"])
|
%nomsu = (Nomsu Code from %tree ["[" if (%tree.type == "List") else "{"])
|
||||||
for %item in %tree at %i:
|
for %item in %tree at %i:
|
||||||
if (%i > 1): %nomsu::add ", "
|
if (%i > 1): %nomsu::add ", "
|
||||||
%nomsu::add (%item decompiled inline)
|
%nomsu::add (%item decompiled inline)
|
||||||
|
|
||||||
%nomsu::add ("]" if (%tree.type == "List") else "}")
|
%nomsu::add ("]" if (%tree.type == "List") else "}")
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"DictEntry":
|
"DictEntry":
|
||||||
set {%key:%tree.1, %value:%tree.2}
|
set {%key:%tree.1, %value:%tree.2}
|
||||||
if (all of [%key.type == "Text", (size of %key) == 1, %key.1 is a nomsu identifier]):
|
if (..)
|
||||||
|
all of [%key.type == "Text", (size of %key) == 1, %key.1 is a nomsu identifier]
|
||||||
|
..:
|
||||||
%nomsu = (Nomsu Code from %key [%key.1])
|
%nomsu = (Nomsu Code from %key [%key.1])
|
||||||
..else:
|
..else:
|
||||||
%nomsu = (%key decompiled inline)
|
%nomsu = (%key decompiled inline)
|
||||||
@ -96,6 +103,7 @@ action [%tree decompiled inline]:
|
|||||||
if %value:
|
if %value:
|
||||||
%nomsu::add ":"
|
%nomsu::add ":"
|
||||||
%nomsu::add (%value decompiled inline)
|
%nomsu::add (%value decompiled inline)
|
||||||
|
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"IndexChain":
|
"IndexChain":
|
||||||
@ -104,46 +112,42 @@ action [%tree decompiled inline]:
|
|||||||
if (%i > 1): %nomsu::add "."
|
if (%i > 1): %nomsu::add "."
|
||||||
if (..)
|
if (..)
|
||||||
all of [..]
|
all of [..]
|
||||||
%i > 1, %bit.type == "Text", (size of %bit) == 1, %bit.1 is text,
|
%i > 1, %bit.type == "Text", (size of %bit) == 1, %bit.1 is text, %bit.1 is a nomsu identifier
|
||||||
%bit.1 is a nomsu identifier
|
|
||||||
..: %nomsu::add %bit.1
|
..: %nomsu::add %bit.1
|
||||||
..else:
|
..else:
|
||||||
%bit_nomsu = (%bit decompiled inline)
|
%bit_nomsu = (%bit decompiled inline)
|
||||||
if (..)
|
if (..)
|
||||||
any of [..]
|
any of [..]
|
||||||
%bit.type == "Action"
|
%bit.type == "Action", %bit.type == "Block", %bit.type == "IndexChain"
|
||||||
%bit.type == "Block"
|
|
||||||
%bit.type == "IndexChain"
|
|
||||||
(%bit.type == "Number") and (%i < (size of %tree))
|
(%bit.type == "Number") and (%i < (size of %tree))
|
||||||
..: %bit_nomsu::parenthesize
|
..:
|
||||||
|
%bit_nomsu::parenthesize
|
||||||
|
|
||||||
%nomsu::add %bit_nomsu
|
%nomsu::add %bit_nomsu
|
||||||
|
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"Number":
|
"Number":
|
||||||
return (Nomsu Code from %tree [(%tree.1 as hex) if %tree.hex else "\(%tree.1)"])
|
return (Nomsu Code from %tree [(%tree.1 as hex) if %tree.hex else "\(%tree.1)"])
|
||||||
|
|
||||||
"Var":
|
"Var":
|
||||||
return (Nomsu Code from %tree ["%\(%tree.1)"])
|
return (Nomsu Code from %tree ["%\(%tree.1)"])
|
||||||
|
"Comment": return (nil)
|
||||||
"Comment":
|
|
||||||
return (nil)
|
|
||||||
|
|
||||||
"FileChunks":
|
"FileChunks":
|
||||||
barf "Can't inline a FileChunks"
|
barf "Can't inline a FileChunks"
|
||||||
|
|
||||||
"Error":
|
"Error":
|
||||||
barf "Can't compile errors"
|
barf "Can't compile errors"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
barf "Unknown type: \(%tree.type)"
|
barf "Unknown type: \(%tree.type)"
|
||||||
|
|
||||||
%MAX_LINE = 90
|
%MAX_LINE = 90
|
||||||
action [%tree decompiled]:
|
externally (%tree decompiled) means:
|
||||||
%nomsu = (Nomsu Code from %tree)
|
%nomsu = (Nomsu Code from %tree)
|
||||||
|
|
||||||
# For concision:
|
# For concision:
|
||||||
local action [recurse on %t]:
|
(recurse on %t) means:
|
||||||
%space = (%MAX_LINE - (%nomsu::trailing line length))
|
%space = (%MAX_LINE - (%nomsu::trailing line length))
|
||||||
if (%space <= 0): go to (Use Indented)
|
if (%space <= 0):
|
||||||
|
go to (Use Indented)
|
||||||
for %subtree in recursive %tree:
|
for %subtree in recursive %tree:
|
||||||
if %subtree.type is:
|
if %subtree.type is:
|
||||||
"Block":
|
"Block":
|
||||||
@ -151,6 +155,7 @@ action [%tree decompiled]:
|
|||||||
go to (Use Indented)
|
go to (Use Indented)
|
||||||
if ((size of "\(%subtree decompiled inline)") > 20):
|
if ((size of "\(%subtree decompiled inline)") > 20):
|
||||||
go to (Use Indented)
|
go to (Use Indented)
|
||||||
|
|
||||||
for %k = %v in %subtree:
|
for %k = %v in %subtree:
|
||||||
if (%v is a "Syntax Tree"):
|
if (%v is a "Syntax Tree"):
|
||||||
recurse %subtree on %v
|
recurse %subtree on %v
|
||||||
@ -158,36 +163,40 @@ action [%tree decompiled]:
|
|||||||
%inline_nomsu = (%t decompiled inline)
|
%inline_nomsu = (%t decompiled inline)
|
||||||
if (%inline_nomsu and ((size of "\%inline_nomsu") <= %space)):
|
if (%inline_nomsu and ((size of "\%inline_nomsu") <= %space)):
|
||||||
return %inline_nomsu
|
return %inline_nomsu
|
||||||
|
|
||||||
=== (Use Indented) ===
|
=== (Use Indented) ===
|
||||||
%indented = (%t decompiled)
|
%indented = (%t decompiled)
|
||||||
if (%t.type == "Action"):
|
if (%t.type == "Action"):
|
||||||
%indented = (Nomsu Code from %t ["(..)\n ", %indented])
|
%indented = (..)
|
||||||
|
Nomsu Code from %t [..]
|
||||||
|
"(..)\n ", %indented
|
||||||
|
|
||||||
return %indented
|
return %indented
|
||||||
|
|
||||||
if %tree.type is:
|
if %tree.type is:
|
||||||
"FileChunks":
|
"FileChunks":
|
||||||
local action [%1 and %2 should clump]:
|
(%1 and %2 should clump) means:
|
||||||
if ((%1.type == "Action") and (%2.type == "Action")):
|
if ((%1.type == "Action") and (%2.type == "Action")):
|
||||||
if (%1.stub == "use 1"): return (%2.stub == "use 1")
|
if (%1.stub == "use 1"):
|
||||||
|
return (%2.stub == "use 1")
|
||||||
if (%1.stub == "test 1"): return (yes)
|
if (%1.stub == "test 1"): return (yes)
|
||||||
if (%2.stub == "test 1"): return (no)
|
if (%2.stub == "test 1"): return (no)
|
||||||
|
|
||||||
return (not ((recurse on %1)::is multi-line))
|
return (not ((recurse on %1)::is multi-line))
|
||||||
|
|
||||||
for %chunk in %tree at %chunk_no:
|
for %chunk in %tree at %chunk_no:
|
||||||
if (%chunk_no > 1):
|
if (%chunk_no > 1):
|
||||||
%nomsu::add "\n\n\("~"::* 80)\n\n"
|
%nomsu::add "\n\n\("~"::* 80)\n\n"
|
||||||
if (%chunk.type == "Block"):
|
if (%chunk.type == "Block"):
|
||||||
for %line in %chunk at %line_no:
|
for %line in %chunk at %line_no:
|
||||||
if (%line_no > 1):
|
if (%line_no > 1):
|
||||||
if (%chunk.(%line_no - 1) and %line should clump):
|
if (%chunk.(%line_no - 1) and %line should clump): %nomsu::add "\n"
|
||||||
%nomsu::add "\n"
|
..else: %nomsu::add "\n\n"
|
||||||
..else:
|
|
||||||
%nomsu::add "\n\n"
|
|
||||||
%nomsu::add (%line decompiled)
|
%nomsu::add (%line decompiled)
|
||||||
..else:
|
..else:
|
||||||
%nomsu::add (%chunk decompiled)
|
%nomsu::add (%chunk decompiled)
|
||||||
unless ("\%nomsu"::matches "\n$"):
|
|
||||||
%nomsu::add "\n"
|
unless ("\%nomsu"::matches "\n$"): %nomsu::add "\n"
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
"Action":
|
"Action":
|
||||||
@ -206,20 +215,19 @@ action [%tree decompiled]:
|
|||||||
%next_space = " \\\n"
|
%next_space = " \\\n"
|
||||||
|
|
||||||
%nomsu::add %next_space
|
%nomsu::add %next_space
|
||||||
|
|
||||||
if (%bit is text):
|
if (%bit is text):
|
||||||
unless (..)
|
unless (..)
|
||||||
all of [..]
|
all of [..]
|
||||||
%tree.(%i - 1) is text
|
%tree.(%i - 1) is text, (%tree.(%i - 1) is a nomsu operator) != (%bit is a nomsu operator)
|
||||||
(%tree.(%i - 1) is a nomsu operator) != (%bit is a nomsu operator)
|
..:
|
||||||
..: %nomsu::add %next_space
|
%nomsu::add %next_space
|
||||||
|
|
||||||
%nomsu::add %bit
|
%nomsu::add %bit
|
||||||
%next_space = " "
|
%next_space = " "
|
||||||
do next %bit
|
do next %bit
|
||||||
|
|
||||||
%bit_nomsu = (recurse on %bit)
|
%bit_nomsu = (recurse on %bit)
|
||||||
if (%bit.type == "Comment"):
|
if (%bit.type == "Comment"): %next_space = "\n"
|
||||||
%next_space = "\n"
|
|
||||||
..else:
|
..else:
|
||||||
%next_space = (" " if (%bit_nomsu::is one line) else "\n..")
|
%next_space = (" " if (%bit_nomsu::is one line) else "\n..")
|
||||||
if (%bit.type == "Action"):
|
if (%bit.type == "Action"):
|
||||||
@ -237,22 +245,22 @@ action [%tree decompiled]:
|
|||||||
|
|
||||||
"Block":
|
"Block":
|
||||||
for %line in %tree at %i:
|
for %line in %tree at %i:
|
||||||
if ((%i > 1) and (%line.type == "Comment")):
|
if ((%i > 1) and (%line.type == "Comment")): %nomsu::add "\n"
|
||||||
%nomsu::add "\n"
|
|
||||||
%line_nomsu = (recurse on %line)
|
%line_nomsu = (recurse on %line)
|
||||||
%nomsu::add
|
%nomsu::add
|
||||||
if (%i < (size of %tree)):
|
if (%i < (size of %tree)):
|
||||||
if ((%line_nomsu::number of lines) > 2):
|
if ((%line_nomsu::number of lines) > 2): %nomsu::add "\n\n"
|
||||||
%nomsu::add "\n\n"
|
..else: %nomsu::add "\n"
|
||||||
..else:
|
|
||||||
%nomsu::add "\n"
|
return (..)
|
||||||
return (Nomsu Code from %tree [":\n ", %nomsu])
|
Nomsu Code from %tree [..]
|
||||||
|
":\n ", %nomsu
|
||||||
|
|
||||||
"Text":
|
"Text":
|
||||||
# Multi-line text has more generous wrap margins
|
# Multi-line text has more generous wrap margins
|
||||||
%max_line = ((1.5 * %MAX_LINE) rounded down)
|
%max_line = ((1.5 * %MAX_LINE) rounded down)
|
||||||
%nomsu = (Nomsu Code from %tree)
|
%nomsu = (Nomsu Code from %tree)
|
||||||
local action [add text from %tree]:
|
(add text from %tree) means:
|
||||||
for %bit in %tree at %i:
|
for %bit in %tree at %i:
|
||||||
if (%bit is text):
|
if (%bit is text):
|
||||||
# TODO: escape properly?
|
# TODO: escape properly?
|
||||||
@ -284,18 +292,23 @@ action [%tree decompiled]:
|
|||||||
"Var":
|
"Var":
|
||||||
if ((%tree.(%i+1) is text) and (not (%tree.(%i+1)::matches "^[ \n\t,.:#(){}[%]]"))):
|
if ((%tree.(%i+1) is text) and (not (%tree.(%i+1)::matches "^[ \n\t,.:#(){}[%]]"))):
|
||||||
%interp_nomsu::parenthesize
|
%interp_nomsu::parenthesize
|
||||||
("List", "Dict"):
|
|
||||||
%interp_nomsu::parenthesize
|
|
||||||
%nomsu::add %interp_nomsu
|
|
||||||
if (%interp_nomsu::is multi-line):
|
|
||||||
%nomsu::add "\n.."
|
|
||||||
add text from %tree
|
|
||||||
return (Nomsu Code from %tree ["\"\\\n ..", %nomsu, "\""])
|
|
||||||
|
|
||||||
("List", "Dict"):
|
"List" "Dict":
|
||||||
|
%interp_nomsu::parenthesize
|
||||||
|
|
||||||
|
%nomsu::add %interp_nomsu
|
||||||
|
if (%interp_nomsu::is multi-line): %nomsu::add "\n.."
|
||||||
|
|
||||||
|
add text from %tree
|
||||||
|
return (..)
|
||||||
|
Nomsu Code from %tree [..]
|
||||||
|
"\"\\\n ..", %nomsu, "\""
|
||||||
|
|
||||||
|
"List" "Dict":
|
||||||
if ((size of %tree) == 0):
|
if ((size of %tree) == 0):
|
||||||
%nomsu::add ("[]" if (%tree.type == "List") else "{}")
|
%nomsu::add ("[]" if (%tree.type == "List") else "{}")
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
for %item in %tree at %i:
|
for %item in %tree at %i:
|
||||||
%item_nomsu = (%item decompiled inline)
|
%item_nomsu = (%item decompiled inline)
|
||||||
if ((not %item_nomsu) or ((size of "\%item_nomsu") > %MAX_LINE)):
|
if ((not %item_nomsu) or ((size of "\%item_nomsu") > %MAX_LINE)):
|
||||||
@ -303,10 +316,10 @@ action [%tree decompiled]:
|
|||||||
%nomsu::add %item_nomsu
|
%nomsu::add %item_nomsu
|
||||||
if (%i < (size of %tree)):
|
if (%i < (size of %tree)):
|
||||||
if any of [..]
|
if any of [..]
|
||||||
%item_nomsu::is multi-line
|
%item_nomsu::is multi-line, ((%nomsu::trailing line length) + (size of "\%item_nomsu")) >= %MAX_LINE
|
||||||
((%nomsu::trailing line length) + (size of "\%item_nomsu")) >= %MAX_LINE
|
|
||||||
..: %nomsu::add "\n"
|
..: %nomsu::add "\n"
|
||||||
..else: %nomsu::add ", "
|
..else: %nomsu::add ", "
|
||||||
|
|
||||||
return (..)
|
return (..)
|
||||||
Nomsu Code from %tree [..]
|
Nomsu Code from %tree [..]
|
||||||
"[..]\n " if (%tree.type == "List") else "{..}\n "
|
"[..]\n " if (%tree.type == "List") else "{..}\n "
|
||||||
@ -314,10 +327,12 @@ action [%tree decompiled]:
|
|||||||
|
|
||||||
"DictEntry":
|
"DictEntry":
|
||||||
set {%key:%tree.1, %value:%tree.2}
|
set {%key:%tree.1, %value:%tree.2}
|
||||||
if (all of [%key.type == "Text", (size of %key) == 1, %key.1 is a nomsu identifier]):
|
if (..)
|
||||||
%nomsu::add %key.1
|
all of [%key.type == "Text", (size of %key) == 1, %key.1 is a nomsu identifier]
|
||||||
|
..: %nomsu::add %key.1
|
||||||
..else:
|
..else:
|
||||||
%nomsu::add (%key decompiled inline)
|
%nomsu::add (%key decompiled inline)
|
||||||
|
|
||||||
if ((%key.type == "Action") or (%key.type == "Block")):
|
if ((%key.type == "Action") or (%key.type == "Block")):
|
||||||
%nomsu::parenthesize
|
%nomsu::parenthesize
|
||||||
%nomsu::add [": ", recurse on %value]
|
%nomsu::add [": ", recurse on %value]
|
||||||
@ -327,11 +342,9 @@ action [%tree decompiled]:
|
|||||||
%nomsu::add ["#", %tree.1::with "\n" -> "\n "]
|
%nomsu::add ["#", %tree.1::with "\n" -> "\n "]
|
||||||
return %nomsu
|
return %nomsu
|
||||||
|
|
||||||
("IndexChain", "Number", "Var"):
|
"IndexChain" "Number" "Var":
|
||||||
return (%tree decompiled inline)
|
return (%tree decompiled inline)
|
||||||
|
|
||||||
"Error":
|
"Error":
|
||||||
barf "Cannot decompile an error"
|
barf "Cannot decompile an error"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
barf "Unknown type: \(%tree.type)"
|
barf "Unknown type: \(%tree.type)"
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# Some file utilities for searching for files recursively and using package.nomsupath
|
# Some file utilities for searching for files recursively and using package.nomsupath
|
||||||
use "lib/os.nom"
|
use "lib/os.nom"
|
||||||
|
|
||||||
@ -6,12 +7,12 @@ use "lib/os.nom"
|
|||||||
%_BROWSE_CACHE = {}
|
%_BROWSE_CACHE = {}
|
||||||
|
|
||||||
# Create a fake file and put it in the cache
|
# Create a fake file and put it in the cache
|
||||||
action [spoof file %filename %contents]:
|
externally (spoof file %filename %contents) means:
|
||||||
%_SPOOFED_FILES.%filename = %contents
|
%_SPOOFED_FILES.%filename = %contents
|
||||||
return %contents
|
return %contents
|
||||||
|
|
||||||
# Read a file's contents
|
# Read a file's contents
|
||||||
action [read file %filename]:
|
externally (read file %filename) means:
|
||||||
%contents = %_FILE_CACHE.%filename
|
%contents = %_FILE_CACHE.%filename
|
||||||
if %contents: return %contents
|
if %contents: return %contents
|
||||||
if (%filename == "stdin"):
|
if (%filename == "stdin"):
|
||||||
@ -23,7 +24,7 @@ action [read file %filename]:
|
|||||||
%_FILE_CACHE.%filename = %contents
|
%_FILE_CACHE.%filename = %contents
|
||||||
return %contents
|
return %contents
|
||||||
|
|
||||||
action [%path sanitized]:
|
externally (%path sanitized) means:
|
||||||
%path = (%path::with "\\" -> "\\\\")
|
%path = (%path::with "\\" -> "\\\\")
|
||||||
%path = (%path::with "`" -> "")
|
%path = (%path::with "`" -> "")
|
||||||
%path = (%path::with "\"" -> "\\\"")
|
%path = (%path::with "\"" -> "\\\"")
|
||||||
@ -34,87 +35,74 @@ action [%path sanitized]:
|
|||||||
try:
|
try:
|
||||||
%lfs = (=lua "require('lfs')")
|
%lfs = (=lua "require('lfs')")
|
||||||
..and if it succeeds:
|
..and if it succeeds:
|
||||||
local action [filesystem has %filename]:
|
(filesystem has %filename) means:
|
||||||
%mode = (call %lfs.attributes with [%filename, "mode"])
|
%mode = (call %lfs.attributes with [%filename, "mode"])
|
||||||
if %mode is:
|
if %mode is:
|
||||||
("file", "directory", "link", "char device"):
|
"file" "directory" "link" "char device": return (yes)
|
||||||
return (yes)
|
|
||||||
else: return (no)
|
else: return (no)
|
||||||
|
|
||||||
action [file %path exists]:
|
externally (file %path exists) means:
|
||||||
if (..)
|
if (any of [%_SPOOFED_FILES.%path, %path == "stdin", filesystem has %path]):
|
||||||
any of [..]
|
|
||||||
%_SPOOFED_FILES.%path
|
|
||||||
%path == "stdin"
|
|
||||||
filesystem has %path
|
|
||||||
..: return (yes)
|
|
||||||
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
|
||||||
if (filesystem has "\%nomsupath/\%path"):
|
|
||||||
return (yes)
|
return (yes)
|
||||||
|
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
||||||
|
if (filesystem has "\(%nomsupath)/\%path"): return (yes)
|
||||||
return (no)
|
return (no)
|
||||||
|
|
||||||
action [files in %path]:
|
externally (files in %path) means:
|
||||||
unless %_BROWSE_CACHE.%path:
|
unless %_BROWSE_CACHE.%path:
|
||||||
if (%_SPOOFED_FILES.%path or (%filename == "stdin")):
|
if (%_SPOOFED_FILES.%path or (%filename == "stdin")):
|
||||||
%_BROWSE_CACHE.%path = [%path]
|
%_BROWSE_CACHE.%path = [%path]
|
||||||
..else:
|
..else:
|
||||||
if (call %lfs.attributes with [%filename, "mode"]) is:
|
if (call %lfs.attributes with [%filename, "mode"]) is:
|
||||||
("file", "char device"):
|
"file" "char device":
|
||||||
%_BROWSE_CACHE.%path = [%filename]
|
%_BROWSE_CACHE.%path = [%filename]
|
||||||
("directory", "link"):
|
"directory" "link":
|
||||||
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
||||||
%files = []
|
%files = []
|
||||||
for %member in (call %lfs.dir with ["\%nomsupath/\%filename"]):
|
for %member in (call %lfs.dir with ["\(%nomsupath)/\%filename"]):
|
||||||
if ((%member == ".") or (%member == "..")):
|
if ((%member == ".") or (%member == "..")): do next %member
|
||||||
do next %member
|
|
||||||
for % in (files in %member): %files::add %
|
for % in (files in %member): %files::add %
|
||||||
|
|
||||||
if ((size of %files) > 0):
|
if ((size of %files) > 0):
|
||||||
%_BROWSE_CACHE.%path = %files
|
%_BROWSE_CACHE.%path = %files
|
||||||
go to (Found Files)
|
go to (Found Files)
|
||||||
|
|
||||||
%_BROWSE_CACHE.%path = []
|
%_BROWSE_CACHE.%path = []
|
||||||
|
|
||||||
else:
|
else:
|
||||||
%_BROWSE_CACHE.%path = []
|
%_BROWSE_CACHE.%path = []
|
||||||
|
|
||||||
=== (Found Files) ===
|
=== (Found Files) ===
|
||||||
return %_BROWSE_CACHE.%filename
|
return %_BROWSE_CACHE.%filename
|
||||||
|
|
||||||
..or if it barfs:
|
..or if it barfs:
|
||||||
# LFS not found! Fall back to shell commands, if available.
|
# LFS not found! Fall back to shell commands, if available.
|
||||||
unless (sh> "find . -maxdepth 0"):
|
unless (sh> "find . -maxdepth 0"):
|
||||||
barf "\
|
barf "\
|
||||||
..Could not find 'luafilesystem' module and couldn't run system command 'find' \
|
..Could not find 'luafilesystem' module and couldn't run system command 'find' (this might happen on Windows). Please install \
|
||||||
..(this might happen on Windows). Please install 'luafilesystem' (which can be \
|
..'luafilesystem' (which can be found at \(..)
|
||||||
..found at \(..)
|
"https://github.com/spacewander/luafilesystem" if %jit else "\
|
||||||
"https://github.com/spacewander/luafilesystem"
|
..https://github.com/keplerproject/luafilesystem"
|
||||||
..if %jit else "https://github.com/keplerproject/luafilesystem"
|
|
||||||
.. or obtained through `luarocks install luafilesystem`)"
|
.. or obtained through `luarocks install luafilesystem`)"
|
||||||
|
|
||||||
|
externally (file %path exists) means:
|
||||||
action [file %path exists]:
|
if (any of [%_SPOOFED_FILES.%path, %path == "stdin", sh> "ls \(%path sanitized)"]) \
|
||||||
if (..)
|
|
||||||
any of [..]
|
|
||||||
%_SPOOFED_FILES.%path
|
|
||||||
%path == "stdin"
|
|
||||||
sh> "ls \(%path sanitized)"
|
|
||||||
..: return (yes)
|
..: return (yes)
|
||||||
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
||||||
if (sh> "ls \(%nomsupath)/\(%path)"):
|
if (sh> "ls \(%nomsupath)/\%path"): return (yes)
|
||||||
return (yes)
|
|
||||||
return (no)
|
return (no)
|
||||||
|
|
||||||
action [files in %path]:
|
externally (files in %path) means:
|
||||||
unless %_BROWSE_CACHE.%path:
|
unless %_BROWSE_CACHE.%path:
|
||||||
if %_SPOOFED_FILES.%path:
|
if %_SPOOFED_FILES.%path:
|
||||||
%_BROWSE_CACHE.%path = [%_SPOOFED_FILES.%path]
|
%_BROWSE_CACHE.%path = [%_SPOOFED_FILES.%path]
|
||||||
..else:
|
..else:
|
||||||
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
for %nomsupath in (%package.nomsupath::all matches of "[^;]+"):
|
||||||
%files = (sh> "find -L '\%path' -not -path '*/\\.*' -type f'")
|
%files = (sh> "find -L '\(%path)' -not -path '*/\\.*' -type f'")
|
||||||
if %files:
|
if %files:
|
||||||
%_BROWSE_CACHE.%path = (%files::lines)
|
%_BROWSE_CACHE.%path = (%files::lines)
|
||||||
go to (Found Files)
|
go to (Found Files)
|
||||||
|
|
||||||
%_BROWSE_CACHE.%path = []
|
%_BROWSE_CACHE.%path = []
|
||||||
|
|
||||||
=== (Found Files) ===
|
=== (Found Files) ===
|
||||||
return %_BROWSE_CACHE.%path
|
return %_BROWSE_CACHE.%path
|
||||||
|
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# This file contains the parser, which converts text into abstract syntax trees
|
# This file contains the parser, which converts text into abstract syntax trees
|
||||||
#use "nomonom/ast.nom"
|
#use "nomonom/ast.nom"
|
||||||
|
|
||||||
%lpeg = (=lua "require('lpeg')")
|
%lpeg = (=lua "require('lpeg')")
|
||||||
%re = (=lua "require('re')")
|
%re = (=lua "require('re')")
|
||||||
call %lpeg.setmaxstack with [20000]
|
call %lpeg.setmaxstack with [20000]
|
||||||
set {..}
|
set {..}
|
||||||
(action (P 1)): %lpeg.P, (action (R 1)): %lpeg.R, (action (Carg 1)): %lpeg.Carg,
|
((P 1)'s meaning):%lpeg.P, ((R 1)'s meaning):%lpeg.R
|
||||||
(action (S 1)): %lpeg.S,
|
((Carg 1)'s meaning):%lpeg.Carg, ((S 1)'s meaning):%lpeg.S
|
||||||
(action (Cc 1)): %lpeg.Cc, (action (lpeg re pattern 1)): %re.compile,
|
((Cc 1)'s meaning):%lpeg.Cc, ((lpeg re pattern 1)'s meaning):%re.compile
|
||||||
(action (lpeg re pattern 1 using 2)): %re.compile
|
((lpeg re pattern 1 using 2)'s meaning):%re.compile
|
||||||
(action (lpeg pattern 1's match of 2)): %lpeg.match
|
((lpeg pattern 1's match of 2)'s meaning):%lpeg.match
|
||||||
(action (lpeg pattern 1's match of 2 with 3)): [%1, %2, %3] -> (call %lpeg.match with [%1, %2, nil, %3])
|
((lpeg pattern 1's match of 2 with 3)'s meaning): (..)
|
||||||
|
[%1, %2, %3] -> (call %lpeg.match with [%1, %2, nil, %3])
|
||||||
|
|
||||||
%source_code_for_tree = {}
|
%source_code_for_tree = {}
|
||||||
%defs = (..)
|
%defs = (..)
|
||||||
@ -34,28 +35,30 @@ set {..}
|
|||||||
%t = (Syntax Tree %t)
|
%t = (Syntax Tree %t)
|
||||||
(Syntax Tree).source_code_for_tree.%t = %userdata.source
|
(Syntax Tree).source_code_for_tree.%t = %userdata.source
|
||||||
return %t
|
return %t
|
||||||
|
|
||||||
..with fallback %key ->:
|
..with fallback %key ->:
|
||||||
if:
|
if:
|
||||||
(%key::matches "^ascii_(%d+)$"):
|
(%key::matches "^ascii_(%d+)$"):
|
||||||
%i = (%key::matching "^ascii_(%d+)$")
|
%i = (%key::matching "^ascii_(%d+)$")
|
||||||
return (call %string.char with [%i as a number])
|
return (call %string.char with [%i as a number])
|
||||||
|
|
||||||
(%key::matches "^number_(%d+)$"):
|
(%key::matches "^number_(%d+)$"):
|
||||||
%i = (%key::matching "^number_(%d+)$")
|
%i = (%key::matching "^number_(%d+)$")
|
||||||
return (Cc (%i as a number))
|
return (Cc (%i as a number))
|
||||||
|
|
||||||
%id_patt = (((P "") - (R "09")) * ((%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09"))^1 * -1))
|
%id_patt = (..)
|
||||||
%operator_patt = ((S "'`~!@$^&*+=|<>?/-")^1 * -1)
|
((P "") - (R "09")) * (..)
|
||||||
|
(%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09")) ^ 1 * -1
|
||||||
|
|
||||||
action [%text is a nomsu id, %text is a nomsu identifier] (..)
|
%operator_patt = ((S "'`~!@$^&*+=|<>?/-") ^ 1 * -1)
|
||||||
|
externally [%text is a nomsu id, %text is a nomsu identifier] all mean (..)
|
||||||
lpeg pattern %id_patt's match of %text
|
lpeg pattern %id_patt's match of %text
|
||||||
|
|
||||||
action [%text is a nomsu operator] (..)
|
externally (%text is a nomsu operator) means (..)
|
||||||
lpeg pattern %operator_patt's match of %text
|
lpeg pattern %operator_patt's match of %text
|
||||||
|
|
||||||
%peg_tidier = (..)
|
%peg_tidier = (..)
|
||||||
lpeg re pattern "\
|
lpeg re pattern "\
|
||||||
file <- %nl* {~ (def/comment) (%nl+ (def/comment))* %nl* ~}
|
..file <- %nl* {~ (def/comment) (%nl+ (def/comment))* %nl* ~}
|
||||||
def <- anon_def / captured_def
|
def <- anon_def / captured_def
|
||||||
anon_def <-
|
anon_def <-
|
||||||
({ident} (" "*) ":" {[^%nl]* (%nl+ " "+ [^%nl]*)*})
|
({ident} (" "*) ":" {[^%nl]* (%nl+ " "+ [^%nl]*)*})
|
||||||
@ -67,18 +70,22 @@ action [%text is a nomsu operator] (..)
|
|||||||
comment <- "--" [^%nl]*
|
comment <- "--" [^%nl]*
|
||||||
"
|
"
|
||||||
|
|
||||||
action [make parser from %peg] (make parser from %peg using (nil))
|
externally (make parser from %peg) means (make parser from %peg using (nil))
|
||||||
|
externally (make parser from %peg using %make_tree) means:
|
||||||
action [make parser from %peg using %make_tree]:
|
|
||||||
%peg = (lpeg pattern %peg_tidier's match of %peg)
|
%peg = (lpeg pattern %peg_tidier's match of %peg)
|
||||||
%peg = (lpeg re pattern %peg using %defs)
|
%peg = (lpeg re pattern %peg using %defs)
|
||||||
local action [%input from %filename parsed]:
|
(%input from %filename parsed) means:
|
||||||
%input = "\%input"
|
%input = "\%input"
|
||||||
%tree_mt = {__index:{source:%input, filename:%filename}}
|
%tree_mt = {__index:{source:%input, filename:%filename}}
|
||||||
%userdata = {..}
|
%userdata = {..}
|
||||||
make_tree:%make_tree or ([%] -> (: set %'s metatable to %tree_mt; return %))
|
make_tree:%make_tree or ([%] -> (: set %'s metatable to %tree_mt; return %))
|
||||||
filename:%filename, source:%input
|
filename:%filename, source:%input
|
||||||
|
|
||||||
%tree = (lpeg pattern %peg's match of %input with %userdata)
|
%tree = (lpeg pattern %peg's match of %input with %userdata)
|
||||||
assume %tree or barf "File \%filename failed to parse:\n\%input"
|
assume %tree or barf "\
|
||||||
|
..File \%filename failed to parse:
|
||||||
|
\%input"
|
||||||
|
|
||||||
return %tree
|
return %tree
|
||||||
return (action (1 from 2 parsed))
|
|
||||||
|
return ((1 from 2 parsed)'s meaning)
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
# This file has code for converting errors to user-friendly format, with colors,
|
# This file has code for converting errors to user-friendly format, with colors,
|
||||||
line numbers, code excerpts, and so on.
|
line numbers, code excerpts, and so on.
|
||||||
|
(visible size of %text) means:
|
||||||
local action [visible size of %text]:
|
|
||||||
return (size of (%text::with "\027%[[0-9;]*m" -> ""))
|
return (size of (%text::with "\027%[[0-9;]*m" -> ""))
|
||||||
|
|
||||||
local action [%text boxed]:
|
(%text boxed) means:
|
||||||
%max_line = (..)
|
%max_line = (max of ((visible size of %line) for %line in (%text::lines)))
|
||||||
max of ((visible size of %line) for %line in (%text::lines))
|
|
||||||
%ret = (..)
|
%ret = (..)
|
||||||
"\n\%text"::with "\n([^\n]*)" -> (..)
|
"\n\%text"::with "\n([^\n]*)" -> (..)
|
||||||
[%] -> (..)
|
[%] -> (..)
|
||||||
@ -14,47 +13,45 @@ local action [%text boxed]:
|
|||||||
return %ret.[2,-1]
|
return %ret.[2,-1]
|
||||||
|
|
||||||
%CONTEXT = 2
|
%CONTEXT = 2
|
||||||
action [pretty %title error at %tree %err hint %hint]:
|
externally (pretty %title error at %tree %err hint %hint) means:
|
||||||
%source_code = (%tree::get source code)
|
%source_code = (%tree::get source code)
|
||||||
%start = %tree.source.start
|
%start = %tree.source.start
|
||||||
%stop = %tree.source.stop
|
%stop = %tree.source.stop
|
||||||
%filename = (%tree.source.filename or "???")
|
%filename = (%tree.source.filename or "???")
|
||||||
|
|
||||||
%err_line = (%source_code::line at %start)
|
%err_line = (%source_code::line at %start)
|
||||||
%err_linenum = (%source_code::line number at %start)
|
%err_linenum = (%source_code::line number at %start)
|
||||||
%err_linepos = (%source_code::line position at %start)
|
%err_linepos = (%source_code::line position at %start)
|
||||||
|
|
||||||
# TODO: better handle multi-line errors
|
# TODO: better handle multi-line errors
|
||||||
%err_size = (..)
|
%err_size = (min of [%stop - %start, (size of %err_line) - %err_linepos + 1])
|
||||||
min of [..]
|
|
||||||
%stop - %start
|
|
||||||
(size of %err_line) - %err_linepos + 1
|
|
||||||
%nl_indicator = (" " if (%err_linepos > (size of %err_line)) else "")
|
%nl_indicator = (" " if (%err_linepos > (size of %err_line)) else "")
|
||||||
%fmt_str = " %\(size of "\(%err_linenum + %CONTEXT)")d|"
|
%fmt_str = " %\(size of "\(%err_linenum + %CONTEXT)")d|"
|
||||||
local action [num %i] (%fmt_str::formatted with %i)
|
(num %i) means (%fmt_str::formatted with %i)
|
||||||
%linenum_size = (size of (num 0))
|
%linenum_size = (size of (num 0))
|
||||||
|
|
||||||
%pointer = "\(" "::* (%err_linepos + %linenum_size - 1))"
|
%pointer = "\(" "::* (%err_linepos + %linenum_size - 1))"
|
||||||
if (%err_size >= 2):
|
if (%err_size >= 2):
|
||||||
%pointer += "╚\("═"::* (%err_size - 2))╝"
|
%pointer += "╚\("═"::* (%err_size - 2))╝"
|
||||||
..else:
|
..else: %pointer += "⬆"
|
||||||
%pointer += "⬆"
|
|
||||||
|
|
||||||
%err_msg = "\027[33;41;1m\(%title or "Error") at \(%filename):\(%err_linenum)\027[0m"
|
%err_msg = "\
|
||||||
|
..\027[33;41;1m\(%title or "Error") at \%filename:\%err_linenum\027[0m"
|
||||||
for %i in (%err_linenum - %CONTEXT) to (%err_linenum - 1):
|
for %i in (%err_linenum - %CONTEXT) to (%err_linenum - 1):
|
||||||
%line = (%source_code::line %i)
|
%line = (%source_code::line %i)
|
||||||
if %line:
|
if %line:
|
||||||
%err_msg += "\n\027[2m\(num %i)\027[0m\(%line)\027[0m"
|
%err_msg += "\n\027[2m\(num %i)\027[0m\(%line)\027[0m"
|
||||||
|
|
||||||
if %err_line:
|
if %err_line:
|
||||||
%before = %err_line.[1, %err_linepos - 1]
|
%before = %err_line.[1, %err_linepos - 1]
|
||||||
%during = %err_line.[%err_linepos, %err_linepos + %err_size - 1]
|
%during = %err_line.[%err_linepos, %err_linepos + %err_size - 1]
|
||||||
%after = %err_line.[%err_linepos + %err_size, -1]
|
%after = %err_line.[%err_linepos + %err_size, -1]
|
||||||
%err_line = "\027[0m\(%before)\027[41;30m\(%during)\(%nl_indicator)\027[0m\(%after)"
|
%err_line = "\027[0m\(%before)\027[41;30m\%during\(%nl_indicator)\027[0m\%after"
|
||||||
%err_msg += "\n\027[2m\(num %err_linenum)\(%err_line)\027[0m"
|
%err_msg += "\n\027[2m\(num %err_linenum)\(%err_line)\027[0m"
|
||||||
|
|
||||||
%err_linenum_end = (%source_code::line number at %stop)
|
%err_linenum_end = (%source_code::line number at %stop)
|
||||||
%err_linepos_end = (%source_code::line position at %stop)
|
%err_linepos_end = (%source_code::line position at %stop)
|
||||||
%err_linenum_end or= %err_linenum
|
%err_linenum_end or= %err_linenum
|
||||||
if (%err_linenum_end == %err_linenum):
|
if (%err_linenum_end == %err_linenum):
|
||||||
%err_msg += "\n\(%pointer)"
|
%err_msg += "\n\%pointer"
|
||||||
..else:
|
..else:
|
||||||
for %i in (%err_linenum + 1) to %err_linenum_end:
|
for %i in (%err_linenum + 1) to %err_linenum_end:
|
||||||
%line = (%source_code::line %i)
|
%line = (%source_code::line %i)
|
||||||
@ -62,24 +59,27 @@ action [pretty %title error at %tree %err hint %hint]:
|
|||||||
if (%i == %err_linenum_end):
|
if (%i == %err_linenum_end):
|
||||||
%during = %line.[1, %err_linepos_end - 1]
|
%during = %line.[1, %err_linepos_end - 1]
|
||||||
%after = %line.[%err_linepos_end, -1]
|
%after = %line.[%err_linepos_end, -1]
|
||||||
%err_msg += "\n\027[2m\(num %i)\027[0;41;30m\(%during)\027[0m\(%after)"
|
%err_msg += "\n\027[2m\(num %i)\027[0;41;30m\(%during)\027[0m\%after"
|
||||||
..else:
|
..else:
|
||||||
%err_msg += "\n\027[2m\(num %i)\027[0;41;30m\(%line)\027[0m"
|
%err_msg += "\n\027[2m\(num %i)\027[0;41;30m\(%line)\027[0m"
|
||||||
|
|
||||||
%box_width = 70
|
%box_width = 70
|
||||||
%err_text = "\
|
%err_text = "\
|
||||||
..\027[47;31;1m\((" \(%err)"::wrapped to %box_width)::with "\n" -> "\n\027[47;31;1m ")"
|
..\027[47;31;1m\((" \%err"::wrapped to %box_width)::with "\n" -> "\n\027[47;31;1m ")"
|
||||||
if %hint:
|
if %hint:
|
||||||
%err_text += "\n\027[47;30m\((" Suggestion: \(%hint)"::wrapped to %box_width)::with "\n" -> "\n\027[47;30m ")"
|
%err_text += "\n\027[47;30m\((" Suggestion: \(%hint)"::wrapped to %box_width)::with "\n" -> "\n\027[47;30m ")"
|
||||||
%err_msg += "\n\027[33;1m \((%err_text boxed)::with "\n" -> "\n ")"
|
%err_msg += "\n\027[33;1m \((%err_text boxed)::with "\n" -> "\n ")"
|
||||||
|
|
||||||
|
%err_msg += "\n\027[33;1m \((%err_text boxed)::with "\n" -> "\n ")"
|
||||||
for %i in (%err_linenum_end + 1) to (%err_linenum_end + %CONTEXT):
|
for %i in (%err_linenum_end + 1) to (%err_linenum_end + %CONTEXT):
|
||||||
%line = (%source_code::line %i)
|
%line = (%source_code::line %i)
|
||||||
if %line:
|
if %line:
|
||||||
%err_msg += "\n\027[2m\(num %i)\027[0m\(%line)\027[0m"
|
%err_msg += "\n\027[2m\(num %i)\027[0m\(%line)\027[0m"
|
||||||
|
|
||||||
return %err_msg
|
return %err_msg
|
||||||
|
|
||||||
action [pretty %title error at %tree %err] (pretty %title error at %tree %err (nil))
|
externally (pretty %title error at %tree %err) means (..)
|
||||||
|
pretty %title error at %tree %err (nil)
|
||||||
|
|
||||||
action [%err_tree as a pretty error] (..)
|
externally (%err_tree as a pretty error) means (..)
|
||||||
pretty %err_tree.title error at %err_tree %err_tree.error hint %err_tree.hint
|
pretty %err_tree.title error at %err_tree %err_tree.error hint %err_tree.hint
|
||||||
|
@ -1,24 +1,28 @@
|
|||||||
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
use "lib/object.nom"
|
use "lib/object.nom"
|
||||||
|
|
||||||
object (Source):
|
object (Source):
|
||||||
action [Source from text %text]:
|
externally (Source from text %text) means:
|
||||||
%match = (%text::matching groups "^@(.-)%[(%d+):(%d+)%]$")
|
%match = (%text::matching groups "^@(.-)%[(%d+):(%d+)%]$")
|
||||||
set {%filename:%match.1, %start:%match.2, %stop:%match.3}
|
set {%filename:%match.1, %start:%match.2, %stop:%match.3}
|
||||||
unless %filename:
|
unless %filename:
|
||||||
%match = (%text::matching groups "^@(.-)%[(%d+)%]$")
|
%match = (%text::matching groups "^@(.-)%[(%d+)%]$")
|
||||||
set {%filename:%match.1, %start:%match.2}
|
set {%filename:%match.1, %start:%match.2}
|
||||||
return (Source {filename:%filename, start:(%start or 1) as number, stop: %stop as number})
|
|
||||||
|
return (..)
|
||||||
|
Source {filename:%filename, start:(%start or 1) as number, stop:%stop as number}
|
||||||
|
|
||||||
my action [as text] "\
|
my action [as text] "\
|
||||||
..@\(%me.filename)[\(%me.start)\(":\(%me.stop)" if %me.stop else "")]"
|
..@\(%me.filename)[\(%me.start)\(":\(%me.stop)" if %me.stop else "")]"
|
||||||
|
|
||||||
my action [as lua] "\
|
my action [as lua] "\
|
||||||
..Source{filename=\(%me.filename::as lua), start=\(%me.start)\
|
..Source{filename=\(%me.filename::as lua), start=\(%me.start)\(..)
|
||||||
..\(", stop=\(%me.stop)" if %stop else "")}"
|
", stop=\(%me.stop)" if %stop else ""
|
||||||
|
..}"
|
||||||
|
|
||||||
my action [as nomsu] "\
|
my action [as nomsu] "\
|
||||||
..(Source {filename:\(%me.filename::as nomsu), start:\(%me.start)\
|
..(Source {filename:\(%me.filename::as nomsu), start:\(%me.start)\(..)
|
||||||
..\(", stop:\(%me.stop)" if %stop else "")})"
|
", stop:\(%me.stop)" if %stop else ""
|
||||||
|
..})"
|
||||||
|
|
||||||
my action [== %other] (..)
|
my action [== %other] (..)
|
||||||
all of [..]
|
all of [..]
|
||||||
@ -46,4 +50,5 @@ object (Source):
|
|||||||
set {%me:%offset, %offset:%me}
|
set {%me:%offset, %offset:%me}
|
||||||
..else:
|
..else:
|
||||||
assume (type of %offset) == "number"
|
assume (type of %offset) == "number"
|
||||||
|
|
||||||
return (Source {filename:%me.filename, start:%me.start + %offset, stop:%me.stop})
|
return (Source {filename:%me.filename, start:%me.start + %offset, stop:%me.stop})
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
Auto-format Nomsu code. Usage:
|
Auto-format Nomsu code. Usage:
|
||||||
nomsu tools/autoformat.nom [-i] file1 file2 directory1 ...
|
nomsu tools/autoformat.nom [-i] file1 file2 directory1 ...
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
Find an action by its stub. Usage:
|
Find an action by its stub. Usage:
|
||||||
nomsu tools/find_action.nom "foo %" file1 file2 directory1 ...
|
nomsu tools/find_action.nom "foo %" file1 file2 directory1 ...
|
||||||
@ -28,6 +28,6 @@ for %path in %files:
|
|||||||
|
|
||||||
if (%t is syntax tree):
|
if (%t is syntax tree):
|
||||||
for %sub in %t: recurse %t on %sub
|
for %sub in %t: recurse %t on %sub
|
||||||
|
|
||||||
sort %results by % -> %.line
|
sort %results by % -> %.line
|
||||||
for % in %results:
|
for % in %results: say %.text
|
||||||
say %.text
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
Tool to print out a parse tree of files in an easy-to-read format. Usage:
|
Tool to print out a parse tree of files in an easy-to-read format. Usage:
|
||||||
nomsu tools/parse.nom file1 file2 directory1 ...
|
nomsu tools/parse.nom file1 file2 directory1 ...
|
||||||
|
|
||||||
use "lib/os.nom"
|
use "lib/os.nom"
|
||||||
|
|
||||||
action [print tree %t at indent %indent]:
|
externally (print tree %t at indent %indent) means:
|
||||||
if %t.type is:
|
if %t.type is:
|
||||||
"Action":
|
"Action":
|
||||||
say "\(%indent)Action (\(%t.stub)):"
|
say "\(%indent)Action (\(%t.stub)):"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
Tool to find and replace one tree with another.
|
Tool to find and replace one tree with another.
|
||||||
nomsu tools/replace.nom [-i] tree_to_replace replacement file1 file2 directory1 ...
|
nomsu tools/replace.nom [-i] tree_to_replace replacement file1 file2 directory1 ...
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env nomsu -V4.8.8.6
|
#!/usr/bin/env nomsu -V4.8.10
|
||||||
#
|
#
|
||||||
Tool to run all tests in a file (i.e. the code block inside a call to 'test %'). Usage:
|
Tool to run all tests in a file (i.e. the code block inside a call to 'test %'). Usage:
|
||||||
nomsu tools/test.nom file1 file2 directory1 ...
|
nomsu tools/test.nom file1 file2 directory1 ...
|
||||||
@ -17,7 +17,6 @@ for %path in (command line args):
|
|||||||
if (%filename::matches "%.nom$"): use %filename
|
if (%filename::matches "%.nom$"): use %filename
|
||||||
|
|
||||||
for %path in (command line args): use %path
|
for %path in (command line args): use %path
|
||||||
|
|
||||||
%tests = ((=lua "Source:from_string(\%s)") = %t for %s = %t in (tests))
|
%tests = ((=lua "Source:from_string(\%s)") = %t for %s = %t in (tests))
|
||||||
for %path in (command line args):
|
for %path in (command line args):
|
||||||
for file %filename in %path:
|
for file %filename in %path:
|
||||||
|
@ -10,27 +10,37 @@ use "lib/os.nom"
|
|||||||
|
|
||||||
%args = (command line args)
|
%args = (command line args)
|
||||||
%inplace = (no)
|
%inplace = (no)
|
||||||
|
%start_version = (nil)
|
||||||
%version = (Nomsu version)
|
%version = (Nomsu version)
|
||||||
repeat:
|
repeat:
|
||||||
if %args.1 is:
|
if %args.1 is:
|
||||||
"-i":
|
"-i":
|
||||||
%inplace = (yes)
|
%inplace = (yes)
|
||||||
%args::remove index 1
|
%args::remove index 1
|
||||||
|
|
||||||
"-t":
|
"-t":
|
||||||
use "lib/consolecolor.nom"
|
use "lib/consolecolor.nom"
|
||||||
%test = (yes)
|
%test = (yes)
|
||||||
%args::remove index 1
|
%args::remove index 1
|
||||||
|
|
||||||
"-V":
|
"-V":
|
||||||
%version = %args.2
|
%version = %args.2
|
||||||
%args::remove index 1
|
%args::remove index 1
|
||||||
%args::remove index 1
|
%args::remove index 1
|
||||||
|
|
||||||
|
"-S":
|
||||||
|
%start_version = %args.2
|
||||||
|
%args::remove index 1
|
||||||
|
%args::remove index 1
|
||||||
|
|
||||||
else: stop
|
else: stop
|
||||||
|
|
||||||
for %path in %args:
|
for %path in %args:
|
||||||
for file %filename in %path:
|
for file %filename in %path:
|
||||||
unless (%filename::matches "%.nom$"): do next %filename
|
unless (%filename::matches "%.nom$"): do next %filename
|
||||||
%tree = (parse (read file %filename) from %filename)
|
%tree = (parse (read file %filename) from %filename)
|
||||||
%uptree = (%tree upgraded to %version)
|
%uptree = (..)
|
||||||
|
%tree upgraded from (%start_version or (%tree.version or (Nomsu version))) to %version
|
||||||
%text = "\
|
%text = "\
|
||||||
..#!/usr/bin/env nomsu -V\%version
|
..#!/usr/bin/env nomsu -V\%version
|
||||||
\(%uptree as nomsu)"
|
\(%uptree as nomsu)"
|
||||||
|
Loading…
Reference in New Issue
Block a user