Class: MixCompiler
- Inherits:
-
Racc::Parser
- Object
- Racc::Parser
- MixCompiler
- Defined in:
- lib/ruby/mix.tab.rb,
lib/ruby/compiler.rb
Constant Summary collapse
- Racc_arg =
[ racc_action_table, racc_action_check, racc_action_default, racc_action_pointer, racc_goto_table, racc_goto_check, racc_goto_default, racc_goto_pointer, racc_nt_base, racc_reduce_table, racc_token_table, racc_shift_n, racc_reduce_n, racc_use_result_var ]
- Racc_token_to_s_table =
[ "$end", "error", "\"!\"", "\"-\u{b7}\"", "\"-1\"", "\"*\"", "\"/\"", "\"%\"", "\"+\"", "\"-\"", "\"<<\"", "\">>\"", "\"<\"", "\"<=\"", "\">\"", "\">=\"", "\"==\"", "\"!=\"", "\"&&\"", "\"||\"", "\"?\"", "\":\"", "\"=\"", "\"&&=\"", "\"||=\"", "\"\u{b7}=\"", "IF", "UNLESS", "WHILE", "UNTIL", "\";\"", "NEWLINE", "MIXIN", "INDENT", "OUTDENT", "IDENTIFIER", "\"#\"", "\"|\"", "\",\"", "SWITCH", "ELSIF", "ELSE", "CASE", "BREAK", "RETURN", "\"(\"", "\")\"", "\".\"", "\"\u{b7}[\"", "\"]\"", "\"\u{b7}#\"", "\"\u{b7}:\"", "NUMBER", "\"'\"", "STRING", "APP", "FALSE", "NULL", "SELF", "TRUE", "\"[\"", "\"{\"", "\"}\"", "KEY", "$start", "statements", "statement", "indented_statement", "inline_statements", "opt_semicolon", "newline", "mixin_statement", "control_statement", "function_statement", "mixin_body", "definitions", "definition", "function_block", "inline_function", "parameter_list", "indent_block", "parameters", "@1", "exp", "else_statement", "opt_else", "cases", "case", "inline_statement", "postfix_statement", "mix_statement", "keyword_statement", "object", "dot_access", "arguments", "functional_object", "lhs", "literal", "literal_number", "literal_string", "literal_constant", "literal_array", "literal_object", "opt_comma", "fields", "parenthetical", "call", "curly_brace_block" ]
- Racc_debug_parser =
false- NODE_TYPES =
{}
- OPERATORS =
{ '+' => :plus, '-' => :minus, '*' => :star, '/' => :slash, '%' => :percent, '>' => :gt, '>>' => :gtgt, '>=' => :gteq, '<' => :lt, '<<' => :ltlt, '<=' => :lteq, '==' => :eq, '!=' => :neq }
Instance Method Summary collapse
- #_reduce_none(val, _values) ⇒ Object
- #append_statement(outermost_statement, final_statement) ⇒ Object
- #build_script ⇒ Object
- #compile ⇒ Object
- #getch ⇒ Object
-
#initialize(filename) ⇒ MixCompiler
constructor
A new instance of MixCompiler.
- #js(node) ⇒ Object
- #newline(statement_node) ⇒ Object
- #next_token ⇒ Object
- #node(type, *values) ⇒ Object
- #on_error(token, value, stack) ⇒ Object
- #parameter(name) ⇒ Object
- #parse ⇒ Object
- #pop_variables ⇒ Object
- #precompile(js_file_string) ⇒ Object
- #push_variables ⇒ Object
- #scan_includes(script, files) ⇒ Object
- #token(token, value = token) ⇒ Object
- #tokenize ⇒ Object
- #unscan ⇒ Object
- #variable(name) ⇒ Object
Constructor Details
#initialize(filename) ⇒ MixCompiler
Returns a new instance of MixCompiler.
8 9 10 |
# File 'lib/ruby/compiler.rb', line 8 def initialize (filename) @primary_filename = filename end |
Instance Method Details
#_reduce_none(val, _values) ⇒ Object
1392 1393 1394 |
# File 'lib/ruby/mix.tab.rb', line 1392 def _reduce_none(val, _values) val[0] end |
#append_statement(outermost_statement, final_statement) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ruby/compiler.rb', line 33 def append_statement (outermost_statement, final_statement) current = outermost_statement current = current[:values][1] while current[:values][1] current[:values][1] = final_statement outermost_statement end |
#build_script ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby/compiler.rb', line 42 def build_script script = File.open(@primary_filename) {|f| "#{f.read}\n" } files = {'mix' => [], 'js' => %w[base/env.js base/eval.js base/object.js base/array.js base/false.js base/function.js base/null.js base/number.js base/string.js base/true.js]} @unordered_mix_filenames = [@primary_filename] scan_includes(script, files) files['mix'].push [@primary_filename, script] @js_filenames = files['js'] @filenames = files['mix'].map {|name,*| name } @script = files['mix'].map {|*,script| script }.join("\034") end |
#compile ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby/compiler.rb', line 66 def compile build_script node = parse node_string = js(node) symbol_string = @symbols.keys.map {|s| "'#{s}'" }.join ?, js_file_string = @js_filenames.map do |filename| File.read(File.('../../javascript/' + filename, __FILE__)) end.join ?\n precompile(js_file_string) <<-JAVASCRIPT mix_result_should_be_undefined = function () { var HTML_TYPES = {}; var SYMBOLS = [#{symbol_string.force_encoding('US-ASCII')}]; #{js_file_string} window.onload = function () { startup(); evaluate(#{node_string}); } }(); JAVASCRIPT rescue SyntaxError => e "window.onload = function () { if (console && console.log) console.log(\"SyntaxError: #{e.}\"); };" end |
#getch ⇒ Object
99 100 101 |
# File 'lib/ruby/compiler.rb', line 99 def getch @script[@pointer += 1] end |
#js(node) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ruby/compiler.rb', line 103 def js (node) return "0" unless node t = node[:type] a, b, c = node[:values] args = [NODE_TYPES[t]] case (t = node[:type]) when :access_brackets, :and, :or, :set, :statement, :while args << js(a) << js(b) when :access_colon, :access_hash, :mix args << js(a) << @symbols[b.to_sym] when :app, :false, :null, :self, :true nil when :array items = a.map {|x| js(x) }.join ?, args << "[#{items}]" when :break, :not, :return args << js(a) when :call function = js(a) arguments = b.map {|x| js(x) }.join ?, args << function << "[#{arguments}]" when :function parameters = a[0].map {|x| @symbols[x.to_sym] }.join ?, variables = a[1].map {|x| @symbols[x.to_sym] }.join ?, body = js(b) args << "[[#{parameters}],[#{variables}]]" << body when :if args << js(a) << js(b) << js(c) when :mixin name = @symbols[a.to_sym] init_statement = js(b[0]) definitions = b[1].map {|s| js(s) }.join ?, args << name << init_statement << "[#{definitions}]" when :newline file, line = @symbols[a[0].to_sym], a[1] statement = js(b) args << file << line << statement when :number args << a when :object entries = a.map {|kv| "[#{@symbols[kv[0].to_sym]},#{js(kv[1])}]" }.join ?, mixins = b.map {|x| @symbols[x.to_sym] }.join ?, args << "[#{entries}]" << "[#{mixins}]" when :op args << @symbols[OPERATORS[a]] << js(b) << js(c) when :string, :variable args << @symbols[a.to_sym] when :switch condition = js(a) cases = b.map {|x| "[#{js(x[0])},#{js(x[1])}]" }.join ?, else_case = js(c) args << condition << cases << else_case else raise "BUG: node type :#{t} not implemented in MixCompiler#js" end return "[#{args.join ?,}]" end |
#newline(statement_node) ⇒ Object
185 186 187 188 |
# File 'lib/ruby/compiler.rb', line 185 def newline (statement_node) statement_node[:values][0] = node(:newline, @location, statement_node[:values][0]) statement_node end |
#next_token ⇒ Object
190 191 192 |
# File 'lib/ruby/compiler.rb', line 190 def next_token @tokens.shift end |
#node(type, *values) ⇒ Object
194 195 196 |
# File 'lib/ruby/compiler.rb', line 194 def node (type, *values) { type: type, values: values } end |
#on_error(token, value, stack) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/ruby/compiler.rb', line 198 def on_error (token, value, stack) if value value, file, line = value[0].inspect, *value[1] else value, file, line = 'EOF', @primary_filename, @line end raise SyntaxError, "Unexpected token #{token_to_str(token)} (#{value}): #{file}:#{line}".gsub(?",'\"') end |
#parameter(name) ⇒ Object
208 209 210 211 |
# File 'lib/ruby/compiler.rb', line 208 def parameter (name) @variables[:parameters] << name @variables[:local] << name end |
#parse ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/ruby/compiler.rb', line 213 def parse tokenize symbol_index = -1 @symbols = Hash.new {|h,k| h[k] = (symbol_index += 1) } @variables = { parameters: [], local: [], embedded: [], prev: nil } return do_parse end |
#pop_variables ⇒ Object
222 223 224 225 226 227 |
# File 'lib/ruby/compiler.rb', line 222 def pop_variables vars = [@variables[:parameters], @variables[:embedded]] @variables = @variables[:prev] return vars end |
#precompile(js_file_string) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/ruby/compiler.rb', line 56 def precompile (js_file_string) NODE_TYPES.each {|k,v| js_file_string.gsub!("case #{k}:","case #{v}:") } %w:ARRAY ELEMENT EVENT FALSE FUNCTION NULL NUMBER OBJECT STRING TEXT TRUE:.each_with_index do |t,i| js_file_string.gsub!("#{t}_TYPE","#{i}") end js_file_string.gsub!(/\s*\/\*.*?\*\/\s*/m,"\n").gsub!(/\s*\n\s*/,?\n).gsub!(/\s*([{;:,=])\s*/,'\1').gsub!("\n}",'}') end |
#push_variables ⇒ Object
229 230 231 232 |
# File 'lib/ruby/compiler.rb', line 229 def push_variables = @variables[:local] + @variables[:embedded] @variables = { parameters: [], local: [], embedded: , prev: @variables } end |
#scan_includes(script, files) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/ruby/compiler.rb', line 242 def scan_includes (script, files) require 'strscan' ss = StringScanner.new(script) js_filenames = [] while ss.scan(/~~\s+(.*\.(js|mix))\s*\n/) name = ss[1] type = ss[2] if type == 'mix' unless @unordered_mix_filenames.include?(name) script = File.open(name) {|f| "#{f.read}\n" } @unordered_mix_filenames << name scan_includes(script, files) files['mix'].push [name,script] end else js_filenames << name end end files['js'] |= js_filenames end |
#token(token, value = token) ⇒ Object
267 268 269 |
# File 'lib/ruby/compiler.rb', line 267 def token (token, value = token) @tokens << [token, [value, [@filename, @line]]] end |
#tokenize ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/ruby/compiler.rb', line 271 def tokenize @filename = @filenames[file_index = 0] @line = 1 @pointer = -1 @indent_stack = [0] @state = :newline @tokens = [] empty_lines = 0 while c = getch case c when ?\034 @filename = @filenames[file_index += 1] @line = 1 when ?~ if getch == ?~ :skip until getch =~ /\n/ unscan next end unscan :skip while getch =~ /\s/ unscan when ' ', ?\t @state = :space if @state == :reference when ?\n if @state == :newline empty_lines += 1 next end current_indent = 0 previous_indent = @indent_stack.last while (c = getch) == ' ' current_indent += 1 end unscan if c == ?\n empty_lines += 1 next end @state = :newline token :NEWLINE @line += (empty_lines + 1) empty_lines = 0 if current_indent > previous_indent @indent_stack << current_indent token :INDENT elsif current_indent < previous_indent unless @indent_stack.include?(current_indent) raise(SyntaxError, "Indent error: #{@filename}:#{@line}") end until @indent_stack.last == current_indent @indent_stack.pop token :OUTDENT end end when ?!, ?= @state = :begin if getch == ?= token c + '=' else unscan token c end when ?+, ?*, ?/, ?% @state = :begin if getch == ?= token '·=', c else unscan token c end when ?- if getch == ?= token '·=', c elsif c =~ /[0-9]/ token '-1' unscan else unscan if @state == :begin || @state == :newline token '-·' else token '-' end end @state = :begin when ??, ?., ?;, ?,, ?(, ?{ @state = :begin token c when ?' @state = :reference s = "" until (c = getch) == ?' s << c end token '\'' token :STRING, s token '\'' when ?>, ?< @state = :begin if getch == c if getch == ?= token '·=', c * 2 else unscan token c * 2 end else unscan if getch == ?= token c + '=' else unscan token c end end when ?&, ?| @state = :begin if getch == c if getch == ?= token c + c + '=' else unscan token c + c end else unscan token c end when /[0-9]/ @state = :reference value = c while (c = getch) =~ /[0-9]/ value << c end unscan token :NUMBER, value.to_i when ?], ?}, ?) @state = :reference token c when ?:, ?[, ?# if @state == :reference token '·' + c else token c end @state = :begin when /[A-Z]/ @state = :begin value = c while (c = getch) =~ /[_a-zA-Z0-9]/ value << c end unscan token :MIXIN, value.to_sym when /[_a-z]/ @state = :reference value = c while (c = getch) =~ /[_a-zA-Z0-9]/ value << c end if c == ?: if getch =~ /[a-z]/ unscan else @state = :begin token :KEY, value.to_sym unscan next end unscan elsif c == ?? || c == ?! value << c else unscan end if %w: app break case elsif else false if null return self switch true unless until while : .include?(value) token value.upcase.to_sym else token :IDENTIFIER, value.to_sym end case c = getch when ?:, ?# token '·' + c when ?[ token '·[' @state = :begin else unscan end else raise SyntaxError, "Invalid token `#{c}': #{@filename}:#{@line}" end end @tokens << [false, false] end |
#unscan ⇒ Object
525 526 527 |
# File 'lib/ruby/compiler.rb', line 525 def unscan @pointer -= 1 end |
#variable(name) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/ruby/compiler.rb', line 234 def variable (name) unless (@variables[:local] + @variables[:embedded]).include?(name) @variables[:local] << name end node :variable, name end |