From d10c0f0ee97be2ff3463129d50f73ce4ea0aac53 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Wed, 20 Apr 2016 02:23:46 -0700 Subject: [PATCH] use Base class pattern; extend off existing classes --- lips/Base.lua | 17 +++++++++++++++++ lips/Dumper.lua | 3 ++- lips/Lexer.lua | 3 ++- lips/Muncher.lua | 10 ++++++---- lips/Parser.lua | 3 +-- lips/Preproc.lua | 3 +-- lips/Token.lua | 4 ++-- lips/overrides.lua | 1 - lips/util.lua | 16 ---------------- 9 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 lips/Base.lua diff --git a/lips/Base.lua b/lips/Base.lua new file mode 100644 index 0000000..83acf74 --- /dev/null +++ b/lips/Base.lua @@ -0,0 +1,17 @@ +-- mostly just semantics over knife.base +-- https://github.com/airstruck/knife/blob/master/knife/base.lua +return { + extend = function(self, subtype) + subtype = subtype or {} + local meta = { __index = subtype } + return setmetatable(subtype, { + __index = self, + __call = function(self, ...) + local obj = setmetatable({}, meta) + return obj, obj:init(...) + end + }) + end, + + init = function() end, +} diff --git a/lips/Dumper.lua b/lips/Dumper.lua index 23a36da..5a41c45 100644 --- a/lips/Dumper.lua +++ b/lips/Dumper.lua @@ -5,10 +5,11 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") local util = require(path.."util") +local Base = require(path.."Base") local bitrange = util.bitrange -local Dumper = util.Class() +local Dumper = Base:extend() function Dumper:init(writer, fn, options) self.writer = writer self.fn = fn or '(string)' diff --git a/lips/Lexer.lua b/lips/Lexer.lua index 9d9f052..6c0ac71 100644 --- a/lips/Lexer.lua +++ b/lips/Lexer.lua @@ -7,6 +7,7 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") local util = require(path.."util") +local Base = require(path.."Base") local simple_escapes = { ['0'] = 0x00, @@ -21,7 +22,7 @@ local simple_escapes = { ['v'] = 0x0B, } -local Lexer = util.Class() +local Lexer = Base:extend() function Lexer:init(asm, fn, options) self.asm = asm self.fn = fn or '(string)' diff --git a/lips/Muncher.lua b/lips/Muncher.lua index b46753f..235fee0 100644 --- a/lips/Muncher.lua +++ b/lips/Muncher.lua @@ -3,7 +3,7 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") -local util = require(path.."util") +local Base = require(path.."Base") local Token = require(path.."Token") local arg_types = { @@ -14,7 +14,7 @@ local arg_types = { RELLABELSYM = true, } -local Muncher = util.Class() +local Muncher = Base:extend() -- no base init method function Muncher:error(msg) @@ -26,9 +26,11 @@ function Muncher:token(t, val) if type(t) == 'table' then t.fn = self.fn t.line = self.line - return Token(t) + local token = Token(t) + return token else - return Token(self.fn, self.line, t, val) + local token = Token(self.fn, self.line, t, val) + return token end end diff --git a/lips/Parser.lua b/lips/Parser.lua index b9ee4ee..bb2ce09 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -2,7 +2,6 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") -local util = require(path.."util") local overrides = require(path.."overrides") local Token = require(path.."Token") local Lexer = require(path.."Lexer") @@ -10,7 +9,7 @@ local Dumper = require(path.."Dumper") local Muncher = require(path.."Muncher") local Preproc = require(path.."Preproc") -local Parser = util.Class(Muncher) +local Parser = Muncher:extend() function Parser:init(writer, fn, options) self.fn = fn or '(string)' self.main_fn = self.fn diff --git a/lips/Preproc.lua b/lips/Preproc.lua index 3027cd1..4baef26 100644 --- a/lips/Preproc.lua +++ b/lips/Preproc.lua @@ -2,7 +2,6 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") -local util = require(path.."util") local Muncher = require(path.."Muncher") local Token = require(path.."Token") @@ -27,7 +26,7 @@ local function RelativeLabel(index, name) } end -local Preproc = util.Class(Muncher) +local Preproc = Muncher:extend() function Preproc:init(options) self.options = options or {} end diff --git a/lips/Token.lua b/lips/Token.lua index 5054de0..ce83e9b 100644 --- a/lips/Token.lua +++ b/lips/Token.lua @@ -1,7 +1,7 @@ local path = string.gsub(..., "[^.]+$", "") -local util = require(path.."util") +local Base = require(path.."Base") -local Token = util.Class() +local Token = Base:extend() function Token:init(...) local args = {...} if #args == 1 then diff --git a/lips/overrides.lua b/lips/overrides.lua index fa59930..f02b0eb 100644 --- a/lips/overrides.lua +++ b/lips/overrides.lua @@ -2,7 +2,6 @@ local insert = table.insert local path = string.gsub(..., "[^.]+$", "") local data = require(path.."data") -local util = require(path.."util") local instructions = data.instructions diff --git a/lips/util.lua b/lips/util.lua index fabd7e9..717fa2d 100644 --- a/lips/util.lua +++ b/lips/util.lua @@ -1,21 +1,6 @@ local floor = math.floor local open = io.open -local function Class(inherit) - local class = {} - local mt_obj = {__index = class} - local mt_class = { - __call = function(self, ...) - local obj = setmetatable({}, mt_obj) - obj:init(...) - return obj - end, - __index = inherit, - } - - return setmetatable(class, mt_class) -end - local function readfile(fn, binary) local mode = binary and 'rb' or 'r' local f = open(fn, mode) @@ -33,7 +18,6 @@ local function bitrange(x, lower, upper) end return { - Class = Class, readfile = readfile, bitrange = bitrange, }