Browse Source

GWARF实现基础结构以加法运算为demo

SongZihuan 5 years ago
parent
commit
dc9a06df2f
15 changed files with 746 additions and 189 deletions
  1. 29 0
      .vscode/launch.json
  2. 34 0
      .vscode/settings.json
  3. BIN
      gwarf
  4. 8 0
      gwarf.c
  5. 0 0
      gwarf.h
  6. 110 0
      gwarf_interpreter/interprete.h
  7. BIN
      gwarf_interpreter/interpreter
  8. 191 0
      gwarf_interpreter/interpreter.c
  9. 0 80
      gwarf_yacc.y
  10. 207 0
      include/var_linked.c
  11. 1 1
      paser/gwarf_lex.l
  12. 74 0
      paser/gwarf_yacc.y
  13. 33 35
      paser/lex.yy.c
  14. 56 71
      paser/y.tab.c
  15. 3 2
      paser/y.tab.h

+ 29 - 0
.vscode/launch.json

@@ -0,0 +1,29 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "gcc - 生成和调试活动文件",
+            "type": "cppdbg",
+            "request": "launch",
+            "program": "${fileDirname}/${fileBasenameNoExtension}",
+            "args": [],
+            "stopAtEntry": false,
+            "cwd": "${workspaceFolder}",
+            "environment": [],
+            "externalConsole": false,
+            "MIMode": "gdb",
+            "setupCommands": [
+                {
+                    "description": "为 gdb 启用整齐打印",
+                    "text": "-enable-pretty-printing",
+                    "ignoreFailures": true
+                }
+            ],
+            "preLaunchTask": "gcc build active file",
+            "miDebuggerPath": "/usr/bin/gdb"
+        }
+    ]
+}

+ 34 - 0
.vscode/settings.json

@@ -0,0 +1,34 @@
+{
+    "files.associations": {
+        "y.tab.h": "c",
+        "stdio.h": "c",
+        "unordered_map": "c",
+        "unordered_set": "c",
+        "*.tcc": "c",
+        "string": "c",
+        "typeinfo": "c",
+        "stdlib.h": "c",
+        "system_error": "c",
+        "bit": "c",
+        "condition_variable": "c",
+        "cstddef": "c",
+        "list": "c",
+        "limits": "c",
+        "memory": "c",
+        "new": "c",
+        "functional": "c",
+        "array": "c",
+        "chrono": "c",
+        "istream": "c",
+        "ostream": "c",
+        "ratio": "c",
+        "tuple": "c",
+        "type_traits": "c",
+        "utility": "c",
+        "variant": "c",
+        "optional": "c",
+        "fstream": "c",
+        "streambuf": "c",
+        "interprete.h": "c"
+    }
+}

BIN
gwarf


+ 8 - 0
gwarf.c

@@ -1,5 +1,13 @@
 #include<stdio.h>
 #include<stdio.h>
+#include"gwarf_interpreter/interpreter.c"
+
 
 
 int main(){
 int main(){
+    global_inter = get_inter();  // 拿全局解释器[并声明全局变量]
+    parser();
+    var_list *the_var;
+    printf("----start run----\n");
+    traverse(global_inter->global_code, the_var);
+    printf("code end...\n");
     return 0;
     return 0;
 }
 }

+ 0 - 0
gwarf.h


+ 110 - 0
gwarf_interpreter/interprete.h

@@ -0,0 +1,110 @@
+
+// the type of data(GWARF_value)
+typedef enum{
+    NUMBER_value = 1,
+    STRING_value,
+} GWARF_value_type;
+
+// all value is GWARF_value
+typedef struct GWARF_value{
+    GWARF_value_type type;
+    union
+    {
+        double double_value;  // NUMBER
+        char *string;  // STRING
+    } value;
+    
+} GWARF_value;
+
+// ------------------------- var
+
+typedef struct var{
+    char *name;  // var name
+    GWARF_value value;
+    struct var *next;  // for list
+} var;
+
+// ------------------------- statement
+
+typedef struct statement{
+    enum statement_type{
+        start=1,  // for base statement
+        operation,  // such as + - * /
+        base_var,  // return var address
+        base_value,  // return an number or number
+        while_cycle,  // while
+    } type;  // the statement type
+
+    union
+    {
+        struct{
+            enum{
+                ADD_func = 1,  // +
+                SUB_func,  // -
+                DIV_func,  // /
+                MUL_func,  // *
+                ASSIGMENT_func, // =
+            } type;
+            struct statement *right_exp;  // the right exp
+            struct statement *left_exp;  // the left exp
+        } operation;
+
+        struct{
+            char *var_name;  // return var
+        } base_var;
+
+        struct{
+            GWARF_value value;  // return value
+        } base_value;
+    } code;
+    struct statement *next;
+} statement;
+
+// ------------------------- result value
+
+typedef struct GWARF_result{
+    GWARF_value value;
+    enum{
+        return_def=1,
+        break_while,
+    } u;  // the result type[from where]
+} GWARF_result;
+
+// ------------------------- var base list [记录每一层变量base的链表]
+
+typedef struct var_list{
+    var *var_base;
+    var *next;
+} var_list;
+
+// ------------------------- inter
+
+typedef struct{
+    var *global_var;  // global var链表
+    statement *global_code;  // global code链表
+} inter;
+
+//------- var func
+var *make_var();
+void append_var(char *, GWARF_value , var *);
+void free_var(var *);
+var *get_var(char *, var *);
+void del_var(char *, var *);
+
+//------- statement func
+statement *make_statement();
+statement *append_statement(statement *, statement*);
+
+//------- run func
+GWARF_result traverse(statement *, var_list *);
+
+//------- inter func
+inter *get_inter();
+
+// //------ paser func
+int yyerror(char const *);
+FILE *yyin;
+char *yytext;
+
+// main
+inter *global_inter;

BIN
gwarf_interpreter/interpreter


+ 191 - 0
gwarf_interpreter/interpreter.c

@@ -0,0 +1,191 @@
+#include <stdio.h>
+#include <stdlib.h>
+// #include "interprete.h"
+#include "../paser/y.tab.c"
+
+// running code
+GWARF_result operation_func(statement *, var_list *);
+GWARF_result add_func(GWARF_result, GWARF_result, var_list *);
+
+// ------------------------- var func
+
+var *make_var(){  // make var with base
+    var *tmp;
+    tmp = malloc(sizeof(var));  // get an address for base var
+    tmp->name = "";  // can't get the name for the real var
+    tmp->next = NULL;
+    return tmp;
+}
+
+void append_var(char *name, GWARF_value value, var *base_var){
+    int break_ = 1;  // get var[2] or not[1]
+    var *tmp = base_var;  // iter var
+    while(1){
+        if (tmp->name == name){
+            break_ = 2;
+            break;
+        }
+        if (tmp->next == NULL){  // not var name *name
+            break_ = 1;
+            break;
+        }
+        tmp = tmp->next;  // get the next to iter
+    }
+    if(break_ == 2){
+        tmp->value = value;
+        return;
+    }
+    var *new_tmp;
+    new_tmp = make_var();  // make a new var
+    tmp->next = new_tmp;
+    new_tmp->name = name;
+    new_tmp->value = value;
+}
+
+void free_var(var *base_var){  // free the address
+    var *tmp = base_var;  // iter var
+    while(1){
+        if (tmp->next == NULL){  // the last
+            free(tmp);
+            break;
+        }
+        var *tmp_2 = tmp;
+        tmp = tmp->next;
+        free(tmp_2);
+    }
+}
+
+var *get_var(char *name, var *base_var){  // get the address
+    var *tmp = base_var;  // iter var
+    while(1){
+        if (tmp->name == name){
+            return tmp;
+        }
+        if (tmp->next == NULL){  // not var name *name
+            return NULL;
+        }
+        tmp = tmp->next;  // get the next to iter
+    }
+}
+
+void del_var(char *name, var *base_var){  // free an address
+    var *tmp = base_var, *last_tmp=NULL;  // iter var
+    while(1){
+        if (tmp->name == name){
+            if(last_tmp != NULL){
+                last_tmp->next = tmp->next;  // if tmp->next is NULL last_tmp->next is NULL too
+            }
+            free(tmp);
+            return;
+        }
+        if (tmp->next == NULL){  // not var name *name
+            return;
+        }
+        tmp = tmp->next;  // get the next to iter
+        last_tmp = tmp;
+    }
+}
+
+// ------------------------- statement list
+
+statement *make_statement(){  // make statement
+    statement *tmp;
+    tmp = malloc(sizeof(statement));  // get an address for base var
+    tmp->next = NULL;
+    tmp->type = start;
+    return tmp;
+}
+
+statement *append_statement(statement *base_statement, statement *new_tmp){  // make statement next
+    statement *tmp = base_statement;  // iter var
+    while(1){
+        if (tmp->next == NULL){  // not var name *name
+            break;
+        }
+        tmp = tmp->next;  // get the next to iter
+    }
+    tmp->next = new_tmp;
+    return new_tmp;
+}
+
+// ------------------------- run code
+
+GWARF_result read_statement_list(statement *the_statement, var_list *the_var){  // read the statement list with case to run by func
+    GWARF_result return_value;
+    switch (the_statement->type)
+    {
+        case operation:  // 表达式运算
+            return_value = operation_func(the_statement, the_var);
+            printf("operation value = %f\n", return_value.value.value.double_value);
+            break;
+        case 4:
+            return_value.value = (the_statement->code).base_value.value;  // code
+            break;
+        default:
+            return_value.value.value.double_value = 10086;
+            break;
+    }
+    return return_value;
+}
+
+GWARF_result operation_func(statement *the_statement, var_list *the_var){  // read the statement list with case to run by func
+    GWARF_result value, left_result, right_result;
+    left_result = traverse((*the_statement).code.operation.left_exp, the_var);
+    right_result = traverse((*the_statement).code.operation.right_exp, the_var);
+    switch (the_statement->code.operation.type)  // 获取运算类型
+    {
+        case ADD_func:
+            value = add_func(left_result, right_result, the_var);
+            break;
+        default:
+            break;
+    }
+    return value;
+}
+
+GWARF_result add_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){  // the func for add and call from read_statement_list
+    GWARF_result return_value;  // the result by call read_statement_list with left and right; value is the result for add
+    
+    // 计算左右表达式的值
+    // void traverse(statement *the_statement, var_list *the_var)
+
+    if((left_result.value.type = NUMBER_value) && (right_result.value.type = NUMBER_value)){  // all is NUMBER
+        return_value.u = return_def;
+        return_value.value.type = NUMBER_value;
+        return_value.value.value.double_value = left_result.value.value.double_value + right_result.value.value.double_value;  // 数值相加运算
+    }
+
+    return return_value;
+}
+
+GWARF_result traverse(statement *the_statement, var_list *the_var){  // traverse the statement
+    statement *tmp = the_statement;
+    GWARF_result result;
+    while(1){
+        if(tmp == NULL){
+            break;  // off
+        }
+        result = read_statement_list(tmp, the_var);
+        tmp = tmp->next;
+    }
+    return result;
+}
+
+// -------inter func
+inter *get_inter(){
+    inter *tmp;
+    tmp = malloc(sizeof(inter));  // get an address for base var
+    tmp->global_var = make_var();
+    tmp->global_code = make_statement();
+    return tmp;
+}
+
+// int main(){
+//     global_inter = get_inter();  // 拿全局解释器[并声明全局变量]
+//     parser();
+//     var_list *the_var;
+//     printf("----start run----\n");
+//     traverse(global_inter->global_code, the_var);
+//     printf("code end...\n");
+//     return 0;
+// }

+ 0 - 80
gwarf_yacc.y

@@ -1,80 +0,0 @@
-%{
-    #include<stdio.h>
-%}
-
-%union{
-    int int_value;
-    double double_value;
-    char *string_value;
-}
-%token <double_value> NUMBER
-%token <string_value> STRING VAR
-%token ADD SUB DIV MUL EQ LESS MORE RB LB RP LP WHILE STOP POW
-%type <double_value> base_number second_number first_number
-%%
-command_list
-    : command
-    | command_list command
-    ;
-
-command
-    : top_exp STOP
-    ;
-
-top_exp
-    : first_number
-    {
-        printf("%f\n", $1);
-    }
-    ;
-
-first_number
-    : second_number
-    | first_number ADD second_number
-    {
-        $$ = $1 + $3;
-    }
-    | first_number SUB second_number
-    {
-        $$ = $1 - $3;
-    }
-    ;
-
-second_number
-    : base_number
-    | second_number MUL base_number
-    {
-        $$ = $1 * $3;
-    }
-    | second_number DIV base_number
-    {
-        $$ = $1 / $3;
-    }
-    ;
-
-base_number
-    : NUMBER
-    {
-        $$ = $1;
-    }
-    ;
-
-%%
-extern int yylex (void);
-
-int yyerror(char const *str)
-{
-    extern char *yytext;
-    fprintf(stderr, "parser error near %s\n", yytext);
-    return 0;
-}
-
-int main(void)
-{
-    extern int yyparse(void);
-    extern FILE *yyin;
-
-    yyin = stdin;
-    yyparse();
-    return 0;
-}

+ 207 - 0
include/var_linked.c

@@ -0,0 +1,207 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define true 1
+#define false 0
+
+typedef enum node_type{
+    REAL = 1,
+    INT,
+    STRING,
+    OBJECT,
+    FIRST,
+} node_type;
+
+
+typedef union
+    {
+        double double_value;
+        int int_value;
+        char *string_value;
+        void *object_value;
+    } union_data;
+
+
+typedef struct linked_node
+{
+    struct linked_node *next_link;
+    struct linked_node *self_link;
+    struct linked_node *last_link;
+    char *name;
+    node_type type;
+    union_data data;
+} linked_node;
+
+
+linked_node *make_first_link();
+void append(union_data data,node_type type, char *name, linked_node *first);
+linked_node get_data(char *, linked_node *);
+void free_all(linked_node *);
+void print_all();
+void to_free(void *);
+void del(char *, linked_node *);
+
+
+void del(char *name, linked_node *first){
+    linked_node *last, *next, *self;
+    linked_node *last_link = first;
+    
+    while (true){
+        if (last_link->next_link == NULL){
+            return;
+        }
+        if (last_link->last_link == NULL){
+            last_link = (*last_link).next_link;
+            continue;
+        }
+        else if (last_link->name == name){
+            next = last_link->next_link;
+            last = last_link->last_link;
+            self = last_link;
+            break;
+        }
+        last_link = (*last_link).next_link;
+    }
+
+    last->next_link = next;
+    if(next != NULL){
+        next->last_link = last;
+    }
+    to_free(self);
+}
+
+
+void free_all(linked_node *first){
+    linked_node *next_link = first;
+    while (true){
+        if ((*next_link).next_link == NULL){
+            to_free(next_link);
+            break;
+        }
+        linked_node *tmp = (*next_link).next_link;
+        to_free(next_link);
+        next_link = tmp;
+    }
+}
+
+void to_free(void *address){
+    puts("-------------");
+    printf("free : %u\n", address);
+    free(address);
+}
+
+void print_all(linked_node *first){
+    linked_node *last_link = first;
+    puts("-------------");
+    while (true){
+        if ((*last_link).next_link == NULL){
+            printf("address = %-9u, data = ",last_link);
+            if ((*last_link).type == REAL){
+                printf("%-7f",last_link->data.double_value);
+            }else if ((*last_link).type == INT){
+                printf("%-7d",last_link->data.int_value);
+            }else if ((*last_link).type == STRING){
+                printf("%-7s",last_link->data.string_value);
+            }else if ((*last_link).type == OBJECT){
+                printf("%-7d",last_link->data.object_value);
+            }
+            printf(", name = %-5s\n",(*last_link).name);
+            break;
+        }
+        if (last_link->last_link == NULL){
+            last_link = (*last_link).next_link;
+            continue;
+        }
+        printf("address = %-9u, data = ",last_link);
+        if ((*last_link).type == REAL){
+            printf("%-7f",last_link->data.double_value);
+        }else if ((*last_link).type == INT){
+            printf("%-7d",last_link->data.int_value);
+        }else if ((*last_link).type == STRING){
+            printf("%-7s",last_link->data.string_value);
+        }else if ((*last_link).type == OBJECT){
+            printf("%-7d",last_link->data.object_value);
+        }
+        printf(", name = %-5s\n",(*last_link).name);
+        last_link = (*last_link).next_link;
+    }
+}
+
+linked_node get_data(char *name, linked_node *first){
+    linked_node *last_link = first;
+    while (true){
+        if ((*last_link).next_link == NULL){  // 这个必须要在首位[如果链表为空]
+            return (*first);
+        }
+        if ((*last_link).last_link == NULL){
+            last_link = (*last_link).next_link;
+            continue;
+        }
+        if ((*last_link).name == name){
+            return (*last_link);
+        }
+        last_link = (*last_link).next_link;
+    }
+}
+
+void append(union_data data,node_type type, char *name, linked_node *first){
+    linked_node *last_link = first;
+    int break_type = 1;
+    while (true){
+        if (last_link->next_link == NULL){  // 元素不存在
+            if (last_link->name == name){  // 已经存在元素
+                break_type = 2;
+            }
+            else{
+                break_type = 1;
+            }
+            break;
+        }
+        if ((*last_link).last_link == NULL){  // 忽略首元素[不参与下面的匹配(上面得参与,不然会越界)]
+            last_link = (*last_link).next_link;
+            continue;
+        }
+        if (last_link->name == name){  // 已经存在元素
+            break_type = 2;
+            break;
+        }
+        last_link = last_link->next_link;
+    }
+
+    if(break_type == 2){
+        last_link->data = data;
+        return;
+    }
+
+    linked_node *new_link;
+    new_link = malloc(sizeof(linked_node));
+    if (new_link == NULL){
+        return;
+    }
+    (*new_link).last_link = last_link;
+    (*new_link).next_link = NULL;
+    (*new_link).self_link = new_link;
+    (*new_link).data = data;
+    (*new_link).type = type;
+
+    // 设置节点序号
+    (*new_link).name = name;
+
+    // 修改节点信息
+    (*last_link).next_link = new_link;
+}
+
+linked_node *make_first_link(){
+    linked_node *first;
+    first = malloc(sizeof(linked_node));
+    if (first == NULL){
+        return NULL;
+    }
+    (*first).next_link = NULL;
+    (*first).self_link = first;
+    (*first).last_link = NULL;
+    (*first).name = "";
+    (*first).data.int_value = -1;
+    (*first).type = FIRST;
+    return first;
+}

+ 1 - 1
gwarf_lex.l → paser/gwarf_lex.l

@@ -24,7 +24,7 @@
 <INITIAL>' {BEGIN STRING_TEXT;}
 <INITIAL>' {BEGIN STRING_TEXT;}
 <INITIAL>\" {BEGIN STRING_TEXT;}
 <INITIAL>\" {BEGIN STRING_TEXT;}
 <INITIAL>[1-9][0-9]*(\.[0-9]+)?|0 {
 <INITIAL>[1-9][0-9]*(\.[0-9]+)?|0 {
-    yylval.double_value = atoi(yytext);
+    yylval.double_value = atof(yytext);
     return NUMBER;
     return NUMBER;
     }
     }
 <INITIAL>[a-zA-Z_][a-zA-Z0-9_]* {
 <INITIAL>[a-zA-Z_][a-zA-Z0-9_]* {

+ 74 - 0
paser/gwarf_yacc.y

@@ -0,0 +1,74 @@
+%{
+    #include<stdio.h>
+    #include"lex.yy.c"
+    #include"../gwarf_interpreter/interprete.h"
+    extern int yylex (void);
+%}
+
+%union{
+    int int_value;
+    double double_value;
+    char *string_value;
+    void *statement_value;
+}
+%token <double_value> NUMBER
+%token <string_value> STRING VAR
+%token ADD SUB DIV MUL EQ LESS MORE RB LB RP LP WHILE STOP POW
+%type <statement_value> base_number first_number top_exp
+%%
+command_list
+    : command
+    | command_list command
+    ;
+
+command
+    : top_exp STOP
+    {
+        append_statement(global_inter->global_code, $1);
+    }
+    ;
+
+top_exp
+    : first_number
+    {
+        $$ = $1;
+    }
+    ;
+
+first_number
+    : base_number
+    | first_number ADD base_number
+    {
+        statement *code_tmp =  make_statement();
+        code_tmp->type = operation;
+        code_tmp->code.operation.type = ADD_func;
+        code_tmp->code.operation.right_exp = $1;
+        code_tmp->code.operation.left_exp = $3;
+        $$ = code_tmp;
+    }
+    ;
+
+base_number
+    : NUMBER
+    {
+        statement *code_tmp =  make_statement();
+        code_tmp->type = base_value;
+        code_tmp->code.base_value.value.type = NUMBER;
+        code_tmp->code.base_value.value.value.double_value = $1;
+        $$ = code_tmp;
+    }
+    ;
+
+%%
+int yyerror(char const *str)
+{
+    fprintf(stderr, "parser error near %s\n", yytext);
+    return 0;
+}
+
+int parser(void)
+{
+    yyin = fopen("/home/songzihuan/test.gwf","r");
+    yyparse();
+    return 0;
+}

+ 33 - 35
lex.yy.c → paser/lex.yy.c

@@ -466,8 +466,8 @@ int yy_flex_debug = 0;
 #define YY_MORE_ADJ 0
 #define YY_MORE_ADJ 0
 #define YY_RESTORE_YY_MORE_OFFSET
 #define YY_RESTORE_YY_MORE_OFFSET
 char *yytext;
 char *yytext;
-#line 1 "cwarf_lex.l"
-#line 2 "cwarf_lex.l"
+#line 1 "gwarf_lex.l"
+#line 2 "gwarf_lex.l"
     #include<stdio.h>
     #include<stdio.h>
     #include"y.tab.h"
     #include"y.tab.h"
 #line 473 "lex.yy.c"
 #line 473 "lex.yy.c"
@@ -691,7 +691,7 @@ YY_DECL
 		}
 		}
 
 
 	{
 	{
-#line 6 "cwarf_lex.l"
+#line 6 "gwarf_lex.l"
 
 
 #line 696 "lex.yy.c"
 #line 696 "lex.yy.c"
 
 
@@ -752,97 +752,95 @@ do_action:	/* This label is used only to access EOF actions. */
 
 
 case 1:
 case 1:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 7 "cwarf_lex.l"
+#line 7 "gwarf_lex.l"
 {return WHILE;}
 {return WHILE;}
 	YY_BREAK
 	YY_BREAK
 case 2:
 case 2:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 9 "cwarf_lex.l"
+#line 9 "gwarf_lex.l"
 {return LB;}
 {return LB;}
 	YY_BREAK
 	YY_BREAK
 case 3:
 case 3:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 10 "cwarf_lex.l"
+#line 10 "gwarf_lex.l"
 {return RB;}
 {return RB;}
 	YY_BREAK
 	YY_BREAK
 case 4:
 case 4:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 11 "cwarf_lex.l"
+#line 11 "gwarf_lex.l"
 {return LP;}
 {return LP;}
 	YY_BREAK
 	YY_BREAK
 case 5:
 case 5:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 12 "cwarf_lex.l"
+#line 12 "gwarf_lex.l"
 {return RP;}
 {return RP;}
 	YY_BREAK
 	YY_BREAK
 case 6:
 case 6:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 14 "cwarf_lex.l"
+#line 14 "gwarf_lex.l"
 {return MORE;}
 {return MORE;}
 	YY_BREAK
 	YY_BREAK
 case 7:
 case 7:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 15 "cwarf_lex.l"
+#line 15 "gwarf_lex.l"
 {return LESS;}
 {return LESS;}
 	YY_BREAK
 	YY_BREAK
 case 8:
 case 8:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 16 "cwarf_lex.l"
+#line 16 "gwarf_lex.l"
 {return EQ;}
 {return EQ;}
 	YY_BREAK
 	YY_BREAK
 case 9:
 case 9:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 17 "cwarf_lex.l"
+#line 17 "gwarf_lex.l"
 {return ADD;}
 {return ADD;}
 	YY_BREAK
 	YY_BREAK
 case 10:
 case 10:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 18 "cwarf_lex.l"
+#line 18 "gwarf_lex.l"
 {return SUB;}
 {return SUB;}
 	YY_BREAK
 	YY_BREAK
 case 11:
 case 11:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 19 "cwarf_lex.l"
+#line 19 "gwarf_lex.l"
 {return MUL;}
 {return MUL;}
 	YY_BREAK
 	YY_BREAK
 case 12:
 case 12:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 20 "cwarf_lex.l"
+#line 20 "gwarf_lex.l"
 {return DIV;}
 {return DIV;}
 	YY_BREAK
 	YY_BREAK
 case 13:
 case 13:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 21 "cwarf_lex.l"
+#line 21 "gwarf_lex.l"
 {return POW;}
 {return POW;}
 	YY_BREAK
 	YY_BREAK
 case 14:
 case 14:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 23 "cwarf_lex.l"
+#line 23 "gwarf_lex.l"
 {BEGIN COMMENT;}
 {BEGIN COMMENT;}
 	YY_BREAK
 	YY_BREAK
 case 15:
 case 15:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 24 "cwarf_lex.l"
+#line 24 "gwarf_lex.l"
 {BEGIN STRING_TEXT;}
 {BEGIN STRING_TEXT;}
 	YY_BREAK
 	YY_BREAK
 case 16:
 case 16:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 25 "cwarf_lex.l"
+#line 25 "gwarf_lex.l"
 {BEGIN STRING_TEXT;}
 {BEGIN STRING_TEXT;}
 	YY_BREAK
 	YY_BREAK
 case 17:
 case 17:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 26 "cwarf_lex.l"
+#line 26 "gwarf_lex.l"
 {
 {
-    double tmp = atoi(yytext);
-    printf("yytext = %s, tmp = %f\n", yytext, tmp);
-    yylval.double_value = tmp;
+    yylval.double_value = atof(yytext);
     return NUMBER;
     return NUMBER;
     }
     }
 	YY_BREAK
 	YY_BREAK
 case 18:
 case 18:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 32 "cwarf_lex.l"
+#line 30 "gwarf_lex.l"
 {
 {
     yylval.string_value = yytext;
     yylval.string_value = yytext;
     return VAR;
     return VAR;
@@ -851,39 +849,39 @@ YY_RULE_SETUP
 case 19:
 case 19:
 /* rule 19 can match eol */
 /* rule 19 can match eol */
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 36 "cwarf_lex.l"
+#line 34 "gwarf_lex.l"
 {return STOP;}
 {return STOP;}
 	YY_BREAK
 	YY_BREAK
 case 20:
 case 20:
 /* rule 20 can match eol */
 /* rule 20 can match eol */
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 38 "cwarf_lex.l"
+#line 36 "gwarf_lex.l"
 {BEGIN INITIAL;}
 {BEGIN INITIAL;}
 	YY_BREAK
 	YY_BREAK
 case 21:
 case 21:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 39 "cwarf_lex.l"
+#line 37 "gwarf_lex.l"
 {BEGIN INITIAL;}
 {BEGIN INITIAL;}
 	YY_BREAK
 	YY_BREAK
 case 22:
 case 22:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 40 "cwarf_lex.l"
+#line 38 "gwarf_lex.l"
 ;
 ;
 	YY_BREAK
 	YY_BREAK
 case 23:
 case 23:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 42 "cwarf_lex.l"
+#line 40 "gwarf_lex.l"
 {BEGIN INITIAL;}
 {BEGIN INITIAL;}
 	YY_BREAK
 	YY_BREAK
 case 24:
 case 24:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 43 "cwarf_lex.l"
+#line 41 "gwarf_lex.l"
 {BEGIN INITIAL;}
 {BEGIN INITIAL;}
 	YY_BREAK
 	YY_BREAK
 case 25:
 case 25:
 /* rule 25 can match eol */
 /* rule 25 can match eol */
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 44 "cwarf_lex.l"
+#line 42 "gwarf_lex.l"
 {
 {
     yylval.string_value = yytext;
     yylval.string_value = yytext;
     return STRING;
     return STRING;
@@ -891,7 +889,7 @@ YY_RULE_SETUP
 	YY_BREAK
 	YY_BREAK
 case 26:
 case 26:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 48 "cwarf_lex.l"
+#line 46 "gwarf_lex.l"
 {
 {
     yylval.string_value = yytext;
     yylval.string_value = yytext;
     return STRING;
     return STRING;
@@ -899,10 +897,10 @@ YY_RULE_SETUP
 	YY_BREAK
 	YY_BREAK
 case 27:
 case 27:
 YY_RULE_SETUP
 YY_RULE_SETUP
-#line 52 "cwarf_lex.l"
+#line 50 "gwarf_lex.l"
 ECHO;
 ECHO;
 	YY_BREAK
 	YY_BREAK
-#line 905 "lex.yy.c"
+#line 903 "lex.yy.c"
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(COMMENT):
 case YY_STATE_EOF(COMMENT):
 case YY_STATE_EOF(STRING_TEXT):
 case YY_STATE_EOF(STRING_TEXT):
@@ -1909,7 +1907,7 @@ void yyfree (void * ptr )
 
 
 #define YYTABLES_NAME "yytables"
 #define YYTABLES_NAME "yytables"
 
 
-#line 52 "cwarf_lex.l"
+#line 50 "gwarf_lex.l"
 
 
 int yywrap(void) {
 int yywrap(void) {
     return 1;
     return 1;

+ 56 - 71
y.tab.c → paser/y.tab.c

@@ -66,11 +66,14 @@
 
 
 
 
 /* First part of user prologue.  */
 /* First part of user prologue.  */
-#line 1 "cwarf_yacc.y"
+#line 1 "gwarf_yacc.y"
 
 
     #include<stdio.h>
     #include<stdio.h>
+    #include"lex.yy.c"
+    #include"../gwarf_interpreter/interprete.h"
+    extern int yylex (void);
 
 
-#line 74 "y.tab.c"
+#line 77 "y.tab.c"
 
 
 # ifndef YY_CAST
 # ifndef YY_CAST
 #  ifdef __cplusplus
 #  ifdef __cplusplus
@@ -160,13 +163,14 @@ extern int yydebug;
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 union YYSTYPE
 union YYSTYPE
 {
 {
-#line 5 "cwarf_yacc.y"
+#line 8 "gwarf_yacc.y"
 
 
     int int_value;
     int int_value;
     double double_value;
     double double_value;
     char *string_value;
     char *string_value;
+    void *statement_value;
 
 
-#line 170 "y.tab.c"
+#line 174 "y.tab.c"
 
 
 };
 };
 typedef union YYSTYPE YYSTYPE;
 typedef union YYSTYPE YYSTYPE;
@@ -483,18 +487,18 @@ union yyalloc
 #endif /* !YYCOPY_NEEDED */
 #endif /* !YYCOPY_NEEDED */
 
 
 /* YYFINAL -- State number of the termination state.  */
 /* YYFINAL -- State number of the termination state.  */
-#define YYFINAL  8
+#define YYFINAL  7
 /* YYLAST -- Last index in YYTABLE.  */
 /* YYLAST -- Last index in YYTABLE.  */
-#define YYLAST   12
+#define YYLAST   6
 
 
 /* YYNTOKENS -- Number of terminals.  */
 /* YYNTOKENS -- Number of terminals.  */
 #define YYNTOKENS  20
 #define YYNTOKENS  20
 /* YYNNTS -- Number of nonterminals.  */
 /* YYNNTS -- Number of nonterminals.  */
-#define YYNNTS  7
+#define YYNNTS  6
 /* YYNRULES -- Number of rules.  */
 /* YYNRULES -- Number of rules.  */
-#define YYNRULES  12
+#define YYNRULES  8
 /* YYNSTATES -- Number of states.  */
 /* YYNSTATES -- Number of states.  */
-#define YYNSTATES  19
+#define YYNSTATES  12
 
 
 #define YYUNDEFTOK  2
 #define YYUNDEFTOK  2
 #define YYMAXUTOK   274
 #define YYMAXUTOK   274
@@ -543,8 +547,7 @@ static const yytype_int8 yytranslate[] =
   /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
   /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
 static const yytype_int8 yyrline[] =
 static const yytype_int8 yyrline[] =
 {
 {
-       0,    16,    16,    17,    21,    25,    32,    33,    37,    44,
-      45,    49,    56
+       0,    20,    20,    21,    25,    32,    39,    40,    52
 };
 };
 #endif
 #endif
 
 
@@ -556,7 +559,7 @@ static const char *const yytname[] =
   "$end", "error", "$undefined", "NUMBER", "STRING", "VAR", "ADD", "SUB",
   "$end", "error", "$undefined", "NUMBER", "STRING", "VAR", "ADD", "SUB",
   "DIV", "MUL", "EQ", "LESS", "MORE", "RB", "LB", "RP", "LP", "WHILE",
   "DIV", "MUL", "EQ", "LESS", "MORE", "RB", "LB", "RP", "LP", "WHILE",
   "STOP", "POW", "$accept", "command_list", "command", "top_exp",
   "STOP", "POW", "$accept", "command_list", "command", "top_exp",
-  "first_number", "second_number", "base_number", YY_NULLPTR
+  "first_number", "base_number", YY_NULLPTR
 };
 };
 #endif
 #endif
 
 
@@ -570,7 +573,7 @@ static const yytype_int16 yytoknum[] =
 };
 };
 # endif
 # endif
 
 
-#define YYPACT_NINF (-8)
+#define YYPACT_NINF (-17)
 
 
 #define yypact_value_is_default(Yyn) \
 #define yypact_value_is_default(Yyn) \
   ((Yyn) == YYPACT_NINF)
   ((Yyn) == YYPACT_NINF)
@@ -584,8 +587,8 @@ static const yytype_int16 yytoknum[] =
      STATE-NUM.  */
      STATE-NUM.  */
 static const yytype_int8 yypact[] =
 static const yytype_int8 yypact[] =
 {
 {
-       7,    -8,     0,    -8,    -6,    -5,    -4,    -8,    -8,    -8,
-      -8,     7,     7,     7,     7,    -4,    -4,    -8,    -8
+      -2,   -17,     0,   -17,   -16,    -1,   -17,   -17,   -17,   -17,
+      -2,   -17
 };
 };
 
 
   /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
   /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
@@ -593,20 +596,20 @@ static const yytype_int8 yypact[] =
      means the default is an error.  */
      means the default is an error.  */
 static const yytype_int8 yydefact[] =
 static const yytype_int8 yydefact[] =
 {
 {
-       0,    12,     0,     2,     0,     5,     6,     9,     1,     3,
-       4,     0,     0,     0,     0,     7,     8,    11,    10
+       0,     8,     0,     2,     0,     5,     6,     1,     3,     4,
+       0,     7
 };
 };
 
 
   /* YYPGOTO[NTERM-NUM].  */
   /* YYPGOTO[NTERM-NUM].  */
 static const yytype_int8 yypgoto[] =
 static const yytype_int8 yypgoto[] =
 {
 {
-      -8,    -8,     9,    -8,    -8,    -3,    -7
+     -17,   -17,     2,   -17,   -17,    -4
 };
 };
 
 
   /* YYDEFGOTO[NTERM-NUM].  */
   /* YYDEFGOTO[NTERM-NUM].  */
 static const yytype_int8 yydefgoto[] =
 static const yytype_int8 yydefgoto[] =
 {
 {
-      -1,     2,     3,     4,     5,     6,     7
+      -1,     2,     3,     4,     5,     6
 };
 };
 
 
   /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
   /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
@@ -614,36 +617,32 @@ static const yytype_int8 yydefgoto[] =
      number is the opposite.  If YYTABLE_NINF, syntax error.  */
      number is the opposite.  If YYTABLE_NINF, syntax error.  */
 static const yytype_int8 yytable[] =
 static const yytype_int8 yytable[] =
 {
 {
-       8,    11,    12,     1,    13,    14,    17,    18,    15,    16,
-       1,     9,    10
+       7,     1,     9,     1,     8,    10,    11
 };
 };
 
 
 static const yytype_int8 yycheck[] =
 static const yytype_int8 yycheck[] =
 {
 {
-       0,     6,     7,     3,     8,     9,    13,    14,    11,    12,
-       3,     2,    18
+       0,     3,    18,     3,     2,     6,    10
 };
 };
 
 
   /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
   /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
      symbol of state STATE-NUM.  */
      symbol of state STATE-NUM.  */
 static const yytype_int8 yystos[] =
 static const yytype_int8 yystos[] =
 {
 {
-       0,     3,    21,    22,    23,    24,    25,    26,     0,    22,
-      18,     6,     7,     8,     9,    25,    25,    26,    26
+       0,     3,    21,    22,    23,    24,    25,     0,    22,    18,
+       6,    25
 };
 };
 
 
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
 static const yytype_int8 yyr1[] =
 static const yytype_int8 yyr1[] =
 {
 {
-       0,    20,    21,    21,    22,    23,    24,    24,    24,    25,
-      25,    25,    26
+       0,    20,    21,    21,    22,    23,    24,    24,    25
 };
 };
 
 
   /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
   /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
 static const yytype_int8 yyr2[] =
 static const yytype_int8 yyr2[] =
 {
 {
-       0,     2,     1,     2,     2,     1,     1,     3,     3,     1,
-       3,     3,     1
+       0,     2,     1,     2,     2,     1,     1,     3,     1
 };
 };
 
 
 
 
@@ -1338,57 +1337,49 @@ yyreduce:
   YY_REDUCE_PRINT (yyn);
   YY_REDUCE_PRINT (yyn);
   switch (yyn)
   switch (yyn)
     {
     {
-  case 5:
-#line 26 "cwarf_yacc.y"
-    {
-        printf("%f\n", (yyvsp[0].double_value));
-    }
-#line 1347 "y.tab.c"
-    break;
-
-  case 7:
-#line 34 "cwarf_yacc.y"
+  case 4:
+#line 26 "gwarf_yacc.y"
     {
     {
-        (yyval.double_value) = (yyvsp[-2].double_value) + (yyvsp[0].double_value);
+        append_statement(global_inter->global_code, (yyvsp[-1].statement_value));
     }
     }
-#line 1355 "y.tab.c"
+#line 1346 "y.tab.c"
     break;
     break;
 
 
-  case 8:
-#line 38 "cwarf_yacc.y"
+  case 5:
+#line 33 "gwarf_yacc.y"
     {
     {
-        (yyval.double_value) = (yyvsp[-2].double_value) - (yyvsp[0].double_value);
+        (yyval.statement_value) = (yyvsp[0].statement_value);
     }
     }
-#line 1363 "y.tab.c"
+#line 1354 "y.tab.c"
     break;
     break;
 
 
-  case 10:
-#line 46 "cwarf_yacc.y"
+  case 7:
+#line 41 "gwarf_yacc.y"
     {
     {
-        (yyval.double_value) = (yyvsp[-2].double_value) * (yyvsp[0].double_value);
+        statement *code_tmp =  make_statement();
+        code_tmp->type = operation;
+        code_tmp->code.operation.type = ADD_func;
+        code_tmp->code.operation.right_exp = (yyvsp[-2].statement_value);
+        code_tmp->code.operation.left_exp = (yyvsp[0].statement_value);
+        (yyval.statement_value) = code_tmp;
     }
     }
-#line 1371 "y.tab.c"
+#line 1367 "y.tab.c"
     break;
     break;
 
 
-  case 11:
-#line 50 "cwarf_yacc.y"
+  case 8:
+#line 53 "gwarf_yacc.y"
     {
     {
-        (yyval.double_value) = (yyvsp[-2].double_value) / (yyvsp[0].double_value);
+        statement *code_tmp =  make_statement();
+        code_tmp->type = base_value;
+        code_tmp->code.base_value.value.type = NUMBER;
+        code_tmp->code.base_value.value.value.double_value = (yyvsp[0].double_value);
+        (yyval.statement_value) = code_tmp;
     }
     }
 #line 1379 "y.tab.c"
 #line 1379 "y.tab.c"
     break;
     break;
 
 
-  case 12:
-#line 57 "cwarf_yacc.y"
-    {
-        (yyval.double_value) = (yyvsp[0].double_value);
-        printf("$1 = %f\n", (yyval.double_value));
-    }
-#line 1388 "y.tab.c"
-    break;
-
 
 
-#line 1392 "y.tab.c"
+#line 1383 "y.tab.c"
 
 
       default: break;
       default: break;
     }
     }
@@ -1620,23 +1611,17 @@ yyreturn:
 #endif
 #endif
   return yyresult;
   return yyresult;
 }
 }
-#line 63 "cwarf_yacc.y"
-
-extern int yylex (void);
+#line 62 "gwarf_yacc.y"
 
 
 int yyerror(char const *str)
 int yyerror(char const *str)
 {
 {
-    extern char *yytext;
     fprintf(stderr, "parser error near %s\n", yytext);
     fprintf(stderr, "parser error near %s\n", yytext);
     return 0;
     return 0;
 }
 }
 
 
-int main(void)
+int parser(void)
 {
 {
-    extern int yyparse(void);
-    extern FILE *yyin;
-
-    yyin = stdin;
+    yyin = fopen("/home/songzihuan/test.gwf","r");
     yyparse();
     yyparse();
     return 0;
     return 0;
 }
 }

+ 3 - 2
y.tab.h → paser/y.tab.h

@@ -91,13 +91,14 @@ extern int yydebug;
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 union YYSTYPE
 union YYSTYPE
 {
 {
-#line 5 "cwarf_yacc.y"
+#line 8 "gwarf_yacc.y"
 
 
     int int_value;
     int int_value;
     double double_value;
     double double_value;
     char *string_value;
     char *string_value;
+    void *statement_value;
 
 
-#line 101 "y.tab.h"
+#line 102 "y.tab.h"
 
 
 };
 };
 typedef union YYSTYPE YYSTYPE;
 typedef union YYSTYPE YYSTYPE;