lib/predicator/visitors/instructions.rb in predicator-1.1.0 vs lib/predicator/visitors/instructions.rb in predicator-1.2.0
- old
+ new
@@ -35,39 +35,66 @@
super
@instructions.push ["not"]
end
def visit_EQ node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
@instructions.push ["compare", "EQ"]
end
def visit_GT node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
@instructions.push ["compare", "GT"]
end
def visit_LT node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
@instructions.push ["compare", "LT"]
end
def visit_BETWEEN node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.middle
+ visit node.right
@instructions.push ["compare", "BETWEEN"]
end
def visit_IN node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
@instructions.push ["compare", "IN"]
end
def visit_NOTIN node
- super
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
@instructions.push ["compare", "NOTIN"]
end
+ def visit_STRSTARTSWITH node
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
+ @instructions.push ["compare", "STARTSWITH"]
+ end
+
+ def visit_STRENDSWITH node
+ visit node.left
+ add_typecast_to_instructions node
+ visit node.right
+ @instructions.push ["compare", "ENDSWITH"]
+ end
+
def visit_ARRAY node
contents = node.left.map{ |item| item.left }
@instructions.push ["array", contents]
end
@@ -78,11 +105,50 @@
def visit_BOOL node
super
@instructions.push ["to_bool"]
end
+ def visit_DATE node
+ @instructions.push ["lit", node.symbol]
+ @instructions.push ["to_date"]
+ end
+
+ def visit_DATEAGO node
+ visit node.left
+ @instructions.push ["date_ago"]
+ end
+
+ def visit_DATEFROMNOW node
+ visit node.left
+ @instructions.push ["date_from_now"]
+ end
+
+ def visit_DURATION node
+ as_seconds = node.symbol.to_i * 24 * 60 * 60
+ @instructions.push ["lit", as_seconds]
+ end
+
+ def visit_BLANK node
+ visit node.left
+ @instructions.push ["blank"]
+ end
+
+ def visit_PRESENT node
+ visit node.left
+ @instructions.push ["present"]
+ end
+
def terminal node
@instructions.push ["lit", node.symbol]
+ end
+
+ def add_typecast_to_instructions node
+ type = case node.right.type.to_s
+ when /INT/ then "to_int"
+ when /STR/ then "to_str"
+ when /DATE/ then "to_date"
+ end
+ @instructions.push [type]
end
def jump_instruction condition, node
["j#{condition}", node.object_id.to_s]
end