Class: Cry::Codegen

Inherits:
Object
  • Object
show all
Defined in:
lib/cry/codegen.rb,
lib/cry/codegen/interface.rb,
lib/cry/codegen/ruby_method.rb,
lib/cry/codegen/crystallizer.rb,
lib/cry/codegen/crystal_function.rb

Defined Under Namespace

Classes: CrystalFunction, Crystallizer, Interface, RubyMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCodegen

Returns a new instance of Codegen.



12
13
14
15
16
17
18
19
# File 'lib/cry/codegen.rb', line 12

def initialize
  @sexp = nil
  @source_path = nil
  @function_names = [] # Function names to be exported
  @interface = {} # Type information for each function
  @crystal_code_blocks = []
  self.load(File.expand_path('codegen/allocation_functions.cr', __dir__))
end

Instance Attribute Details

#crystal_code_blocksObject

Returns the value of attribute crystal_code_blocks.



9
10
11
# File 'lib/cry/codegen.rb', line 9

def crystal_code_blocks
  @crystal_code_blocks
end

#function_namesObject

Returns the value of attribute function_names.



9
10
11
# File 'lib/cry/codegen.rb', line 9

def function_names
  @function_names
end

#sexpObject

Returns the value of attribute sexp.



9
10
11
# File 'lib/cry/codegen.rb', line 9

def sexp
  @sexp
end

#source_pathObject

Returns the value of attribute source_path.



10
11
12
# File 'lib/cry/codegen.rb', line 10

def source_path
  @source_path
end

Instance Method Details

#add_crystal_function(name, crystal_arg_types, crystal_ret_type, line_number) ⇒ Object



48
49
50
51
52
53
# File 'lib/cry/codegen.rb', line 48

def add_crystal_function(name, crystal_arg_types, crystal_ret_type, line_number)
  @function_names << name
  interface = Interface.new(name, crystal_arg_types, crystal_ret_type)
  @interface[name] = interface
  @crystal_code_blocks << crystallize(name, interface, line_number)
end

#crystal_codeObject



40
41
42
# File 'lib/cry/codegen.rb', line 40

def crystal_code
  @crystal_code_blocks.join("\n")
end

#crystallize(name, interface, line_number) ⇒ Object



55
56
57
58
59
# File 'lib/cry/codegen.rb', line 55

def crystallize(name, interface, line_number)
  ruby_method = extract_ruby_method(name, line_number)
  c = Crystallizer.new(ruby_method, interface)
  cry_func = c.source
end

#extract_ruby_method(name, line_number) ⇒ Object



61
62
63
64
# File 'lib/cry/codegen.rb', line 61

def extract_ruby_method(name, line_number)
  exp = find_ruby_method(name, line_number)
  RubyMethod.new_from_sexp(name, exp)
end

#find_ruby_method(name, line_number) ⇒ Object



66
67
68
69
70
# File 'lib/cry/codegen.rb', line 66

def find_ruby_method(name, line_number)
  @line_number = line_number
  name = name.to_s
  find_ruby_method2(@sexp, name)
end

#find_ruby_method2(arr, name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cry/codegen.rb', line 72

def find_ruby_method2(arr, name)
  arr.each do |item|
    if item.is_a?(Array)
      if item[0] == :def
        return item if item[1][0] == :@ident && (item[1][1] == name) && (item[1][2][0] > @line_number)
      elsif (r = find_ruby_method2(item, name))
        return r
      end
    end
  end
  nil
end

#interface(name) ⇒ Object



44
45
46
# File 'lib/cry/codegen.rb', line 44

def interface(name)
  @interface[name]
end

#load(fpath) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/cry/codegen.rb', line 21

def load(fpath)
  @crystal_code_blocks << IO.read(fpath)
  # FIXME
  File.readlines(fpath).grep(/fun /).map do |l|
    @function_names << l.split[1].split('(')[0]
  end
end