Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 568e93e94da3
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fbb265075b37
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 13, 2013

  1. Copy the full SHA
    e453796 View commit details
  2. Cleanup Compiler

    adambeynon committed Nov 13, 2013
    Copy the full SHA
    fbb2650 View commit details
Showing with 21 additions and 24 deletions.
  1. +20 −24 lib/opal/compiler.rb
  2. +1 −0 lib/opal/nodes/top.rb
44 changes: 20 additions & 24 deletions lib/opal/compiler.rb
Original file line number Diff line number Diff line change
@@ -13,10 +13,8 @@ class Compiler
COMPARE = %w[< > <= >=]

# defines a compiler option, also creating method of form 'name?'
def self.compiler_option(name, default_value)
mid = [true, false].include?(default_value) ? "#{name}?" : name

define_method(mid) do
def self.compiler_option(name, default_value, mid = nil)
define_method(mid || name) do
@options.fetch(name) { default_value }
end
end
@@ -25,30 +23,24 @@ def self.compiler_option(name, default_value)
compiler_option :file, '(file)'

# adds method stubs for all used methods in file
compiler_option :method_missing, true
compiler_option :method_missing, true, :method_missing?

# adds an arity check to every method definition
compiler_option :arity_check, false
compiler_option :arity_check, false, :arity_check?

# checks every constant access, delagating to const_missing if needed
compiler_option :const_missing, false
compiler_option :const_missing, false, :const_missing?

# compile top level local vars with support for irb style vars
compiler_option :irb, false
compiler_option :irb, false, :irb?

# how to handle dynamic requires (:error, :warning, :ignore)
compiler_option :dynamic_require_severity, :error

attr_reader :result, :fragments

# Current scope
attr_reader :scope

# Any helpers required by this file
attr_reader :helpers

# Method calls made in this file
attr_reader :method_calls
attr_accessor :scope

# Current case_stmt
attr_reader :case_stmt
@@ -58,20 +50,16 @@ def initialize
@indent = ''
@unique = 0
@options = {}

@method_calls = Set.new
@helpers = Set.new([:breaker, :slice])
end

# Compile some ruby code to a string.
def compile(source, options = {})
@source = source
@options.update options

@sexp = Parser.new.parse(@source, self.file) || s(:nil)
@sexp = s(:top, Parser.new.parse(@source, self.file) || s(:nil))

top_node = Nodes::TopNode.new(s(:top, @sexp), :expr, self)
@fragments = top_node.compile_to_fragments.flatten
@fragments = process(@sexp).flatten

@result = @fragments.map(&:code).join('')
end
@@ -80,6 +68,16 @@ def source_map(source_file = nil)
Opal::SourceMap.new(@fragments, source_file || self.file)
end

# Any helpers required by this file
def helpers
@helpers ||= Set.new([:breaker, :slice])
end

# Method calls made in this file
def method_calls
@method_calls ||= Set.new
end

# This is called when a parsing/processing error occurs. This
# method simply appends the filename and curent line number onto
# the message and raises it.
@@ -122,11 +120,9 @@ def unique_temp

# Use the given helper
def helper(name)
@helpers << name
self.helpers << name
end

attr_accessor :scope

# To keep code blocks nicely indented, this will yield a block after
# adding an extra layer of indent, and then returning the resulting
# code after reverting the indent.
1 change: 1 addition & 0 deletions lib/opal/nodes/top.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ module Opal
module Nodes
# Generates code for an entire file, i.e. the base sexp
class TopNode < BaseScopeNode
handle :top

children :body