move to typed structure
on the way to removing the language, the at needs to be replaced with a typed structure.
This commit is contained in:
5
lib/soml/code/assignment.rb
Normal file
5
lib/soml/code/assignment.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class Assignment < Statement
|
||||
attr_accessor :name , :value
|
||||
end
|
||||
end
|
38
lib/soml/code/basic_values.rb
Normal file
38
lib/soml/code/basic_values.rb
Normal file
@ -0,0 +1,38 @@
|
||||
module Soml
|
||||
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
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
class ClassExpression < Expression
|
||||
attr_accessor :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
end
|
5
lib/soml/code/call_site.rb
Normal file
5
lib/soml/code/call_site.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class CallSite < Expression
|
||||
attr_accessor :name , :receiver , :arguments
|
||||
end
|
||||
end
|
25
lib/soml/code/code.rb
Normal file
25
lib/soml/code/code.rb
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
# Base class for Expresssion and Statement
|
||||
module Soml
|
||||
|
||||
class Code
|
||||
|
||||
end
|
||||
|
||||
class Statement < Code
|
||||
end
|
||||
class Expression < Code
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
require_relative "while_statement"
|
||||
require_relative "if_statement"
|
||||
require_relative "return_statement"
|
||||
require_relative "statements"
|
||||
require_relative "operator_expression"
|
||||
require_relative "field_access"
|
||||
require_relative "call_site"
|
||||
require_relative "basic_values"
|
||||
require_relative "assignment"
|
||||
require_relative "to_code"
|
5
lib/soml/code/field_access.rb
Normal file
5
lib/soml/code/field_access.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class FieldAccess < Expression
|
||||
attr_accessor :receiver , :field
|
||||
end
|
||||
end
|
5
lib/soml/code/if_statement.rb
Normal file
5
lib/soml/code/if_statement.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class IfStatement < Statement
|
||||
attr_accessor :branch_type , :condition , :if_true , :if_false
|
||||
end
|
||||
end
|
5
lib/soml/code/operator_expression.rb
Normal file
5
lib/soml/code/operator_expression.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class OperatorExpression < Expression
|
||||
attr_accessor :operator , :left_expression , :right_expression
|
||||
end
|
||||
end
|
5
lib/soml/code/return_statement.rb
Normal file
5
lib/soml/code/return_statement.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class ReturnStatement < Statement
|
||||
attr_accessor :return_value
|
||||
end
|
||||
end
|
5
lib/soml/code/statements.rb
Normal file
5
lib/soml/code/statements.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class Statements < Statement
|
||||
attr_accessor :statements
|
||||
end
|
||||
end
|
108
lib/soml/code/to_code.rb
Normal file
108
lib/soml/code/to_code.rb
Normal file
@ -0,0 +1,108 @@
|
||||
module Soml
|
||||
|
||||
def self.ast_to_code statement
|
||||
compiler = ToCode.new
|
||||
compiler.process statement
|
||||
end
|
||||
|
||||
class ToCode < AST::Processor
|
||||
|
||||
def on_while_statement statement
|
||||
#puts statement.inspect
|
||||
branch_type , condition , statements = *statement
|
||||
w = WhileStatement.new()
|
||||
w.branch_type = branch_type
|
||||
w.condition = process(condition) #.first
|
||||
w.statements = process_all(statements)
|
||||
w
|
||||
end
|
||||
|
||||
def on_if_statement statement
|
||||
branch_type , condition , if_true , if_false = *statement
|
||||
w = IfStatement.new()
|
||||
w.branch_type = branch_type
|
||||
w.condition = process(condition) #.first
|
||||
w.if_true = process_all(if_true)
|
||||
w.if_false = process_all(if_false)
|
||||
w
|
||||
end
|
||||
|
||||
def on_statements statement
|
||||
w = Statements.new()
|
||||
w.statements = process_all(statement.children)
|
||||
w
|
||||
end
|
||||
|
||||
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 = CallSite.new()
|
||||
w.name = name_s
|
||||
w.arguments = process(arguments)
|
||||
w.receiver = process(receiver)
|
||||
w
|
||||
end
|
||||
|
||||
def on_int expression
|
||||
IntegerExpression.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_true expression
|
||||
TrueExpression.new
|
||||
end
|
||||
|
||||
def on_false expression
|
||||
FalseExpression.new
|
||||
end
|
||||
|
||||
def on_nil expression
|
||||
NilExpression.new
|
||||
end
|
||||
|
||||
def on_name statement
|
||||
NameExpression.new(statement.children.first)
|
||||
end
|
||||
def on_string expression
|
||||
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
|
5
lib/soml/code/while_statement.rb
Normal file
5
lib/soml/code/while_statement.rb
Normal file
@ -0,0 +1,5 @@
|
||||
module Soml
|
||||
class WhileStatement < Statement
|
||||
attr_accessor :branch_type , :condition , :statements
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user