removing exceptions
This commit is contained in:
parent
c7c4387e16
commit
93bdd8a243
@ -62,4 +62,8 @@ module Ast
|
|||||||
value
|
value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
class AssignmentExpression < Expression
|
||||||
|
def compile frame , method
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
@ -1,7 +1,8 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class IfExpression < Expression
|
class IfExpression < Expression
|
||||||
# attr_reader :cond, :if_true, :if_false
|
# attr_reader :cond, :if_true, :if_false
|
||||||
def compile context
|
def compile frame , method
|
||||||
|
return nil
|
||||||
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
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class OperatorExpression < Expression
|
class OperatorExpression < Expression
|
||||||
# attr_reader :operator, :left, :right
|
# attr_reader :operator, :left, :right
|
||||||
def compile context
|
def compile frame , method
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
def scratch
|
||||||
into = context.function
|
into = context.function
|
||||||
puts "compiling operator #{to_s}"
|
puts "compiling operator #{to_s}"
|
||||||
r_val = right.compile(context)
|
r_val = right.compile(context)
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class ReturnExpression < Expression
|
class ReturnExpression < Expression
|
||||||
# attr_reader :expression
|
# attr_reader :expression
|
||||||
def compile context
|
def compile frame ,method
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
def sc
|
||||||
into = context.function
|
into = context.function
|
||||||
puts "compiling return expression #{expression}, now return in return_regsiter"
|
puts "compiling return expression #{expression}, now return in return_regsiter"
|
||||||
expression_value = expression.compile(context)
|
expression_value = expression.compile(context)
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class WhileExpression < Expression
|
class WhileExpression < Expression
|
||||||
# attr_reader :condition, :body
|
# attr_reader :condition, :body
|
||||||
def compile context
|
def compile frame , method
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
def old
|
||||||
into = context.function
|
into = context.function
|
||||||
ret = into.new_block "while_end"
|
ret = into.new_block "while_end"
|
||||||
while_block = into.new_block "while_start"
|
while_block = into.new_block "while_start"
|
||||||
|
@ -15,6 +15,9 @@ module Virtual
|
|||||||
def attributes
|
def attributes
|
||||||
[:next]
|
[:next]
|
||||||
end
|
end
|
||||||
|
def initialize nex = nil
|
||||||
|
@next = nex
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
||||||
|
@ -15,15 +15,15 @@ module Virtual
|
|||||||
Method.new(:main , [] , Virtual::SelfReference )
|
Method.new(:main , [] , Virtual::SelfReference )
|
||||||
end
|
end
|
||||||
def attributes
|
def attributes
|
||||||
[:name , :args , :receiver , :start , :return_type]
|
[:name , :args , :receiver , :return_type , :start]
|
||||||
end
|
end
|
||||||
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference
|
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference , start = MethodEnter.new
|
||||||
@name = name.to_sym
|
@name = name.to_sym
|
||||||
@args = args
|
@args = args
|
||||||
@locals = []
|
@locals = []
|
||||||
@receiver = receiver
|
@receiver = receiver
|
||||||
@return_type = return_type
|
@return_type = return_type
|
||||||
@start = MethodEnter.new
|
@start = start
|
||||||
@current = @start
|
@current = @start
|
||||||
end
|
end
|
||||||
attr_reader :name , :args , :receiver , :start
|
attr_reader :name , :args , :receiver , :start
|
||||||
|
@ -9,7 +9,7 @@ def foo(x)
|
|||||||
5
|
5
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new())]
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(),Virtual::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -31,6 +31,7 @@ def foo(x)
|
|||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_if
|
def test_function_if
|
||||||
@ -43,9 +44,8 @@ def ofthen(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"}, :if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_return
|
def test_function_return
|
||||||
@ -55,9 +55,8 @@ def retvar(n)
|
|||||||
return i
|
return i
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"i"}, :o=>"= ", :r=>{:integer=>"5"}}, {:return=>"return", :return_expression=>{:name=>"i"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:i),Ast::IntegerExpression.new(5)),Ast::ReturnExpression.new(Ast::NameExpression.new(:i) )] ,nil )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_return_if
|
def test_function_return_if
|
||||||
@ -70,9 +69,8 @@ def retvar(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :if_true=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"10"}}], :else=>"else"}, :if_false=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"20"}}], :end=>"end"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new("n")] , [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(5)), [Ast::ReturnExpression.new(Ast::IntegerExpression.new(10) )],[Ast::ReturnExpression.new(Ast::IntegerExpression.new(20) )] )] )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_return_while
|
def test_function_return_while
|
||||||
@ -84,9 +82,8 @@ def retvar(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(5)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1))), Ast::ReturnExpression.new(Ast::NameExpression.new(:n) )] )] ,nil )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_while
|
def test_function_while
|
||||||
@ -99,9 +96,8 @@ def fibonaccit(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :end=>"end"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::WhileExpression.new(Ast::NameExpression.new(:n), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:some),Ast::IntegerExpression.new(43)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:other),Ast::OperatorExpression.new("*", Ast::NameExpression.new(:some),Ast::IntegerExpression.new(4)))] )] ,nil )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_function_big_while
|
def test_function_big_while
|
||||||
@ -118,8 +114,7 @@ def fibonaccit(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||||
@transform_output = Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )] ,nil )
|
check
|
||||||
@parser = @parser.function_definition
|
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user