move ast to tree, remove seperate helper file
This commit is contained in:
14
lib/typed/tree/assignment.rb
Normal file
14
lib/typed/tree/assignment.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class Assignment < Statement
|
||||
attr_accessor :name , :value
|
||||
def initialize(n = nil , v = nil )
|
||||
@name , @value = n , v
|
||||
end
|
||||
end
|
||||
|
||||
class FieldDef < Statement
|
||||
attr_accessor :name , :type , :value
|
||||
end
|
||||
end
|
||||
end
|
41
lib/typed/tree/basic_values.rb
Normal file
41
lib/typed/tree/basic_values.rb
Normal file
@ -0,0 +1,41 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class IntegerExpression < Expression
|
||||
attr_accessor :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
class FloatExpression < Expression
|
||||
attr_accessor :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
class TrueExpression < Expression
|
||||
end
|
||||
class FalseExpression < Expression
|
||||
end
|
||||
class NilExpression < Expression
|
||||
end
|
||||
class StringExpression < Expression
|
||||
attr_accessor :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
class NameExpression < Expression
|
||||
attr_accessor :value
|
||||
alias :name :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
class ClassExpression < Expression
|
||||
attr_accessor :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
7
lib/typed/tree/call_site.rb
Normal file
7
lib/typed/tree/call_site.rb
Normal file
@ -0,0 +1,7 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class CallSite < Expression
|
||||
attr_accessor :name , :receiver , :arguments
|
||||
end
|
||||
end
|
||||
end
|
11
lib/typed/tree/class_statement.rb
Normal file
11
lib/typed/tree/class_statement.rb
Normal file
@ -0,0 +1,11 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class ClassStatement < Statement
|
||||
attr_accessor :name , :derives , :statements
|
||||
end
|
||||
|
||||
class ClassField < Statement
|
||||
attr_accessor :name , :type
|
||||
end
|
||||
end
|
||||
end
|
5
lib/typed/tree/field_access.rb
Normal file
5
lib/typed/tree/field_access.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Typed
|
||||
class FieldAccess < Expression
|
||||
attr_accessor :receiver , :field
|
||||
end
|
||||
end
|
7
lib/typed/tree/function_statement.rb
Normal file
7
lib/typed/tree/function_statement.rb
Normal file
@ -0,0 +1,7 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class FunctionStatement < Statement
|
||||
attr_accessor :return_type , :name , :parameters, :statements , :receiver
|
||||
end
|
||||
end
|
||||
end
|
7
lib/typed/tree/if_statement.rb
Normal file
7
lib/typed/tree/if_statement.rb
Normal file
@ -0,0 +1,7 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class IfStatement < Statement
|
||||
attr_accessor :branch_type , :condition , :if_true , :if_false
|
||||
end
|
||||
end
|
||||
end
|
5
lib/typed/tree/operator_expression.rb
Normal file
5
lib/typed/tree/operator_expression.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Typed
|
||||
class OperatorExpression < Expression
|
||||
attr_accessor :operator , :left_expression , :right_expression
|
||||
end
|
||||
end
|
5
lib/typed/tree/return_statement.rb
Normal file
5
lib/typed/tree/return_statement.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Typed
|
||||
class ReturnStatement < Statement
|
||||
attr_accessor :return_value
|
||||
end
|
||||
end
|
5
lib/typed/tree/statements.rb
Normal file
5
lib/typed/tree/statements.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Typed
|
||||
class Statements < Statement
|
||||
attr_accessor :statements
|
||||
end
|
||||
end
|
163
lib/typed/tree/to_code.rb
Normal file
163
lib/typed/tree/to_code.rb
Normal file
@ -0,0 +1,163 @@
|
||||
module Typed
|
||||
|
||||
def self.ast_to_code statement
|
||||
compiler = ToCode.new
|
||||
compiler.process statement
|
||||
end
|
||||
|
||||
class ToCode < AST::Processor
|
||||
|
||||
def handler_missing node
|
||||
raise "No handler on_#{node.type}(node)"
|
||||
end
|
||||
|
||||
def on_class statement
|
||||
name , derives , statements = *statement
|
||||
w = Tree::ClassStatement.new()
|
||||
w.name = name
|
||||
w.derives = derives.children.first
|
||||
w.statements = process(statements)
|
||||
w
|
||||
end
|
||||
|
||||
def on_function statement
|
||||
return_type , name , parameters, statements , receiver = *statement
|
||||
w = Tree::FunctionStatement.new()
|
||||
w.return_type = return_type
|
||||
w.name = name.children.first
|
||||
w.parameters = parameters.to_a.collect do |p|
|
||||
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
||||
p.children
|
||||
end
|
||||
w.statements = process(statements)
|
||||
w.receiver = receiver
|
||||
w
|
||||
end
|
||||
|
||||
def on_field_def statement
|
||||
type , name , value = *statement
|
||||
w = FieldDef.new()
|
||||
w.type = type
|
||||
w.name = process(name)
|
||||
w.value = process(value) if value
|
||||
w
|
||||
end
|
||||
|
||||
def on_class_field statement
|
||||
type , name = *statement
|
||||
w = ClassField.new()
|
||||
w.type = type
|
||||
w.name = name
|
||||
w
|
||||
end
|
||||
|
||||
def on_while_statement statement
|
||||
branch_type , condition , statements = *statement
|
||||
w = WhileStatement.new()
|
||||
w.branch_type = branch_type
|
||||
w.condition = process(condition)
|
||||
w.statements = process(statements)
|
||||
w
|
||||
end
|
||||
|
||||
def on_if_statement statement
|
||||
branch_type , condition , if_true , if_false = *statement
|
||||
w = Tree::IfStatement.new()
|
||||
w.branch_type = branch_type
|
||||
w.condition = process(condition)
|
||||
w.if_true = process(if_true)
|
||||
w.if_false = process(if_false)
|
||||
w
|
||||
end
|
||||
|
||||
def process_first code
|
||||
raise "Too many children #{code.inspect}" if code.children.length != 1
|
||||
process code.children.first
|
||||
end
|
||||
alias :on_conditional :process_first
|
||||
alias :on_condition :process_first
|
||||
alias :on_field :process_first
|
||||
|
||||
def on_statements statement
|
||||
w = Statements.new()
|
||||
return w unless statement.children
|
||||
return w unless statement.children.first
|
||||
w.statements = process_all(statement.children)
|
||||
w
|
||||
end
|
||||
alias :on_true_statements :on_statements
|
||||
alias :on_false_statements :on_statements
|
||||
|
||||
def on_return statement
|
||||
w = ReturnStatement.new()
|
||||
w.return_value = process(statement.children.first)
|
||||
w
|
||||
end
|
||||
|
||||
def on_operator_value statement
|
||||
operator , left_e , right_e = *statement
|
||||
w = OperatorExpression.new()
|
||||
w.operator = operator
|
||||
w.left_expression = process(left_e)
|
||||
w.right_expression = process(right_e)
|
||||
w
|
||||
end
|
||||
|
||||
def on_field_access statement
|
||||
receiver_ast , field_ast = *statement
|
||||
w = FieldAccess.new()
|
||||
w.receiver = process(receiver_ast)
|
||||
w.field = process(field_ast)
|
||||
w
|
||||
end
|
||||
|
||||
def on_receiver expression
|
||||
process expression.children.first
|
||||
end
|
||||
|
||||
def on_call statement
|
||||
name_s , arguments , receiver = *statement
|
||||
w = Tree::CallSite.new()
|
||||
w.name = name_s.children.first
|
||||
w.arguments = process_all(arguments)
|
||||
w.receiver = process(receiver)
|
||||
w
|
||||
end
|
||||
|
||||
def on_int expression
|
||||
Tree::IntegerExpression.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_true expression
|
||||
Tree::TrueExpression.new
|
||||
end
|
||||
|
||||
def on_false expression
|
||||
Tree::FalseExpression.new
|
||||
end
|
||||
|
||||
def on_nil expression
|
||||
Tree::NilExpression.new
|
||||
end
|
||||
|
||||
def on_name statement
|
||||
Tree::NameExpression.new(statement.children.first)
|
||||
end
|
||||
def on_string expression
|
||||
Tree::StringExpression.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_class_name expression
|
||||
ClassExpression.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_assignment statement
|
||||
name , value = *statement
|
||||
w = Assignment.new()
|
||||
w.name = process name
|
||||
w.value = process(value)
|
||||
w
|
||||
end
|
||||
|
||||
end
|
||||
end
|
7
lib/typed/tree/while_statement.rb
Normal file
7
lib/typed/tree/while_statement.rb
Normal file
@ -0,0 +1,7 @@
|
||||
module Typed
|
||||
module Tree
|
||||
class WhileStatement < Statement
|
||||
attr_accessor :branch_type , :condition , :statements
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user