2018-08-06 21:51:53 -07:00
|
|
|
require "./sprite.cr"
|
|
|
|
|
2018-07-26 19:47:56 -07:00
|
|
|
module Chalk
|
2018-08-01 22:40:41 -07:00
|
|
|
module Compiler
|
2018-08-02 01:09:48 -07:00
|
|
|
# An entry that represents a function in the symbol table.
|
2018-08-05 20:32:10 -07:00
|
|
|
class FunctionEntry
|
2018-08-02 01:09:48 -07:00
|
|
|
# Gets the function stored in this entry.
|
|
|
|
getter function
|
|
|
|
# Gets the address in code of this function.
|
|
|
|
getter addr
|
|
|
|
# Sets the address in code of this function.
|
|
|
|
setter addr
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-02 01:09:48 -07:00
|
|
|
def initialize(@function : Trees::TreeFunction | Builtin::BuiltinFunction | Builtin::InlineFunction,
|
|
|
|
@addr = -1)
|
2018-08-01 22:40:41 -07:00
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-01 22:40:41 -07:00
|
|
|
def to_s(io)
|
|
|
|
io << "[function]"
|
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
end
|
|
|
|
|
2018-08-02 01:09:48 -07:00
|
|
|
# An entry that represents a variable in the symbol table.
|
2018-08-05 20:32:10 -07:00
|
|
|
class VarEntry
|
2018-08-02 01:09:48 -07:00
|
|
|
# Gets the register occupied by the variable
|
|
|
|
# in this entry.
|
|
|
|
getter register
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-02 01:09:48 -07:00
|
|
|
def initialize(@register : Int32)
|
2018-08-01 22:40:41 -07:00
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-01 22:40:41 -07:00
|
|
|
def to_s(io)
|
|
|
|
io << "[variable] " << "(R" << @register.to_s(16) << ")"
|
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
end
|
|
|
|
|
2018-08-06 21:51:53 -07:00
|
|
|
class SpriteEntry
|
2018-08-06 23:43:46 -07:00
|
|
|
property sprite : Sprite
|
2018-08-08 21:47:47 -07:00
|
|
|
property addr : Int32
|
2018-08-06 21:51:53 -07:00
|
|
|
|
2018-08-08 21:47:47 -07:00
|
|
|
def initialize(@sprite, @addr = -1)
|
2018-08-06 21:51:53 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-02 01:09:48 -07:00
|
|
|
# A symbol table.
|
2018-08-01 22:40:41 -07:00
|
|
|
class Table
|
2018-08-02 01:09:48 -07:00
|
|
|
# Gets the parent of this table.
|
|
|
|
getter parent
|
2018-08-06 23:43:46 -07:00
|
|
|
# Gets the functions hash.
|
|
|
|
getter functions
|
|
|
|
# Gets the variables hash.
|
|
|
|
getter vars
|
|
|
|
# Gets the sprites hash.
|
|
|
|
getter sprites
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-02 01:09:48 -07:00
|
|
|
def initialize(@parent : Table? = nil)
|
2018-08-05 20:32:10 -07:00
|
|
|
@functions = {} of String => FunctionEntry
|
|
|
|
@vars = {} of String => VarEntry
|
2018-08-06 21:51:53 -07:00
|
|
|
@sprites = {} of String => SpriteEntry
|
2018-08-01 22:40:41 -07:00
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-05 20:32:10 -07:00
|
|
|
macro table_functions(name)
|
|
|
|
def get_{{name}}?(key)
|
|
|
|
@{{name}}s[key]? || @parent.try &.get_{{name}}?(key)
|
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-05 20:32:10 -07:00
|
|
|
def set_{{name}}(key, value)
|
|
|
|
@{{name}}s[key] = value
|
|
|
|
end
|
2018-08-01 22:40:41 -07:00
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
|
2018-08-05 20:32:10 -07:00
|
|
|
table_functions function
|
|
|
|
table_functions var
|
2018-08-06 21:51:53 -07:00
|
|
|
table_functions sprite
|
2018-08-05 20:32:10 -07:00
|
|
|
|
2018-08-01 22:40:41 -07:00
|
|
|
def to_s(io)
|
|
|
|
@parent.try &.to_s(io)
|
|
|
|
end
|
2018-07-26 19:47:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|