| @@ -3,6 +3,20 @@ require 'ruby_parser' | |||||
| require 'sexp_processor' | require 'sexp_processor' | ||||
| module Bitshift | module Bitshift | ||||
| class Tuple | |||||
| attr_accessor :objects | |||||
| def initialize(arr) | |||||
| @objects = arr | |||||
| end | |||||
| def inspect | |||||
| s = "(" | |||||
| @objects.each {|o| s += "#{o},"} | |||||
| s = s[0..-2] + ')' | |||||
| end | |||||
| end | |||||
| class Parser | class Parser | ||||
| def initialize(source) | def initialize(source) | ||||
| @source = source | @source = source | ||||
| @@ -25,7 +39,7 @@ module Bitshift | |||||
| def initialize(offset, tree) | def initialize(offset, tree) | ||||
| super() | super() | ||||
| module_hash = Hash.new {|hash, key| hash[key] = { assignments: [], uses: [] }} | |||||
| module_hash = Hash.new {|hash, key| hash[key] = { decls: [], uses: [] }} | |||||
| class_hash = module_hash.clone | class_hash = module_hash.clone | ||||
| function_hash = module_hash.clone | function_hash = module_hash.clone | ||||
| var_hash = module_hash.clone | var_hash = module_hash.clone | ||||
| @@ -50,7 +64,7 @@ module Bitshift | |||||
| break if cur_exp == nil | break if cur_exp == nil | ||||
| end | end | ||||
| pos = [start_ln, -1, end_ln, -1] | |||||
| pos = Tuple.new([start_ln, -1, end_ln, -1]) | |||||
| return pos | return pos | ||||
| end | end | ||||
| @@ -58,7 +72,7 @@ module Bitshift | |||||
| pos = Hash.new | pos = Hash.new | ||||
| end_ln = start_ln = exp.line - offset | end_ln = start_ln = exp.line - offset | ||||
| pos = [start_ln, -1, end_ln, -1] | |||||
| pos = Tuple.new([start_ln, -1, end_ln, -1]) | |||||
| return pos | return pos | ||||
| end | end | ||||
| @@ -66,7 +80,7 @@ module Bitshift | |||||
| pos = block_position(exp) | pos = block_position(exp) | ||||
| exp.shift | exp.shift | ||||
| name = exp.shift | name = exp.shift | ||||
| symbols[:modules][name][:assignments] << pos | |||||
| symbols[:modules][name][:decls] << pos | |||||
| exp.each_sexp {|s| process(s)} | exp.each_sexp {|s| process(s)} | ||||
| return exp.clear | return exp.clear | ||||
| end | end | ||||
| @@ -75,7 +89,7 @@ module Bitshift | |||||
| pos = block_position(exp) | pos = block_position(exp) | ||||
| exp.shift | exp.shift | ||||
| name = exp.shift | name = exp.shift | ||||
| symbols[:classes][name][:assignments] << pos | |||||
| symbols[:classes][name][:decls] << pos | |||||
| exp.each_sexp {|s| process(s)} | exp.each_sexp {|s| process(s)} | ||||
| return exp.clear | return exp.clear | ||||
| end | end | ||||
| @@ -84,7 +98,7 @@ module Bitshift | |||||
| pos = block_position(exp) | pos = block_position(exp) | ||||
| exp.shift | exp.shift | ||||
| name = exp.shift | name = exp.shift | ||||
| symbols[:functions][name][:assignments] << pos | |||||
| symbols[:functions][name][:decls] << pos | |||||
| exp.each_sexp {|s| process(s)} | exp.each_sexp {|s| process(s)} | ||||
| return exp.clear | return exp.clear | ||||
| end | end | ||||
| @@ -103,7 +117,7 @@ module Bitshift | |||||
| pos = statement_position(exp) | pos = statement_position(exp) | ||||
| exp.shift | exp.shift | ||||
| name = exp.shift | name = exp.shift | ||||
| symbols[:vars][name][:assignments] << pos | |||||
| symbols[:vars][name][:decls] << pos | |||||
| exp.each_sexp {|s| process(s)} | exp.each_sexp {|s| process(s)} | ||||
| return exp.clear | return exp.clear | ||||
| end | end | ||||
| @@ -112,14 +126,22 @@ module Bitshift | |||||
| pos = statement_position(exp) | pos = statement_position(exp) | ||||
| exp.shift | exp.shift | ||||
| name = exp.shift | name = exp.shift | ||||
| symbols[:vars][name][:assignments] << pos | |||||
| symbols[:vars][name][:decls] << pos | |||||
| exp.each_sexp {|s| process(s)} | exp.each_sexp {|s| process(s)} | ||||
| return exp.clear | return exp.clear | ||||
| end | end | ||||
| def to_s | def to_s | ||||
| str = symbols.to_s | |||||
| str = str.gsub(/:(\w*)=>/, '"\1":') | |||||
| new_symbols = Hash.new {|hash, key| hash[key] = []} | |||||
| symbols.each do |type, sym_list| | |||||
| sym_list.each do |name, sym| | |||||
| new_symbols[type.to_s] << Tuple.new(["'#{name}'", sym[:decls], sym[:uses]]) | |||||
| end | |||||
| end | |||||
| str = new_symbols.to_s | |||||
| str = str.gsub(/=>/, ":") | |||||
| return str | return str | ||||
| end | end | ||||
| end | end | ||||