Class: MixTranslator
- Inherits:
-
Object
- Object
- MixTranslator
- Defined in:
- lib/ruby/translator.rb
Overview
Copyright © 2011-2012 Jesse Sielaff
Constant Summary collapse
- NODE_TYPES =
%w: access_brackets access_colon access_hash and app array break call false function if mix mixin newline not null number object op or return self set statement string switch true variable while : .each_with_object({}).with_index {|(t,h),i| h[t.to_sym] = i + 1 }
- OPERATORS =
{ '+' => :plus, '-' => :minus, '*' => :star, '/' => :slash, '%' => :percent, '>' => :gt, '>>' => :gtgt, '>=' => :gteq, '<' => :lt, '<<' => :ltlt, '<=' => :lteq, '==' => :eq, '!=' => :neq }
Instance Method Summary collapse
- #array(array) ⇒ Object
- #node(node) ⇒ Object
- #symbol(value) ⇒ Object
- #translate(node) ⇒ Object
- #write_access_brackets(a, b) ⇒ Object
- #write_access_colon(a, b) ⇒ Object
- #write_access_hash(a, b) ⇒ Object
- #write_and(a, b) ⇒ Object
- #write_app ⇒ Object
- #write_array(a) ⇒ Object
- #write_break(a) ⇒ Object
- #write_call(a, b) ⇒ Object
- #write_false ⇒ Object
- #write_function(a, b, c) ⇒ Object
- #write_if(a, b, c) ⇒ Object
- #write_mix(a, b) ⇒ Object
- #write_mixin(a, b, c) ⇒ Object
- #write_newline(a, b, c) ⇒ Object
- #write_not(a) ⇒ Object
- #write_null ⇒ Object
- #write_number(a) ⇒ Object
- #write_object(a, b) ⇒ Object
- #write_op(a, b, c) ⇒ Object
- #write_or(a, b) ⇒ Object
- #write_return(a) ⇒ Object
- #write_self ⇒ Object
- #write_set(a, b) ⇒ Object
- #write_statement(a, b) ⇒ Object
- #write_string(a) ⇒ Object
- #write_switch(a, b, c) ⇒ Object
- #write_true ⇒ Object
- #write_variable(a) ⇒ Object
- #write_while(a, b) ⇒ Object
Instance Method Details
#array(array) ⇒ Object
24 25 26 |
# File 'lib/ruby/translator.rb', line 24 def array (array) "[#{array.join ?,}]" end |
#node(node) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/ruby/translator.rb', line 28 def node (node) return "0" unless node t = node[:type] node_array = [NODE_TYPES[t], *send(:"write_#{t}", *node[:values])] array(node_array) end |
#symbol(value) ⇒ Object
36 37 38 |
# File 'lib/ruby/translator.rb', line 36 def symbol (value) @symbols[value.to_sym] end |
#translate(node) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby/translator.rb', line 40 def translate (node) symbol_index = -1 @symbols = Hash.new {|h,k| h[k] = (symbol_index += 1) } ast_string = node(node) symbol_string = array(@symbols.keys.map {|x| "'#{x}'" }) return ast_string, symbol_string end |
#write_access_brackets(a, b) ⇒ Object
50 51 52 |
# File 'lib/ruby/translator.rb', line 50 def write_access_brackets (a, b) object, key = node(a), node(b) end |
#write_access_colon(a, b) ⇒ Object
54 55 56 |
# File 'lib/ruby/translator.rb', line 54 def write_access_colon (a, b) object, name = node(a), symbol(b) end |
#write_access_hash(a, b) ⇒ Object
58 59 60 |
# File 'lib/ruby/translator.rb', line 58 def write_access_hash (a, b) object, fname = node(a), symbol(b) end |
#write_and(a, b) ⇒ Object
62 63 64 |
# File 'lib/ruby/translator.rb', line 62 def write_and (a, b) left, right = node(a), node(b) end |
#write_app ⇒ Object
66 67 68 |
# File 'lib/ruby/translator.rb', line 66 def write_app nil end |
#write_array(a) ⇒ Object
70 71 72 73 74 |
# File 'lib/ruby/translator.rb', line 70 def write_array (a) items_array = a.map {|x| node(x) } items = array(items_array) end |
#write_break(a) ⇒ Object
76 77 78 |
# File 'lib/ruby/translator.rb', line 76 def write_break (a) value = node(a) end |
#write_call(a, b) ⇒ Object
80 81 82 83 84 |
# File 'lib/ruby/translator.rb', line 80 def write_call (a, b) arguments_array = b.map {|x| node(x) } function, arguments = node(a), array(arguments_array) end |
#write_false ⇒ Object
86 87 88 |
# File 'lib/ruby/translator.rb', line 86 def write_false nil end |
#write_function(a, b, c) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/ruby/translator.rb', line 90 def write_function (a, b, c) parameters_array = a.map {|x| symbol(x) } = b.map {|x| symbol(x) } parameters, , body_statement = array(parameters_array), array(), node(c) end |
#write_if(a, b, c) ⇒ Object
97 98 99 |
# File 'lib/ruby/translator.rb', line 97 def write_if (a, b, c) condition, then_statement, else_statement = node(a), node(b), node(c) end |
#write_mix(a, b) ⇒ Object
101 102 103 |
# File 'lib/ruby/translator.rb', line 101 def write_mix (a, b) mixin, object = symbol(a), node(b) end |
#write_mixin(a, b, c) ⇒ Object
105 106 107 108 109 |
# File 'lib/ruby/translator.rb', line 105 def write_mixin (a, b, c) definitions_array = c.map {|x| node(x) } name, init_statement, definitions = symbol(a), node(b), array(definitions_array) end |
#write_newline(a, b, c) ⇒ Object
111 112 113 |
# File 'lib/ruby/translator.rb', line 111 def write_newline (a, b, c) file, line, statement = symbol(a), b, node(c) end |
#write_not(a) ⇒ Object
115 116 117 |
# File 'lib/ruby/translator.rb', line 115 def write_not (a) value = node(a) end |
#write_null ⇒ Object
119 120 121 |
# File 'lib/ruby/translator.rb', line 119 def write_null nil end |
#write_number(a) ⇒ Object
123 124 125 |
# File 'lib/ruby/translator.rb', line 123 def write_number (a) number = a end |
#write_object(a, b) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/ruby/translator.rb', line 127 def write_object (a, b) entries_array = a.map {|kv| array [symbol(kv[0]), node(kv[1])] } mixins_array = b.map {|x| symbol(x) } entries, mixins = array(entries_array), array(mixins_array) end |
#write_op(a, b, c) ⇒ Object
134 135 136 |
# File 'lib/ruby/translator.rb', line 134 def write_op (a, b, c) operator, left, right = symbol(OPERATORS[a]), node(b), node(c) end |
#write_or(a, b) ⇒ Object
138 139 140 |
# File 'lib/ruby/translator.rb', line 138 def write_or (a, b) left, right = node(a), node(b) end |
#write_return(a) ⇒ Object
142 143 144 |
# File 'lib/ruby/translator.rb', line 142 def write_return (a) value = node(a) end |
#write_self ⇒ Object
146 147 148 |
# File 'lib/ruby/translator.rb', line 146 def write_self nil end |
#write_set(a, b) ⇒ Object
150 151 152 |
# File 'lib/ruby/translator.rb', line 150 def write_set (a, b) lhs, value = node(a), node(b) end |
#write_statement(a, b) ⇒ Object
154 155 156 |
# File 'lib/ruby/translator.rb', line 154 def write_statement (a, b) current_statement, next_statement = node(a), node(b) end |
#write_string(a) ⇒ Object
158 159 160 |
# File 'lib/ruby/translator.rb', line 158 def write_string (a) value = symbol(a) end |
#write_switch(a, b, c) ⇒ Object
162 163 164 165 166 |
# File 'lib/ruby/translator.rb', line 162 def write_switch (a, b, c) cases_array = b.map {|x| array(x.map {|y| node(y) }) } condition, cases, else_statement = node(a), array(cases_array), node(c) end |
#write_true ⇒ Object
168 169 170 |
# File 'lib/ruby/translator.rb', line 168 def write_true nil end |
#write_variable(a) ⇒ Object
172 173 174 |
# File 'lib/ruby/translator.rb', line 172 def write_variable (a) value = symbol(a) end |
#write_while(a, b) ⇒ Object
176 177 178 |
# File 'lib/ruby/translator.rb', line 176 def write_while (a, b) condition, statement = node(a), node(b) end |