add missing as types

This commit is contained in:
Torsten Ruger 2016-03-06 14:07:25 +02:00
parent 4a9b492dd9
commit f51ed376c4
6 changed files with 77 additions and 4 deletions

View File

@ -2,4 +2,9 @@ module Soml
class Assignment < Statement class Assignment < Statement
attr_accessor :name , :value attr_accessor :name , :value
end end
class FieldDef < Statement
attr_accessor :name , :type , :value
end
end end

View File

@ -25,6 +25,7 @@ module Soml
end end
class NameExpression < Expression class NameExpression < Expression
attr_accessor :value attr_accessor :value
alias :name :value
def initialize(value) def initialize(value)
@value = value @value = value
end end

View File

@ -0,0 +1,9 @@
module Soml
class ClassStatement < Statement
attr_accessor :name , :derives , :statements
end
class ClassField < Statement
attr_accessor :name , :type
end
end

View File

@ -22,4 +22,6 @@ require_relative "field_access"
require_relative "call_site" require_relative "call_site"
require_relative "basic_values" require_relative "basic_values"
require_relative "assignment" require_relative "assignment"
require_relative "class_statement"
require_relative "function_statement"
require_relative "to_code" require_relative "to_code"

View File

@ -0,0 +1,5 @@
module Soml
class FunctionStatement < Statement
attr_accessor :return_type , :name , :parameters, :statements , :receiver
end
end

View File

@ -7,12 +7,55 @@ module Soml
class ToCode < AST::Processor 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 = ClassStatement.new()
w.name = name
w.derives = derives.children.first
w.statements = process_all(statements)
w
end
def on_function statement
return_type , name , parameters, statements , receiver = *statement
w = 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_all(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 def on_while_statement statement
#puts statement.inspect
branch_type , condition , statements = *statement branch_type , condition , statements = *statement
w = WhileStatement.new() w = WhileStatement.new()
w.branch_type = branch_type w.branch_type = branch_type
w.condition = process(condition) #.first w.condition = process(condition)
w.statements = process_all(statements) w.statements = process_all(statements)
w w
end end
@ -21,12 +64,20 @@ module Soml
branch_type , condition , if_true , if_false = *statement branch_type , condition , if_true , if_false = *statement
w = IfStatement.new() w = IfStatement.new()
w.branch_type = branch_type w.branch_type = branch_type
w.condition = process(condition) #.first w.condition = process(condition)
w.if_true = process_all(if_true) w.if_true = process_all(if_true)
w.if_false = process_all(if_false) w.if_false = process_all(if_false)
w w
end 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 def on_statements statement
w = Statements.new() w = Statements.new()
w.statements = process_all(statement.children) w.statements = process_all(statement.children)
@ -64,7 +115,7 @@ module Soml
name_s , arguments , receiver = *statement name_s , arguments , receiver = *statement
w = CallSite.new() w = CallSite.new()
w.name = name_s w.name = name_s
w.arguments = process(arguments) w.arguments = process_all(arguments)
w.receiver = process(receiver) w.receiver = process(receiver)
w w
end end