1. Mikołaj Baranowski
  2. cookery

Commits

Mikołaj Baranowski  committed d832259

move parsing into separate file

  • Participants
  • Parent commits f1dc536
  • Branches master

Comments (0)

Files changed (3)

File action.rb

View file
     @procedure = procedure
   end
 
-  def act(*arguments)
-    instance_exec(*arguments, &@procedure)
+  def act(subject: nil, conditions: [])
+    if subject.nil?
+      instance_eval(&@procedure)
+    else
+      data = subject.get
+      conditions.each do |c|
+        data = c.call(data)
+      end
+      instance_exec(data, &@procedure)
+    end
   end
 end
 
-
 Actions = Hash.new # { |h, k| h[k] = Hash.new }
 
-def action(name, type = nil, &procedure)
+def action(name, &procedure)
   Actions[name] = CookeryAction.new(name, &procedure)
 end

File cookery.rb

View file
+def cookery(text)
+  actions = Actions.keys.map { |a| Regexp.new(a.to_s.sub('_', ' '))}
+
+  p regex = /(?<action>#{Regexp.union(*actions)}) ?(?<subject>#{Regexp.union(*Subjects.keys)})? ?(?<arguments>#{Regexp.union(*Subjects.values.map(&:arguments))})?/i
+
+  p cond_regex = /(?:with )?(?<condition>#{Regexp.union(*Conditions.keys)})/
+
+
+  text.split("\n").each do |line|
+    if line.empty? or line[-1] != '.'
+      next
+    else
+      line = line[0...-1]
+    end
+
+    puts "- " * 20
+    line.downcase!
+
+    activity, conditions = line.split('-').map(&:strip)
+    puts "activity: #{activity}"
+    puts "conditions: #{conditions}"
+    puts
+
+    if match = regex.match(activity)
+      puts "action: #{match[:action]}"
+      puts "subject: #{match[:subject]}"
+      puts "arguments: #{match[:arguments]}"
+
+      action_name = match[:action].sub(' ', '_').to_sym
+
+      condition_names = Array.new
+      conditions.scan(cond_regex) do |cond_match|
+        condition_names << cond_match.first
+      end unless conditions.nil?
+
+      if match[:subject]
+        Subjects[match[:subject].to_sym].args(match[:arguments])
+        Actions[action_name].act(subject: Subjects[match[:subject].to_sym],
+                                 conditions: condition_names.map { |cn| Conditions[cn] })
+      else
+        Actions[action_name].act
+      end
+    end
+  end
+end

File file-operations.rb

View file
 require_relative 'action'
 require_relative 'channel'
 require_relative 'condition'
+require_relative 'cookery'
 require 'zlib'
 
-
 subject(:file, /(.+)/, :file) do |f|
   path f
 end
 
-action(:read, :file) do |subject, conditions|
+action(:read) do |subject|
   puts "in action"
-  x = subject.get()
-  gz = Zlib::GzipReader.new(StringIO.new(x))
-  channel_put(:foo, gz.read)
-  gz.close
+
+  channel_put(:foo, subject)
 end
 
-condition(:zip_compression) do |subject|
-  gz = Zlib::GzipReader.new(StringIO.new(subject.get()))
-  result = gz.rezd
+condition("with zip compression") do |data|
+  puts "in condition"
+  gz = Zlib::GzipReader.new(StringIO.new(data))
+  result = gz.read
   gz.close
   result
 end
 
+condition("with nothing") do |data|
+  data
+end
+
 action(:print_result) do
   puts "RESULT: #{channel_get(:bar)}"
 end
 end
 
 text = """
-Read file /tmp/test_data.gzip - with zip compression.
+Read file /tmp/test_data.gzip - with zip compression, with nothing.
 Count words.
 Print result.
 """
 
-actions = Actions.keys.map { |a| Regexp.new(a.to_s.sub('_', ' '))}
-
-p regex = /(?<action>#{Regexp.union(*actions)}) ?(?<subject>#{Regexp.union(*Subjects.keys)})? ?(?<arguments>#{Regexp.union(*Subjects.values.map(&:arguments))})?/i
-
-conditions = Conditions.keys.map { |c| Regexp.new(c.to_s.sub('_', ' ')) }
-
-p cond_regex = /(?:with )?(?<condition>#{Regexp.union(*conditions)})/
-
-text.split("\n").each do |line|
-  if line.empty? or line[-1] != '.'
-    next
-  else
-    line = line[0...-1]
-  end
-
-  puts "- " * 20
-  line.downcase!
-
-  activity, conditions = line.split('-').map(&:strip)
-  puts "activity: #{activity}"
-  puts "conditions: #{conditions}"
-  puts
-
-  if match = regex.match(activity)
-    puts "action: #{match[:action]}"
-    puts "subject: #{match[:subject]}"
-    puts "arguments: #{match[:arguments]}"
-    # puts "condition: #{match[:conditions]}"
-
-    action_name = match[:action].sub(' ', '_').to_sym
-
-    if cond_match = cond_regex.match(conditions)
-      p cond_match
-    end
-
-    if match[:subject]
-      Subjects[match[:subject].to_sym].args(match[:arguments])
-      Actions[match[:action].to_sym].act(Subjects[match[:subject].to_sym])
-    else
-      Actions[action_name].act
-    end
-
-
-  end
-end
+cookery(text)