reduce method return type to type and introduce a single instruction for instance get
This commit is contained in:
parent
b1893482ff
commit
b59a9da34e
@ -7,7 +7,7 @@ GIT
|
||||
|
||||
GIT
|
||||
remote: https://github.com/crystal-vm/crystal-reader.git
|
||||
revision: bc8697c733113b19863df632b546c7eab2b429c0
|
||||
revision: 0c88e7eff569a6c1b0f354418daacb9ad2ac621a
|
||||
specs:
|
||||
crystal-reader (0.1.0)
|
||||
|
||||
|
@ -63,7 +63,18 @@ module Ast
|
||||
end
|
||||
end
|
||||
class AssignmentExpression < Expression
|
||||
#attr_reader :left, :right
|
||||
|
||||
def compile frame , method
|
||||
raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression
|
||||
r = right.compile(frame,method)
|
||||
frame.compile_set( method , left.name , r )
|
||||
end
|
||||
end
|
||||
|
||||
class VariableExpression < NameExpression
|
||||
def compile frame ,method
|
||||
Virtual::ObjectGet.new(name)
|
||||
end
|
||||
end
|
||||
end
|
@ -53,12 +53,4 @@ module Ast
|
||||
end
|
||||
end
|
||||
|
||||
class VariableExpression < CallSiteExpression
|
||||
# super( :_get_instance_variable , [StringExpression.new(name)] )
|
||||
def make_setter
|
||||
@name = :_set_instance_variable
|
||||
@args << StringExpression.new("value")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -2,7 +2,9 @@ module Ast
|
||||
class IfExpression < Expression
|
||||
# attr_reader :cond, :if_true, :if_false
|
||||
def compile frame , method
|
||||
return nil
|
||||
Virtual::Reference
|
||||
end
|
||||
def old
|
||||
f = context.function
|
||||
# to execute the logic as the if states it, the blocks are the other way around
|
||||
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block
|
||||
|
@ -2,7 +2,7 @@ module Ast
|
||||
class OperatorExpression < Expression
|
||||
# attr_reader :operator, :left, :right
|
||||
def compile frame , method
|
||||
nil
|
||||
Virtual::Reference.new
|
||||
end
|
||||
def scratch
|
||||
into = context.function
|
||||
|
@ -2,7 +2,7 @@ module Ast
|
||||
class ReturnExpression < Expression
|
||||
# attr_reader :expression
|
||||
def compile frame ,method
|
||||
nil
|
||||
Virtual::Reference.new
|
||||
end
|
||||
def sc
|
||||
into = context.function
|
||||
|
@ -2,7 +2,7 @@ module Ast
|
||||
class WhileExpression < Expression
|
||||
# attr_reader :condition, :body
|
||||
def compile frame , method
|
||||
nil
|
||||
Virtual::Reference.new
|
||||
end
|
||||
def old
|
||||
into = context.function
|
||||
|
@ -19,6 +19,9 @@ module Virtual
|
||||
def inspect
|
||||
self.class.name + ".new(#{@integer})"
|
||||
end
|
||||
def type
|
||||
Virtual::Integer
|
||||
end
|
||||
end
|
||||
|
||||
# The name really says it all.
|
||||
|
@ -35,5 +35,9 @@ module Virtual
|
||||
def compile_send method , name , with = []
|
||||
method.add FrameSend.new(name , with )
|
||||
end
|
||||
|
||||
def compile_set method , name , val
|
||||
method.add FrameSet.new(name , val )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -39,6 +39,10 @@ module Virtual
|
||||
def initialize name
|
||||
@name = name
|
||||
end
|
||||
attr_reader :name
|
||||
def attributes
|
||||
[:name]
|
||||
end
|
||||
end
|
||||
|
||||
class FrameSend < Instruction
|
||||
@ -48,13 +52,29 @@ module Virtual
|
||||
@args = args
|
||||
end
|
||||
attr_reader :name , :args
|
||||
|
||||
def == other
|
||||
self.class == other.class && self.name == other.name
|
||||
def attributes
|
||||
[:name , :args]
|
||||
end
|
||||
def inspect
|
||||
self.class.name + ".new(:#{@name} , [ " + args.collect{|a| a.inspect}.join(",")+ "])"
|
||||
end
|
||||
class FrameSet < Instruction
|
||||
|
||||
def initialize name , val
|
||||
@name = name.to_sym
|
||||
@value = val
|
||||
end
|
||||
attr_reader :name , :value
|
||||
def attributes
|
||||
[:name , :value]
|
||||
end
|
||||
end
|
||||
|
||||
class ObjectGet < Instruction
|
||||
def initialize name
|
||||
@name = name
|
||||
end
|
||||
attr_reader :name
|
||||
def attributes
|
||||
[:name]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -39,7 +39,7 @@ class TestBasic < MiniTest::Test
|
||||
|
||||
def test_instance_variable
|
||||
@string_input = '@foo_bar '
|
||||
@output = [Virtual::FrameSend.new(:_get_instance_variable , [ Virtual::StringConstant.new('foo_bar')])]
|
||||
@output = [Virtual::ObjectGet.new(:foo_bar)]
|
||||
check
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user