added int type to lexer

This commit is contained in:
Pedro 2014-10-27 13:54:16 +01:00
parent e177b8cef7
commit b11e52bd6e
6 changed files with 50 additions and 27 deletions

View File

@ -59,9 +59,10 @@ typedef void* yyscan_t;
** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html) ** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html)
*********************************/ *********************************/
%union { %union {
float number; double fval;
uint uintnum; int64_t ival;
char* sval; char* sval;
uint uintnum;
Statement* statement; Statement* statement;
SelectStatement* select_statement; SelectStatement* select_statement;
@ -83,7 +84,8 @@ typedef void* yyscan_t;
%token CREATE TABLE DATABASE INDEX %token CREATE TABLE DATABASE INDEX
%token AS NOT AND OR NULL LIKE %token AS NOT AND OR NULL LIKE
%token <sval> NAME STRING COMPARISON %token <sval> NAME STRING COMPARISON
%token <number> FLOAT %token <fval> FLOAT
%token <ival> INT
%token <uintnum> EQUALS NOTEQUALS LESS GREATER LESSEQ GREATEREQ %token <uintnum> EQUALS NOTEQUALS LESS GREATER LESSEQ GREATEREQ
/********************************* /*********************************
@ -245,8 +247,9 @@ column_name:
; ;
literal: literal:
STRING { $$ = makeStringLiteral($1); } STRING { $$ = Expr::makeLiteral($1); }
| FLOAT { $$ = makeFloatLiteral($1); } | FLOAT { $$ = Expr::makeLiteral($1); }
| INT { $$ = Expr::makeLiteral($1); }
; ;
star_expr: star_expr:

View File

@ -68,13 +68,16 @@ OR TOKEN(OR)
[-+*/(),.;] { return yytext[0]; } [-+*/(),.;] { return yytext[0]; }
[0-9]+ |
[0-9]+"."[0-9]* | [0-9]+"."[0-9]* |
"."[0-9]* { "."[0-9]* {
yylval->number = atof(yytext); yylval->fval = atof(yytext);
return SQL_FLOAT; return SQL_FLOAT;
} }
[0-9]+ {
yylval->ival = atol(yytext);
return SQL_INT;
}
[A-Za-z][A-Za-z0-9_]* { [A-Za-z][A-Za-z0-9_]* {
yylval->sval = strdup(yytext); yylval->sval = strdup(yytext);

View File

@ -7,4 +7,4 @@ make clean
make execution make execution
./bin/sql_execution "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3" ./bin/sql_execution "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3"
./bin/sql_execution "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" ./bin/sql_execution "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1;"
./bin/sql_execution "SELECT * from table WHERE NOT a AND a = 12.5" ./bin/sql_execution "SELECT * from table WHERE (b OR NOT a) AND a = 12.5"

View File

@ -24,18 +24,6 @@ Expr* makeFunctionRef(char* func_name, Expr* expr) {
return e; return e;
} }
Expr* makeFloatLiteral(float value) {
ALLOC_EXPR(e, kExprLiteralFloat);
e->float_literal = value;
return e;
}
Expr* makeStringLiteral(char* string) {
ALLOC_EXPR(e, kExprLiteralString);
e->name = substr(string, 1, strlen(string)-1);
delete string;
return e;
}
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
@ -46,6 +34,8 @@ Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
return e; return e;
} }
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
ALLOC_EXPR(e, kExprOperator); ALLOC_EXPR(e, kExprOperator);
e->op_type = op; e->op_type = op;
@ -63,3 +53,25 @@ Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) {
e->expr2 = expr2; e->expr2 = expr2;
return e; return e;
} }
Expr* Expr::makeLiteral(int64_t val) {
ALLOC_EXPR(e, kExprLiteralInt);
e->ival = val;
return e;
}
Expr* Expr::makeLiteral(double value) {
ALLOC_EXPR(e, kExprLiteralFloat);
e->float_literal = value;
return e;
}
Expr* Expr::makeLiteral(char* string) {
ALLOC_EXPR(e, kExprLiteralString);
e->name = substr(string, 1, strlen(string)-1);
delete string;
return e;
}

View File

@ -8,6 +8,7 @@
typedef enum { typedef enum {
kExprLiteralFloat, kExprLiteralFloat,
kExprLiteralString, kExprLiteralString,
kExprLiteralInt,
kExprStar, kExprStar,
kExprColumnRef, kExprColumnRef,
kExprFunctionRef, kExprFunctionRef,
@ -48,6 +49,7 @@ struct Expr {
Expr* expr2; Expr* expr2;
char* name; char* name;
float float_literal; float float_literal;
int64_t ival;
OperatorType op_type; OperatorType op_type;
char op_char; char op_char;
@ -55,6 +57,10 @@ struct Expr {
static Expr* makeOpUnary(OperatorType op, Expr* expr); static Expr* makeOpUnary(OperatorType op, Expr* expr);
static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2);
static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2);
static Expr* makeLiteral(int64_t val);
static Expr* makeLiteral(double val);
static Expr* makeLiteral(char* val);
}; };
// Zero initializes an Expr object and assigns it to a space in the heap // Zero initializes an Expr object and assigns it to a space in the heap
@ -70,8 +76,5 @@ struct Expr {
Expr* makeColumnRef(char* name); Expr* makeColumnRef(char* name);
Expr* makeFunctionRef(char* func_name, Expr* expr); Expr* makeFunctionRef(char* func_name, Expr* expr);
Expr* makeFloatLiteral(float value);
Expr* makeStringLiteral(char* string);
#endif #endif

View File

@ -7,10 +7,11 @@ void printExpression(Expr* expr, uint num_indent);
void printOperatorExpression(Expr* expr, uint num_indent); void printOperatorExpression(Expr* expr, uint num_indent);
const char* indent(uint num_indent) { return std::string(num_indent, '\t').c_str(); } const char* indent(uint num_indent) { return std::string(num_indent, '\t').c_str(); }
void inprint(int val, uint num_indent) { printf("%s%d\n", indent(num_indent), val); } void inprint(int64_t val, uint num_indent) { printf("%s%ld \n", indent(num_indent), val); }
void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent), val); } void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent), val); }
void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent), val); } void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent), val); }
void inprint(char val, uint num_indent) { printf("%s%c\n", indent(num_indent), val); } void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent), val); }
void inprintU(uint64_t val, uint num_indent) { printf("%s%lu\n", indent(num_indent), val); }
void printTableRefInfo(TableRef* table, uint num_indent) { void printTableRefInfo(TableRef* table, uint num_indent) {
switch (table->type) { switch (table->type) {
@ -30,11 +31,11 @@ void printOperatorExpression(Expr* expr, uint num_indent) {
if (expr == NULL) { inprint("null", num_indent); return; } if (expr == NULL) { inprint("null", num_indent); return; }
switch (expr->op_type) { switch (expr->op_type) {
case SIMPLE_OP: inprint(expr->op_char, num_indent); break; case SIMPLE_OP: inprintC(expr->op_char, num_indent); break;
case AND: inprint("AND", num_indent); break; case AND: inprint("AND", num_indent); break;
case OR: inprint("OR", num_indent); break; case OR: inprint("OR", num_indent); break;
case NOT: inprint("NOT", num_indent); break; case NOT: inprint("NOT", num_indent); break;
default: inprint(expr->op_type, num_indent); break; default: inprintU(expr->op_type, num_indent); break;
} }
printExpression(expr->expr, num_indent+1); printExpression(expr->expr, num_indent+1);
if (expr->expr2 != NULL) printExpression(expr->expr2, num_indent+1); if (expr->expr2 != NULL) printExpression(expr->expr2, num_indent+1);
@ -45,6 +46,7 @@ void printExpression(Expr* expr, uint num_indent) {
case kExprStar: inprint("*", num_indent); break; case kExprStar: inprint("*", num_indent); break;
case kExprColumnRef: inprint(expr->name, num_indent); break; case kExprColumnRef: inprint(expr->name, num_indent); break;
case kExprLiteralFloat: inprint(expr->float_literal, num_indent); break; case kExprLiteralFloat: inprint(expr->float_literal, num_indent); break;
case kExprLiteralInt: inprint(expr->ival, num_indent); break;
case kExprLiteralString: inprint(expr->name, num_indent); break; case kExprLiteralString: inprint(expr->name, num_indent); break;
case kExprFunctionRef: /* todo */ break; case kExprFunctionRef: /* todo */ break;
case kExprOperator: printOperatorExpression(expr, num_indent); break; case kExprOperator: printOperatorExpression(expr, num_indent); break;