|
@@ -11,16 +11,37 @@ module Chervil
|
11
|
11
|
!(pairs.map { |pair| pair[0].send(method, pair[1]) }.any?(false))
|
12
|
12
|
end
|
13
|
13
|
|
|
14
|
+ def self.display_class_name(cls)
|
|
15
|
+ case cls.inspect
|
|
16
|
+ when "Float"
|
|
17
|
+ "number"
|
|
18
|
+ when "String"
|
|
19
|
+ "string"
|
|
20
|
+ end
|
|
21
|
+ end
|
|
22
|
+
|
|
23
|
+ def self.type_check(args, cls)
|
|
24
|
+ args_with_wrong_type = args.select { |arg| !arg.is_a?(cls) }
|
|
25
|
+
|
|
26
|
+ if args_with_wrong_type.length.zero?
|
|
27
|
+ nil
|
|
28
|
+ else
|
|
29
|
+ expected_type = display_class_name(cls)
|
|
30
|
+ received_type = display_class_name(args_with_wrong_type.first.class)
|
|
31
|
+ ::Chervil::Error.new("Expected an argument of type #{expected_type} but got #{received_type}")
|
|
32
|
+ end
|
|
33
|
+ end
|
|
34
|
+
|
14
|
35
|
CORE = {
|
15
|
|
- "+" => Proc.new { |args| args.inject(:+) },
|
16
|
|
- "-" => Proc.new { |args| args.inject(:-) },
|
17
|
|
- "*" => Proc.new { |args| args.inject(:*) },
|
18
|
|
- "/" => Proc.new { |args| args.inject(:/) },
|
|
36
|
+ "+" => Proc.new { |args| type_check(args, Float) || args.inject(:+) },
|
|
37
|
+ "-" => Proc.new { |args| type_check(args, Float) || args.inject(:-) },
|
|
38
|
+ "*" => Proc.new { |args| type_check(args, Float) || args.inject(:*) },
|
|
39
|
+ "/" => Proc.new { |args| type_check(args, Float) || args.inject(:/) },
|
19
|
40
|
"=" => Proc.new { |args| compare_pairs(args, :==) },
|
20
|
|
- "<" => Proc.new { |args| compare_pairs(args, :<) },
|
21
|
|
- ">" => Proc.new { |args| compare_pairs(args, :>) },
|
22
|
|
- "<=" => Proc.new { |args| compare_pairs(args, :<=) },
|
23
|
|
- ">=" => Proc.new { |args| compare_pairs(args, :>=) },
|
|
41
|
+ "<" => Proc.new { |args| type_check(args, Float) || compare_pairs(args, :<) },
|
|
42
|
+ ">" => Proc.new { |args| type_check(args, Float) || compare_pairs(args, :>) },
|
|
43
|
+ "<=" => Proc.new { |args| type_check(args, Float) || compare_pairs(args, :<=) },
|
|
44
|
+ ">=" => Proc.new { |args| type_check(args, Float) || compare_pairs(args, :>=) },
|
24
|
45
|
"and" => Proc.new { |args| !(args.include?(false)) },
|
25
|
46
|
"or" => Proc.new { |args| args.any? { |arg| !!arg == true } },
|
26
|
47
|
}
|