diff --git a/src/bison/bison_parser.y b/src/bison/bison_parser.y index e26ef28..b730d02 100644 --- a/src/bison/bison_parser.y +++ b/src/bison/bison_parser.y @@ -231,8 +231,8 @@ scalar_expr: ; unary_expr: - '-' expr { $$ = Expr::makeOpUnary(UMINUS, $2); } - | NOT expr { $$ = Expr::makeOpUnary(NOT, $2); } + '-' expr { $$ = Expr::makeOpUnary(Expr::UMINUS, $2); } + | NOT expr { $$ = Expr::makeOpUnary(Expr::NOT, $2); } ; binary_expr: @@ -241,26 +241,26 @@ binary_expr: | expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); } | expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); } | expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); } - | expr AND expr { $$ = Expr::makeOpBinary($1, AND, $3); } - | expr OR expr { $$ = Expr::makeOpBinary($1, OR, $3); } + | expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); } + | expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); } ; comp_expr: expr EQUALS expr { $$ = Expr::makeOpBinary($1, '=', $3); } - | expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, NOT_EQUALS, $3); } + | expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); } | expr LESS expr { $$ = Expr::makeOpBinary($1, '<', $3); } | expr GREATER expr { $$ = Expr::makeOpBinary($1, '>', $3); } - | expr LESSEQ expr { $$ = Expr::makeOpBinary($1, LESS_EQ, $3); } - | expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, GREATER_EQ, $3); } + | expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); } + | expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); } ; function_expr: - NAME '(' expr ')' { $$ = makeFunctionRef($1, $3); } + NAME '(' expr ')' { $$ = Expr::makeFunctionRef($1, $3); } ; column_name: - NAME { $$ = makeColumnRef($1); } + NAME { $$ = Expr::makeColumnRef($1); } ; literal: diff --git a/src/lib/Expr.cpp b/src/lib/Expr.cpp index 2cf99bd..0f20b57 100644 --- a/src/lib/Expr.cpp +++ b/src/lib/Expr.cpp @@ -11,18 +11,6 @@ char* substr(const char* source, int from, int to) { return copy; } -Expr* makeColumnRef(char* name) { - ALLOC_EXPR(e, kExprColumnRef); - e->name = name; - return e; -} - -Expr* makeFunctionRef(char* func_name, Expr* expr) { - ALLOC_EXPR(e, kExprFunctionRef); - e->name = func_name; - e->expr = expr; - return e; -} @@ -64,7 +52,7 @@ Expr* Expr::makeLiteral(int64_t val) { Expr* Expr::makeLiteral(double value) { ALLOC_EXPR(e, kExprLiteralFloat); - e->float_literal = value; + e->fval = value; return e; } @@ -75,3 +63,16 @@ Expr* Expr::makeLiteral(char* string) { return e; } + +Expr* Expr::makeColumnRef(char* name) { + ALLOC_EXPR(e, kExprColumnRef); + e->name = name; + return e; +} + +Expr* Expr::makeFunctionRef(char* func_name, Expr* expr) { + ALLOC_EXPR(e, kExprFunctionRef); + e->name = func_name; + e->expr = expr; + return e; +} \ No newline at end of file diff --git a/src/lib/Expr.h b/src/lib/Expr.h index 18533df..4d4b071 100644 --- a/src/lib/Expr.h +++ b/src/lib/Expr.h @@ -3,8 +3,6 @@ #include - - typedef enum { kExprLiteralFloat, kExprLiteralString, @@ -15,32 +13,33 @@ typedef enum { kExprOperator } ExprType; - -/** - * Trivial types are those that can be descriped by a sigle character e.g: - * + - * / < > = % - * Non-trivial are: - * <> <= >= LIKE ISNULL NOT - */ -typedef enum { - SIMPLE_OP, - // Binary - NOT_EQUALS, - LESS_EQ, - GREATER_EQ, - LIKE, - AND, - OR, - // Unary - NOT, - UMINUS, - ISNULL -} OperatorType; - - - typedef struct Expr Expr; + struct Expr { + /** + * Operator types. These are important for expressions of type kExprOperator + * Trivial types are those that can be descriped by a sigle character e.g: + * + - * / < > = % + * Non-trivial are: + * <> <= >= LIKE ISNULL NOT + */ + typedef enum { + SIMPLE_OP, + // Binary + NOT_EQUALS, + LESS_EQ, + GREATER_EQ, + LIKE, + AND, + OR, + // Unary + NOT, + UMINUS, + ISNULL + } OperatorType; + + + Expr(ExprType type) : type(type) {}; ExprType type; @@ -48,7 +47,7 @@ struct Expr { Expr* expr; Expr* expr2; char* name; - float float_literal; + float fval; int64_t ival; OperatorType op_type; @@ -61,6 +60,9 @@ struct Expr { static Expr* makeLiteral(int64_t val); static Expr* makeLiteral(double val); static Expr* makeLiteral(char* val); + + static Expr* makeColumnRef(char* name); + static Expr* makeFunctionRef(char* func_name, Expr* expr); }; // Zero initializes an Expr object and assigns it to a space in the heap @@ -73,8 +75,4 @@ struct Expr { *var = zero; \ } while(0) - -Expr* makeColumnRef(char* name); -Expr* makeFunctionRef(char* func_name, Expr* expr); - #endif diff --git a/src/lib/sqlhelper.cpp b/src/lib/sqlhelper.cpp index 3a94efa..4f6d572 100644 --- a/src/lib/sqlhelper.cpp +++ b/src/lib/sqlhelper.cpp @@ -31,10 +31,10 @@ void printOperatorExpression(Expr* expr, uint num_indent) { if (expr == NULL) { inprint("null", num_indent); return; } switch (expr->op_type) { - case SIMPLE_OP: inprintC(expr->op_char, num_indent); break; - case AND: inprint("AND", num_indent); break; - case OR: inprint("OR", num_indent); break; - case NOT: inprint("NOT", num_indent); break; + case Expr::SIMPLE_OP: inprintC(expr->op_char, num_indent); break; + case Expr::AND: inprint("AND", num_indent); break; + case Expr::OR: inprint("OR", num_indent); break; + case Expr::NOT: inprint("NOT", num_indent); break; default: inprintU(expr->op_type, num_indent); break; } printExpression(expr->expr, num_indent+1); @@ -45,7 +45,7 @@ void printExpression(Expr* expr, uint num_indent) { switch (expr->type) { case kExprStar: inprint("*", num_indent); break; case kExprColumnRef: inprint(expr->name, num_indent); break; - case kExprLiteralFloat: inprint(expr->float_literal, num_indent); break; + case kExprLiteralFloat: inprint(expr->fval, num_indent); break; case kExprLiteralInt: inprint(expr->ival, num_indent); break; case kExprLiteralString: inprint(expr->name, num_indent); break; case kExprFunctionRef: /* todo */ break;