Restructure code

This commit is contained in:
pjht 2018-07-10 08:52:34 -05:00
parent 7476eed452
commit 9192bc9684
16 changed files with 58 additions and 29 deletions

View File

@ -1 +1 @@
*.rb --private --exclude old/*
--private --exclude old/*

7
lib/rcircuit.rb Normal file
View File

@ -0,0 +1,7 @@
require_relative "rcircuit/port.rb"
require_relative "rcircuit/gate.rb"
require_relative "rcircuit/device.rb"
require_relative "rcircuit/not.rb"
require_relative "rcircuit/and.rb"
require_relative "rcircuit/or.rb"
require_relative "rcircuit/xor.rb"

View File

@ -1,6 +1,3 @@
require_relative "gate.rb"
require_relative "port.rb"
# Represents an AND gate
class AndGate < Gate

47
lib/rcircuit/device.rb Normal file
View File

@ -0,0 +1,47 @@
# Class for devices such as adders and registers
class Device
# Add an input port
# @param name [String] Name of the port
# @param width [Integer] Width of the port
# @return [void]
def add_input(name, width=1)
self.class.class_eval{attr_reader name.to_sym}
port = Port.new(width)
instance_variable_set("@#{name}", port)
port.add_callback { |val| on_change(val) }
end
# Add an output port
# @param name [String] Name of the port
# @param width [Integer] Width of the port
# @return [void]
def add_output(name, width=1)
self.class.class_eval{attr_reader name.to_sym}
port = Port.new(width)
instance_variable_set("@#{name}", port)
end
# Add a subdevice
# @param name [String] Name of the subdevice
# @param device [Device] Device to add
# @return [void]
def define_device(name, device)
instance_variable_set("@#{name}", device)
self.class.class_eval{attr_reader name.to_sym}
end
private
# Used to set the ports from the argument hash in initialize
# @param hash [Hash{String=>Port}] Hash of names to ports
# @return [void]
def init_assign(hash)
hash.each do |name, port|
#check if there is a defined method (port) that matches
if self.respond_to?(name)
method(name).call(port)
else
raise ArgumentError, "No defined input '#{name}'"
end
end
end
end

View File

@ -1,5 +1,3 @@
require_relative "port.rb"
# @abstract Subclass and override {#inputs_changed} to implement a gate
## Base class for gates
class Gate

View File

@ -1,6 +1,3 @@
require_relative "gate.rb"
require_relative "port.rb"
# Represents a NOT gate
class NotGate < Gate

View File

@ -1,6 +1,3 @@
require_relative "gate.rb"
require_relative "port.rb"
# Represents an OR gate
class OrGate < Gate

View File

@ -12,11 +12,8 @@ class Port
attr_reader :width
# Returns a new instance of Port
# @overload set(width)
# @param width [Integer] The width of the port
# @overload set(width, name)
# @param width [Integer] The width of the port
# @param name [String] The name of the port
# @param width [Integer] The width of the port
# @param name [String] The name of the port
def initialize(width, name="")
@connected=[]
@callbacks=[]

View File

@ -1,6 +1,3 @@
require_relative "gate.rb"
require_relative "port.rb"
# Represents an XOR gate
class XorGate < Gate

View File

@ -1,4 +1,3 @@
require_relative "../and.rb"
describe AndGate do
it "should AND together all inputs" do
table=[

View File

@ -1,6 +1,3 @@
require_relative "../port.rb"
require_relative "../gate.rb"
describe Gate do
let(:klass) do
Class.new(Gate) do

View File

@ -1,4 +1,3 @@
require_relative "../not.rb"
describe NotGate do
it "should NOT the input" do
table=[

View File

@ -1,4 +1,3 @@
require_relative "../or.rb"
describe OrGate do
it "should OR together all inputs" do
table=[

View File

@ -1,5 +1,3 @@
require_relative "../port.rb"
describe Port do
it "should set the port's value when we call #setval" do
a=Port.new(4)

1
spec/spec_helper.rb Normal file
View File

@ -0,0 +1 @@
require_relative "../lib/rcircuit.rb"

View File

@ -1,4 +1,3 @@
require_relative "../xor.rb"
describe XorGate do
it "should XOR together all inputs" do
table=[