rubyx/lib/bosl/compiler.rb

48 lines
1.8 KiB
Ruby
Raw Normal View History

module Bosl
class Compiler < AST::Processor
def initialize()
end
def handler_missing node
raise "No handler on_#{node.type}(node)"
end
# Compiling is the conversion of the AST into 2 things:
2015-07-03 19:13:03 +02:00
# - code (ie sequences of Instructions inside Blocks) carried by MethodSource
# - an object graph containing all the Methods, their classes and Constants
#
# Some compile methods just add code, some may add structure (ie Blocks) while
# others instantiate Class and Method objects
#
# Everything in ruby is an expression, ie returns a value. So the effect of every compile
# is that a value is put into the ReturnSlot of the current Message.
2015-09-27 13:07:02 +02:00
# The compile method (so every compile method) returns the value that it deposits.
#
# The process uses a visitor pattern (from AST::Processor) to dispatch according to the
# type the expression. So a s(:if xx) will become an on_if(node) call.
# This makes the dispatch extensible, ie Expressions may be added by external code,
# as long as matching compile methods are supplied too.
#
def self.compile expression
compiler = Compiler.new
compiler.process expression
end
end
end
2015-09-27 10:26:53 +02:00
require_relative "ast_helper"
2015-09-27 11:59:26 +02:00
require_relative "compiler/basic_expressions"
require_relative "compiler/callsite_expression"
2015-09-27 11:59:26 +02:00
require_relative "compiler/class_field"
require_relative "compiler/compound_expressions"
2015-09-20 15:30:07 +02:00
require_relative "compiler/expression_list"
require_relative "compiler/field_def"
2015-09-27 10:26:53 +02:00
require_relative "compiler/field_access"
require_relative "compiler/function_expression"
2015-09-20 15:30:07 +02:00
require_relative "compiler/if_expression"
require_relative "compiler/module_expression"
2015-09-20 15:30:07 +02:00
require_relative "compiler/name_expression"
require_relative "compiler/operator_expressions"
require_relative "compiler/return_expression"
require_relative "compiler/while_expression"