From f2048235f5cc7ff02db39a0e2fe5c79c7f390e0b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Sep 2018 00:30:28 -0700 Subject: Incremental checkin, currently not working, just saving progress. --- nomnom/code_obj.nom | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 nomnom/code_obj.nom (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom new file mode 100644 index 0000000..4df312e --- /dev/null +++ b/nomnom/code_obj.nom @@ -0,0 +1,180 @@ +# 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 + indentation levels. + +use "lib/object.nom" + +object (Code): + my action [set up]: + %old_bits = %me.bits + %me.bits = [] + if (%me.source is text): + %me.source = (Source from text %me.source) + for % in %old_bits: + %me::add % + + my action [as text]: + if (%me.__str == (nil)): + set {%buff:[], %indent:0} + for % in %me.bits: + if (% is text): + %spaces = (%::matching "\n([ ]*)[^\n]*$") + if %spaces.1: %indent = (size of %spaces) + ..else: + % = "\%" + if (%indent > 0): + % = (%::with "\n" -> "\n\(" "::* %indent)") + %buff::add % + %me.__str = (%buff::joined) + 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 nomsu] (..) + "(\(%me.class.name) \(%me.source::as nomsu) \(%me.bits::as nomsu))" + + my action [size] (size of "\%me") + + my action [mark as dirty]: + %me.__str = (nil) + %me._trailing_line_len = (nil) + %me._num_lines = (nil) + + my action [add %new_bits]: + unless (%new_bits is a "List"): + %new_bits = [%new_bits] + for % in %new_bits: + if (% == ""): do next % + 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]*$")) + return %me._trailing_line_len + + my action [number of lines]: + unless %me._num_lines: + %num_lines = 1 + for % in %me: + if (% is text): + %num_lines += (size of (%::all matches of "\n")) + ..else: + %num_lines += ((%::number of lines) - 1) + %me._num_lines = %num_lines + return %me._num_lines + + 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 [add %values joined with %joiner]: + %me::add %values joined with %joiner or %joiner + + my action [add %values joined with %joiner or %wrapping_joiner]: + %line_len = 0 + %bits = %me.bits + for %i = % in %values: + if (%i > 1): + if (%line_len > 80): + %bits::add %wrapping_joiner + %line_len = 0 + ..else: + %bits::add %joiner + %bits::add % + %line = ("\%"::matching "\n([^\n]*)$") + if %line: + %line_len = (size of %line) + ..else: + %line_len += (size of %) + %me::mark as dirty + + my action [prepend %]: + if ((% isn't text) and (% isn't a %me.__type)): + % = (%::as lua) + %me.bits::add % at index 1 + %me::mark as dirty + + my action [parenthesize]: + %me.bits::add "(" at index 1 + %me.bits::add ")" + %me::mark as dirty + + +object (Lua Code) extends (Code): + my action [add free vars %vars]: + if ((size of %vars) == 0): return + %seen = (%v = (yes) for %v in %me.free_vars) + for %var in %vars: + assume (%var is text) + unless %seen.%var: + %me.free_vars::add %var + %seen.%var = (yes) + %me::mark as dirty + + my action [remove free vars %vars]: + if ((size of %vars) == 0): return + %removals = {} + for %var in %vars: + assume (%var is text) + %removals.%var = (yes) + + %stack = [%me] + 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 + for % in %lua.bits: + if (% is a "Lua Code"): + %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 %to_declare]: + if ((size of %to_declare) > 0): + %me::remove free vars %to_declare + %me::prepend "local \(%to_declare::joined with ", ");\n" + return %to_declare + + my action [as statements] (%me::as statements "" ";") + my action [as statements %prefix] (%me::as statements %prefix ";") + my action [as statements %prefix %suffix]: + unless %me.is_value: + return %me + %statements = (Lua Code %me.source []) + if (%prefix != ""): + %statements::add %prefix + %statements::add %me + if (%suffix != ""): + %statements::add %suffix + return %statements + + action [Lua Code from %tree] (..) + Lua Code {source:%tree.source, bits:[], is_value:(no), free_vars:[]} + action [Lua Code from %tree %bits] (..) + Lua Code {source:%tree.source, bits:%bits, is_value:(no), free_vars:[]} + + action [Lua Value from %tree] (..) + Lua Code {source:%tree.source, bits:[], is_value:(yes), free_vars:[]} + action [Lua Value from %tree %bits] (..) + Lua Code {source:%tree.source, bits:%bits, is_value:(yes), free_vars:[]} + +object (Nomsu Code) extends (Code): + action [Nomsu Code from %tree] (..) + Nomsu Code {source:%tree.source, bits:[]} + action [Nomsu Code from %tree %bits] (..) + Nomsu Code {source:%tree.source, bits:%bits} -- cgit v1.2.3 From 692fae5416ce1f2702b599ffb27b2e3d2235eba7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 12:45:08 -0700 Subject: Incremental fixes and more nomnom ports. --- nomnom/code_obj.nom | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index 4df312e..057748f 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -163,18 +163,20 @@ object (Lua Code) extends (Code): %statements::add %suffix return %statements - action [Lua Code from %tree] (..) - Lua Code {source:%tree.source, bits:[], is_value:(no), free_vars:[]} - action [Lua Code from %tree %bits] (..) - Lua Code {source:%tree.source, bits:%bits, is_value:(no), free_vars:[]} - - action [Lua Value from %tree] (..) - Lua Code {source:%tree.source, bits:[], is_value:(yes), free_vars:[]} - action [Lua Value from %tree %bits] (..) - Lua Code {source:%tree.source, bits:%bits, is_value:(yes), free_vars:[]} + action [Lua Code 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:(no), free_vars:[]} + action [Lua Value from %tree {%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:[]} object (Nomsu Code) extends (Code): - action [Nomsu Code from %tree] (..) - Nomsu Code {source:%tree.source, bits:[]} - action [Nomsu Code from %tree %bits] (..) - Nomsu Code {source:%tree.source, bits:%bits} + action [Nomsu Code from %source {%bits:[]}]: + if (%bits is text): %bits = [%bits] + if (%source is a "Syntax Tree"): %source = %source.source + return (..) + Nomsu Code {source:%source, bits:%bits} -- cgit v1.2.3 From 3f31b09e7404e1ea374bbeb230bf34664b641efb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 14:00:05 -0700 Subject: Updated to the point of actually compiling. --- nomnom/code_obj.nom | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index 057748f..cc7bc04 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -163,20 +163,23 @@ object (Lua Code) extends (Code): %statements::add %suffix return %statements - action [Lua Code from %source {%bits:[]}]: + action [Lua Code 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:(no), free_vars:[]} - action [Lua Value from %tree {%bits:[]}]: + action [Lua Code from %source] (Lua Code from %source []) + action [Lua Value from %tree %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 []) object (Nomsu Code) extends (Code): - action [Nomsu Code from %source {%bits:[]}]: + action [Nomsu Code from %source %bits]: if (%bits is text): %bits = [%bits] if (%source is a "Syntax Tree"): %source = %source.source return (..) Nomsu Code {source:%source, bits:%bits} + action [Nomsu Code from %source] (Nomsu Code from %source []) -- cgit v1.2.3 From 678344182b1f04e35063d7185ac1d74317b011ea Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 18:36:36 -0700 Subject: Forward progress on getting nomnom working. --- nomnom/code_obj.nom | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index cc7bc04..7ff1b23 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -6,6 +6,7 @@ use "lib/object.nom" object (Code): my action [set up]: + assume %me.source %old_bits = %me.bits %me.bits = [] if (%me.source is text): @@ -13,19 +14,24 @@ 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()" if (%me.__str == (nil)): set {%buff:[], %indent:0} - for % in %me.bits: - if (% is text): - %spaces = (%::matching "\n([ ]*)[^\n]*$") - if %spaces.1: %indent = (size of %spaces) + for %bit in %me.bits: + if (%bit is text): + %spaces = (%bit::matching "\n([ ]*)[^\n]*$") + if %spaces: %indent = (size of %spaces.1) ..else: - % = "\%" + %bit = "\%bit" if (%indent > 0): - % = (%::with "\n" -> "\n\(" "::* %indent)") - %buff::add % + %bit = (%bit::with "\n" -> "\n\(" "::* %indent)") + %buff::add %bit %me.__str = (%buff::joined) + external %depth = (%depth - 1) return %me.__str my action [as lua] (..) @@ -76,19 +82,20 @@ object (Code): my action [add %values joined with %joiner or %wrapping_joiner]: %line_len = 0 %bits = %me.bits - for %i = % in %values: + for %value in %values at %i: + assume (%value != %me) if (%i > 1): if (%line_len > 80): %bits::add %wrapping_joiner %line_len = 0 ..else: %bits::add %joiner - %bits::add % - %line = ("\%"::matching "\n([^\n]*)$") + %bits::add %value + %line = ("\%value"::matching "\n([^\n]*)$") if %line: %line_len = (size of %line) ..else: - %line_len += (size of %) + %line_len += (size of %value) %me::mark as dirty my action [prepend %]: @@ -155,7 +162,7 @@ object (Lua Code) extends (Code): my action [as statements %prefix %suffix]: unless %me.is_value: return %me - %statements = (Lua Code %me.source []) + %statements = (Lua Code from %me.source []) if (%prefix != ""): %statements::add %prefix %statements::add %me -- cgit v1.2.3 From 63d8b1cd3f34b15bf86210b99209e8b57e7019bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 22:15:06 -0700 Subject: Fully working, I think? (with a lot of shims) --- nomnom/code_obj.nom | 74 +++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 34 deletions(-) (limited to 'nomnom/code_obj.nom') 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]: -- cgit v1.2.3 From 2f68357cb6800e97edd31abfc707d7c7905faa64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Oct 2018 16:26:24 -0700 Subject: Some incremental progress. --- nomnom/code_obj.nom | 55 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index f573599..363713c 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -4,10 +4,27 @@ use "lib/object.nom" +object (Hole): + action [Hole from %lua]: + return (Hole {lua:%lua}) + + my action [as lua]: + return %me.lua + + my action [as nomsu]: + return "(Hole {lua:\(%me.lua)})" + + my action [as text]: + barf "Not implemented" + + my action [as smext]: + barf "Must fill in holes before smexting" + + object (Code): my action [set up]: assume %me.source - %old_bits = %me.bits + %old_bits = (%me.bits if (%me.bits is a "List") else [%me.bits]) %me.bits = [] if (%me.source is text): %me.source = (Source from text %me.source) @@ -34,10 +51,10 @@ object (Code): my action [as lua]: barf - return "\(%me.class.name::as lua id)_from_1_2(\(%me.source::as lua), \(%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] (..) - "(\(%me.class.name) \(%me.source::as nomsu) \(%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)) @@ -170,20 +187,42 @@ object (Lua Code) extends (Code): if (%suffix != ""): %statements::add (%suffix or ";") return %statements + + my action [variables]: + %vars = [] + for %code in recursive %me: + if %code.is_variable: + %vars::add (%code::as smext) + for % in %code.bits: + unless (% is text): + recurse %code on % + return %vars action [Lua Code from %source %bits]: - if (%bits is text): %bits = [%bits] + assume %source + unless (%bits is a "List"): %bits = [%bits] if (%source is a "Syntax Tree"): %source = %source.source return (..) - Lua Code {source:%source, bits:%bits, is_value:(no), free_vars:[]} + 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]: - if (%bits is text): %bits = [%bits] + assume %source + unless (%bits is a "List"): %bits = [%bits] if (%source is a "Syntax Tree"): %source = %source.source return (..) - Lua Code {source:%source, bits:%bits, is_value:(yes), free_vars:[]} + 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 []) + action [Lua Variable from %source %bits]: + assume %source + unless (%bits is a "List"): %bits = [%bits] + if (%source is a "Syntax Tree"): %source = %source.source + return (..) + Lua Code {source:%source, bits:%bits, is_value: yes, is_variable: yes, free_vars:[]} + +# TODO: remove this shim (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 @@ -196,3 +235,5 @@ object (Nomsu Code) extends (Code): 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) + action [Nomsu Code] (Nomsu Code from (nil) []) -- cgit v1.2.3 From 7a35e38d8778670fe0662f203e82638355db3bba Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 31 Oct 2018 15:05:17 -0700 Subject: Renamed (action %) -> (%'s meaning) --- nomnom/code_obj.nom | 132 +++++++++++++++++++++++++++------------------------- 1 file changed, 69 insertions(+), 63 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index 363713c..8379f36 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -1,25 +1,19 @@ +#!/usr/bin/env nomsu -V4.8.10 # 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 indentation levels. - use "lib/object.nom" object (Hole): - action [Hole from %lua]: + externally (Hole from %lua) means: return (Hole {lua:%lua}) - - my action [as lua]: - return %me.lua - + my action [as lua]: return %me.lua my action [as nomsu]: return "(Hole {lua:\(%me.lua)})" - my action [as text]: barf "Not implemented" - my action [as smext]: barf "Must fill in holes before smexting" - object (Code): my action [set up]: @@ -28,12 +22,10 @@ object (Code): %me.bits = [] if (%me.source is text): %me.source = (Source from text %me.source) - for % in %old_bits: - %me::add % - + for % in %old_bits: %me::add % + my action [as text]: barf "Not implemented" - my action [as smext]: if (%me.__str == (nil)): set {%buff:[], %indent:0} @@ -45,32 +37,41 @@ object (Code): %bit = (%bit::as smext) if (%indent > 0): %bit = (%bit::with "\n" -> "\n\(" "::* %indent)") + %buff::add %bit + %me.__str = (%buff::joined) + return %me.__str - + my action [as lua]: barf - 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] (..) - "(\(%me.class.name) \((%me.source::as nomsu) if %me.source else "(nil)") \(%me.bits::as nomsu))" - - my action [size] (size of (%me::as smext)) + 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] "\ + ..(\(%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 [mark as dirty]: %me.__str = (nil) %me._trailing_line_len = (nil) %me._num_lines = (nil) - + my action [add %new_bits]: unless (%new_bits is a "List"): %new_bits = [%new_bits] for % in %new_bits: if (% == ""): do next % + #if ((% isn't text) and (% isn't a (Code))): % = (%::as lua) %me.bits::add % + %me::mark as dirty my action [trailing line length]: @@ -86,15 +87,15 @@ object (Code): %num_lines += (size of (%::all matches of "\n")) ..else: %num_lines += ((%::number of lines) - 1) + %me._num_lines = %num_lines + return %me._num_lines - + 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 [add %values joined with %joiner]: %me::add %values joined with %joiner or %joiner - my action [add %values joined with %joiner or %wrapping_joiner]: %line_len = 0 %bits = %me.bits @@ -103,8 +104,8 @@ object (Code): if (%line_len > 80): %bits::add %wrapping_joiner %line_len = 0 - ..else: - %bits::add %joiner + ..else: %bits::add %joiner + %bits::add %value unless (%value is text): %value = (%value::as smext) @@ -113,6 +114,7 @@ object (Code): %line_len = (size of %line) ..else: %line_len += (size of %value) + %me::mark as dirty my action [prepend %]: @@ -120,13 +122,12 @@ object (Code): % = (%::as lua) %me.bits::add % at index 1 %me::mark as dirty - + my action [parenthesize]: %me.bits::add "(" at index 1 %me.bits::add ")" %me::mark as dirty - object (Lua Code) extends (Code): my action [add free vars %vars]: if ((size of %vars) == 0): return @@ -136,6 +137,7 @@ object (Lua Code) extends (Code): unless %seen.%var: %me.free_vars::add %var %seen.%var = (yes) + %me::mark as dirty my action [remove free vars %vars]: @@ -153,11 +155,13 @@ object (Lua Code) extends (Code): lua> "table.remove(\%lua.free_vars, \%i)" #TODO: reinstate this #%lua.free_vars::remove at index %i + + for % in %lua.bits: - unless (% is text): - %stack::add % + unless (% is text): %stack::add % + %me::mark as dirty - + my action [declare locals] (%me::declare locals (nil)) my action [declare locals %to_declare]: unless %to_declare: @@ -167,19 +171,19 @@ object (Lua Code) extends (Code): unless %seen.%var: %seen.%var = (yes) %to_declare::add %var + for % in %lua.bits: - unless (% is text): - recurse %lua on % + 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" return %to_declare - + my action [as statements] (%me::as statements "" ";") my action [as statements %prefix] (%me::as statements %prefix ";") my action [as statements %prefix %suffix]: - unless %me.is_value: - return %me + unless %me.is_value: return %me %statements = (Lua Code from %me.source []) if ((%prefix or "") != ""): %statements::add %prefix @@ -194,46 +198,48 @@ object (Lua Code) extends (Code): if %code.is_variable: %vars::add (%code::as smext) for % in %code.bits: - unless (% is text): - recurse %code on % + unless (% is text): recurse %code on % + return %vars - - action [Lua Code from %source %bits]: + + externally (Lua Code from %source %bits) means: assume %source unless (%bits is a "List"): %bits = [%bits] - if (%source is a "Syntax Tree"): %source = %source.source - 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]: + if (%source is a "Syntax Tree"): + %source = %source.source + return (Lua Code {source:%source, bits:%bits, is_value:no, free_vars:[]}) + + externally (Lua Code from %source) means (Lua Code from %source []) + externally (Lua Value from %source %bits) means: assume %source unless (%bits is a "List"): %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 %source] (Lua Value from %source []) - - action [Lua Variable from %source] (Lua Variable from %source []) - action [Lua Variable from %source %bits]: + if (%source is a "Syntax Tree"): + %source = %source.source + return (Lua Code {source:%source, bits:%bits, is_value:yes, free_vars:[]}) + + externally (Lua Value from %source) means (Lua Value from %source []) + externally (Lua Variable from %source) means (Lua Variable from %source []) + externally (Lua Variable from %source %bits) means: assume %source 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 (..) - 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 (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]: + externally (Nomsu Code from %source %bits) means: if (%bits is text): %bits = [%bits] - if (%source is a "Syntax Tree"): %source = %source.source - 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) - action [Nomsu Code] (Nomsu Code from (nil) []) + if (%source is a "Syntax Tree"): + %source = %source.source + return (Nomsu Code {source:%source, bits:%bits}) + + externally (Nomsu Code from %source) means (Nomsu Code from %source []) + externally (Nomsu Code %bits) means (Nomsu Code from (nil) %bits) + externally (Nomsu Code) means (Nomsu Code from (nil) []) -- cgit v1.2.3 From 307dea18815ba4a06a3098edb170d7ad90708815 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 14:38:24 -0700 Subject: Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of foo_1_baz_2, removed "smext", made some cleanup changes. --- nomnom/code_obj.nom | 269 +++++++++++++++++++++++----------------------------- 1 file changed, 117 insertions(+), 152 deletions(-) (limited to 'nomnom/code_obj.nom') diff --git a/nomnom/code_obj.nom b/nomnom/code_obj.nom index 8379f36..c8d2784 100644 --- a/nomnom/code_obj.nom +++ b/nomnom/code_obj.nom @@ -2,103 +2,89 @@ # 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 indentation levels. -use "lib/object.nom" +use "lib/things.nom" -object (Hole): - externally (Hole from %lua) means: - return (Hole {lua:%lua}) - my action [as lua]: return %me.lua - my action [as nomsu]: - return "(Hole {lua:\(%me.lua)})" - my action [as text]: - barf "Not implemented" - my action [as smext]: - barf "Must fill in holes before smexting" +a (Code Buffer) is a thing: + that can (set up) by: + assume %its.source + %old_bits = (%its.bits if (%its.bits is a "List") else [%its.bits]) + %its.bits = [] + if (type of %its.source) is: + "Text": + %its.source = (Source from text %its.source) + "Syntax Tree": + %its.source = %its.source.source -object (Code): - my action [set up]: - assume %me.source - %old_bits = (%me.bits if (%me.bits is a "List") else [%me.bits]) - %me.bits = [] - if (%me.source is text): - %me.source = (Source from text %me.source) - for % in %old_bits: %me::add % - - my action [as text]: - barf "Not implemented" - my action [as smext]: - if (%me.__str == (nil)): - set {%buff:[], %indent:0} - for %bit in %me.bits: + for % in %old_bits: %its::add % + + whose (text) means: + if (%its._text == (nil)): + %buff = [] + %indent = 0 + for %bit in %its.bits: if (%bit is text): %spaces = (%bit::matching "\n([ ]*)[^\n]*$") if %spaces: %indent = (size of %spaces.1) ..else: - %bit = (%bit::as smext) + %bit = (%bit::text) if (%indent > 0): %bit = (%bit::with "\n" -> "\n\(" "::* %indent)") - %buff::add %bit - - %me.__str = (%buff::joined) - - return %me.__str - - my action [as lua]: - barf - 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] "\ - ..(\(%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 [mark as dirty]: - %me.__str = (nil) - %me._trailing_line_len = (nil) - %me._num_lines = (nil) - - my action [add %new_bits]: + %its._text = (%buff::joined) + return %its._text + + whose (lua code) means "\ + ..a_\(%its.class.name::as lua id)_with{source=\(..) + (%its.source::as lua) if %its.source else "nil" + .., \(%its.bits::as lua)}" + + whose (nomsu code) means "\ + ..(a \(%its.class.name) with {source: \((%its.source::as nomsu) if %its.source else "(nil)"), bits: \(..) + %its.bits::as nomsu + ..})" + + whose (size) means (size of (%its::text)) + + that can (mark as dirty) by: + %its._text = (nil) + %its._trailing_line_len = (nil) + %its._num_lines = (nil) + + that can (add %new_bits) by: unless (%new_bits is a "List"): %new_bits = [%new_bits] for % in %new_bits: if (% == ""): do next % - #if ((% isn't text) and (% isn't a (Code))): % = (%::as lua) - %me.bits::add % - - %me::mark as dirty + %its.bits::add % + %its::mark as dirty - my action [trailing line length]: - if (%me._trailing_line_len == (nil)): - %me._trailing_line_len = (size of ((%me::as smext)::matching "[^\n]*$")) - return %me._trailing_line_len + whose (trailing line length) means: + if (%its._trailing_line_len == (nil)): + %its._trailing_line_len = (size of ((%its::text)::matching "[^\n]*$")) + return %its._trailing_line_len - my action [number of lines]: - unless %me._num_lines: + whose (number of lines) means: + unless %its._num_lines: %num_lines = 1 - for % in %me: + for % in %its: if (% is text): %num_lines += (size of (%::all matches of "\n")) ..else: %num_lines += ((%::number of lines) - 1) - %me._num_lines = %num_lines + %its._num_lines = %num_lines - return %me._num_lines + return %its._num_lines - 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 [add %values joined with %joiner]: - %me::add %values joined with %joiner or %joiner - my action [add %values joined with %joiner or %wrapping_joiner]: + whose [is multiline, is multi-line] all mean ((%its::number of lines) > 1) + whose [is one line, is single line] all mean ((%its::number of lines) == 1) + that can (add %values joined with %joiner) by: + %its::add %values joined with %joiner or %joiner + that can [add %values joined with %joiner or %wrapping_joiner] by: %line_len = 0 - %bits = %me.bits + %bits = %its.bits for %value in %values at %i: if (%i > 1): if (%line_len > 80): @@ -108,65 +94,71 @@ object (Code): %bits::add %value unless (%value is text): - %value = (%value::as smext) + %value = (%value::text) %line = (%value::matching "\n([^\n]*)$") if %line: %line_len = (size of %line) ..else: %line_len += (size of %value) - - %me::mark as dirty + %its::mark as dirty - my action [prepend %]: - #if ((% isn't text) and (% isn't a %me.__type)): + that can (prepend %) by: + #if ((% isn't text) and (% isn't a %its.__type)): % = (%::as lua) - %me.bits::add % at index 1 - %me::mark as dirty + %its.bits::add % at index 1 + %its::mark as dirty - my action [parenthesize]: - %me.bits::add "(" at index 1 - %me.bits::add ")" - %me::mark as dirty + that can (parenthesize) by: + %its.bits::add "(" at index 1 + %its.bits::add ")" + %its::mark as dirty -object (Lua Code) extends (Code): - my action [add free vars %vars]: +a (Lua Buffer) is a thing: + that has [..] + text, lua code, nomsu code, trailing line length, size, number of lines, + is multiline, is multi-line, is one line, is single line, + ..like a (Code Buffer) + that can [..] + set up, mark as dirty, add %, prepend %, parenthesize, + add % joined with %, add % joined with % or %, + ..like a (Code Buffer) + + that can (add free vars %vars) by: if ((size of %vars) == 0): return - %seen = (%v = (yes) for %v in %me.free_vars) + %seen = (%v = (yes) for %v in %its.free_vars) for %var in %vars: assume (%var is text) unless %seen.%var: - %me.free_vars::add %var + %its.free_vars::add %var %seen.%var = (yes) - %me::mark as dirty + %its::mark as dirty - my action [remove free vars %vars]: + that can (remove free vars %vars) by: if ((size of %vars) == 0): return %removals = {} for %var in %vars: assume (%var is text) %removals.%var = (yes) - %stack = [%me] + %stack = [%its] 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> "table.remove(\%lua.free_vars, \%i)" - #TODO: reinstate this - #%lua.free_vars::remove at index %i - + %lua.free_vars::remove at index %i for % in %lua.bits: unless (% is text): %stack::add % - %me::mark as dirty + %its::mark as dirty - my action [declare locals] (%me::declare locals (nil)) - my action [declare locals %to_declare]: + that can (declare locals) by (%its::declare locals (nil)) + that can (declare locals %to_declare) by: unless %to_declare: - set {%to_declare:[], %seen:{}} - for %lua in recursive %me: + %to_declare = [] + %seen = {} + for %lua in recursive %its: for %var in %lua.free_vars: unless %seen.%var: %seen.%var = (yes) @@ -176,70 +168,43 @@ object (Lua Code) extends (Code): 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" + %its::remove free vars %to_declare + %its::prepend "local \(%to_declare::joined with ", ");\n" return %to_declare - my action [as statements] (%me::as statements "" ";") - my action [as statements %prefix] (%me::as statements %prefix ";") - my action [as statements %prefix %suffix]: - unless %me.is_value: return %me - %statements = (Lua Code from %me.source []) + whose (as statements) means (%its::as statements with "") + whose (as statements with %prefix) means: + unless %its.is_value: return %its + %statements = (a Lua Buffer with {source:%its.source}) if ((%prefix or "") != ""): %statements::add %prefix - %statements::add %me - if (%suffix != ""): - %statements::add (%suffix or ";") + %statements::add %its + %statements::add ";" return %statements - my action [variables]: + that can (mark as value) by: + %its.is_value = (yes) + + that can (mark as variable) by: + %its.is_variable = (yes) + %its.is_value = (yes) + + that can (variables) by: %vars = [] - for %code in recursive %me: + for %code in recursive %its: if %code.is_variable: - %vars::add (%code::as smext) + %vars::add (%code::text) for % in %code.bits: unless (% is text): recurse %code on % return %vars - externally (Lua Code from %source %bits) means: - assume %source - unless (%bits is a "List"): %bits = [%bits] - if (%source is a "Syntax Tree"): - %source = %source.source - return (Lua Code {source:%source, bits:%bits, is_value:no, free_vars:[]}) - - externally (Lua Code from %source) means (Lua Code from %source []) - externally (Lua Value from %source %bits) means: - assume %source - unless (%bits is a "List"): %bits = [%bits] - if (%source is a "Syntax Tree"): - %source = %source.source - return (Lua Code {source:%source, bits:%bits, is_value:yes, free_vars:[]}) - - externally (Lua Value from %source) means (Lua Value from %source []) - externally (Lua Variable from %source) means (Lua Variable from %source []) - externally (Lua Variable from %source %bits) means: - assume %source - unless (%bits is a "List"): %bits = [%bits] - if (%source is a "Syntax Tree"): - %source = %source.source - return (..) - Lua Code {..} - source:%source, bits:%bits, is_value:yes, is_variable:yes, free_vars:[] - -# TODO: remove this shim -(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): - externally (Nomsu Code from %source %bits) means: - if (%bits is text): %bits = [%bits] - if (%source is a "Syntax Tree"): - %source = %source.source - return (Nomsu Code {source:%source, bits:%bits}) - - externally (Nomsu Code from %source) means (Nomsu Code from %source []) - externally (Nomsu Code %bits) means (Nomsu Code from (nil) %bits) - externally (Nomsu Code) means (Nomsu Code from (nil) []) +a (Nomsu Buffer) is a thing: + that has [..] + text, lua code, nomsu code, trailing line length, size, number of lines, + is multiline, is multi-line, is one line, is single line, + ..like a (Code Buffer) + that can [..] + set up, mark as dirty, add %, prepend %, parenthesize, + add % joined with %, add % joined with % or %, + ..like a (Code Buffer) -- cgit v1.2.3