1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-11-14 10:09:03 -08:00

use Base class pattern; extend off existing classes

This commit is contained in:
Connor Olding 2016-04-20 02:23:46 -07:00
parent e593ea0c68
commit d10c0f0ee9
9 changed files with 31 additions and 29 deletions

17
lips/Base.lua Normal file
View file

@ -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,
}

View file

@ -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)'

View file

@ -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)'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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,
}