myuan преди 2 години
родител
ревизия
00d4ee789a
променени са 2 файла, в които са добавени 47 реда и са изтрити 5 реда
  1. 41 5
      src/calc.y
  2. 6 0
      xmake.lua

+ 41 - 5
src/calc.y

@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <cjson/cJSON.h>
 
 
 extern int yylex();
@@ -13,6 +14,8 @@ extern char*  yytext;
 int yydebug=1;
 
 void yyerror(const char* s);
+
+char* catstr(char* s1, char* s2);
 %}
 
 %union {
@@ -31,32 +34,58 @@ void yyerror(const char* s);
 
 %token QUIT NEWLINE
 
+%type <sv> IDENTIFIER data_type create_definition
+
 %start statement
 
 %%
 
 statement: NEWLINE
 	| QUIT {printf("bye!\n"); exit(0); }
-	| sql_statement { printf("%s\n", yytext); exit(0);}
+	| sql_statement 
 	
 	
 ;
 
-sql_statement: create_statement
+sql_statement: create_table_stmt 
+
+create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
+	{
+		printf("CREATE TABLE %s\n", $3);
+	}
 
-create_statement: CREATE TABLE IDENTIFIER '(' ')' NEWLINE
+create_table_stmt: CREATE TABLE IDENTIFIER '(' create_definition ')' NEWLINE
 	{
-		printf("CREATE TABLE %s\n", yytext);
+		printf("CREATE TABLE %s %s\n", $3, $5);
 	}
 
+create_col_list: create_definition
+    | create_col_list ',' create_definition
+    ;
+
+create_definition: IDENTIFIER data_type;
+
+
+data_type: INT
+	| FLOAT
+	| STRING
+;
 %%
 
 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));
+
 	if (ac != 2) {
 		printf("Usage: %s <sql>\n", av[0]);
 		exit(1);
 	}
-	
+
 	// yyin = av[1];
 	yy_scan_string(av[1]);
 
@@ -69,3 +98,10 @@ void yyerror(const char *s) {
 	exit(1);
 }
 
+char* catstr(char* s1, char* s2) {
+	char* s = malloc(strlen(s1)+strlen(s2)+1);
+	strcpy(s, s1);
+	strcat(s, s2);
+	return s;
+}
+

+ 6 - 0
xmake.lua

@@ -1,10 +1,16 @@
 add_rules("mode.debug", "mode.release")
+add_requires("cjson")
 
 target("sql-parser")
+
     set_kind("binary")
+    add_packages("cjson")
+
     add_rules("lex", "yacc")
     add_files("src/*.l", "src/*.y")
 
+    add_ldflags("-L/home/myuan/.xmake/packages/c/cjson/1.7.15/385bd0d4d94d4d20b56a4adc35851b4c/lib/ -lcjson")
+
 --
 -- If you want to known more usage about xmake, please see https://xmake.io
 --