From 64fd33c0890803f15cf7a8cf99ab796ea7f64af2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 27 Sep 2011 08:40:18 -0700 Subject: [PATCH] refactor types out of compile --- moonscript/compile.lua | 12 ++++++++++-- moonscript/compile.moon | 5 +++-- moonscript/compile/format.lua | 11 ++++++++++- moonscript/compile/format.moon | 3 ++- moonscript/compile/line.lua | 9 ++++++--- moonscript/compile/line.moon | 4 +--- moonscript/compile/value.lua | 6 +++++- moonscript/compile/value.moon | 2 +- moonscript/data.lua | 7 ------- moonscript/data.moon | 12 ++---------- moonscript/init.lua | 2 +- moonscript/init.moon | 2 +- moonscript/parse.lua | 6 ++++-- moonscript/{compile => }/types.lua | 10 ++++++++-- moonscript/{compile => }/types.moon | 15 +++++++++------ test.lua | 2 ++ todo | 7 ++++++- 17 files changed, 71 insertions(+), 44 deletions(-) rename moonscript/{compile => }/types.lua (92%) rename moonscript/{compile => }/types.moon (83%) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 8fa972a..502d303 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -1,11 +1,19 @@ module("moonscript.compile", package.seeall) local util = require("moonscript.util") -local data = require("moonscript.data") local dump = require("moonscript.dump") require("moonscript.compile.format") require("moonscript.compile.line") require("moonscript.compile.value") -local ntype, Set = data.ntype, data.Set +local Set +do + local _table_0 = require("moonscript.data") + Set = _table_0.Set +end +local ntype +do + local _table_0 = require("moonscript.types") + ntype = _table_0.ntype +end local concat, insert = table.concat, table.insert local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim local bubble_names = { diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 4d849c6..e1fe92d 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -1,14 +1,15 @@ module "moonscript.compile", package.seeall util = require "moonscript.util" -data = require "moonscript.data" dump = require "moonscript.dump" require "moonscript.compile.format" require "moonscript.compile.line" require "moonscript.compile.value" -import ntype, Set from data +import Set from require "moonscript.data" +import ntype from require "moonscript.types" + import concat, insert from table import pos_to_line, get_closest_line, trim from util diff --git a/moonscript/compile/format.lua b/moonscript/compile/format.lua index 6dfc591..7fbad85 100644 --- a/moonscript/compile/format.lua +++ b/moonscript/compile/format.lua @@ -1,7 +1,16 @@ module("moonscript.compile", package.seeall) local util = require("moonscript.util") local data = require("moonscript.data") -local Set, ntype = data.Set, data.ntype +local Set +do + local _table_0 = require("moonscript.data") + Set = _table_0.Set +end +local ntype +do + local _table_0 = require("moonscript.types") + ntype = _table_0.ntype +end local concat, insert = table.concat, table.insert indent_char = " " user_error = function(...) diff --git a/moonscript/compile/format.moon b/moonscript/compile/format.moon index a36d41e..4818cb3 100644 --- a/moonscript/compile/format.moon +++ b/moonscript/compile/format.moon @@ -3,7 +3,8 @@ module "moonscript.compile", package.seeall util = require "moonscript.util" data = require "moonscript.data" -import Set, ntype from data +import Set from require "moonscript.data" +import ntype from require "moonscript.types" import concat, insert from table export indent_char, default_return, moonlib, cascading, non_atomic, has_value, is_non_atomic diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index 7444ebe..7765bb6 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -1,10 +1,13 @@ module("moonscript.compile", package.seeall) local util = require("moonscript.util") -local data = require("moonscript.data") require("moonscript.compile.format") -require("moonscript.compile.types") local reversed = util.reversed -local ntype = data.ntype +local ntype, smart_node +do + local _table_0 = require("moonscript.types") + ntype = _table_0.ntype + smart_node = _table_0.smart_node +end local concat, insert = table.concat, table.insert local constructor_name = "new" line_compile = { diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index 96ca9a7..d15b97f 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -1,13 +1,11 @@ module "moonscript.compile", package.seeall util = require "moonscript.util" -data = require "moonscript.data" require "moonscript.compile.format" -require "moonscript.compile.types" import reversed from util -import ntype from data +import ntype, smart_node from require "moonscript.types" import concat, insert from table export line_compile diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index ae2f31b..934ab5f 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -2,7 +2,11 @@ module("moonscript.compile", package.seeall) local util = require("moonscript.util") local data = require("moonscript.data") require("moonscript.compile.format") -local ntype = data.ntype +local ntype +do + local _table_0 = require("moonscript.types") + ntype = _table_0.ntype +end local concat, insert = table.concat, table.insert local table_append table_append = function(name, len, value) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 871c69c..62048f1 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -6,7 +6,7 @@ data = require "moonscript.data" require "moonscript.compile.format" -import ntype from data +import ntype from require "moonscript.types" import concat, insert from table export value_compile diff --git a/moonscript/data.lua b/moonscript/data.lua index e3b32b0..c6d9d93 100644 --- a/moonscript/data.lua +++ b/moonscript/data.lua @@ -1,12 +1,5 @@ module("moonscript.data", package.seeall) local concat = table.concat -ntype = function(node) - if type(node) ~= "table" then - return "value" - else - return node[1] - end -end Set = function(items) local self = { } do diff --git a/moonscript/data.moon b/moonscript/data.moon index 770e37b..6b9050f 100644 --- a/moonscript/data.moon +++ b/moonscript/data.moon @@ -1,19 +1,11 @@ --- data structure utils - +-- data structures & static data module "moonscript.data", package.seeall export Set, Stack -export ntype, lua_keywords +export lua_keywords import concat from table --- type of node as string -ntype = (node) -> - if type(node) != "table" - "value" - else - node[1] - Set = (items) -> self = {} self[key] = true for key in *items diff --git a/moonscript/init.lua b/moonscript/init.lua index 3f9b459..e8ed93a 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -1,6 +1,6 @@ module("moonscript", package.seeall) -require("moonscript.parse") require("moonscript.compile") +require("moonscript.parse") require("moonscript.util") local concat, insert = table.concat, table.insert local split, dump = util.split, util.dump diff --git a/moonscript/init.moon b/moonscript/init.moon index 086bfd1..c315f8b 100644 --- a/moonscript/init.moon +++ b/moonscript/init.moon @@ -1,8 +1,8 @@ module "moonscript", package.seeall -require "moonscript.parse" require "moonscript.compile" +require "moonscript.parse" require "moonscript.util" import concat, insert from table diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 39902c0..92f68dc 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -4,10 +4,12 @@ local util = require"moonscript.util" require"lpeg" -local dump = require"moonscript.dump" local data = require"moonscript.data" +local types = require"moonscript.types" -local ntype = data.ntype +local ntype = types.ntype + +local dump = util.dump local trim = util.trim local Stack = data.Stack diff --git a/moonscript/compile/types.lua b/moonscript/types.lua similarity index 92% rename from moonscript/compile/types.lua rename to moonscript/types.lua index aaefc54..1cfe58b 100644 --- a/moonscript/compile/types.lua +++ b/moonscript/types.lua @@ -1,7 +1,13 @@ -module("moonscript.compile", package.seeall) +module("moonscript.types", package.seeall) local util = require("moonscript.util") local data = require("moonscript.data") -local ntype = data.ntype +ntype = function(node) + if type(node) ~= "table" then + return "value" + else + return node[1] + end +end local t = { } local node_types = { fndef = { diff --git a/moonscript/compile/types.moon b/moonscript/types.moon similarity index 83% rename from moonscript/compile/types.moon rename to moonscript/types.moon index 6d0ce52..84df9f1 100644 --- a/moonscript/compile/types.moon +++ b/moonscript/types.moon @@ -1,13 +1,15 @@ -module "moonscript.compile", package.seeall - +module "moonscript.types", package.seeall util = require "moonscript.util" data = require "moonscript.data" -import ntype from data -export smart_node, build +export ntype, smart_node, build --- todo: this should be merged into data --- lets us index a node by item name based on it's type +-- type of node as string +ntype = (node) -> + if type(node) != "table" + "value" + else + node[1] t = {} @@ -51,6 +53,7 @@ build = setmetatable {}, { rawget self, name } +-- makes it so node properties can be accessed by name instead of index smart_node = (node) -> index = key_table[ntype node] if not index then return node diff --git a/test.lua b/test.lua index 6bb5088..073a450 100755 --- a/test.lua +++ b/test.lua @@ -1,3 +1,5 @@ +#!/usr/bin/env lua + require "lfs" require "alt_getopt" diff --git a/todo b/todo index 63ce19c..4f75988 100644 --- a/todo +++ b/todo @@ -1,6 +1,11 @@ # TODO -..= append string +if hello else world + -> if hello then hello else world + + +- enhance with documentation +- fix variable names starting with and, not, or, etc.. - varargs that get put in a nested generated function aren't valid anymore: