Skip to content

Commit

Permalink
Revise OOP pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Crystalflxme committed Apr 14, 2024
1 parent 4e9a6fd commit 87bb997
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
16 changes: 8 additions & 8 deletions src/chain/src/Bindings.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export type TargetsDict = { [string]: { Instance } }

local builtinCalls: CallsDict

local classPrototype = {}
local classMetatable = { __index = classPrototype }
local Bindings = {}
local metatable = { __index = Bindings }
export type Identity = typeof(setmetatable(
{} :: {
_valid: boolean,
_calls: CallsDict,
_targets: TargetsDict,
},
classMetatable
metatable
))

local function reconcileBuiltinCalls()
Expand All @@ -41,7 +41,7 @@ local function constructor(calls: CallsDict, targets: TargetsDict): Identity
end
end

local self = setmetatable({}, classMetatable)
local self = setmetatable({}, metatable)

self._valid = true
self._calls = calls
Expand All @@ -50,19 +50,19 @@ local function constructor(calls: CallsDict, targets: TargetsDict): Identity
return self
end

function classPrototype.GetCall(self: Identity, name: string): Types.CallDefinition?
function Bindings.GetCall(self: Identity, name: string): Types.CallDefinition?
return builtinCalls[name] or self._calls[name]
end

function classPrototype.GetTargets(self: Identity, name: string): { Instance }?
function Bindings.GetTargets(self: Identity, name: string): { Instance }?
return self._targets[name]
end

function classPrototype.IsInvalid(self: Identity): boolean
function Bindings.IsInvalid(self: Identity): boolean
return not self._valid
end

function classPrototype.Destroy(self: Identity)
function Bindings.Destroy(self: Identity)
self._valid = false

table.clear(self._calls)
Expand Down
26 changes: 13 additions & 13 deletions src/chain/src/Interpreter.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ local Types = require(script.Parent:WaitForChild("Types"))

local COMPLEXITY_LIMIT = 800

local classPrototype = {}
local classMetatable = { __index = classPrototype }
local Interpreter = {}
local metatable = { __index = Interpreter }
export type Identity = typeof(setmetatable(
{} :: {
_ast: Types.ASTNode,
Expand All @@ -18,11 +18,11 @@ export type Identity = typeof(setmetatable(

_process: Types.Process,
},
classMetatable
metatable
))

local function constructor(ast: Types.ASTNode, bindings: Bindings.Identity): Identity
local self = setmetatable({}, classMetatable)
local self = setmetatable({}, metatable)

self._ast = ast
self._bindings = bindings
Expand All @@ -42,7 +42,7 @@ local function constructor(ast: Types.ASTNode, bindings: Bindings.Identity): Ide
return self
end

function classPrototype.MemDef(self: Identity, name: string, value: Types.Type)
function Interpreter.MemDef(self: Identity, name: string, value: Types.Type)
if self._mem[name] then
Logger.Warn(`mem contains a "{name}" def already`)
return
Expand All @@ -56,11 +56,11 @@ function classPrototype.MemDef(self: Identity, name: string, value: Types.Type)
self._mem[name] = value
end

function classPrototype.MemRead(self: Identity, name: string): Types.Type?
function Interpreter.MemRead(self: Identity, name: string): Types.Type?
return self._mem[name]
end

function classPrototype.InterpretRootLike(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretRootLike(self: Identity, node: Types.ASTNode): Types.Type?
local children = node.children :: Types.ASTChildren_Root | Types.ASTChildren_Thunk

-- Pairs used to make typechecker happy
Expand All @@ -71,7 +71,7 @@ function classPrototype.InterpretRootLike(self: Identity, node: Types.ASTNode):
return nil
end

function classPrototype.InterpretCall(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretCall(self: Identity, node: Types.ASTNode): Types.Type?
local callName = node.data :: string
local callDef = self._bindings:GetCall(callName)
if not callDef then
Expand Down Expand Up @@ -150,7 +150,7 @@ function classPrototype.InterpretCall(self: Identity, node: Types.ASTNode): Type
})
end

function classPrototype.InterpretBinExp(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretBinExp(self: Identity, node: Types.ASTNode): Types.Type?
local children = node.children :: Types.ASTChildren_BinExp
local op = node.data :: string

Expand All @@ -169,13 +169,13 @@ function classPrototype.InterpretBinExp(self: Identity, node: Types.ASTNode): Ty
return BinExp.Eval(op, left :: BinExp.Value, right :: BinExp.Value)
end

function classPrototype.InterpretThunk(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretThunk(self: Identity, node: Types.ASTNode): Types.Type?
return function()
self:InterpretRootLike(node)
end
end

function classPrototype.InterpretNot(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretNot(self: Identity, node: Types.ASTNode): Types.Type?
local children = node.children :: Types.ASTChildren_Not

local result = self:InterpretASTNode(children)
Expand All @@ -187,7 +187,7 @@ function classPrototype.InterpretNot(self: Identity, node: Types.ASTNode): Types
return not result
end

function classPrototype.InterpretASTNode(self: Identity, node: Types.ASTNode): Types.Type?
function Interpreter.InterpretASTNode(self: Identity, node: Types.ASTNode): Types.Type?
local newCount = self._complexity + 1
self._complexity = newCount
if newCount > COMPLEXITY_LIMIT then
Expand All @@ -209,7 +209,7 @@ function classPrototype.InterpretASTNode(self: Identity, node: Types.ASTNode): T
return node.data
end

function classPrototype.Begin(self: Identity)
function Interpreter.Begin(self: Identity)
if self._bindings:IsInvalid() then
error("interpreter's bindings are invalid on begin", 2)
end
Expand Down
22 changes: 11 additions & 11 deletions src/chain/src/Lexer.luau
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ local WHITESPACE = table.freeze({
["\t"] = true,
})

local classPrototype = {}
local classMetatable = { __index = classPrototype }
local Lexer = {}
local metatable = { __index = Lexer }
export type Identity = typeof(setmetatable(
{} :: {
_source: string,
_len: number,
_pos: number,
},
classMetatable
metatable
))

local function isWhitespace(input: string?): boolean
Expand All @@ -54,7 +54,7 @@ local function new(id: string, data: string?): Types.Lexeme
end

local function constructor(source: string): Identity
local self = setmetatable({}, classMetatable)
local self = setmetatable({}, metatable)

self._source = source
self._len = string.len(source)
Expand All @@ -63,7 +63,7 @@ local function constructor(source: string): Identity
return self
end

function classPrototype.Peek(self: Identity, amount: number): string?
function Lexer.Peek(self: Identity, amount: number): string?
local nextPos = self._pos + amount
if nextPos > self._len then
return nil
Expand All @@ -72,11 +72,11 @@ function classPrototype.Peek(self: Identity, amount: number): string?
return string.sub(self._source, nextPos, nextPos)
end

function classPrototype.Consume(self: Identity)
function Lexer.Consume(self: Identity)
self._pos += 1
end

function classPrototype.ReadString(self: Identity): Types.Lexeme
function Lexer.ReadString(self: Identity): Types.Lexeme
local final = ""

local prev = nil
Expand All @@ -102,7 +102,7 @@ function classPrototype.ReadString(self: Identity): Types.Lexeme
return new(LexemeIds.Str, final)
end

function classPrototype.ReadNumber(self: Identity, negative: boolean): Types.Lexeme
function Lexer.ReadNumber(self: Identity, negative: boolean): Types.Lexeme
local final = if negative then "-" else ""
if negative then
self:Consume()
Expand All @@ -121,7 +121,7 @@ function classPrototype.ReadNumber(self: Identity, negative: boolean): Types.Lex
return new(LexemeIds.Num, final)
end

function classPrototype.ReadIdentifier(self: Identity): Types.Lexeme
function Lexer.ReadIdentifier(self: Identity): Types.Lexeme
local final = ""

while true do
Expand All @@ -137,7 +137,7 @@ function classPrototype.ReadIdentifier(self: Identity): Types.Lexeme
return new(LexemeIds.Id, final)
end

function classPrototype.ReadNext(self: Identity): Types.Lexeme
function Lexer.ReadNext(self: Identity): Types.Lexeme
local char = self:Peek(0)

if char == nil then
Expand Down Expand Up @@ -267,7 +267,7 @@ function classPrototype.ReadNext(self: Identity): Types.Lexeme
return new(LexemeIds.UknChar, char)
end

function classPrototype.Begin(self: Identity): Types.Result<Types.Lexemes>
function Lexer.Begin(self: Identity): Types.Result<Types.Lexemes>
if self._len > CHARACTER_LIMIT then
return {
ok = false,
Expand Down
30 changes: 15 additions & 15 deletions src/chain/src/Parser.luau
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ local RESOLVE_ID = table.freeze({
}),
})

local classPrototype = {}
local classMetatable = { __index = classPrototype }
local Parser = {}
local metatable = { __index = Parser }
export type Identity = typeof(setmetatable(
{} :: {
_lexemes: Types.Lexemes,
_len: number,
_pos: number,
},
classMetatable
metatable
))

local function node(id: string, data: Types.Type?, children: Types.ASTChildren?): Types.ASTNode
Expand All @@ -76,7 +76,7 @@ local function nodeUnexp(lexeme: Types.Lexeme?): Types.ASTNode
end

local function constructor(lexemes: Types.Lexemes): Identity
local self = setmetatable({}, classMetatable)
local self = setmetatable({}, metatable)

self._lexemes = lexemes
self._len = #lexemes
Expand All @@ -85,7 +85,7 @@ local function constructor(lexemes: Types.Lexemes): Identity
return self
end

function classPrototype.Peek(self: Identity, amount: number): string
function Parser.Peek(self: Identity, amount: number): string
local lexeme = self:Look(amount)
if not lexeme then
return LexemeIds.Eof
Expand All @@ -94,7 +94,7 @@ function classPrototype.Peek(self: Identity, amount: number): string
return lexeme.id
end

function classPrototype.Look(self: Identity, amount: number): Types.Lexeme?
function Parser.Look(self: Identity, amount: number): Types.Lexeme?
local nextPos = self._pos + amount
if nextPos > self._len then
return nil
Expand All @@ -103,11 +103,11 @@ function classPrototype.Look(self: Identity, amount: number): Types.Lexeme?
return self._lexemes[nextPos]
end

function classPrototype.Consume(self: Identity)
function Parser.Consume(self: Identity)
self._pos += 1
end

function classPrototype.ParseFactor(self: Identity): Types.ASTNode
function Parser.ParseFactor(self: Identity): Types.ASTNode
local preOpId = self:Peek(0)
if preOpId == LexemeIds.Not then
self:Consume()
Expand Down Expand Up @@ -148,7 +148,7 @@ function classPrototype.ParseFactor(self: Identity): Types.ASTNode
return nodeUnexp(self:Look(0))
end

function classPrototype.ParseBinExp(self: Identity, min: number): Types.ASTNode
function Parser.ParseBinExp(self: Identity, min: number): Types.ASTNode
local result = self:ParseFactor()
local id = self:Peek(0)

Expand All @@ -168,7 +168,7 @@ function classPrototype.ParseBinExp(self: Identity, min: number): Types.ASTNode
return result
end

function classPrototype.ParseExpression(self: Identity): Types.ASTNode
function Parser.ParseExpression(self: Identity): Types.ASTNode
local id = self:Peek(0)
if id == LexemeIds.Eof then
return nodeUnexp(nil) -- We know the lexeme will be nil here, UnexpEof
Expand Down Expand Up @@ -204,7 +204,7 @@ function classPrototype.ParseExpression(self: Identity): Types.ASTNode
return nodeUnexp(lexeme)
end

function classPrototype.ParseCall(self: Identity, expr: boolean, callTarget: Types.Lexeme?): Types.ASTNode
function Parser.ParseCall(self: Identity, expr: boolean, callTarget: Types.Lexeme?): Types.ASTNode
local target = if callTarget then node(ASTNodeIds.Str, callTarget.data) else nil

local callName = self:Look(0) :: Types.Lexeme
Expand Down Expand Up @@ -256,7 +256,7 @@ function classPrototype.ParseCall(self: Identity, expr: boolean, callTarget: Typ
})
end

function classPrototype.ParseTargetedCall(self: Identity, expr: boolean): Types.ASTNode
function Parser.ParseTargetedCall(self: Identity, expr: boolean): Types.ASTNode
local targetName = self:Look(0) :: Types.Lexeme
self:Consume()

Expand All @@ -276,7 +276,7 @@ function classPrototype.ParseTargetedCall(self: Identity, expr: boolean): Types.
return nodeUnexp(self:Look(0))
end

function classPrototype.ParseCallLike(self: Identity, expr: boolean): Types.ASTNode
function Parser.ParseCallLike(self: Identity, expr: boolean): Types.ASTNode
local nextId = self:Peek(1)

if nextId == LexemeIds.Arrow then
Expand All @@ -288,7 +288,7 @@ function classPrototype.ParseCallLike(self: Identity, expr: boolean): Types.ASTN
return nodeUnexp(self:Look(1))
end

function classPrototype.ParseGeneralContext(self: Identity): Types.ASTNode?
function Parser.ParseGeneralContext(self: Identity): Types.ASTNode?
local id = self:Peek(0)

if id == LexemeIds.Eof then
Expand All @@ -300,7 +300,7 @@ function classPrototype.ParseGeneralContext(self: Identity): Types.ASTNode?
return nodeUnexp(self:Look(0))
end

function classPrototype.Begin(self: Identity): Types.Result<Types.ASTNode>
function Parser.Begin(self: Identity): Types.Result<Types.ASTNode>
if self._len > LEXEME_LIMIT then
return {
ok = false,
Expand Down

0 comments on commit 87bb997

Please sign in to comment.