Skip to content

Commit

Permalink
Explicitly add compiler options to compiler and cli runner
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 24, 2013
1 parent 3d13b52 commit edddc0f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
17 changes: 11 additions & 6 deletions bin/opal
Expand Up @@ -5,10 +5,15 @@ options = Opal::CLIOptions.new
options.parse!

require 'opal/cli'
cli = Opal::CLI.new options.options.merge(:filename => ARGV.first)
begin
cli.run
rescue Opal::CLI::MissingNode => e
$stderr.puts e.message
exit 127

if ARGV.empty?
p options
else
cli = Opal::CLI.new options.options.merge(:filename => ARGV.first)
begin
cli.run
rescue Opal::CLI::MissingNode => e
$stderr.puts e.message
exit 127
end
end
24 changes: 12 additions & 12 deletions lib/opal/cli_options.rb
Expand Up @@ -60,20 +60,16 @@ def initialize
opts.separator ''
opts.separator 'Compilation Options:'

opts.on('-M', '--no-method-missing', 'Disable method missing') do |value|
options[:method_missing] = false
opts.on('-M', '--[no-]method-missing', 'Disable method missing') do |val|
options[:method_missing] = val
end

opts.on('-O', '--no-optimized-operators', 'Disable optimized operators') do |value|
options[:optimized_operators_enabled] = false
opts.on('-A', '--[no-]arity-check', 'Enable arity check') do |value|
options[:arity_check] = value
end

opts.on('-A', '--arity-check', 'Enable arity check') do |value|
options[:arity_check] = true
end

opts.on('-C', '--no-const-missing', 'Disable const missing') do |value|
options[:const_missing] = false
opts.on('-C', '--[no-]const-missing', 'Disable const missing') do |value|
options[:const_missing] = value
end

dynamic_require_levels = %w[error warning ignore]
Expand All @@ -86,8 +82,12 @@ def initialize
options[:source_map_enabled] = false
end

opts.on("--irb", "IRB var mode") do |i|
options[:irb] = true
opts.on('-F', '--file FILE', 'Set filename for compiled code') do |file|
options[:file] = file
end

opts.on("--[no-]irb", "IRB var mode") do |flag|
options[:irb] = flag
end
end
end
Expand Down
61 changes: 30 additions & 31 deletions lib/opal/compiler.rb
Expand Up @@ -15,6 +15,33 @@ class Compiler
# math comparisons
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
@options.fetch(name) { default_value }
end
end

# used for __FILE__ directives as well as finding relative require()
compiler_option :file, '(file)'

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

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

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

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

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

attr_reader :result, :fragments

# Current scope
Expand All @@ -33,6 +60,7 @@ def initialize
@line = 1
@indent = ''
@unique = 0
@options = {}

@method_calls = Set.new
@helpers = Set.new([:breaker, :slice])
Expand All @@ -41,45 +69,16 @@ def initialize
# Compile some ruby code to a string.
def compile(source, options = {})
@source = source
setup_options options
@options.update options

@sexp = Grammar.new.parse(@source, @file)
@sexp = Grammar.new.parse(@source, self.file)

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

@result = @fragments.map(&:code).join('')
end

def setup_options(options = {})
@file = (options[:file] || '(file)')
@source_file = (options[:source_file] || @file)
@method_missing = (options[:method_missing] != false)
@arity_check = (options[:arity_check])
@const_missing = (options[:const_missing] == true)
@irb_vars = (options[:irb] == true)
@dynamic_require_severity = (options[:dynamic_require_severity] || :error)
end

# Is method_missing enabled for this file
def method_missing?
@method_missing
end

# const_missing enabled or not
def const_missing?
@const_missing
end

def arity_check?
@arity_check
end

# Are top level irb style vars enabled
def irb_vars?
@irb_vars
end

def source_map
Opal::SourceMap.new(@fragments, '(file)')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/call.rb
Expand Up @@ -88,7 +88,7 @@ def recv_sexp
end

def using_irb?
@compiler.irb_vars? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
@compiler.irb? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/top.rb
Expand Up @@ -37,7 +37,7 @@ def stmts
end

def compile_irb_vars
if compiler.irb_vars?
if compiler.irb?
line "if (!$opal.irb_vars) { $opal.irb_vars = {}; }"
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/opal/nodes/variables.rb
Expand Up @@ -8,7 +8,7 @@ class LocalVariableNode < Base
children :var_name

def using_irb?
compiler.irb_vars? and scope.top?
compiler.irb? and scope.top?
end

def compile
Expand All @@ -27,7 +27,7 @@ class LocalAssignNode < Base
children :var_name, :value

def using_irb?
compiler.irb_vars? and scope.top?
compiler.irb? and scope.top?
end

def compile
Expand Down

0 comments on commit edddc0f

Please sign in to comment.