refactored Expr to be struct and have a type
This commit is contained in:
parent
0e5477f916
commit
d6598d6cf6
|
@ -1,3 +1,5 @@
|
||||||
|
utils/
|
||||||
|
|
||||||
# Compiled Object files
|
# Compiled Object files
|
||||||
*.slo
|
*.slo
|
||||||
*.lo
|
*.lo
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Makefile
|
# Makefile
|
||||||
|
|
||||||
LIB_FILES = lex_lexer.c lex_parser.c lib/Statement.cpp lib/SQLParser.cpp
|
LIB_FILES = lex_lexer.c lex_parser.c lib/Statement.cpp lib/Expr.cpp lib/SQLParser.cpp
|
||||||
|
|
||||||
|
|
||||||
TESTS_MAIN = sql_tests.cpp
|
TESTS_MAIN = sql_tests.cpp
|
||||||
|
|
|
@ -131,8 +131,8 @@ comparison_predicate:
|
||||||
|
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
column_name { $$ = new Expr($1); }
|
column_name { $$ = makeColumnRef($1); }
|
||||||
| NAME '(' column_name ')' { $$ = new Expr($3, $1); }
|
| NAME '(' expr ')' { $$ = makeFunctionRef($1, $3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
#include "Expr.h"
|
||||||
|
|
||||||
|
|
||||||
|
Expr* makeColumnRef(char* name) {
|
||||||
|
ALLOC_EXPR(e, eExprColumnRef);
|
||||||
|
e->name = name;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Expr* makeFunctionRef(char* func_name, Expr* expr) {
|
||||||
|
ALLOC_EXPR(e, eExprFunctionRef);
|
||||||
|
e->name = func_name;
|
||||||
|
return e;
|
||||||
|
}
|
|
@ -3,15 +3,34 @@
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
eExprLiteralFloat,
|
||||||
|
eExprLiteralString,
|
||||||
|
eExprColumnRef,
|
||||||
|
eExprFunctionRef,
|
||||||
|
eExprPredicate
|
||||||
|
} EExprType;
|
||||||
|
|
||||||
class Expr {
|
|
||||||
public:
|
struct Expr {
|
||||||
Expr(char* name) : name(name), func_name(NULL) {};
|
EExprType type;
|
||||||
Expr(char* name, char* func_name) : name(name), func_name(func_name) {};
|
|
||||||
|
|
||||||
char* name;
|
char* name;
|
||||||
char* func_name;
|
Expr* expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Zero initializes an Expr object and assigns it to a space in the heap
|
||||||
|
#define ALLOC_EXPR(var, type) \
|
||||||
|
Expr* var; \
|
||||||
|
do { \
|
||||||
|
Expr zero = {type}; \
|
||||||
|
var = (Expr*)malloc(sizeof *var); \
|
||||||
|
*var = zero; \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
|
||||||
|
Expr* makeColumnRef(char* name);
|
||||||
|
Expr* makeFunctionRef(char* func_name, Expr* expr);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -75,12 +75,10 @@ void SelectTest3() {
|
||||||
|
|
||||||
ASSERT(select->_select_list->size() == 2);
|
ASSERT(select->_select_list->size() == 2);
|
||||||
|
|
||||||
ASSERT(select->_select_list->at(0)->func_name == NULL);
|
ASSERT(select->_select_list->at(0)->type == eExprColumnRef);
|
||||||
|
ASSERT(select->_select_list->at(1)->type == eExprFunctionRef);
|
||||||
ASSERT_STR("name", select->_select_list->at(0)->name);
|
ASSERT_STR("name", select->_select_list->at(0)->name);
|
||||||
|
|
||||||
ASSERT(select->_select_list->at(1)->func_name != NULL);
|
|
||||||
ASSERT_STR("age", select->_select_list->at(1)->name);
|
|
||||||
ASSERT_STR("AVG", select->_select_list->at(1)->func_name);
|
|
||||||
|
|
||||||
ASSERT(select->_group_by != NULL);
|
ASSERT(select->_group_by != NULL);
|
||||||
ASSERT(select->_group_by->size() == 1);
|
ASSERT(select->_group_by->size() == 1);
|
||||||
|
|
Loading…
Reference in New Issue