aboutsummaryrefslogtreecommitdiff
path: root/nomnom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-09-28 22:15:06 -0700
committerBruce Hill <bruce@bruce-hill.com>2018-09-28 22:15:19 -0700
commit63d8b1cd3f34b15bf86210b99209e8b57e7019bb (patch)
treecdaa28cbbe3e1fad493780a2f55a3f4f6075c1ea /nomnom
parent678344182b1f04e35063d7185ac1d74317b011ea (diff)
Fully working, I think? (with a lot of shims)
Diffstat (limited to 'nomnom')
-rw-r--r--nomnom/ast.nom3
-rw-r--r--nomnom/code_obj.nom74
-rw-r--r--nomnom/compile.nom30
-rw-r--r--nomnom/decompile.nom2
-rw-r--r--nomnom/parser.nom2
5 files changed, 62 insertions, 49 deletions
diff --git a/nomnom/ast.nom b/nomnom/ast.nom
index da1d0c4..377f4ae 100644
--- a/nomnom/ast.nom
+++ b/nomnom/ast.nom
@@ -16,6 +16,8 @@ object (Syntax Tree):
%stub_bits::add "\%argnum"
%argnum += 1
%me.stub = (%stub_bits::joined with " ")
+ if (%me.stub == "Lua Code 1 2"):
+ lua> "require('ldt').breakpoint()"
(Syntax Tree).source_code_for_tree = (..)
{} with fallback % -> (read file %.source.filename)
@@ -86,3 +88,4 @@ object (Syntax Tree):
%args::add %
return %args
+(Syntax Tree).map = (Syntax Tree).map_1
diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom
index 7ff1b23..f573599 100644
--- a/nomnom/code_obj.nom
+++ b/nomnom/code_obj.nom
@@ -14,11 +14,10 @@ object (Code):
for % in %old_bits:
%me::add %
- %depth = 0
my action [as text]:
- external %depth = (%depth + 1)
- if (%depth > 10):
- lua> "require('ldt').breakpoint()"
+ barf "Not implemented"
+
+ my action [as smext]:
if (%me.__str == (nil)):
set {%buff:[], %indent:0}
for %bit in %me.bits:
@@ -26,21 +25,21 @@ object (Code):
%spaces = (%bit::matching "\n([ ]*)[^\n]*$")
if %spaces: %indent = (size of %spaces.1)
..else:
- %bit = "\%bit"
+ %bit = (%bit::as smext)
if (%indent > 0):
%bit = (%bit::with "\n" -> "\n\(" "::* %indent)")
%buff::add %bit
%me.__str = (%buff::joined)
- external %depth = (%depth - 1)
return %me.__str
- my action [as lua] (..)
- "\(%me.class.name::as lua id)_1_2(\(%me.source::as lua), \(%me.bits::as lua))"
+ my action [as lua]:
+ barf
+ return "\(%me.class.name::as lua id)_from_1_2(\(%me.source::as lua), \(%me.bits::as lua))"
my action [as nomsu] (..)
"(\(%me.class.name) \(%me.source::as nomsu) \(%me.bits::as nomsu))"
- my action [size] (size of "\%me")
+ my action [size] (size of (%me::as smext))
my action [mark as dirty]:
%me.__str = (nil)
@@ -52,14 +51,14 @@ object (Code):
%new_bits = [%new_bits]
for % in %new_bits:
if (% == ""): do next %
- if ((% isn't text) and (% isn't a (Code))):
+ #if ((% isn't text) and (% isn't a (Code))):
% = (%::as lua)
%me.bits::add %
%me::mark as dirty
my action [trailing line length]:
if (%me._trailing_line_len == (nil)):
- %me._trailing_line_len = (size of ("\%me"::matching "[^\n]*$"))
+ %me._trailing_line_len = (size of ((%me::as smext)::matching "[^\n]*$"))
return %me._trailing_line_len
my action [number of lines]:
@@ -83,7 +82,6 @@ object (Code):
%line_len = 0
%bits = %me.bits
for %value in %values at %i:
- assume (%value != %me)
if (%i > 1):
if (%line_len > 80):
%bits::add %wrapping_joiner
@@ -91,7 +89,9 @@ object (Code):
..else:
%bits::add %joiner
%bits::add %value
- %line = ("\%value"::matching "\n([^\n]*)$")
+ unless (%value is text):
+ %value = (%value::as smext)
+ %line = (%value::matching "\n([^\n]*)$")
if %line:
%line_len = (size of %line)
..else:
@@ -99,7 +99,7 @@ object (Code):
%me::mark as dirty
my action [prepend %]:
- if ((% isn't text) and (% isn't a %me.__type)):
+ #if ((% isn't text) and (% isn't a %me.__type)):
% = (%::as lua)
%me.bits::add % at index 1
%me::mark as dirty
@@ -129,29 +129,30 @@ object (Lua Code) extends (Code):
%removals.%var = (yes)
%stack = [%me]
- while ((size of %stack) > 0):
+ repeat while ((size of %stack) > 0):
%lua = (%stack::pop)
for %i in (size of %lua.free_vars) to 1 by -1:
- if %removals.(%lua.%free_vars.%i):
- %lua.free_vars::remove index %i
+ if %removals.(%lua.free_vars.%i):
+ lua> "table.remove(\%lua.free_vars, \%i)"
+ #TODO: reinstate this
+ #%lua.free_vars::remove at index %i
for % in %lua.bits:
- if (% is a "Lua Code"):
+ unless (% is text):
%stack::add %
%me::mark as dirty
- my action [declare locals]:
- set {%to_declare:[], %seen:{}}
- for %lua in recursive %me:
- for %var in %lua.free_vars:
- unless %seen.%var:
- %seen.%var = (yes)
- %to_declare::add %var
- for % in %lua.bits:
- if (% is a "Lua Code"):
- recurse %lua on %
- return (%me::declare locals %to_declare)
-
+ my action [declare locals] (%me::declare locals (nil))
my action [declare locals %to_declare]:
+ unless %to_declare:
+ set {%to_declare:[], %seen:{}}
+ for %lua in recursive %me:
+ for %var in %lua.free_vars:
+ unless %seen.%var:
+ %seen.%var = (yes)
+ %to_declare::add %var
+ for % in %lua.bits:
+ unless (% is text):
+ recurse %lua on %
if ((size of %to_declare) > 0):
%me::remove free vars %to_declare
%me::prepend "local \(%to_declare::joined with ", ");\n"
@@ -163,11 +164,11 @@ object (Lua Code) extends (Code):
unless %me.is_value:
return %me
%statements = (Lua Code from %me.source [])
- if (%prefix != ""):
+ if ((%prefix or "") != ""):
%statements::add %prefix
%statements::add %me
if (%suffix != ""):
- %statements::add %suffix
+ %statements::add (%suffix or ";")
return %statements
action [Lua Code from %source %bits]:
@@ -176,12 +177,17 @@ object (Lua Code) extends (Code):
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 %tree %bits]:
+ action [Lua Value from %source %bits]:
if (%bits is text): %bits = [%bits]
if (%source is a "Syntax Tree"): %source = %source.source
return (..)
Lua Code {source:%source, bits:%bits, is_value:(yes), free_vars:[]}
- action [Lua Value from %tree] (Lua Value from %tree [])
+ action [Lua Value from %source] (Lua Value from %source [])
+
+(Lua Code).add_free_vars = (Lua Code).add_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).as_statements = (Lua Code).as_statements_1_2
object (Nomsu Code) extends (Code):
action [Nomsu Code from %source %bits]:
diff --git a/nomnom/compile.nom b/nomnom/compile.nom
index 19daa6a..64465bd 100644
--- a/nomnom/compile.nom
+++ b/nomnom/compile.nom
@@ -4,6 +4,7 @@ use "nomnom/code_obj.nom"
use "nomnom/parser.nom"
use "nomnom/pretty_errors.nom"
+# TODO: use pretty_errors
local action [report compile error at %pos %err]:
barf "Compile error at \%pos: \%err"
@@ -23,7 +24,9 @@ action [compile %tree using %compile_actions]:
%compile_action = %compile_actions.%stub
# Don't apply compiler actions to methods
if (%compile_action and (not %tree.target)):
- %args = ["tree", "compile_actions"]
+ # TODO: restore this:
+ #%args = [%tree, %compile_actions]
+ %args = [%nomsu, %tree]
for % in (%tree::get args): %args::add %
%result = (call %compile_action with %args)
if (%result == (nil)):
@@ -43,7 +46,7 @@ action [compile %tree using %compile_actions]:
%lua = (Lua Value from %tree)
if %tree.target: # Method call
%target_lua = (compile %tree.target using %compile_actions)
- if (("\%target_lua"::matches "^%(.*%)$") or ("\%target_lua"::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, ":"]
..else:
%lua::add ["(", %target_lua, "):"]
@@ -51,12 +54,13 @@ action [compile %tree using %compile_actions]:
%args = []
for %tok in %tree at %i:
if (%tok is text): do next %tok
- # TODO: maybe translate Lua comments
- if (%tok.type == "Comment"): do next %tok
+ # TODO: maybe don't translate Lua comments
+ #if (%tok.type == "Comment"): do next %tok
if (%tok.type == "Block"):
- %values = (..)
- (compile %line using %compile_actions) for %line in %tok
- ..unless (%line.type == "Comment")
+ %values = []
+ for %line in %tok:
+ #unless (%line.type == "Comment"):
+ %values::add (compile %line using %compile_actions)
if all of (%.is_value for % in %values):
if ((size of %values) == 1):
%arg_lua = %values.1
@@ -153,11 +157,11 @@ action [compile %tree using %compile_actions]:
unless %value_lua.is_value:
report compile error at %tree.2 "\
..Can't use this as a dict value, since it's not an expression."
- %key_str = ("\%key_lua"::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
+ %key_str = ((%key_lua::as smext)::matching "^[\"']([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
if:
%key_str:
return (Lua Code from %tree [%key_str, "=", %value_lua])
- ("\%key_lua".1 == "["):
+ ((%key_lua::as smext).1 == "["):
# NOTE: this *must* use a space after the [ to avoid freaking out
Lua's parser if the inner expression is a long string. Lua
parses x[[[y]]] as x("[y]"), not as x["y"]
@@ -170,7 +174,7 @@ action [compile %tree using %compile_actions]:
unless %lua.is_value:
report compile error at %tree.1 "\
..Can't index into this, since it's not an expression."
- %first_char = "\%lua".1
+ %first_char = (%lua::as smext).1
if (any of [%first_char == "{", %first_char == "\"", %first_char == "["]):
%lua::parenthesize
@@ -180,7 +184,7 @@ action [compile %tree using %compile_actions]:
unless %key_lua.is_value:
report compile error at %key "\
..Can't use this as an index, since it's not an expression."
- %key_lua_str = "\%key_lua"
+ %key_lua_str = (%key_lua::as smext)
%lua_id = (%key_lua_str::matching "^['\"]([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
if:
%lua_id:
@@ -206,8 +210,8 @@ action [compile %tree using %compile_actions]:
..compilation depends on the earlier chunks"
"Comment":
- # TODO: implement?
- return (Lua Code from %tree)
+ # TODO: de-implement?
+ return (Lua Code from %tree "-- \(%tree.1::with "\n" -> "\n-- ")")
"Error":
barf "Can't compile errors"
diff --git a/nomnom/decompile.nom b/nomnom/decompile.nom
index e002f61..58679a2 100644
--- a/nomnom/decompile.nom
+++ b/nomnom/decompile.nom
@@ -266,7 +266,7 @@ action [decompile %tree]:
(((size of %line) > 10) and ((%nomsu::trailing line length) > %max_line)):
%nomsu::add "\\\n.."
- while ((size of %line) > 0):
+ repeat while ((size of %line) > 0):
%space = (%max_line - (%nomsu::trailing line length))
%split = (%line::position of "[%p%s]" after %space)
if ((not %split) or (%split > %space + 10)):
diff --git a/nomnom/parser.nom b/nomnom/parser.nom
index ef06e08..0be394c 100644
--- a/nomnom/parser.nom
+++ b/nomnom/parser.nom
@@ -30,7 +30,7 @@ set {..}
Tree: [%t, %userdata] ->:
%source = (..)
Source {filename:%userdata.filename, start:%t.start, stop:%t.stop}
- set {%t.start: nil, %t.stop: nil}
+ set {%t.start: nil, %t.stop: nil, %t.source: %source}
%t = (Syntax Tree %t)
(Syntax Tree).source_code_for_tree.%t = %userdata.source
return %t