cleaning out stash
most of the stuff is now reimplemented fragments still open (as interpreter is not working yet)
This commit is contained in:
100
stash/fragments/collector.rb
Normal file
100
stash/fragments/collector.rb
Normal file
@ -0,0 +1,100 @@
|
||||
require "risc/interpreter"
|
||||
require "parser/ruby22"
|
||||
require "yaml"
|
||||
|
||||
# An experiment to find out how much ruby there is to achieve bootstrap
|
||||
#
|
||||
# currently (jan/2017) just under 150 classes, 1.5k methods , 10k sends, only 10 yields
|
||||
# 1 retry and redo (to be avoided), 4 ensure , 5 rescue
|
||||
#
|
||||
class Walker < AST::Processor
|
||||
def initialize collector
|
||||
@collector = collector
|
||||
end
|
||||
|
||||
def on_send node
|
||||
_ , method , file_node = *node
|
||||
if method == :require
|
||||
file = file_node.children[0]
|
||||
@collector.load("#{file}.rb") unless file.include?("parslet")
|
||||
end
|
||||
if method == :require_relative
|
||||
@collector.load File.dirname(@collector.current) + "/" + file_node.children[0] + ".rb"
|
||||
end
|
||||
if method.to_s.include?("eval")
|
||||
@collector.evals << method
|
||||
end
|
||||
handler_missing(node)
|
||||
end
|
||||
|
||||
def on_class node
|
||||
@collector.class_defs << node.children[0].children[1]
|
||||
handler_missing(node)
|
||||
end
|
||||
def on_const node
|
||||
@collector.const_uses[node.children[1]] += 1
|
||||
handler_missing(node)
|
||||
end
|
||||
def handler_missing node
|
||||
type = node.type
|
||||
@collector.types[type] += 1
|
||||
node.children.each do |kid|
|
||||
process(kid) if kid.is_a? AST::Node
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Collector
|
||||
|
||||
def initialize
|
||||
@parser = Parser::Ruby22
|
||||
@paths = Bundler.load.specs.collect { |s| s.gem_dir + "/lib/" }
|
||||
@class_defs = []
|
||||
@const_uses = Hash.new(0)
|
||||
@types = Hash.new(0)
|
||||
@not_found = []
|
||||
@walker = Walker.new(self)
|
||||
@files = []
|
||||
@evals = []
|
||||
@current = nil
|
||||
end
|
||||
attr_reader :class_defs , :const_uses , :types , :current , :evals
|
||||
|
||||
def file_content file_name
|
||||
return nil if @files.include? file_name
|
||||
@paths.each do |name|
|
||||
next unless File.exist? name + file_name
|
||||
@files << file_name
|
||||
return File.open(name + file_name).read
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def run
|
||||
load "rubyx.rb"
|
||||
load "parser/ruby22.rb"
|
||||
# load "../../../.rbenv/versions/2.2.3/lib/ruby/2.2.0/racc/parser.rb"
|
||||
print
|
||||
end
|
||||
|
||||
def load file
|
||||
str = file_content(file)
|
||||
return @not_found.push(file) unless str
|
||||
ast = @parser.parse str
|
||||
was = @current
|
||||
@current = file
|
||||
@walker.process ast
|
||||
@current = was
|
||||
end
|
||||
def print
|
||||
@class_defs.uniq!
|
||||
@files.uniq!
|
||||
puts "Types #{@types.to_yaml}"
|
||||
puts "Class defs #{@class_defs.length}"
|
||||
puts "Class defs #{@class_defs}"
|
||||
puts "evals=#{@evals.length} #{@evals.uniq}"
|
||||
puts "Not found #{@not_found.length} #{@not_found}"
|
||||
end
|
||||
end
|
||||
|
||||
Collector.new.run
|
17
stash/fragments/helper.rb
Normal file
17
stash/fragments/helper.rb
Normal file
@ -0,0 +1,17 @@
|
||||
require_relative '../helper'
|
||||
require "risc/interpreter"
|
||||
require "parser/ruby22"
|
||||
|
||||
module Rubyx
|
||||
module RubyxTests
|
||||
include CompilerHelper
|
||||
include Risc::InterpreterHelpers
|
||||
subs = ObjectSpace.each_object(Class).select { |klass| klass < Risc::Instruction }
|
||||
subs.each do |clazz|
|
||||
name = clazz.to_s
|
||||
next if name.include?("Arm")
|
||||
scoped = name.split("::").last
|
||||
module_eval "#{scoped} = #{name}"
|
||||
end
|
||||
end
|
||||
end
|
26
stash/fragments/test_adds.rb
Normal file
26
stash/fragments/test_adds.rb
Normal file
@ -0,0 +1,26 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestRubyAdds < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_adds
|
||||
@string_input = <<HERE
|
||||
def fibo( n)
|
||||
a = 0
|
||||
b = 1
|
||||
i = 1
|
||||
while( i < n ) do
|
||||
result = a + b
|
||||
a = b
|
||||
b = result
|
||||
i+= 1
|
||||
end
|
||||
return result
|
||||
end
|
||||
HERE
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
24
stash/fragments/test_calls.rb
Normal file
24
stash/fragments/test_calls.rb
Normal file
@ -0,0 +1,24 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestRubyCalls < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_calls
|
||||
@string_input = <<HERE
|
||||
|
||||
def fibo_r( n )
|
||||
if( n < 2 )
|
||||
return n
|
||||
else
|
||||
return fibo_r(n - 1) + fibo_r(n - 2)
|
||||
end
|
||||
end
|
||||
|
||||
fibo 40
|
||||
HERE
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
45
stash/fragments/test_hello.rb
Normal file
45
stash/fragments/test_hello.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestRubyHello #< MiniTest::Test
|
||||
include RubyxTests
|
||||
Branch = Risc::Branch
|
||||
Label = Risc::Label
|
||||
|
||||
def setup
|
||||
@string_input = as_main '"Hello there".putstring'
|
||||
Risc.machine.boot
|
||||
# do_clean_compile
|
||||
RubyxCompiler.compile @string_input
|
||||
Risc::Collector.collect_space
|
||||
@interpreter = Risc::Interpreter.new
|
||||
@interpreter.start Risc.machine.init
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
SlotToReg, RegToSlot, SlotToReg, LoadConstant, RegToSlot,
|
||||
LoadConstant, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label,
|
||||
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
|
||||
RiscTransfer, Syscall, RiscTransfer, RiscTransfer, RegToSlot,
|
||||
Label, FunctionReturn, RiscTransfer, SlotToReg, SlotToReg,
|
||||
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
|
||||
end
|
||||
|
||||
def test_overflow
|
||||
instruction = ticks( 24 )
|
||||
assert_equal Risc::FunctionCall , instruction.class
|
||||
assert_equal :putstring , instruction.method.name
|
||||
end
|
||||
|
||||
def test_ruby_hello
|
||||
done = ticks(45)
|
||||
assert_equal NilClass , done.class
|
||||
assert_equal "Hello there" , @interpreter.stdout
|
||||
end
|
||||
|
||||
end
|
||||
end
|
16
stash/fragments/test_itos.rb
Normal file
16
stash/fragments/test_itos.rb
Normal file
@ -0,0 +1,16 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestRubyItos < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_itos
|
||||
@string_input = <<HERE
|
||||
100000.to_s
|
||||
HERE
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
19
stash/fragments/test_loop.rb
Normal file
19
stash/fragments/test_loop.rb
Normal file
@ -0,0 +1,19 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestRubyLoop < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_loop
|
||||
@string_input = <<HERE
|
||||
counter = 100000
|
||||
while(counter > 0) do
|
||||
counter -= 1
|
||||
end
|
||||
HERE
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
34
stash/fragments/test_many_adds.rb
Normal file
34
stash/fragments/test_many_adds.rb
Normal file
@ -0,0 +1,34 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestManyAdds < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_adds_looping
|
||||
@string_input = <<HERE
|
||||
def fibo( n)
|
||||
a = 0
|
||||
b = 1
|
||||
i = 1
|
||||
while( i < n ) do
|
||||
result = a + b
|
||||
a = b
|
||||
b = result
|
||||
i+= 1
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
counter = 100000
|
||||
|
||||
while(counter > 0) do
|
||||
fibo(40)
|
||||
counter -= 1
|
||||
end
|
||||
HERE
|
||||
@length = 37
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
30
stash/fragments/test_many_calls.rb
Normal file
30
stash/fragments/test_many_calls.rb
Normal file
@ -0,0 +1,30 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestManyCalls < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_calls_looping
|
||||
@string_input = <<HERE
|
||||
|
||||
def fibo_r( n )
|
||||
if( n < 2 )
|
||||
return n
|
||||
else
|
||||
return fibo_r(n - 1) + fibo_r(n - 2)
|
||||
end
|
||||
end
|
||||
|
||||
counter = 1000
|
||||
|
||||
while(counter > 0) do
|
||||
fibo_r(20)
|
||||
counter -= 1
|
||||
end
|
||||
HERE
|
||||
@length = 37
|
||||
@stdout = ""
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
21
stash/fragments/test_many_hello.rb
Normal file
21
stash/fragments/test_many_hello.rb
Normal file
@ -0,0 +1,21 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestManyHello < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_hello_looping
|
||||
@string_input = <<HERE
|
||||
counter = 100000;
|
||||
while(counter > 0) do
|
||||
puts "Hello there"
|
||||
STDOUT.flush
|
||||
counter = counter - 1
|
||||
end
|
||||
HERE
|
||||
@length = 37
|
||||
@stdout = "Hello there"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
22
stash/fragments/test_many_itos.rb
Normal file
22
stash/fragments/test_many_itos.rb
Normal file
@ -0,0 +1,22 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Rubyx
|
||||
class TestManyItos < MiniTest::Test
|
||||
include RubyxTests
|
||||
|
||||
def pest_ruby_itos_looping
|
||||
@string_input = <<HERE
|
||||
counter = 100000
|
||||
|
||||
while(counter > 0) do
|
||||
str = counter.to_s
|
||||
counter = counter - 1
|
||||
end
|
||||
str
|
||||
HERE
|
||||
@length = 37
|
||||
@stdout = "Hello Raisa, I am rubyx"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user