From 765cc704d5e9d652dc606440203e8c6a0fd3361e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 23 Jul 2018 15:28:35 -0700 Subject: Renamed nomsu_tree -> syntax_tree, so anyone looking for syntax trees knows where to look. --- Makefile | 4 +- README.md | 2 +- nomsu_compiler.lua | 2 +- nomsu_compiler.moon | 2 +- nomsu_tree.lua | 165 ---------------------------------------------------- nomsu_tree.moon | 68 ---------------------- parser.lua | 2 +- parser.moon | 2 +- syntax_tree.lua | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++ syntax_tree.moon | 68 ++++++++++++++++++++++ 10 files changed, 240 insertions(+), 240 deletions(-) delete mode 100644 nomsu_tree.lua delete mode 100644 nomsu_tree.moon create mode 100644 syntax_tree.lua create mode 100644 syntax_tree.moon diff --git a/Makefile b/Makefile index 117ab76..dfd1eec 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,9 @@ PREFIX= UNINSTALL_VERSION= # ========= You shouldn't need to mess with any of these variables below ================ -MOON_FILES= code_obj.moon error_handling.moon files.moon nomsu.moon nomsu_compiler.moon nomsu_tree.moon parser.moon +MOON_FILES= code_obj.moon error_handling.moon files.moon nomsu.moon nomsu_compiler.moon syntax_tree.moon parser.moon LUA_FILES= code_obj.lua consolecolors.lua error_handling.lua files.lua nomsu.lua nomsu_compiler.lua \ - nomsu_tree.lua parser.lua utils.lua uuid.lua + syntax_tree.lua parser.lua utils.lua uuid.lua CORE_NOM_FILES= $(wildcard core/*.nom) CORE_LUA_FILES= $(patsubst %.nom,%.lua,$(CORE_NOM_FILES)) LIB_NOM_FILES= $(wildcard lib/*.nom) diff --git a/README.md b/README.md index e19f27a..26301da 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ All `.moon` files have been precompiled into corresponding `.lua` files, so you * `nomsu.peg` - The [Parsing Expression Grammar](https://en.wikipedia.org/wiki/Parsing_expression_grammar) used to define Nomsu's syntax. The format of this file is a slightly modified version of the format accepted by LPEG's `re` module. * `nomsu_compiler.moon` - **The actual Nomsu compiler**. This file can be imported and used without going through the regular command line interface (e.g. for applications that want to embed the compiler). * `parser.moon` - The Nomsu parser. This file can also be imported and used directly for applications that only need to *parse* Nomsu, not compile it. -* `nomsu_tree.moon` - Datastructures used for Nomsu ASTs. +* `syntax_tree.moon` - Datastructures used for Nomsu Abstract Syntax Trees. * `code_obj.moon` - Datastructures used for incrementally building generated code, while preserving code origins. * `error_handling.moon` - The logic for producing good error messages within Lua that reference the Nomsu source code that led to them. * `utils.lua` - A set of utility actions used by nomsu.moon. diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua index 0299156..42b1a1d 100644 --- a/nomsu_compiler.lua +++ b/nomsu_compiler.lua @@ -28,7 +28,7 @@ do local _obj_0 = require("code_obj") NomsuCode, LuaCode, Source = _obj_0.NomsuCode, _obj_0.LuaCode, _obj_0.Source end -local AST = require("nomsu_tree") +local AST = require("syntax_tree") local Parser = require("parser") SOURCE_MAP = { } string.as_lua_id = function(str) diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon index 2856ff2..ee5eca4 100644 --- a/nomsu_compiler.moon +++ b/nomsu_compiler.moon @@ -21,7 +21,7 @@ colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..tostring unpack or= table.unpack {:match, :sub, :gsub, :format, :byte, :find} = string {:NomsuCode, :LuaCode, :Source} = require "code_obj" -AST = require "nomsu_tree" +AST = require "syntax_tree" Parser = require("parser") -- Mapping from source string (e.g. "@core/metaprogramming.nom[1:100]") to a mapping -- from lua line number to nomsu line number diff --git a/nomsu_tree.lua b/nomsu_tree.lua deleted file mode 100644 index 64b4efc..0000000 --- a/nomsu_tree.lua +++ /dev/null @@ -1,165 +0,0 @@ -local repr -repr = require('utils').repr -local insert, remove, concat -do - local _obj_0 = table - insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat -end -local Source -Source = require("code_obj").Source -local unpack = unpack or table.unpack -local AST = { } -AST.is_syntax_tree = function(n, t) - if t == nil then - t = nil - end - return type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t) -end -local types = { - "Number", - "Var", - "Block", - "EscapedNomsu", - "Text", - "List", - "Dict", - "DictEntry", - "IndexChain", - "Action", - "FileChunks" -} -for _index_0 = 1, #types do - local name = types[_index_0] - local cls = { } - do - cls.__class = cls - cls.__index = cls - cls.__name = name - cls.type = name - cls.is_instance = function(self, x) - return getmetatable(x) == self - end - cls.__tostring = function(self) - return tostring(self.type) .. "(" .. tostring(repr(tostring(self.source))) .. ", " .. tostring(concat((function() - local _accum_0 = { } - local _len_0 = 1 - for _index_1 = 1, #self do - local v = self[_index_1] - _accum_0[_len_0] = repr(v) - _len_0 = _len_0 + 1 - end - return _accum_0 - end)(), ', ')) .. ")" - end - cls.map = function(self, fn) - local replacement = fn(self) - if replacement == false then - return nil - end - if replacement then - replacement = (replacement.__class)(self.source, unpack(replacement)) - else - local replacements = { } - local changes = false - for i, v in ipairs(self) do - local _continue_0 = false - repeat - replacements[#replacements + 1] = v - if AST.is_syntax_tree(v) then - local r = v:map(fn) - if r == v or r == nil then - _continue_0 = true - break - end - changes = true - replacements[#replacements] = r - end - _continue_0 = true - until true - if not _continue_0 then - break - end - end - if not (changes) then - return self - end - replacement = (self.__class)(self.source, unpack(replacements)) - end - if self.comments then - do - local _accum_0 = { } - local _len_0 = 1 - local _list_0 = self.comments - for _index_1 = 1, #_list_0 do - local c = _list_0[_index_1] - _accum_0[_len_0] = c - _len_0 = _len_0 + 1 - end - replacement.comments = _accum_0 - end - end - return replacement - end - cls.__eq = function(self, other) - if type(self) ~= type(other) or #self ~= #other or getmetatable(self) ~= getmetatable(other) then - return false - end - for i = 1, #self do - if self[i] ~= other[i] then - return false - end - end - return true - end - end - AST[name] = setmetatable(cls, { - __tostring = function(self) - return self.name - end, - __call = function(self, source, ...) - if type(source) == 'string' then - source = Source:from_string(source) - end - for i = 1, select('#', ...) do - assert(select(i, ...)) - end - assert(Source:is_instance(source)) - local inst = { - source = source, - ... - } - setmetatable(inst, self) - if inst.__init then - inst:__init() - end - return inst - end - }) -end -AST.Action.__init = function(self) - local stub_bits - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #self do - local a = self[_index_0] - _accum_0[_len_0] = type(a) == 'string' and a or '%' - _len_0 = _len_0 + 1 - end - stub_bits = _accum_0 - end - self.stub = concat(stub_bits, " ") -end -AST.Action.get_args = function(self) - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #self do - local tok = self[_index_0] - if type(tok) ~= 'string' then - _accum_0[_len_0] = tok - _len_0 = _len_0 + 1 - end - end - return _accum_0 -end -return AST diff --git a/nomsu_tree.moon b/nomsu_tree.moon deleted file mode 100644 index 885892f..0000000 --- a/nomsu_tree.moon +++ /dev/null @@ -1,68 +0,0 @@ --- This file contains the datastructures used to represent parsed Nomsu syntax trees, --- as well as the logic for converting them to Lua code. -{:repr} = require 'utils' -{:insert, :remove, :concat} = table -{:Source} = require "code_obj" -unpack or= table.unpack - -AST = {} -AST.is_syntax_tree = (n, t=nil)-> - type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t) - -types = {"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry", - "IndexChain", "Action", "FileChunks"} -for name in *types - cls = {} - with cls - .__class = cls - .__index = cls - .__name = name - .type = name - .is_instance = (x)=> getmetatable(x) == @ - .__tostring = => "#{@type}(#{repr tostring(@source)}, #{concat([repr(v) for v in *@], ', ')})" - .map = (fn)=> - replacement = fn(@) - if replacement == false then return nil - if replacement - -- Clone the replacement, but give it a proper source - replacement = (replacement.__class)(@source, unpack(replacement)) - else - replacements = {} - changes = false - for i,v in ipairs(@) - replacements[#replacements+1] = v - if AST.is_syntax_tree(v) - r = v\map(fn) - continue if r == v or r == nil - changes = true - replacements[#replacements] = r - return @ unless changes - replacement = (@__class)(@source, unpack(replacements)) - replacement.comments = [c for c in *@comments] if @comments - return replacement - .__eq = (other)=> - return false if type(@) != type(other) or #@ != #other or getmetatable(@) != getmetatable(other) - for i=1,#@ - return false if @[i] != other[i] - return true - - AST[name] = setmetatable cls, - __tostring: => @name - __call: (source, ...)=> - if type(source) == 'string' - source = Source\from_string(source) - for i=1,select('#', ...) do assert(select(i,...)) - assert(Source\is_instance(source)) - inst = {:source, ...} - setmetatable(inst, @) - if inst.__init then inst\__init! - return inst - -AST.Action.__init = => - stub_bits = [type(a) == 'string' and a or '%' for a in *@] - @stub = concat stub_bits, " " - -AST.Action.get_args = => - [tok for tok in *@ when type(tok) != 'string'] - -return AST diff --git a/parser.lua b/parser.lua index d563005..1a567c8 100644 --- a/parser.lua +++ b/parser.lua @@ -19,7 +19,7 @@ do local _obj_0 = require("code_obj") NomsuCode, LuaCode, Source = _obj_0.NomsuCode, _obj_0.LuaCode, _obj_0.Source end -local AST = require("nomsu_tree") +local AST = require("syntax_tree") local NOMSU_DEFS do local _with_0 = { } diff --git a/parser.moon b/parser.moon index cf67205..a961b0b 100644 --- a/parser.moon +++ b/parser.moon @@ -7,7 +7,7 @@ lpeg.setmaxstack 10000 {:insert, :remove} = table files = require 'files' {:NomsuCode, :LuaCode, :Source} = require "code_obj" -AST = require "nomsu_tree" +AST = require "syntax_tree" NOMSU_DEFS = with {} -- Newline supports either windows-style CR+LF or unix-style LF diff --git a/syntax_tree.lua b/syntax_tree.lua new file mode 100644 index 0000000..64b4efc --- /dev/null +++ b/syntax_tree.lua @@ -0,0 +1,165 @@ +local repr +repr = require('utils').repr +local insert, remove, concat +do + local _obj_0 = table + insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat +end +local Source +Source = require("code_obj").Source +local unpack = unpack or table.unpack +local AST = { } +AST.is_syntax_tree = function(n, t) + if t == nil then + t = nil + end + return type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t) +end +local types = { + "Number", + "Var", + "Block", + "EscapedNomsu", + "Text", + "List", + "Dict", + "DictEntry", + "IndexChain", + "Action", + "FileChunks" +} +for _index_0 = 1, #types do + local name = types[_index_0] + local cls = { } + do + cls.__class = cls + cls.__index = cls + cls.__name = name + cls.type = name + cls.is_instance = function(self, x) + return getmetatable(x) == self + end + cls.__tostring = function(self) + return tostring(self.type) .. "(" .. tostring(repr(tostring(self.source))) .. ", " .. tostring(concat((function() + local _accum_0 = { } + local _len_0 = 1 + for _index_1 = 1, #self do + local v = self[_index_1] + _accum_0[_len_0] = repr(v) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ', ')) .. ")" + end + cls.map = function(self, fn) + local replacement = fn(self) + if replacement == false then + return nil + end + if replacement then + replacement = (replacement.__class)(self.source, unpack(replacement)) + else + local replacements = { } + local changes = false + for i, v in ipairs(self) do + local _continue_0 = false + repeat + replacements[#replacements + 1] = v + if AST.is_syntax_tree(v) then + local r = v:map(fn) + if r == v or r == nil then + _continue_0 = true + break + end + changes = true + replacements[#replacements] = r + end + _continue_0 = true + until true + if not _continue_0 then + break + end + end + if not (changes) then + return self + end + replacement = (self.__class)(self.source, unpack(replacements)) + end + if self.comments then + do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = self.comments + for _index_1 = 1, #_list_0 do + local c = _list_0[_index_1] + _accum_0[_len_0] = c + _len_0 = _len_0 + 1 + end + replacement.comments = _accum_0 + end + end + return replacement + end + cls.__eq = function(self, other) + if type(self) ~= type(other) or #self ~= #other or getmetatable(self) ~= getmetatable(other) then + return false + end + for i = 1, #self do + if self[i] ~= other[i] then + return false + end + end + return true + end + end + AST[name] = setmetatable(cls, { + __tostring = function(self) + return self.name + end, + __call = function(self, source, ...) + if type(source) == 'string' then + source = Source:from_string(source) + end + for i = 1, select('#', ...) do + assert(select(i, ...)) + end + assert(Source:is_instance(source)) + local inst = { + source = source, + ... + } + setmetatable(inst, self) + if inst.__init then + inst:__init() + end + return inst + end + }) +end +AST.Action.__init = function(self) + local stub_bits + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #self do + local a = self[_index_0] + _accum_0[_len_0] = type(a) == 'string' and a or '%' + _len_0 = _len_0 + 1 + end + stub_bits = _accum_0 + end + self.stub = concat(stub_bits, " ") +end +AST.Action.get_args = function(self) + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #self do + local tok = self[_index_0] + if type(tok) ~= 'string' then + _accum_0[_len_0] = tok + _len_0 = _len_0 + 1 + end + end + return _accum_0 +end +return AST diff --git a/syntax_tree.moon b/syntax_tree.moon new file mode 100644 index 0000000..885892f --- /dev/null +++ b/syntax_tree.moon @@ -0,0 +1,68 @@ +-- This file contains the datastructures used to represent parsed Nomsu syntax trees, +-- as well as the logic for converting them to Lua code. +{:repr} = require 'utils' +{:insert, :remove, :concat} = table +{:Source} = require "code_obj" +unpack or= table.unpack + +AST = {} +AST.is_syntax_tree = (n, t=nil)-> + type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t) + +types = {"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry", + "IndexChain", "Action", "FileChunks"} +for name in *types + cls = {} + with cls + .__class = cls + .__index = cls + .__name = name + .type = name + .is_instance = (x)=> getmetatable(x) == @ + .__tostring = => "#{@type}(#{repr tostring(@source)}, #{concat([repr(v) for v in *@], ', ')})" + .map = (fn)=> + replacement = fn(@) + if replacement == false then return nil + if replacement + -- Clone the replacement, but give it a proper source + replacement = (replacement.__class)(@source, unpack(replacement)) + else + replacements = {} + changes = false + for i,v in ipairs(@) + replacements[#replacements+1] = v + if AST.is_syntax_tree(v) + r = v\map(fn) + continue if r == v or r == nil + changes = true + replacements[#replacements] = r + return @ unless changes + replacement = (@__class)(@source, unpack(replacements)) + replacement.comments = [c for c in *@comments] if @comments + return replacement + .__eq = (other)=> + return false if type(@) != type(other) or #@ != #other or getmetatable(@) != getmetatable(other) + for i=1,#@ + return false if @[i] != other[i] + return true + + AST[name] = setmetatable cls, + __tostring: => @name + __call: (source, ...)=> + if type(source) == 'string' + source = Source\from_string(source) + for i=1,select('#', ...) do assert(select(i,...)) + assert(Source\is_instance(source)) + inst = {:source, ...} + setmetatable(inst, @) + if inst.__init then inst\__init! + return inst + +AST.Action.__init = => + stub_bits = [type(a) == 'string' and a or '%' for a in *@] + @stub = concat stub_bits, " " + +AST.Action.get_args = => + [tok for tok in *@ when type(tok) != 'string'] + +return AST -- cgit v1.2.3