Browse Source

添加插入

myuan 2 years ago
parent
commit
ae6269b721
2 changed files with 66 additions and 11 deletions
  1. 6 5
      src/calc.l
  2. 60 6
      src/calc.y

+ 6 - 5
src/calc.l

@@ -18,8 +18,9 @@ extern YYSTYPE yylval;
 %%
 
 [ \t\n]	;
-[0-9]+\.[0-9]+ 	{yylval.fv = atof(yytext); return FLOAT;}
-[0-9]+		    {yylval.iv = atoi(yytext); return INT;}
+[0-9]+\.[0-9]+ 	{yylval.fv = atof(yytext); return FLOAT_V;}
+[0-9]+		    {yylval.iv = atoi(yytext); return INT_V;}
+['][a-zA-Z][a-zA-Z0-9]*[']	    {cp_yylval_and_return(STRING_V);}
 
 
 "CREATE"	{return CREATE;}
@@ -35,9 +36,9 @@ extern YYSTYPE yylval;
 "JOIN"      {return JOIN;}
 "TABLE"		{return TABLE;}
 
-"INT"       {cp_yylval_and_return(INT);}
-"FLOAT"     {cp_yylval_and_return(FLOAT);}
-"STRING"    {cp_yylval_and_return(STRING);}
+"INT"       {cp_yylval_and_return(INT_T);}
+"FLOAT"     {cp_yylval_and_return(FLOAT_T);}
+"STRING"    {cp_yylval_and_return(STRING_T);}
 
 
 ;		    {return NEWLINE;}

+ 60 - 6
src/calc.y

@@ -31,14 +31,20 @@ cJSON* jroot;
 
 %token SELECT FROM WHERE INSERT INTO VALUES DELETE UPDATE SET JOIN CREATE TABLE 
 %token AND OR NOT IN 
-%token INT FLOAT STRING
+%token INT_V FLOAT_V STRING_V // 作为 value 出现的
+%token INT_T FLOAT_T STRING_T // 作为 字面量 type 出现的情况
+
 %token LPAREN RPAREN COMMA
 %token PRIMARY_KEY
 
 %token QUIT NEWLINE
 
+%type <iv> INT_V
+%type <fv> FLOAT_V
+%type <sv> STRING_V
 %type <sv> IDENTIFIER data_type PRIMARY_KEY col_options
-%type <jv> create_definition create_col_list create_table_stmt
+%type <jv> create_definition create_col_list create_table_stmt data_value
+%type <jv> insert_stmt insert_list 
 
 %start statement
 
@@ -49,7 +55,9 @@ statement: NEWLINE
 	| sql_statement 
 ;
 
-sql_statement: create_table_stmt 
+sql_statement: create_table_stmt
+	| insert_stmt
+;
 
 create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
 	{
@@ -106,10 +114,56 @@ col_options: {$$ = "";}
 		$$ = $1;
 	}
 
-data_type: INT
-	| FLOAT
-	| STRING
+data_type: INT_T
+	| FLOAT_T
+	| STRING_T
+;
+
+insert_stmt: INSERT INTO IDENTIFIER VALUES '(' insert_list ')' NEWLINE {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "insert");
+		cJSON_AddStringToObject(node, "table_name", $3);
+		cJSON_AddItemToObject(node, "values", $6);
+		cJSON_AddItemToArray(jroot, node);
+		$$=node;
+};
+
+insert_list: data_value {
+	cJSON* node = cJSON_CreateArray();
+	cJSON_AddItemToArray(node, $1);
+	$$=node;
+	// printf("得create_definition %s\n", cJSON_Print($1));
+}
+    |  insert_list ',' data_value {
+		// printf("迭代 create_col_list\n %s", cJSON_Print($1));
+		// printf("迭代 create_definition\n %s", cJSON_Print($3));
+
+		cJSON_AddItemToArray($1, $3);
+
+		$$=$1;
+		// printf("迭代\n %s", cJSON_Print($$));
+	}
 ;
+data_value: INT_V {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "int");
+		cJSON_AddNumberToObject(node, "value", $1);
+		$$=node;
+	}
+	| FLOAT_V {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "float");
+		cJSON_AddNumberToObject(node, "value", $1);
+		$$=node;
+	}
+	| STRING_V {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "string");
+		cJSON_AddStringToObject(node, "value", $1);
+		$$=node;
+	}
+;
+
 %%
 
 int main(int ac, char** av) {