logical and, logical or

This commit is contained in:
Josh Wolfe 2017-09-20 21:37:56 -07:00
parent 05c1a8b3cc
commit 4c8443d96d
3 changed files with 22 additions and 8 deletions

View File

@ -14,8 +14,8 @@
static const char *bin_op_str(BinOpType bin_op) {
switch (bin_op) {
case BinOpTypeInvalid: return "(invalid)";
case BinOpTypeBoolOr: return "||";
case BinOpTypeBoolAnd: return "&&";
case BinOpTypeBoolOr: return "or";
case BinOpTypeBoolAnd: return "and";
case BinOpTypeCmpEq: return "==";
case BinOpTypeCmpNotEq: return "!=";
case BinOpTypeCmpLessThan: return "<";

View File

@ -976,8 +976,7 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo
case BO_GE:
return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpGreaterOrEq, stmt->getRHS());
case BO_EQ:
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_EQ");
return nullptr;
return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeCmpEq, stmt->getRHS());
case BO_NE:
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_NE");
return nullptr;
@ -991,11 +990,10 @@ static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *blo
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Or");
return nullptr;
case BO_LAnd:
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_LAnd");
return nullptr;
return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeBoolAnd, stmt->getRHS());
case BO_LOr:
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_LOr");
return nullptr;
// TODO: int vs bool
return trans_create_bin_op(c, block, stmt->getLHS(), BinOpTypeBoolOr, stmt->getRHS());
case BO_Assign:
(void)result_used;
emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Assign");

View File

@ -373,6 +373,22 @@ pub fn addCases(cases: &tests.ParseCContext) {
\\}
);
cases.add("logical and, logical or",
\\int max(int a, int b) {
\\ if (a < b || a == b)
\\ return b;
\\ if (a >= b && a == b)
\\ return a;
\\ return a;
\\}
,
\\export fn max(a: c_int, b: c_int) -> c_int {
\\ if ((a < b) or (a == b)) return b;
\\ if ((a >= b) and (a == b)) return a;
\\ return a;
\\}
);
cases.add("shift right assign with a fixed size type",
\\#include <stdint.h>
\\int log2(uint32_t a) {