Class: Logicuit::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/logicuit/dsl.rb

Overview

base class for all gates and circuits

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DSL

Returns a new instance of DSL.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/logicuit/dsl.rb', line 7

def initialize(*args)
  @input_targets = []
  @inputs_as_bool_struct = nil
  @output_targets = []
  @clock = false
  @components = []
  inputs(*args)
  outputs
  assembling
  @initialized = true
  evaluate
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



25
26
27
# File 'lib/logicuit/dsl.rb', line 25

def clock
  @clock
end

#componentsObject (readonly)

Returns the value of attribute components.



25
26
27
# File 'lib/logicuit/dsl.rb', line 25

def components
  @components
end

#initializedObject (readonly)

Returns the value of attribute initialized.



25
26
27
# File 'lib/logicuit/dsl.rb', line 25

def initialized
  @initialized
end

#input_targetsObject (readonly)

Returns the value of attribute input_targets.



25
26
27
# File 'lib/logicuit/dsl.rb', line 25

def input_targets
  @input_targets
end

#output_targetsObject (readonly)

Returns the value of attribute output_targets.



25
26
27
# File 'lib/logicuit/dsl.rb', line 25

def output_targets
  @output_targets
end

Class Method Details

.assembling(&block) ⇒ Object



91
92
93
94
95
96
# File 'lib/logicuit/dsl.rb', line 91

def self.assembling(&block)
  define_method(:assembling) do
    ret = instance_eval(&block)
    ret.each { @components << it } if ret.is_a?(Array)
  end
end

.diagram(source) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/logicuit/dsl.rb', line 98

def self.diagram(source)
  define_method(:to_s) do
    source_ = @input_targets.reduce(source) do |result, input|
      result.gsub(/\(#{input}\)/i, "(#{instance_variable_get("@#{input}")})#{"-" * (input.size - 1)}")
    end
    @output_targets.reduce(source_) do |result, output|
      result.gsub(/\(#{output}\)/i, "#{"-" * (output.size - 1)}(#{instance_variable_get("@#{output}")})")
    end
  end
end

.inputs(*args, **kwargs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/logicuit/dsl.rb', line 27

def self.inputs(*args, **kwargs)
  # define getter methods for inputs
  attr_reader(*args)

  # define initializer for inputs
  define_method(:inputs) do |*instance_method_args|
    @clock = true if kwargs&.key?(:clock)
    args.each_with_index do |input, index|
      signal = Signals::Signal.new(instance_method_args[index] == 1)
      signal >> self unless clock
      instance_variable_set("@#{input}", signal)
      @input_targets << input
    end
    Signals::Clock >> self if clock
    @inputs_as_bool_struct = Struct.new(*@input_targets)
  end
end

.outputs(*args, **kwargs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logicuit/dsl.rb', line 55

def self.outputs(*args, **kwargs)
  # define getter methods for outputs
  attr_reader(*(args + kwargs.keys))

  # define initializer for outputs
  define_method(:outputs) do
    (args + kwargs.keys).each do |output|
      instance_variable_set("@#{output}", Signals::Signal.new(false))
      @output_targets << output
    end
  end

  # define evaluate method
  return if kwargs.empty?

  define_method(:evaluate) do |*override_args|
    return unless initialized

    kwargs.each do |output, evaluator|
      signal = instance_variable_get("@#{output}")
      e_args = if override_args.empty?
                 @input_targets.map do |input|
                   instance_variable_get("@#{input}").current
                 end
               else
                 override_args
               end
      if @inputs_as_bool_struct.new(*e_args).instance_exec(&evaluator)
        signal.on
      else
        signal.off
      end
    end
  end
end

.run(opts = {}) ⇒ Object



154
155
156
# File 'lib/logicuit/dsl.rb', line 154

def self.run(opts = {})
  ::Logicuit.run(new, **opts)
end

.truth_table(source) ⇒ Object



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
# File 'lib/logicuit/dsl.rb', line 109

def self.truth_table(source)
  define_method(:truth_table) do
    rows = source.strip.split("\n").map { |row| row.gsub(/#.*$/, "") }
    headers = rows.shift.split("|").map(&:strip).reject(&:empty?).map(&:downcase).map(&:to_sym)
    rows.shift # devide line
    table = rows.map do |row|
      row.split("|").map(&:strip).reject(&:empty?).map(&:downcase).map do |v|
        case v
        when "^"
          :clock
        when "x"
          :any
        when "1"
          true
        when "0"
          false
        else
          raise "Invalid value in truth table: #{v}" unless headers.include?(v.to_sym)

          [:ref, v.to_sym]
        end
      end
    end.select do |values|
      headers.size == values.size
    end.map do |values|
      array = [values]
      while array.any? { it.any? { |v| v == :any } }
        target_index = array.find_index { it.any? { |v| v == :any } }
        target = array[target_index]
        prop_index = target.find_index { |v| v == :any }
        array.delete_at(target_index)
        array.insert(target_index, *[true, false].map do |v|
          target.dup.tap do |a|
            a[prop_index] = v
          end
        end)
      end
      array
    end.flatten!(1).map do |values|
      headers.zip(values).to_h
    end
    table
  end
end

Instance Method Details

#[](*keys) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/logicuit/dsl.rb', line 45

def [](*keys)
  if keys.size == 1
    send(keys.first)
  elsif keys.size > 1
    Signals::SignalGroup.new(*(keys.map { |key| send(key) }))
  else
    raise ArgumentError, "Invalid number of arguments"
  end
end

#assemblingObject



22
# File 'lib/logicuit/dsl.rb', line 22

def assembling; end

#evaluate(*args) ⇒ Object



23
# File 'lib/logicuit/dsl.rb', line 23

def evaluate(*args); end

#inputs(*args) ⇒ Object



20
# File 'lib/logicuit/dsl.rb', line 20

def inputs(*args); end

#outputsObject



21
# File 'lib/logicuit/dsl.rb', line 21

def outputs; end