Class: Ruby2JS::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2js/converter.rb,
lib/ruby2js/converter/if.rb,
lib/ruby2js/converter/arg.rb,
lib/ruby2js/converter/def.rb,
lib/ruby2js/converter/for.rb,
lib/ruby2js/converter/nil.rb,
lib/ruby2js/converter/sym.rb,
lib/ruby2js/converter/var.rb,
lib/ruby2js/converter/args.rb,
lib/ruby2js/converter/case.rb,
lib/ruby2js/converter/defs.rb,
lib/ruby2js/converter/dstr.rb,
lib/ruby2js/converter/hash.rb,
lib/ruby2js/converter/ivar.rb,
lib/ruby2js/converter/next.rb,
lib/ruby2js/converter/self.rb,
lib/ruby2js/converter/send.rb,
lib/ruby2js/converter/xstr.rb,
lib/ruby2js/converter/array.rb,
lib/ruby2js/converter/begin.rb,
lib/ruby2js/converter/block.rb,
lib/ruby2js/converter/break.rb,
lib/ruby2js/converter/casgn.rb,
lib/ruby2js/converter/class.rb,
lib/ruby2js/converter/const.rb,
lib/ruby2js/converter/masgn.rb,
lib/ruby2js/converter/undef.rb,
lib/ruby2js/converter/until.rb,
lib/ruby2js/converter/vasgn.rb,
lib/ruby2js/converter/while.rb,
lib/ruby2js/converter/ivasgn.rb,
lib/ruby2js/converter/module.rb,
lib/ruby2js/converter/opasgn.rb,
lib/ruby2js/converter/orasgn.rb,
lib/ruby2js/converter/regexp.rb,
lib/ruby2js/converter/return.rb,
lib/ruby2js/converter/andasgn.rb,
lib/ruby2js/converter/boolean.rb,
lib/ruby2js/converter/defined.rb,
lib/ruby2js/converter/kwbegin.rb,
lib/ruby2js/converter/literal.rb,
lib/ruby2js/converter/logical.rb,
lib/ruby2js/converter/blockpass.rb,
lib/ruby2js/converter/untilpost.rb,
lib/ruby2js/converter/whilepost.rb

Constant Summary collapse

LOGICAL =
:and, :not, :or
OPERATORS =
[:[], :[]=], [:not, :!], [:*, :/, :%], [:+, :-], [:>>, :<<], 
[:<=, :<, :>, :>=], [:==, :!=, :===, :"!=="], [:and, :or]
@@handlers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, vars = {}) ⇒ Converter

Returns a new instance of Converter.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby2js/converter.rb', line 11

def initialize( ast, vars = {} )
  @ast, @vars = ast, vars.dup
  @sep = '; '
  @nl = ''
  @ws = ' '
  @varstack = []

  @handlers = {}
  @@handlers.each do |name|
    @handlers[name] = method("on_#{name}")
  end
end

Instance Attribute Details

#bindingObject

Returns the value of attribute binding.



9
10
11
# File 'lib/ruby2js/converter.rb', line 9

def binding
  @binding
end

#ivarsObject

Returns the value of attribute ivars.



9
10
11
# File 'lib/ruby2js/converter.rb', line 9

def ivars
  @ivars
end

Class Method Details

.handle(*types, &block) ⇒ Object



63
64
65
66
67
68
# File 'lib/ruby2js/converter.rb', line 63

def self.handle(*types, &block)
  types.each do |type| 
    define_method("on_#{type}", block)
    @@handlers << type
  end
end

Instance Method Details

#enable_vertical_whitespaceObject



24
25
26
27
28
# File 'lib/ruby2js/converter.rb', line 24

def enable_vertical_whitespace
  @sep = ";\n"
  @nl = "\n"
  @ws = @nl
end

#group(ast) ⇒ Object



84
85
86
# File 'lib/ruby2js/converter.rb', line 84

def group( ast )
  "(#{ parse ast })"
end

#is_method?(node) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/ruby2js/converter.rb', line 54

def is_method?(node)
  return false unless node.type == :send
  return true unless node.loc
  selector = node.loc.selector
  return true unless selector.source_buffer
  selector.source_buffer.source[selector.end_pos] == '('
end

#operator_index(op) ⇒ Object



38
39
40
# File 'lib/ruby2js/converter.rb', line 38

def operator_index op
  OPERATORS.index( OPERATORS.find{ |el| el.include? op } ) || -1
end

#parse(ast, state = :expression) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby2js/converter.rb', line 70

def parse(ast, state=:expression)
  return ast unless ast

  @state = state
  @ast = ast
  handler = @handlers[ast.type]

  unless handler
    raise NotImplementedError, "unknown AST type #{ ast.type }"
  end

  handler.call(*ast.children) if handler
end

#s(type, *args) ⇒ Object



50
51
52
# File 'lib/ruby2js/converter.rb', line 50

def s(type, *args)
  Parser::AST::Node.new(type, args)
end

#scope(ast, args = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ruby2js/converter.rb', line 42

def scope( ast, args=nil )
  @varstack.push @vars.dup
  @vars = args if args
  parse( ast, :statement )
ensure
  @vars = @varstack.pop
end

#to_jsObject



34
35
36
# File 'lib/ruby2js/converter.rb', line 34

def to_js
  parse( @ast, :statement )
end