myuan преди 2 години
родител
ревизия
008926eda5
променени са 1 файла, в които са добавени 22 реда и са изтрити 46 реда
  1. 22 46
      src/calc.y

+ 22 - 46
src/calc.y

@@ -42,7 +42,10 @@ cJSON* jroot;
 	cJSON_Add##json_type_name##ToObject(node, "value", value); \
 	res = node;
 
-#define MEET(msg) fprintf(stderr, "MEET: %s\n", #msg);
+#define MEET(msg) fprintf(stderr, "MEET %s\n", #msg);
+#define MEET_VAR(msg, msg2) fprintf(stderr, "MEET %s %s\n", #msg, msg2);
+#define MEET_JSON(msg, msg2) MEET_VAR(msg, cJSON_Print(msg2));
+
 %}
 
 %union {
@@ -73,10 +76,10 @@ cJSON* jroot;
 %type <jv> create_definition create_col_list create_table_stmt data_value
 %type <jv> insert_stmt insert_list 
 %type <jv> update_stmt update_list single_assign_item
-%type <jv> where_condition_item where_conditions identifier identifier_or_const_value the_whole_where_smt 
+%type <jv> where_condition_item identifier identifier_or_const_value the_whole_where_smt 
 %type <jv> delete_stmt select_stmt select_item select_items 
 %type <jv> data_value_list identifier_or_const_value_or_const_value_list
-%type <jv> search_expr compare_expr single_expr expr where_expr logical_expr negative_expr op_where_expr
+%type <jv> search_expr compare_expr single_expr expr where_expr logical_expr negative_expr op_where_expr expr_list
 
 
 // %left OR
@@ -239,7 +242,10 @@ single_expr: identifier {$$=$1;}
 	| data_value {$$=$1;}
 	| '(' single_expr ')' {$$=$2;}
 ;
-where_expr: logical_expr {$$=$1;};
+where_expr: logical_expr {
+		MEET(logical_expr from where_expr);
+		$$=$1;
+	}
 ;
 op_where_expr: {$$=cJSON_CreateObject();} 
 	| WHERE where_expr {$$=$2;}
@@ -247,7 +253,7 @@ op_where_expr: {$$=cJSON_CreateObject();}
 compare_expr: compare_expr bin_cmp_op compare_expr {
 	fprintf(stderr, "compare_expr %s\n", $2);
 	SIMPLE_OP_NODE($$, $2, $1, $3);}
-	| single_expr {MEET(single_expr from cmp) $$=$1;}
+	| single_expr {MEET_JSON(single_expr from cmp, $1); $$=$1;}
 	| where_expr {MEET(where_expr from cmp) $$=$1;}
 
 ;
@@ -282,52 +288,22 @@ logical_expr: logical_expr bin_logical_op negative_expr {
 // // expr: expr bin_logical_op expr {SIMPLE_OP_NODE($$, $2, $1, $3)}
 
 
-// expr_list: expr{
-// 		cJSON* node = cJSON_CreateArray();
-// 		cJSON_AddItemToArray(node, $1);
-// 		$$=node;
-// 	}
-// 	| expr_list ',' expr {
-// 		fprintf(stderr, "迭代 expr_list\n %s", cJSON_Print($1));
-// 		cJSON_AddItemToArray($1, $3);
-// 		$$=$1;
-// 	}
-
-// expr: expr bin_contains_op '(' expr_list ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
-// 	| expr bin_contains_op '(' select_stmt ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
-
-
-where_conditions: where_condition_item {
-		$$=$1;
-	}
-	| '(' where_condition_item ')' {
-		$$=$2;
-	}
-
-	|  where_conditions bin_logical_op where_condition_item {
-		cJSON* node = cJSON_CreateObject();
-		cJSON_AddStringToObject(node, "type", $2);
+expr_list: where_expr {
+		MEET(where_expr from expr_list);
 
-		cJSON_AddItemToObject(node, "left", $1);
-		cJSON_AddItemToObject(node, "right", $3);
+		cJSON* node = cJSON_CreateArray();
+		cJSON_AddItemToArray(node, $1);
 		$$=node;
 	}
-	| unary_compare_op where_condition_item {
-		cJSON* node = cJSON_CreateObject();
-		cJSON_AddStringToObject(node, "type", $1);
-		cJSON_AddItemToObject(node, "right", $2);
-		$$=node;
+	| expr_list ',' where_expr {
+		fprintf(stderr, "迭代 expr_list\n %s", cJSON_Print($1));
+		cJSON_AddItemToArray($1, $3);
+		$$=$1;
 	}
-	|  where_conditions bin_logical_op '(' where_conditions ')' {
-		cJSON* node = cJSON_CreateObject();
-		cJSON_AddStringToObject(node, "type", $2);
 
-		cJSON_AddItemToObject(node, "left", $1);
-		cJSON_AddItemToObject(node, "right", $4);
-		$$=node;
-	}
+where_expr: where_expr bin_contains_op '(' expr_list ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
+	| where_expr bin_contains_op '(' select_stmt ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
 
-;
 
 identifier: IDENTIFIER {
 		cJSON* node = cJSON_CreateObject();
@@ -394,7 +370,7 @@ delete_stmt: DELETE FROM IDENTIFIER op_where_expr NEWLINE {
 		$$=node;
 };
 
-select_stmt: SELECT select_items FROM IDENTIFIER the_whole_where_smt NEWLINE {
+select_stmt: SELECT select_items FROM IDENTIFIER op_where_expr NEWLINE {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", "select");
 		cJSON_AddItemToObject(node, "select_cols", $2);