Class: Duby::AST::MacroDefinition

Inherits:
Node
  • Object
show all
Includes:
Named
Defined in:
lib/duby/ast/intrinsics.rb

Instance Attribute Summary collapse

Attributes included from Named

#name

Attributes inherited from Node

#children, #inferred_type, #newline, #parent, #position

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Named

#to_s

Methods inherited from Node

#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #expr?, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(parent, line_number, name, &block) ⇒ MacroDefinition

Returns a new instance of MacroDefinition.



18
19
20
21
# File 'lib/duby/ast/intrinsics.rb', line 18

def initialize(parent, line_number, name, &block)
  super(parent, line_number, &block)
  @name = name
end

Instance Attribute Details

#proxyObject

Returns the value of attribute proxy.



11
12
13
# File 'lib/duby/ast/intrinsics.rb', line 11

def proxy
  @proxy
end

Class Method Details

.new(*args, &block) ⇒ Object



13
14
15
16
# File 'lib/duby/ast/intrinsics.rb', line 13

def self.new(*args, &block)
  real_node = super
  real_node.proxy = NodeProxy.new(real_node)
end

Instance Method Details

#annotate(type, name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/duby/ast/intrinsics.rb', line 81

def annotate(type, name)
  node = type.unmeta.node
  if node
    extension = node.annotation('duby.anno.Extensions')
    extension ||= begin
      node.annotations << Annotation.new(
          nil, nil, BiteScript::ASM::Type.getObjectType('duby/anno/Extensions'))
      node.annotations[-1].runtime = false
      node.annotations[-1]
    end
    extension['macros'] ||= []
    macro = Annotation.new(nil, nil,
                           BiteScript::ASM::Type.getObjectType('duby/anno/Macro'))
    macro['name'] = name
    macro['signature'] = BiteScript::Signature.signature(*signature)
    extension['macros'] << macro
  else
    puts "Warning: No ClassDefinition for #{type.name}. Macros can't be loaded from disk."
  end
end

#argument_typesObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/duby/ast/intrinsics.rb', line 49

def argument_types
  arguments.map do |arg|
    if arg.kind_of?(BlockArgument)
      TypeReference::BlockType
    else
      # TODO support typed args. Also there should be a way
      # to accept any AST node.
      Duby::JVM::Types::Object
    end
  end
end

#build_and_load_extension(parent, name, state) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/duby/ast/intrinsics.rb', line 69

def build_and_load_extension(parent, name, state)
  transformer = Duby::Transform::Transformer.new(state)
  ast = build_ast(name, parent, transformer)
  puts ast.inspect if state.verbose
  classes = compile_ast(name, ast, transformer)
  loader = DubyClassLoader.new(
      JRuby.runtime.jruby_class_loader, classes)
  klass = loader.loadClass(name, true)
  annotate(parent, name)
  klass
end

#build_ast(name, parent, transformer) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/duby/ast/intrinsics.rb', line 123

def build_ast(name, parent, transformer)
  # TODO should use a new type factory too.
  ast = Duby::AST.parse_ruby("import duby.lang.compiler.Node")
  ast = transformer.transform(ast, nil)

  # Start building the extension class
  extension = transformer.define_class(position, name)
  #extension.superclass = Duby::AST.type('duby.lang.compiler.Macro')
  extension.interfaces = [Duby::AST.type('duby.lang.compiler.Macro')]

  # The constructor just saves the state
  extension.define_constructor(
      position,
      ['duby', Duby::AST.type('duby.lang.compiler.Compiler')],
      ['call', Duby::AST.type('duby.lang.compiler.Call')]) do |c|
    transformer.eval("@duby = duby;@call = call", '-', c, 'duby', 'call')
  end

  node_type = Duby::AST.type('duby.lang.compiler.Node')

  # expand() parses the arguments out of call and then passes them off to
  # _expand
  expand = extension.define_method(
      position, 'expand', node_type)
  args = []
  arguments.each_with_index do |arg, i|
    # TODO optional args
    args << if arg.kind_of?(BlockArgument)
      "@call.block"
    else
      "Node(args.get(#{i}))"
    end
  end
  expand.body = transformer.eval(<<-end)
    args = @call.arguments
    _expand(#{args.join(', ')})
  end
  actual_args = arguments.map do |arg|
    [arg.name, node_type, arg.position]
  end
  m = extension.define_method(position, '_expand', node_type, *actual_args)
  m.body = self.body
  ast
end

#compile_ast(name, ast, transformer) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/duby/ast/intrinsics.rb', line 102

def compile_ast(name, ast, transformer)
  filename = name.gsub(".", "/")
  typer = Duby::Typer::JVM.new(filename, transformer)
  typer.infer(ast)
  typer.resolve(true)
  compiler = Duby::Compiler::JVM.new(filename)
  ast.compile(compiler, false)
  class_map = {}
  compiler.generate do |outfile, builder|
    outfile = "#{transformer.destination}#{outfile}"
    FileUtils.mkdir_p(File.dirname(outfile))
    File.open(outfile, 'w') do |f|
      bytes = builder.generate
      name = builder.class_name.gsub(/\//, '.')
      class_map[name] = bytes
      f.write(bytes)
    end
  end
  class_map
end

#infer(typer) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/duby/ast/intrinsics.rb', line 23

def infer(typer)
  resolve_if(typer) do
    self_type = typer.self_type
    extension_name = "%s$%s" % [self_type.name,
                                typer.transformer.tmp("Extension%s")]
    klass = build_and_load_extension(self_type,
                                     extension_name,
                                     typer.transformer.state)

    # restore the self type since we're sharing a type factory
    typer.known_types['self'] = self_type

    arg_types = argument_types
    macro = self_type.add_macro(name, *arg_types) do |duby, call|
      expander = klass.constructors[0].newInstance(duby, call)
      expander.expand
    end
    if arguments[-1].kind_of?(BlockArgument) && arguments[-1].optional?
      arg_types.pop
      typer.self_type.add_method(name, arg_types, macro)
    end
    proxy.__inline__(Noop.new(parent, position))
    proxy.infer(typer)
  end
end

#signatureObject



61
62
63
64
65
66
67
# File 'lib/duby/ast/intrinsics.rb', line 61

def signature
  args = argument_types
  if args.size > 0 && args[-1].block?
    args[-1] = BiteScript::ASM::Type.getObjectType('duby.lang.compiler.Block')
  end
  [nil] + args
end