Parcourir la source

基础的create

myuan il y a 2 ans
Parent
commit
5161fb6e01
1 fichiers modifiés avec 43 ajouts et 21 suppressions
  1. 43 21
      src/calc.y

+ 43 - 21
src/calc.y

@@ -16,12 +16,14 @@ int yydebug=1;
 void yyerror(const char* s);
 
 char* catstr(char* s1, char* s2);
+cJSON* jroot;
 %}
 
 %union {
 	int iv;
 	double fv;
 	char* sv;
+	cJSON* jv;
 	int subtok;
 }
 
@@ -34,7 +36,8 @@ char* catstr(char* s1, char* s2);
 
 %token QUIT NEWLINE
 
-%type <sv> IDENTIFIER data_type create_definition
+%type <sv> IDENTIFIER data_type
+%type <jv> create_definition create_col_list create_table_stmt
 
 %start statement
 
@@ -43,27 +46,52 @@ char* catstr(char* s1, char* s2);
 statement: NEWLINE
 	| QUIT {printf("bye!\n"); exit(0); }
 	| sql_statement 
-	
-	
 ;
 
 sql_statement: create_table_stmt 
 
 create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
 	{
-		printf("CREATE TABLE %s\n", $3);
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "create_table");
+		cJSON_AddStringToObject(node, "table_name", $3);
+		cJSON_AddItemToArray(jroot, node);
 	}
 
-create_table_stmt: CREATE TABLE IDENTIFIER '(' create_definition ')' NEWLINE
-	{
-		printf("CREATE TABLE %s %s\n", $3, $5);
-	}
+create_table_stmt: CREATE TABLE IDENTIFIER '(' create_col_list ')' NEWLINE {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "create_table");
+		cJSON_AddStringToObject(node, "table_name", $3);
+		cJSON_AddItemToObject(node, "cols", $5);
+		cJSON_AddItemToArray(jroot, node);
+		$$=node;
+};
+
+create_col_list: create_definition {
+	cJSON* node = cJSON_CreateArray();
+	cJSON_AddItemToArray(node, $1);
+	$$=node;
+	// printf("得create_definition %s\n", cJSON_Print($1));
+}
+    |  create_col_list ',' create_definition {
+		// printf("迭代 create_col_list\n %s", cJSON_Print($1));
+		// printf("迭代 create_definition\n %s", cJSON_Print($3));
+
+		cJSON_AddItemToArray($1, $3);
 
-create_col_list: create_definition
-    | create_col_list ',' create_definition
+		$$=$1;
+		// printf("迭代\n %s", cJSON_Print($$));
+	}
     ;
 
-create_definition: IDENTIFIER data_type;
+create_definition: IDENTIFIER data_type {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "create_column");
+		cJSON_AddStringToObject(node, "column_name", $1);
+		cJSON_AddStringToObject(node, "data_type", $2);
+		$$ = node;
+		printf("得列 %s %s\n", $1, $2);
+};
 
 
 data_type: INT
@@ -73,13 +101,7 @@ data_type: INT
 %%
 
 int main(int ac, char** av) {
-	cJSON *monitor = cJSON_CreateObject();
-    char* name = cJSON_CreateString("Awesome 4K");
-    cJSON_AddItemToObject(monitor, "name", name);
-
-    cJSON* resolutions = cJSON_CreateArray();
-    cJSON_AddItemToObject(monitor, "resolutions", resolutions);
-	printf("%s\n", cJSON_Print(monitor));
+	jroot = cJSON_CreateArray();
 
 	if (ac != 2) {
 		printf("Usage: %s <sql>\n", av[0]);
@@ -88,9 +110,9 @@ int main(int ac, char** av) {
 
 	// yyin = av[1];
 	yy_scan_string(av[1]);
-
-	return yyparse();
-
+	int res = yyparse();
+	printf("%s\n", cJSON_Print(jroot));
+	return res;
 }
 
 void yyerror(const char *s) {