Add sprites to source code and table.

This commit is contained in:
Danila Fedorin 2018-08-06 21:51:53 -07:00
parent 7fe5cec941
commit 60c320dfea
7 changed files with 82 additions and 7 deletions

View File

@ -0,0 +1,9 @@
sprite dum [
` xx `
` xx `
` xx `
]
fun main(): u0 {
}

View File

@ -33,8 +33,9 @@ module Chalk
@logger.debug("Beginning constant folding") @logger.debug("Beginning constant folding")
folder = Trees::ConstantFolder.new folder = Trees::ConstantFolder.new
trees.map! do |tree| trees.map! do |tree|
next tree unless tree.is_a?(Trees::TreeFunction)
@logger.debug("Constant folding #{tree.name}") @logger.debug("Constant folding #{tree.name}")
tree.apply(folder).as(Trees::TreeFunction) tree.apply(folder)
end end
@logger.debug("Done constant folding") @logger.debug("Done constant folding")
return trees return trees
@ -48,8 +49,16 @@ module Chalk
table = Table.new table = Table.new
@logger.debug("Creating symbol table") @logger.debug("Creating symbol table")
trees.each do |tree| trees.each do |tree|
@logger.debug("Storing #{tree.name} in symbol table") case tree
when Trees::TreeSprite
@logger.debug("Storing sprite #{tree.name} in symbol table")
table.set_sprite tree.name, SpriteEntry.new tree.sprite
when Trees::TreeFunction
@logger.debug("Storing function #{tree.name} in symbol table")
table.set_function tree.name, FunctionEntry.new tree table.set_function tree.name, FunctionEntry.new tree
else
@logger.debug("Unexpected tree type in input.")
end
end end
@logger.debug("Done creating symbol table") @logger.debug("Done creating symbol table")
@ -93,6 +102,11 @@ module Chalk
return code return code
end end
private def create_code(trees : Array(Trees::Tree), table)
functions = trees.select &.is_a?(Trees::TreeFunction)
return create_code(functions.map &.as(Trees::TreeFunction), table)
end
# Runs in the tree `Ui::OutputMode`. The file is # Runs in the tree `Ui::OutputMode`. The file is
# tokenized and parsed, and the result is printed # tokenized and parsed, and the result is printed
# to the standard output. # to the standard output.

View File

@ -7,6 +7,7 @@ module Chalk
Any, Any,
Str, Str,
Id, Id,
SpriteRow,
LitDec, LitDec,
LitBin, LitBin,
LitHex, LitHex,
@ -54,6 +55,8 @@ module Chalk
TokenType::Str.value) TokenType::Str.value)
@lexer.add_pattern("[a-zA-Z_][a-zA-Z_0-9]*", @lexer.add_pattern("[a-zA-Z_][a-zA-Z_0-9]*",
TokenType::Id.value) TokenType::Id.value)
@lexer.add_pattern("`[ x]*`",
TokenType::SpriteRow.value)
@lexer.add_pattern("[0-9]+", @lexer.add_pattern("[0-9]+",
TokenType::LitDec.value) TokenType::LitDec.value)
@lexer.add_pattern("0b[0-1]+", @lexer.add_pattern("0b[0-1]+",

View File

@ -6,6 +6,20 @@ module Chalk
class Parser class Parser
include ParserBuilder include ParserBuilder
private def create_sprite
type(Compiler::TokenType::KwSprite)
.then(type(Compiler::TokenType::Id))
.then(char('['))
.then(many(type(Compiler::TokenType::SpriteRow)))
.then(char(']'))
.transform do |array|
array = array.flatten
name = array[1].string
sprite = Compiler::Sprite.new(array[3..array.size - 2].map &.string)
Trees::TreeSprite.new(name, sprite).as(Trees::Tree)
end
end
# Creates a parser for a type. # Creates a parser for a type.
private def create_type private def create_type
either(type(Compiler::TokenType::KwU0), either(type(Compiler::TokenType::KwU0),
@ -185,14 +199,14 @@ module Chalk
Compiler::TokenType::KwU8 => Compiler::Type::U8, Compiler::TokenType::KwU8 => Compiler::Type::U8,
Compiler::TokenType::KwU12 => Compiler::Type::U12 Compiler::TokenType::KwU12 => Compiler::Type::U12
} }
Trees::TreeFunction.new(name, params, table[type], code) Trees::TreeFunction.new(name, params, table[type], code).as(Trees::Tree)
end end
return func return func
end end
def initialize def initialize
_, block = create_statement_block _, block = create_statement_block
@parser = many(create_func(block, create_type)).as(BasicParser(Array(Trees::TreeFunction))) @parser = many(either(create_func(block, create_type), create_sprite)).as(BasicParser(Array(Trees::Tree)))
end end
# Parses the given tokens into a tree. # Parses the given tokens into a tree.

View File

@ -1,5 +1,5 @@
module Chalk module Chalk
module Table module Compiler
class Sprite class Sprite
def initialize def initialize
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8) @pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
@ -21,6 +21,22 @@ module Chalk
end end
end end
def initialize(tokens)
raise "Invalid sprite" if tokens.size > 15
@pixels = Hash(UInt8, UInt8).new(default_value: 0_u8)
tokens.each_with_index do |token, i|
byte = 0_u8
substring = token[1..token.size - 2]
raise "Invalid sprite" if substring.size > 8
bit = 0b10000000
substring.each_char do |char|
byte |= bit if char == 'x'
bit >>= 1
end
@pixels[i.to_u8] = byte
end
end
def set_pixel(x, y) def set_pixel(x, y)
raise "Invalid x-coordinate" if x > 7 raise "Invalid x-coordinate" if x > 7
raise "Invalid y-coordinate" if y > 15 raise "Invalid y-coordinate" if y > 15

View File

@ -1,3 +1,5 @@
require "./sprite.cr"
module Chalk module Chalk
module Compiler module Compiler
# An entry that represents a function in the symbol table. # An entry that represents a function in the symbol table.
@ -32,6 +34,14 @@ module Chalk
end end
end end
class SpriteEntry
getter sprite : Sprite
getter addr : Int32
def initialize(@sprite, @addr = -1)
end
end
# A symbol table. # A symbol table.
class Table class Table
# Gets the parent of this table. # Gets the parent of this table.
@ -40,6 +50,7 @@ module Chalk
def initialize(@parent : Table? = nil) def initialize(@parent : Table? = nil)
@functions = {} of String => FunctionEntry @functions = {} of String => FunctionEntry
@vars = {} of String => VarEntry @vars = {} of String => VarEntry
@sprites = {} of String => SpriteEntry
end end
macro table_functions(name) macro table_functions(name)
@ -54,10 +65,10 @@ module Chalk
table_functions function table_functions function
table_functions var table_functions var
table_functions sprite
def to_s(io) def to_s(io)
@parent.try &.to_s(io) @parent.try &.to_s(io)
io << @data.map { |k, v| k + ": " + v.to_s }.join("\n")
end end
end end
end end

View File

@ -296,5 +296,13 @@ module Chalk
r.reduce(self, [@rvalue.reduce(r)]) r.reduce(self, [@rvalue.reduce(r)])
end end
end end
class TreeSprite < Tree
property name : String
property sprite : Compiler::Sprite
def initialize(@name, @sprite)
end
end
end end
end end