Add comparison operator support for if conditions

- Extend _emit_expr() to handle 'compare' type expressions
- Map Python comparison ops (Gt, Lt, Eq, etc) to LLVM icmp predicates
- Convert i1 result to i32 for compatibility
- All conditional tests passing (3/3)
This commit is contained in:
Aaron Sinclair committed 2025-11-15 03:29:57 -08:00
1 parent 044eda2293
commit 0063a8536c
2 files changed
+23

No files matched your search

+23
View File
@@ -247,6 +247,29 @@ class SignalLLVMIRGenerator:
elif expr["type"] == "constant": elif expr["type"] == "constant":
# Return constant inline # Return constant inline
return str(expr["value"]) return str(expr["value"])
elif expr["type"] == "compare":
# Handle comparison (e.g., count > 5)
left_var = self._emit_expr(expr["left"])
right_var = self._emit_expr(expr["comparators"][0])
# Map Python comparison ops to LLVM icmp predicates
op_map = {
"Gt": "sgt", # signed greater than
"Lt": "slt", # signed less than
"GtE": "sge", # signed greater or equal
"LtE": "sle", # signed less or equal
"Eq": "eq", # equal
"NotEq": "ne", # not equal
}
icmp_op = op_map.get(expr["op"], "eq")
result_var = self._new_var()
self.lines.append(f" %{result_var} = icmp {icmp_op} i32 %{left_var}, {right_var}")
# Convert i1 to i32 for compatibility
result_i32 = self._new_var()
self.lines.append(f" %{result_i32} = zext i1 %{result_var} to i32")
return result_i32
else: else:
# Unknown expression type # Unknown expression type
return "0" return "0"