Class: Metal::Generator::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/metal/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(codes) ⇒ Meta

Returns a new instance of Meta.



36
37
38
39
40
41
# File 'lib/metal/generator.rb', line 36

def initialize(codes)
	@grammars, @precodes = codes.partition {|i| i.is_a?(Grammar) }
	@vars = []
	@grammar = nil
	@rule = nil
end

Instance Attribute Details

#grammarObject

Returns the value of attribute grammar.



89
90
91
# File 'lib/metal/generator.rb', line 89

def grammar
  @grammar
end

#metaObject

Returns the value of attribute meta.



89
90
91
# File 'lib/metal/generator.rb', line 89

def meta
  @meta
end

#ruleObject

Returns the value of attribute rule.



89
90
91
# File 'lib/metal/generator.rb', line 89

def rule
  @rule
end

Instance Method Details

#generate(fname, source = '', header = '', rbcode = '') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/metal/generator.rb', line 42

def generate(fname, source = '', header = '', rbcode = '')
	@grammars.each {|grammar|
		grammar.preprocess(self)
	}
	
	header << "#import \"metal/runtime.h\"\n"
	source << "#import \"#{fname}.h\"\n"
	source << "using namespace Metal;\n"
	
	@vars.sort!
	@vars.uniq!
	@vars.each {|var|
		source << "static param_t const PARAM_#{var} = param_lookup(\"#{var}\");\n"
	}
	
	rbcode << <<END
#import "ruby.h"
#import "metal/rbcode.h"
#import "#{fname}.h"
#ifdef __cplusplus
extern "C" {
#endif
void Init_#{fname}() {
VALUE mMetal = rb_define_module("Metal");
VALUE cBase = rb_define_class_under(mMetal, "ParserBase", rb_cObject);
VALUE code;
END

@precodes.each {|precode|
	code, code_len = Generator.escape_quoated_string(precode.code)
	rbcode << "rb_eval_string(\"#{code}\");\n"
}

@grammars.each {|grammar|
	grammar.generate(self, source, header, rbcode)
}

rbcode << <<END
}
#ifdef __cplusplus
}
#endif
END
	
	return [source, header, rbcode]
end

#register_var(name) ⇒ Object



91
92
93
# File 'lib/metal/generator.rb', line 91

def register_var(name)
	@vars.push name
end