Format code.

This commit is contained in:
Danila Fedorin 2018-07-28 17:53:07 -07:00
parent 3f931c4a65
commit 946db467ee
4 changed files with 109 additions and 110 deletions

View File

@ -1,21 +1,21 @@
module Chalk
class BuiltinFunction
getter param_count : Int32
getter param_count : Int32
def initialize(@param_count)
end
def initialize(@param_count)
end
def generate!(into)
end
def generate!(into)
end
end
class InlineFunction
getter param_count : Int32
def initialize(@param_count)
end
getter param_count : Int32
def generate!(codegen, params, table, target, free)
end
def initialize(@param_count)
end
def generate!(codegen, params, table, target, free)
end
end
end

View File

@ -22,45 +22,45 @@ module Chalk
end
def generate!(tree, function : InlineFunction, table, target, free)
start = free
function.generate!(self, tree.params, table, target, free)
start = free
function.generate!(self, tree.params, table, target, free)
end
def generate!(tree, function : TreeFunction | BuiltinFunction, table, target, free)
start_at = free
# Move I to stack
setis
# Get to correct stack position
addi STACK_REG
# Store variables
store (start_at - 1) unless start_at == 0
# Increment I and stack position
load free, start_at
opr TokenType::OpAdd, STACK_REG, free
addi free
start_at = free
# Move I to stack
setis
# Get to correct stack position
addi STACK_REG
# Store variables
store (start_at - 1) unless start_at == 0
# Increment I and stack position
load free, start_at
opr TokenType::OpAdd, STACK_REG, free
addi free
# Calculate the parameters
tree.params.each do |param|
generate! param, table, free, free + 1
free += 1
end
# Call the function
tree.params.size.times do |time|
loadr time, time + start_at
end
call tree.name
# Calculate the parameters
tree.params.each do |param|
generate! param, table, free, free + 1
free += 1
end
# Call the function
tree.params.size.times do |time|
loadr time, time + start_at
end
call tree.name
# Reduce stack pointer
load free, start_at
opr TokenType::OpSub, STACK_REG, free
# Move I to stack
setis
# Get to correct stack position
addi STACK_REG
# Restore
restore (start_at - 1) unless start_at == 0
# Get call value into target
loadr target, RETURN_REG
# Reduce stack pointer
load free, start_at
opr TokenType::OpSub, STACK_REG, free
# Move I to stack
setis
# Get to correct stack position
addi STACK_REG
# Restore
restore (start_at - 1) unless start_at == 0
# Get call value into target
loadr target, RETURN_REG
end
def generate!(tree, table, target, free)
@ -124,7 +124,7 @@ module Chalk
old_size = @instructions.size
generate! tree.block, table, free, free + 1
after_jump = jr 0
cond_jump.offset = @instructions.size - old_size + 1
after_jump.offset = before_cond - instructions.size + 1
when TreeReturn

View File

@ -1,59 +1,58 @@
module Chalk
class InlineDrawFunction < InlineFunction
def initialize
@param_count = 3
end
def generate!(emitter, params, table, target, free)
if !params[2].is_a?(TreeLit)
raise "Third parameter must be a constant."
end
emitter.generate! params[0], table, free, free + 1
emitter.generate! params[1], table, free + 1, free + 2
emitter.instructions << DrawInstruction.new free, free + 1, params[2].as(TreeLit).lit.to_i32
end
class InlineDrawFunction < InlineFunction
def initialize
@param_count = 3
end
class InlineAwaitKeyFunction < InlineFunction
def initialize
@param_count = 0
end
def generate!(emitter, params, table, target, free)
if !params[2].is_a?(TreeLit)
raise "Third parameter must be a constant."
end
emitter.generate! params[0], table, free, free + 1
emitter.generate! params[1], table, free + 1, free + 2
emitter.instructions << DrawInstruction.new free, free + 1, params[2].as(TreeLit).lit.to_i32
end
end
def generate!(emitter, params, table, target, free)
emitter.instructions << AwaitKeyInstruction.new target
end
class InlineAwaitKeyFunction < InlineFunction
def initialize
@param_count = 0
end
class InlineGetFontFunction < InlineFunction
def initialize
@param_count = 1
end
def generate!(emitter, params, table, target, free)
emitter.instructions << AwaitKeyInstruction.new target
end
end
def generate!(emitter, params, table, target, free)
emitter.generate! params[0], table, free, free + 1
emitter.instructions << GetFontInstruction.new free
end
class InlineGetFontFunction < InlineFunction
def initialize
@param_count = 1
end
class InlineSetDelayFunction < InlineFunction
def initialize
@param_count = 1
end
def generate!(emitter, params, table, target, free)
emitter.generate! params[0], table, free, free + 1
emitter.instructions << GetFontInstruction.new free
end
end
def generate!(emitter, params, table, target, free)
emitter.generate! params[0], table, free, free + 1
emitter.instructions << SetDelayTimerInstruction.new free
end
class InlineSetDelayFunction < InlineFunction
def initialize
@param_count = 1
end
class InlineGetDelayFunction < InlineFunction
def initialize
@param_count = 0
end
def generate!(emitter, params, table, target, free)
emitter.instructions << GetDelayTimerInstruction.new target
end
def generate!(emitter, params, table, target, free)
emitter.generate! params[0], table, free, free + 1
emitter.instructions << SetDelayTimerInstruction.new free
end
end
class InlineGetDelayFunction < InlineFunction
def initialize
@param_count = 0
end
def generate!(emitter, params, table, target, free)
emitter.instructions << GetDelayTimerInstruction.new target
end
end
end

View File

@ -295,15 +295,15 @@ module Chalk
end
def to_s(io)
io << "draw R"
x.to_s(16, io)
io << " R"
y.to_s(16, io)
io << " " << height
io << "draw R"
x.to_s(16, io)
io << " R"
y.to_s(16, io)
io << " " << height
end
def to_bin(i, index)
return 0xd000 | (@x << 8) | (@y << 4) | height
return 0xd000 | (@x << 8) | (@y << 4) | height
end
end
@ -314,28 +314,28 @@ module Chalk
end
def to_s(io)
io << "getk R"
@into.to_s(16, io)
io << "getk R"
@into.to_s(16, io)
end
def to_bin(i, index)
return 0xf00a | (@into << 8)
return 0xf00a | (@into << 8)
end
end
class GetFontInstruction < Instruction
property from : Int32
def initialize(@from)
end
def to_s(io)
io << "font R"
@from.to_s(16, io)
io << "font R"
@from.to_s(16, io)
end
def to_bin(i, index)
return 0xf029 | (@from << 8)
return 0xf029 | (@from << 8)
end
end
@ -346,12 +346,12 @@ module Chalk
end
def to_s(io)
io << "set_delay R"
@from.to_s(16, io)
io << "set_delay R"
@from.to_s(16, io)
end
def to_bin(i, index)
return 0xf015 | (@from << 8)
return 0xf015 | (@from << 8)
end
end
@ -362,12 +362,12 @@ module Chalk
end
def to_s(io)
io << "get_delay R"
@into.to_s(16, io)
io << "get_delay R"
@into.to_s(16, io)
end
def to_bin(i, index)
return 0xf007 | (@into << 8)
return 0xf007 | (@into << 8)
end
end
end