Permalink
Browse files

Added the Interpreter related ruby files.

  • Loading branch information...
1 parent f0aa4e2 commit ef25dba16a547866cec8d68bcfec4995d0b7c5af Attila Domokos committed Jan 4, 2010
View
@@ -0,0 +1,14 @@
+require 'expression'
+
+class And < Expression
+ def initialize(expression1, expression2)
+ @expression1 = expression1
+ @expression2 = expression2
+ end
+
+ def evaluate(dir)
+ result1 = @expression1.evaluate(dir)
+ result2 = @expression2.evaluate(dir)
+ (result1 & result2)
+ end
+end
View
@@ -0,0 +1,22 @@
+require 'expression'
+
+class Bigger < Expression
+ def initialize(size)
+ @size = size
+ end
+
+ def evaluate(dir)
+ results = []
+ Find.find(dir) do |p|
+ next unless File.file?(p)
+ results << p if(File.size(p) > @size)
+ end
+ results
+ end
+end
+
+=begin
+bigger_expr = Bigger.new(400)
+files_larger_than_15k = bigger_expr.evaluate('/Users/adomokos/Programming/Ruby')
+puts 'number of files larger than 15k: ' + files_larger_than_15k.length.to_s'
+=end
View
@@ -0,0 +1,16 @@
+require 'find'
+
+class Expression
+ # Common expression code will go here soon
+end
+
+class All < Expression
+ def evaluate(dir)
+ results = []
+ Find.find(dir) do |p|
+ next unless File.file?(p)
+ results << p
+ end
+ results
+ end
+end
View
@@ -0,0 +1,30 @@
+require 'expression'
+
+class FileName < Expression
+ def initialize(pattern)
+ @pattern = pattern
+ end
+
+ def evaluate(dir)
+ results = []
+ Find.find(dir) do |p|
+ next unless File.file?(p)
+ name = File.basename(p)
+ results << p if File.fnmatch(@pattern, name)
+ end
+ results
+ end
+end
+
+=begin
+expr_all = All.new
+files = expr_all.evaluate('/Users/adomokos/Programming/Ruby')
+
+puts files.length
+
+# But if we are just interested in ruby files
+expr_ruby_files = FileName.new('*.rb')
+ruby_files = expr_ruby_files.evaluate('/Users/adomokos/Programming/Ruby')
+
+puts ruby_files.length
+=end
View
@@ -0,0 +1,11 @@
+require 'expression'
+
+class Not < Expression
+ def initialize(expression)
+ @expression = expression
+ end
+
+ def evaluate(dir)
+ All.new.evaluate(dir) - @expression.evaluate(dir)
+ end
+end
View
@@ -0,0 +1,14 @@
+require 'expression'
+
+class Or < Expression
+ def initialize(expression1, expression2)
+ @expression1 = expression1
+ @expression2 = expression2
+ end
+
+ def evaluate(dir)
+ result1 = @expression1.evaluate(dir)
+ result2 = @expression2.evaluate(dir)
+ (result1 + result2).sort.uniq
+ end
+end
View
@@ -0,0 +1,55 @@
+require 'and'
+require 'or'
+require 'bigger'
+require 'file_name'
+require 'writable'
+
+class Parser
+ def initialize(text)
+ @tokens = text.scan(/\(|\)|[\w\.\*]+/)
+ end
+
+ def next_token
+ @tokens.shift
+ end
+
+ def expression
+ token = next_token
+
+ if token == nil
+ return nil
+
+ elsif token == '('
+ result = expression
+ raise 'Expected )' unless next_token == ')'
+ result
+
+ elsif token == 'all'
+ return All.new
+
+ elsif token == 'writable'
+ return Writable.new
+
+ elsif token == 'bigger'
+ return Bigger.new(next_token.to_i)
+
+ elsif token == 'filename'
+ return FileName.new(next_token)
+
+ elsif token == 'not'
+ return Not.new(expression)
+
+ elsif token == 'and'
+ return And.new(expression, expression)
+
+ elsif token == 'or'
+ return Or.new(expression, expression)
+
+ else
+ raise "Unexpected token: #{token}"
+ end
+ end
+end
+
+parser = Parser.new("and (and(bigger 1024)(filename *.mp3)) writable")
+ast = parser.expression
@@ -0,0 +1,49 @@
+require 'not'
+require 'writable'
+require 'bigger'
+require 'file_name'
+require 'or'
+require 'and'
+
+test_dir = '/Users/adomokos/Programming/Ruby'
+
+expr_not_writable = Not.new(Writable.new)
+readonly_files = expr_not_writable.evaluate(test_dir)
+
+puts "Number of read-only files: #{readonly_files.length}"
+# readonly_files.each { |file| puts file }
+
+small_expr = Not.new(Bigger.new(1024))
+small_files = small_expr.evaluate(test_dir)
+
+puts "Number of files less than 1Kb: #{small_files.length}"
+
+not_ruby_expr = Not.new(FileName.new('*.rb'))
+not_ruby_files = not_ruby_expr.evaluate(test_dir)
+puts "Number of non-ruby files: #{not_ruby_files.length}"
+
+big_or_ruby_expr = Or.new(Bigger.new(1024), FileName.new('*.rb'))
+big_or_ruby_files = big_or_ruby_expr.evaluate(test_dir)
+puts "Number or large ruby files: #{big_or_ruby_files.length}"
+
+complex_expression = And.new(
+ And.new(Bigger.new(1024), FileName.new('*.rb')), Not.new(Writable.new))
+files_result = complex_expression.evaluate(test_dir)
+puts "Number of files from the complex expression result: #{files_result.length}"
+
+# files_result = complex_expression.evaluate('/Bin')
+# puts files_result.length
+
+class Expression
+ def |(other)
+ Or.new(self, other)
+ end
+
+ def &(other)
+ And.new(self, other)
+ end
+end
+
+complex_expression = (Bigger.new(2000) & Not.new(Writable.new)) | FileName.new("*.mp3")
+files_result = complex_expression.evaluate(test_dir)
+puts "Number of files from the complex expression: #{files_result.length}"
View
@@ -0,0 +1,9 @@
+require 'rubygems'
+require 'runt'
+
+mondays = Runt::DIWeek.new(Runt::Monday)
+wednesdays = Runt::DIWeek.new(Runt::Wednesday)
+fridays = Runt::DIWeek.new(Runt::Friday)
+
+puts fridays.include?(Date.new(2015, 12, 25))
+puts mondays.include?(Date.new(2015, 12, 25))
View
@@ -0,0 +1,19 @@
+require 'expression'
+
+class Writable < Expression
+ def evaluate(dir)
+ results = []
+ Find.find(dir) do |p|
+ next unless File.file?(p)
+ results << p if(File.writable?(p))
+ end
+ results
+ end
+end
+
+=begin
+writable_expr = Writable.new
+writable_files = writable_expr.evaluate('/Users/adomokos/Programming/Ruby')
+puts "Number of writable files: #{writable_files.length}"
+=end
+

0 comments on commit ef25dba

Please sign in to comment.