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
|
GIT
|
||||||
remote: https://github.com/crystal-vm/crystal-reader.git
|
remote: https://github.com/crystal-vm/crystal-reader.git
|
||||||
revision: bc8697c733113b19863df632b546c7eab2b429c0
|
revision: 0c88e7eff569a6c1b0f354418daacb9ad2ac621a
|
||||||
specs:
|
specs:
|
||||||
crystal-reader (0.1.0)
|
crystal-reader (0.1.0)
|
||||||
|
|
||||||
|
@ -63,7 +63,18 @@ module Ast
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
class AssignmentExpression < Expression
|
class AssignmentExpression < Expression
|
||||||
|
#attr_reader :left, :right
|
||||||
|
|
||||||
def compile frame , method
|
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
|
end
|
||||||
end
|
end
|
@ -53,12 +53,4 @@ module Ast
|
|||||||
end
|
end
|
||||||
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
|
end
|
@ -2,7 +2,9 @@ module Ast
|
|||||||
class IfExpression < Expression
|
class IfExpression < Expression
|
||||||
# attr_reader :cond, :if_true, :if_false
|
# attr_reader :cond, :if_true, :if_false
|
||||||
def compile frame , method
|
def compile frame , method
|
||||||
return nil
|
Virtual::Reference
|
||||||
|
end
|
||||||
|
def old
|
||||||
f = context.function
|
f = context.function
|
||||||
# to execute the logic as the if states it, the blocks are the other way around
|
# 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
|
# 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
|
class OperatorExpression < Expression
|
||||||
# attr_reader :operator, :left, :right
|
# attr_reader :operator, :left, :right
|
||||||
def compile frame , method
|
def compile frame , method
|
||||||
nil
|
Virtual::Reference.new
|
||||||
end
|
end
|
||||||
def scratch
|
def scratch
|
||||||
into = context.function
|
into = context.function
|
||||||
|
@ -2,7 +2,7 @@ module Ast
|
|||||||
class ReturnExpression < Expression
|
class ReturnExpression < Expression
|
||||||
# attr_reader :expression
|
# attr_reader :expression
|
||||||
def compile frame ,method
|
def compile frame ,method
|
||||||
nil
|
Virtual::Reference.new
|
||||||
end
|
end
|
||||||
def sc
|
def sc
|
||||||
into = context.function
|
into = context.function
|
||||||
|
@ -2,7 +2,7 @@ module Ast
|
|||||||
class WhileExpression < Expression
|
class WhileExpression < Expression
|
||||||
# attr_reader :condition, :body
|
# attr_reader :condition, :body
|
||||||
def compile frame , method
|
def compile frame , method
|
||||||
nil
|
Virtual::Reference.new
|
||||||
end
|
end
|
||||||
def old
|
def old
|
||||||
into = context.function
|
into = context.function
|
||||||
|
@ -19,6 +19,9 @@ module Virtual
|
|||||||
def inspect
|
def inspect
|
||||||
self.class.name + ".new(#{@integer})"
|
self.class.name + ".new(#{@integer})"
|
||||||
end
|
end
|
||||||
|
def type
|
||||||
|
Virtual::Integer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# The name really says it all.
|
# The name really says it all.
|
||||||
|
@ -35,5 +35,9 @@ module Virtual
|
|||||||
def compile_send method , name , with = []
|
def compile_send method , name , with = []
|
||||||
method.add FrameSend.new(name , with )
|
method.add FrameSend.new(name , with )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compile_set method , name , val
|
||||||
|
method.add FrameSet.new(name , val )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,6 +39,10 @@ module Virtual
|
|||||||
def initialize name
|
def initialize name
|
||||||
@name = name
|
@name = name
|
||||||
end
|
end
|
||||||
|
attr_reader :name
|
||||||
|
def attributes
|
||||||
|
[:name]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FrameSend < Instruction
|
class FrameSend < Instruction
|
||||||
@ -48,13 +52,29 @@ module Virtual
|
|||||||
@args = args
|
@args = args
|
||||||
end
|
end
|
||||||
attr_reader :name , :args
|
attr_reader :name , :args
|
||||||
|
def attributes
|
||||||
def == other
|
[:name , :args]
|
||||||
self.class == other.class && self.name == other.name
|
|
||||||
end
|
end
|
||||||
def inspect
|
end
|
||||||
self.class.name + ".new(:#{@name} , [ " + args.collect{|a| a.inspect}.join(",")+ "])"
|
class FrameSet < Instruction
|
||||||
|
|
||||||
|
def initialize name , val
|
||||||
|
@name = name.to_sym
|
||||||
|
@value = val
|
||||||
|
end
|
||||||
|
attr_reader :name , :value
|
||||||
|
def attributes
|
||||||
|
[:name , :value]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ObjectGet < Instruction
|
||||||
|
def initialize name
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
attr_reader :name
|
||||||
|
def attributes
|
||||||
|
[:name]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,7 +39,7 @@ class TestBasic < MiniTest::Test
|
|||||||
|
|
||||||
def test_instance_variable
|
def test_instance_variable
|
||||||
@string_input = '@foo_bar '
|
@string_input = '@foo_bar '
|
||||||
@output = [Virtual::FrameSend.new(:_get_instance_variable , [ Virtual::StringConstant.new('foo_bar')])]
|
@output = [Virtual::ObjectGet.new(:foo_bar)]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user