Parcourir la source

style: 修正了代码风格

部分的宏使用全大写
struct结构体使用大驼峰命名法
添加了函数的注释
pasers函数增加了前缀(operation函数在上一个commit时已经增加前缀了)
SongZihuan il y a 4 ans
Parent
commit
12bb068a42
18 fichiers modifiés avec 289 ajouts et 198 suppressions
  1. 5 4
      include/__macro.h
  2. 4 4
      include/function.h
  3. 9 9
      include/grammar.h
  4. 20 20
      include/lexical.h
  5. 1 1
      include/mem.h
  6. 1 1
      include/statement.h
  7. 5 5
      include/syntax.h
  8. 23 28
      include/token.h
  9. 7 7
      main.c
  10. 2 2
      memory/mem.c
  11. 74 31
      parser/grammar.c
  12. 15 15
      parser/lexical.c
  13. 7 7
      parser/syntax.c
  14. 48 27
      parser/token.c
  15. 25 18
      src/operation.c
  16. 27 8
      src/run.c
  17. 1 1
      src/value.c
  18. 15 10
      src/var.c

+ 5 - 4
include/__macro.h

@@ -9,10 +9,11 @@
 #define true 1
 #define false 0
 
-#define pass ;
+#define PASS ;
 
-#define number_type long int
-#define baseFunctionSig Statement *st, Inter *inter, VarList *var_list
-#define callFunctionSig(st, var_list) st, inter, var_list
+#define NUMBER_TYPE long int
+#define HASH_INDEX unsigned int
+#define INTER_FUNCTIONSIG Statement *st, Inter *inter, VarList *var_list
+#define CALL_INTER_FUNCTIONSIG(st, var_list) st, inter, var_list
 
 #endif //VIRTUALMATH___MACRO_H

+ 4 - 4
include/function.h

@@ -26,15 +26,15 @@ void connectStatement(Statement *base, Statement *new);
 void freeStatement(Statement *st);
 
 // run的处理
-Result iterStatement(baseFunctionSig);
-Result operationStatement(baseFunctionSig);
+Result iterStatement(INTER_FUNCTIONSIG);
+Result operationStatement(INTER_FUNCTIONSIG);
 Result globalIterStatement(Inter *inter);
 
 // var的处理
 VarList *makeVarList(Inter *inter);
 VarList *freeVarList(VarList *vl, bool self);
-LinkValue *findFromVarList(char *name, VarList *var_list, number_type times);
-void addFromVarList(char *name, VarList *var_list, number_type times, LinkValue *value);
+LinkValue *findFromVarList(char *name, VarList *var_list, NUMBER_TYPE times);
+void addFromVarList(char *name, VarList *var_list, NUMBER_TYPE times, LinkValue *value);
 void freeHashTable(HashTable *ht, Inter *inter);
 
 #endif //VIRTUALMATH_FUNCTION_H

+ 9 - 9
include/grammar.h

@@ -1,21 +1,21 @@
 #ifndef VIRTUALMATH_GRAMMAR_H
 #define VIRTUALMATH_GRAMMAR_H
-#include "run.h"
+#include "__macro.h"
 
-#define PASERSSIGNATURE parserMessage *pm, Inter *inter /*pasers函数的统一签名*/
+#define PASERSSIGNATURE ParserMessage *pm, Inter *inter /*pasers函数的统一签名*/
 #define CALLPASERSSIGNATURE pm, inter /*pasers函数调用的统一实参*/
 
-typedef struct parserMessage{
-    struct tokenMessage *tm;
-    enum parserMessageStatus{
+typedef struct ParserMessage{
+    struct TokenMessage *tm;
+    enum {
         success = 1,
         syntax_error,
         command_list_error,
     } status;
     char *status_message;
-} parserMessage;
+} ParserMessage;
 
-parserMessage *makeParserMessage(char *file_dir);
-void freePasersMessage(parserMessage *pm, bool self);
-void commandList(parserMessage *pm, Inter *inter, bool global, Statement *st);
+ParserMessage *makeParserMessage(char *file_dir);
+void freePasersMessage(ParserMessage *pm, bool self);
+void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *st);
 #endif //VIRTUALMATH_GRAMMAR_H

+ 20 - 20
include/lexical.h

@@ -3,20 +3,20 @@
 #include "__macro.h"
 #include "stdio.h"
 
-typedef struct lexFile{
+typedef struct LexFile{
     FILE *file;
-    struct back{
+    struct {
         bool is_back;
         char p;
     } back;
-} lexFile;
+} LexFile;
 
-typedef struct lexMather{
+typedef struct LexMather{
     int len;
     int string_type;
     char *str;
     char *second_str;
-    enum status{
+    enum {
         LEXMATHER_START=1,
         LEXMATHER_ING,
         LEXMATHER_INGPOINT,
@@ -26,25 +26,25 @@ typedef struct lexMather{
         LEXMATHER_END_SECOND,
         LEXMATHER_MISTAKE,
     } status;
-} lexMather;
+} LexMather;
 
-typedef struct lexMathers{
+typedef struct LexMathers{
     int size;
-    struct lexMather **mathers;
-} lexMathers;
+    struct LexMather **mathers;
+} LexMathers;
 
-char readChar(lexFile *file);
-void backChar(lexFile *file);
+char readChar(LexFile *file);
+void backChar(LexFile *file);
 
-lexFile *makeLexFile(char *dir);
-void freeLexFile(lexFile *file, bool self);
+LexFile *makeLexFile(char *dir);
+void freeLexFile(LexFile *file, bool self);
 
-void setupMather(lexMather *mather);
-lexMather *makeMather();
-void freeMather(lexMather *mather, bool self);
+void setupMather(LexMather *mather);
+LexMather *makeMather();
+void freeMather(LexMather *mather, bool self);
 
-lexMathers *makeMathers(int size);
-void freeMathers(lexMathers *mathers, bool self);
-void setupMathers(lexMathers *mathers);
-int checkoutMather(lexMathers *mathers, int max);
+LexMathers *makeMathers(int size);
+void freeMathers(LexMathers *mathers, bool self);
+void setupMathers(LexMathers *mathers);
+int checkoutMather(LexMathers *mathers, int max);
 #endif //VIRTUALMATH_LEXICAL_H

+ 1 - 1
include/mem.h

@@ -10,7 +10,7 @@ char *memStrcpy(char *str, size_t nsize, int free_old, int write, ...);
 char *memString(size_t size);
 size_t memStrlen(char *p);
 char *memStrcat(char *first, char *second);
-char *memStrcpySelf(char *str, number_type times);
+char *memStrcpySelf(char *str, NUMBER_TYPE times);
 char *memStrrev(char *str);
 
 #define memFree(p) p=memFreeCore(p)

+ 1 - 1
include/statement.h

@@ -12,7 +12,7 @@ typedef struct VirtualMathValue{
     } type;
     union data{
         struct Number{
-            number_type num;
+            NUMBER_TYPE num;
         } num;
         struct String{
             char *str;

+ 5 - 5
include/syntax.h

@@ -4,11 +4,11 @@
 #include "lexical.h"
 #include "token.h"
 
-void numberMather(char p, lexMather *mather);
-void varMather(char p, lexMather *mather);
-void stringMather(char p, lexMather *mather);
-void strMather(char p, lexMather *mather, const char *dest_p);
-void charMather(char p, lexMather *mather, char dest_p);
+void numberMather(char p, LexMather *mather);
+void varMather(char p, LexMather *mather);
+void stringMather(char p, LexMather *mather);
+void strMather(char p, LexMather *mather, const char *dest_p);
+void charMather(char p, LexMather *mather, char dest_p);
 
 #define strMatherMacro(n, word) strMather(p, mathers->mathers[n], word) /*这个宏只能用于getMatherStatus*/
 #define charMatherMacro(n, word) charMather(p, mathers->mathers[n], word) /*这个宏只能用于getMatherStatus*/

+ 23 - 28
include/token.h

@@ -92,47 +92,42 @@
 #define POLYNOMIAL -8
 #define BASEVALUE -9
 
-// 预定义一部分的内容
-struct Statement;
-struct lexFile;
-struct lexMathers;
-
-typedef struct token{
+typedef struct Token{
     int token_type;  // 记录token的类型,大于0的数字均为lex匹配器所匹配,小于0的为syntax解析器所匹配
     struct{
         char *str;
         char *second_str;  // 针对123.4j这种形式设定的,其中second_str存储j
         struct Statement *st;
     } data;
-} token;
+} Token;
 
-typedef struct tokenStream{
-    token **token_list;  // 存储token的列表
-    token **token_ahead;  // 提前存储token的列表
+typedef struct TokenStream{
+    Token **token_list;  // 存储token的列表
+    Token **token_ahead;  // 提前存储token的列表
     int size;
     int ahead;
-} tokenStream;
+} TokenStream;
 
-typedef struct tokenMessage{
-    tokenStream *ts;
-    struct lexFile *file;
-    struct lexMathers *mathers;
-} tokenMessage;
+typedef struct TokenMessage{
+    TokenStream *ts;
+    struct LexFile *file;
+    struct LexMathers *mathers;
+} TokenMessage;
 
-token *makeToken();
-token *makeLexToken(int type, char *str, char *second_str);
-token *makeStatementToken(int type, struct Statement *st);
-void freeToken(token *tk, bool self, bool error);
+Token *makeToken();
+Token *makeLexToken(int type, char *str, char *second_str);
+Token *makeStatementToken(int type, struct Statement *st);
+void freeToken(Token *tk, bool self, bool error);
 
-extern token *getToken(struct lexFile *file, struct lexMathers *mathers);
+extern Token *getToken(struct LexFile *file, struct LexMathers *mathers);
 
 
-int safeGetToken(tokenMessage *tm);
-token *forwardToken(tokenStream *ts);
-token *backToken(tokenStream *ts);
-void addToken(tokenStream *ts, token *new_tk);
-token *popToken(tokenStream *ts);
+int safeGetToken(TokenMessage *tm);
+Token *forwardToken(TokenStream *ts);
+Token *backToken(TokenStream *ts);
+void addToken(TokenStream *ts, Token *new_tk);
+Token *popToken(TokenStream *ts);
 
-tokenMessage *makeTokenMessage(char *file_dir);
-void freeTokenMessage(tokenMessage *tm, bool self);
+TokenMessage *makeTokenMessage(char *file_dir);
+void freeTokenMessage(TokenMessage *tm, bool self);
 #endif //VIRTUALMATH_TOKEN_H

+ 7 - 7
main.c

@@ -10,22 +10,22 @@ int main(int argc, char *argv[]) {
 int testMain(int argc, char *argv[]) {
     if (argc != 2) {
         printf("Too many or little argc\n");
+        goto argc_error_;
     }
     Inter *global_iter = makeInter();
-    parserMessage *pm = makeParserMessage(argv[1]);
-    commandList(pm, global_iter, true, global_iter->statement);
+    ParserMessage *pm = makeParserMessage(argv[1]);
+    pasersCommandList(pm, global_iter, true, global_iter->statement);
     if (pm->status != success){
         printf("Syntax Error: %s\n", pm->status_message);
-        goto error_;
+        goto return_;
     }
     globalIterStatement(global_iter);
 
+    return_:
     freePasersMessage(pm, true);
     freeInter(global_iter, true);
     return 0;
 
-    error_:
-    freePasersMessage(pm, true);
-    freeInter(global_iter, true);
-    return 0;
+    argc_error_:
+    return 1;
 }

+ 2 - 2
memory/mem.c

@@ -77,7 +77,7 @@ char *memStrcat(char *first, char *second){
     return new;
 }
 
-char *memStrcpySelf(char *str, number_type times){
+char *memStrcpySelf(char *str, NUMBER_TYPE times){
     bool need_free = false;
     if (times < 0){
         str = memStrrev(str);
@@ -85,7 +85,7 @@ char *memStrcpySelf(char *str, number_type times){
         need_free = true;
     }
     char *new_str = memStrcpy(str, 0, false, false), *tmp;
-    for (number_type i=0;i < times - 1;i++){
+    for (NUMBER_TYPE i=0; i < times - 1; i++){
         tmp = memStrcat(new_str, str);
         memFree(new_str);
         new_str = tmp;

+ 74 - 31
parser/grammar.c

@@ -8,10 +8,10 @@ backToken(pm->tm->ts); \
 #define popAheadToken(token_var, pm) do{ \
 safeGetToken(pm->tm); \
 token_var = popToken(pm->tm->ts); \
-} while(0)
+} while(0) /*弹出预读的token*/
 
 #define addStatementToken(type, st, pm) do{\
-token *tmp_new_token; \
+Token *tmp_new_token; \
 tmp_new_token = makeStatementToken(type, st); \
 addToken(pm->tm->ts, tmp_new_token); \
 backToken(pm->tm->ts); \
@@ -24,22 +24,22 @@ backToken(pm->tm->ts); \
 
 #define call_success(pm) (pm->status == success)
 
-void command(PASERSSIGNATURE);
+void parserCommand(PASERSSIGNATURE);
 void parserOperation(PASERSSIGNATURE);
-void polynomial(PASERSSIGNATURE);
-void baseValue(PASERSSIGNATURE);
+void parserPolynomial(PASERSSIGNATURE);
+void parserBaseValue(PASERSSIGNATURE);
 
-void syntaxError(parserMessage *pm, char *message, enum parserMessageStatus status);
+void syntaxError(ParserMessage *pm, char *message, int status);
 
-parserMessage *makeParserMessage(char *file_dir){
-    parserMessage *tmp = memCalloc(1, sizeof(parserMessage));
+ParserMessage *makeParserMessage(char *file_dir){
+    ParserMessage *tmp = memCalloc(1, sizeof(ParserMessage));
     tmp->tm = makeTokenMessage(file_dir);
     tmp->status = success;
     tmp->status_message = NULL;
     return tmp;
 }
 
-void freePasersMessage(parserMessage *pm, bool self) {
+void freePasersMessage(ParserMessage *pm, bool self) {
     freeTokenMessage(pm->tm, true);
     memFree(pm->status_message);
     if (self){
@@ -47,26 +47,42 @@ void freePasersMessage(parserMessage *pm, bool self) {
     }
 }
 
-// TODO-szh 代码重构, 匹配器函数加前缀pasers或者grammar
-void commandList(parserMessage *pm, Inter *inter, bool global, Statement *st) {
+/**
+ * 命令表匹配
+ * pasersCommandList :
+ * | MATHER_EOF
+ * | parserCommand MATHER_ENTER
+ * | parserCommand MATHER_EOF
+ */
+void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *st) {
     int token_type, command_int, stop;
     struct Statement *base_st = st;
     while (true){
         readBackToken(token_type, pm);
         if (token_type == MATHER_EOF){
             // printf("get EOF\n");
+            Token *tmp;
+            popAheadToken(tmp, pm);
+            freeToken(tmp, true, false);
             goto return_;
         }
+        else if (token_type == MATHER_ENTER){
+            // 处理空语句
+            Token *tmp;
+            popAheadToken(tmp, pm);
+            freeToken(tmp, true, false);
+            continue;
+        }
         else{
-            token *command_token,*stop_token;
-            command(CALLPASERSSIGNATURE);
+            Token *command_token,*stop_token;
+            parserCommand(CALLPASERSSIGNATURE);
             if (!call_success(pm)){
                 goto return_;
             }
             readBackToken(command_int, pm);
             if (COMMAND != command_int){
                 if (global){
-                    syntaxError(pm, "ERROR from command list(get command)", command_list_error);
+                    syntaxError(pm, "ERROR from command list(get parserCommand)", command_list_error);
                 }
                 goto return_;
             }
@@ -82,7 +98,7 @@ void commandList(parserMessage *pm, Inter *inter, bool global, Statement *st) {
                 backToken_(pm, stop_token);
             }
             else{
-                syntaxError(pm, "ERROR from command list(get stop)", command_list_error);
+                syntaxError(pm, "ERROR from parserCommand list(get stop)", command_list_error);
                 freeToken(command_token, true, true);
                 goto return_;
             }
@@ -96,16 +112,21 @@ void commandList(parserMessage *pm, Inter *inter, bool global, Statement *st) {
     addStatementToken(COMMANDLIST, base_st, pm);
 }
 
-void command(PASERSSIGNATURE){
+/**
+ * 命令匹配
+ * parserCommand:
+ * | parserOperation
+ */
+void parserCommand(PASERSSIGNATURE){
     int token_type;
     Statement *st = NULL;
     readBackToken(token_type, pm);
     if (false){
-        pass
+        PASS
     }
     else{
         int command_int;
-        token *command_token;
+        Token *command_token;
         parserOperation(CALLPASERSSIGNATURE);
         if (!call_success(pm)){
             goto return_;
@@ -126,9 +147,14 @@ void command(PASERSSIGNATURE){
     return;
 }
 
+/**
+ * 表达式匹配
+ * parserOperation:
+ * | parserPolynomial
+ */
 void parserOperation(PASERSSIGNATURE){
     int operation_int;
-    polynomial(CALLPASERSSIGNATURE);
+    parserPolynomial(CALLPASERSSIGNATURE);
     if (!call_success(pm)){
         goto return_;
     }
@@ -136,7 +162,7 @@ void parserOperation(PASERSSIGNATURE){
     if (operation_int != POLYNOMIAL){
         goto return_;
     }
-    token *operation_token;
+    Token *operation_token;
     popAheadToken(operation_token, pm);
     /*...do something for operation...*/
     // printf("do something for operation\n");
@@ -148,14 +174,22 @@ void parserOperation(PASERSSIGNATURE){
     return;
 }
 
-void polynomial(PASERSSIGNATURE){
+/**
+ * 多项式匹配
+ * parserPolynomial:
+ * | parserBaseValue [1]
+ * | parserPolynomial ADD parserBaseValue
+ * | parserPolynomial SUB parserBaseValue
+ */
+void parserPolynomial(PASERSSIGNATURE){
     while(true){
         int left, symbol, right;
-        token *left_token, *symbol_token, *right_token;
+        Token *left_token, *symbol_token, *right_token;
         struct Statement *st = NULL;
         readBackToken(left, pm);
         if (left != POLYNOMIAL){
-            baseValue(CALLPASERSSIGNATURE);  // 获得左值
+            // 情况[1]
+            parserBaseValue(CALLPASERSSIGNATURE);  // 获得左值
             if (!call_success(pm)){
                 goto return_;
             }
@@ -163,6 +197,10 @@ void polynomial(PASERSSIGNATURE){
             if (left != BASEVALUE){  // 若非正确数值
                 goto return_;
             }
+            popAheadToken(left_token, pm);
+            addStatementToken(POLYNOMIAL, left_token->data.st, pm);
+            freeToken(left_token, true, false);
+            continue;
             // printf("polynomial: get base value\n");
         }
         popAheadToken(left_token, pm);
@@ -194,7 +232,7 @@ void polynomial(PASERSSIGNATURE){
                 goto return_;
         }
 
-        baseValue(CALLPASERSSIGNATURE);  // 获得左值
+        parserBaseValue(CALLPASERSSIGNATURE);  // 获得左值
         if (!call_success(pm)){
             freeToken(left_token, true, false);
             freeStatement(st);
@@ -202,7 +240,7 @@ void polynomial(PASERSSIGNATURE){
         }
         readBackToken(right, pm);
         if (right != BASEVALUE){  // 若非正确数值
-            syntaxError(pm, "ERROR from polynomial(get right)", syntax_error);
+            syntaxError(pm, "ERROR from parserPolynomial(get right)", syntax_error);
             freeToken(left_token, true, true);
             freeStatement(st);
             goto return_;
@@ -223,18 +261,17 @@ void polynomial(PASERSSIGNATURE){
 
 /**
  * 字面量匹配
- * baseValue:
+ * parserBaseValue:
  * | MATHER_NUMBER
  * | MATHER_STRING
- * @param pm
  */
-void baseValue(PASERSSIGNATURE){
+void parserBaseValue(PASERSSIGNATURE){
     int token_type;
     struct Statement *st = NULL;
     readBackToken(token_type, pm);
     if(MATHER_NUMBER == token_type){
         // 匹配到正常字面量
-        token *value_token;
+        Token *value_token;
         char *stop;
         popAheadToken(value_token, pm);
         st = makeStatement();
@@ -243,7 +280,7 @@ void baseValue(PASERSSIGNATURE){
         freeToken(value_token, true, false);
     }
     else if(MATHER_STRING == token_type){
-        token *value_token;
+        Token *value_token;
         popAheadToken(value_token, pm);
         st = makeStatement();
         st->type = base_value;
@@ -258,7 +295,13 @@ void baseValue(PASERSSIGNATURE){
     return;
 }
 
-void syntaxError(parserMessage *pm, char *message, enum parserMessageStatus status){
+/**
+ * syntax错误处理器
+ * @param pm
+ * @param message 错误信息
+ * @param status 错误类型
+ */
+void syntaxError(ParserMessage *pm, char *message, int status){
     pm->status = status;
     pm->status_message = memStrcpy(message, 0, false, false);
 }

+ 15 - 15
parser/lexical.c

@@ -6,7 +6,7 @@
  * @param file
  * @return 返回一个字符,若为EOF则返回-1
  */
-char readChar(lexFile *file){
+char readChar(LexFile *file){
     if (file->back.is_back){
         file->back.is_back = false;
     }
@@ -19,19 +19,19 @@ char readChar(lexFile *file){
  * 设置字符回退
  * @param file
  */
-void backChar(lexFile *file){
+void backChar(LexFile *file){
     file->back.is_back = true;
 }
 
-lexFile *makeLexFile(char *dir){
-    lexFile *tmp = memCalloc(1, sizeof(lexFile));
+LexFile *makeLexFile(char *dir){
+    LexFile *tmp = memCalloc(1, sizeof(LexFile));
     tmp->file = fopen(dir, "r");
     tmp->back.is_back = false;
     tmp->back.p = EOF;
     return tmp;
 }
 
-void freeLexFile(lexFile *file, bool self){
+void freeLexFile(LexFile *file, bool self){
     fclose(file->file);
     if (self){
         memFree(file);
@@ -42,7 +42,7 @@ void freeLexFile(lexFile *file, bool self){
  * 初始化mather,代码被复用
  * @param mather
  */
-void setupMather(lexMather *mather){
+void setupMather(LexMather *mather){
     mather->len = 0;
     mather->str = NULL;
     mather->second_str = NULL;
@@ -50,13 +50,13 @@ void setupMather(lexMather *mather){
     mather->status = LEXMATHER_START;
 }
 
-lexMather *makeMather(){
-    lexMather *tmp = memCalloc(1, sizeof(lexMather));
+LexMather *makeMather(){
+    LexMather *tmp = memCalloc(1, sizeof(LexMather));
     setupMather(tmp);
     return tmp;
 }
 
-void freeMather(lexMather *mather, bool self){
+void freeMather(LexMather *mather, bool self){
     memFree(mather->str);
     memFree(mather->second_str);
     mather->len = 0;
@@ -65,17 +65,17 @@ void freeMather(lexMather *mather, bool self){
     }
 }
 
-lexMathers *makeMathers(int size){
-    lexMathers *tmp = memCalloc(1, sizeof(lexMathers));
+LexMathers *makeMathers(int size){
+    LexMathers *tmp = memCalloc(1, sizeof(LexMathers));
     tmp->size = size;
-    tmp->mathers = (struct lexMather**)memCalloc(size, sizeof(lexMather*));
+    tmp->mathers = (struct LexMather**)memCalloc(size, sizeof(LexMather*));
     for(int i=0;i < size; i++){
         tmp->mathers[i] = makeMather();
     }
     return tmp;
 }
 
-void freeMathers(lexMathers *mathers, bool self){
+void freeMathers(LexMathers *mathers, bool self){
     for(int i=0;i < mathers->size; i++){
         freeMather(mathers->mathers[i], true);
     }
@@ -90,7 +90,7 @@ void freeMathers(lexMathers *mathers, bool self){
  * 初始化mathers,本质是初始化mathers.mathers内所有的mather
  * @param mathers
  */
-void setupMathers(lexMathers *mathers){
+void setupMathers(LexMathers *mathers){
     for (int i=0;i < mathers->size;i++){
         if(mathers->mathers[i]->str != NULL){
             memFree(mathers->mathers[i]->str);
@@ -110,7 +110,7 @@ void setupMathers(lexMathers *mathers){
  * @param max
  * @return
  */
-int checkoutMather(lexMathers *mathers, int max) {
+int checkoutMather(LexMathers *mathers, int max) {
     int mistake_count = 0;
     int end_count = 0, end_index = -1;
     int end_second_count = 0, end_second_index = -1;

+ 7 - 7
parser/syntax.c

@@ -12,7 +12,7 @@
  * @param p
  * @param mather
  */
-void numberMather(char p, lexMather *mather){
+void numberMather(char p, LexMather *mather){
     if (mather->status == LEXMATHER_START || mather->status == LEXMATHER_ING || mather->status == LEXMATHER_INGPOINT){
         if ('0'<= p && '9' >= p || '.' == p && mather->status == LEXMATHER_ING){
             mather->str = memStrcpy(mather->str, 1, true, true, p);
@@ -57,7 +57,7 @@ void numberMather(char p, lexMather *mather){
  * @param p
  * @param mather
  */
-void varMather(char p, lexMather *mather){
+void varMather(char p, LexMather *mather){
     if (mather->status == LEXMATHER_START || mather->status == LEXMATHER_ING){
         if ('A'<= p && 'Z' >= p ||'a'<= p && 'z' >= p ||'_' == p ||
             '0'<= p && '9' >= p && mather->status == LEXMATHER_ING){
@@ -85,7 +85,7 @@ void varMather(char p, lexMather *mather){
  * @param p
  * @param mather
  */
-void stringMather(char p, lexMather *mather){
+void stringMather(char p, LexMather *mather){
     if (mather->status == LEXMATHER_START){
         if ('\"' == p || '\'' == p){
             mather->status = LEXMATHER_ING;
@@ -134,7 +134,7 @@ void stringMather(char p, lexMather *mather){
  * @param mather
  * @param dest_p
  */
-void strMather(char p, lexMather *mather, const char *dest_p){
+void strMather(char p, LexMather *mather, const char *dest_p){
     if (mather->status == LEXMATHER_START || mather->status == LEXMATHER_ING){
         if (p == dest_p[mather->len]){
             mather->str = memStrcpy(mather->str, 1, true, true, p);
@@ -160,7 +160,7 @@ void strMather(char p, lexMather *mather, const char *dest_p){
  * @param mather
  * @param dest_p
  */
-void charMather(char p, lexMather *mather, char dest_p){
+void charMather(char p, LexMather *mather, char dest_p){
     int tmp_p = (int)p, tmp_dest = (int)dest_p;
     if (tmp_p == tmp_dest && mather->status == LEXMATHER_START){
         mather->str = memStrcpy(mather->str, 1, true, true, p);
@@ -181,7 +181,7 @@ void charMather(char p, lexMather *mather, char dest_p){
  * @param mathers
  * @return
  */
-int getMatherStatus(lexFile *file, lexMathers *mathers){
+int getMatherStatus(LexFile *file, LexMathers *mathers){
     setupMathers(mathers);
     int status = -1;
     while (status == -1){
@@ -285,7 +285,7 @@ int getMatherStatus(lexFile *file, lexMathers *mathers){
  * @param mathers
  * @return
  */
-token *getToken(lexFile *file, lexMathers *mathers){
+Token *getToken(LexFile *file, LexMathers *mathers){
     int status = MATHER_SPACE;
     while (status == MATHER_SPACE){
         status = getMatherStatus(file, mathers);

+ 48 - 27
parser/token.c

@@ -1,7 +1,7 @@
 #include "__virtualmath.h"
 
-token *makeToken(){
-    token *tmp = memCalloc(1, sizeof(token));
+Token *makeToken(){
+    Token *tmp = memCalloc(1, sizeof(Token));
     tmp->token_type = 0;
     tmp->data.str = NULL;
     tmp->data.st = NULL;
@@ -9,22 +9,22 @@ token *makeToken(){
     return tmp;
 }
 
-token *makeLexToken(int type, char *str, char *second_str) {
-    struct token *tmp = makeToken();
+Token *makeLexToken(int type, char *str, char *second_str) {
+    struct Token *tmp = makeToken();
     tmp->token_type = type;
     tmp->data.str = memStrcpy(str, 0, false, false);
     tmp->data.second_str = memStrcpy(second_str, 0, false, false);
     return tmp;
 }
 
-token *makeStatementToken(int type, struct Statement *st){
-    struct token *tmp = makeToken();
+Token *makeStatementToken(int type, struct Statement *st){
+    struct Token *tmp = makeToken();
     tmp->token_type = type;
     tmp->data.st = st;
     return tmp;
 }
 
-void freeToken(token *tk, bool self, bool error) {
+void freeToken(Token *tk, bool self, bool error) {
     memFree(tk->data.str);
     memFree(tk->data.second_str);
     if (error){
@@ -35,8 +35,8 @@ void freeToken(token *tk, bool self, bool error) {
     }
 }
 
-tokenStream *makeTokenStream(){
-    tokenStream *tmp = memCalloc(1, sizeof(tokenStream));
+TokenStream *makeTokenStream(){
+    TokenStream *tmp = memCalloc(1, sizeof(TokenStream));
     tmp->size = 0;
     tmp->ahead = 0;
     tmp->token_list = NULL;
@@ -44,7 +44,7 @@ tokenStream *makeTokenStream(){
     return tmp;
 }
 
-void freeToekStream(tokenStream *ts, bool self) {
+void freeToekStream(TokenStream *ts, bool self) {
     for (int i=0; i < ts->size; i++){
         freeToken(ts->token_list[i], true, false);
     }
@@ -58,15 +58,15 @@ void freeToekStream(tokenStream *ts, bool self) {
     }
 }
 
-tokenMessage *makeTokenMessage(char *file_dir){
-    tokenMessage *tm = memCalloc(1, sizeof(tokenMessage));
+TokenMessage *makeTokenMessage(char *file_dir){
+    TokenMessage *tm = memCalloc(1, sizeof(TokenMessage));
     tm->file = makeLexFile(file_dir);
     tm->mathers = makeMathers(MATHER_MAX);
     tm->ts = makeTokenStream();
     return tm;
 }
 
-void freeTokenMessage(tokenMessage *tm, bool self) {
+void freeTokenMessage(TokenMessage *tm, bool self) {
     freeLexFile(tm->file, true);
     freeToekStream(tm->ts, true);
     freeMathers(tm->mathers, true);
@@ -75,8 +75,13 @@ void freeTokenMessage(tokenMessage *tm, bool self) {
     }
 }
 
-void addToken(tokenStream *ts, token *new_tk){
-    token **new_list = memCalloc(ts->size + 1, sizeof(token *));
+/**
+ * 添加一个token到token_list,token_ahend保持
+ * @param ts
+ * @param new_tk
+ */
+void addToken(TokenStream *ts, Token *new_tk){
+    Token **new_list = memCalloc(ts->size + 1, sizeof(Token *));
     for (int i=0; i < ts->size; i++){
         new_list[i] = ts->token_list[i];
     }
@@ -86,21 +91,31 @@ void addToken(tokenStream *ts, token *new_tk){
     ts->token_list = new_list;
 }
 
-token *popToken(tokenStream *ts){
-    token **new_list = memCalloc(ts->size - 1, sizeof(token *));
+/**
+ * 从token_list弹出一个token,保持token_ahend
+ * @param ts
+ * @return
+ */
+Token *popToken(TokenStream *ts){
+    Token **new_list = memCalloc(ts->size - 1, sizeof(Token *));
     for (int i=0; i < ts->size - 1; i++){
         new_list[i] = ts->token_list[i];
     }
-    token *tmp = ts->token_list[ts->size - 1];
+    Token *tmp = ts->token_list[ts->size - 1];
     memFree(ts->token_list);
     ts->token_list = new_list;
     ts->size --;
     return tmp;
 }
 
-token *backToken(tokenStream *ts){
-    token **new_list = memCalloc(ts->size - 1, sizeof(token *));
-    token **new_ahead = memCalloc(ts->ahead + 1, sizeof(token *));
+/**
+ * 把token_list的一个token退回到token_ahend
+ * @param ts
+ * @return
+ */
+Token *backToken(TokenStream *ts){
+    Token **new_list = memCalloc(ts->size - 1, sizeof(Token *));
+    Token **new_ahead = memCalloc(ts->ahead + 1, sizeof(Token *));
     for (int i=0; i < ts->size - 1; i++){
         new_list[i] = ts->token_list[i];
     }
@@ -117,9 +132,14 @@ token *backToken(tokenStream *ts){
     return new_ahead[ts->ahead - 1];
 }
 
-token *forwardToken(tokenStream *ts){
-    token **new_list = memCalloc(ts->size + 1, sizeof(token *));
-    token **new_ahead = memCalloc(ts->ahead - 1, sizeof(token *));
+/**
+ * backToken的逆向操作
+ * @param ts
+ * @return
+ */
+Token *forwardToken(TokenStream *ts){
+    Token **new_list = memCalloc(ts->size + 1, sizeof(Token *));
+    Token **new_ahead = memCalloc(ts->ahead - 1, sizeof(Token *));
     for (int i=0; i < ts->size; i++){
         new_list[i] = ts->token_list[i];
     }
@@ -137,12 +157,13 @@ token *forwardToken(tokenStream *ts){
 }
 
 /**
- * 获取token
+ * 获取token, 并且放入token_list中
+ * 自动处理backToken
  * @param tm
  * @return 返回获取token的token_type
  */
-int safeGetToken(tokenMessage *tm){
-    token *tmp;
+int safeGetToken(TokenMessage *tm){
+    Token *tmp;
     if (tm->ts->ahead == 0){
         tmp = getToken(tm->file, tm->mathers);
         addToken(tm->ts, tmp);

+ 25 - 18
src/operation.c

@@ -3,36 +3,43 @@
  * operation.c中是用于数学计算的函数
  */
 
-#define getresult(base, var, inter) var = iterStatement(callFunctionSig(st->u.operation. base, var_list))
+#define getresult(base, var, inter) var = iterStatement(CALL_INTER_FUNCTIONSIG(st->u.operation. base, var_list))
 #define viewtype_core(a, b, valuetype_a, valuetype_a_b) a .value->value->type == valuetype_a && b.value->value->type == valuetype_a_b
 #define viewtype(a, b, valuetype) viewtype_core(a, b, valuetype, valuetype)
 #define operationValue(a, b, type, symbol) a.value->value->data.type symbol b.value->value->data.type
 #define valueToResult(result, result_value, type, inter) result.value->value = make##type##Value(result_value, inter)
 
-Result addOperation(baseFunctionSig);
-Result subOperation(baseFunctionSig);
-Result mulOperation(baseFunctionSig);
-Result divOperation(baseFunctionSig);
-Result assOperation(baseFunctionSig);
+Result addOperation(INTER_FUNCTIONSIG);
+Result subOperation(INTER_FUNCTIONSIG);
+Result mulOperation(INTER_FUNCTIONSIG);
+Result divOperation(INTER_FUNCTIONSIG);
+Result assOperation(INTER_FUNCTIONSIG);
 
-Result operationStatement(baseFunctionSig) {
+/**
+ * operation的整体操作
+ * @param st
+ * @param inter
+ * @param var_list
+ * @return
+ */
+Result operationStatement(INTER_FUNCTIONSIG) {
     Result result;
     setResult(&result, true, inter);
     switch (st->u.operation.OperationType) {
         case ADD:
-            result = addOperation(callFunctionSig(st, var_list));
+            result = addOperation(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         case SUB:
-            result = subOperation(callFunctionSig(st, var_list));
+            result = subOperation(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         case MUL:
-            result = mulOperation(callFunctionSig(st, var_list));
+            result = mulOperation(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         case DIV:
-            result = divOperation(callFunctionSig(st, var_list));
+            result = divOperation(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         case ASS:
-            result = assOperation(callFunctionSig(st, var_list));
+            result = assOperation(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         default:
             break;
@@ -40,7 +47,7 @@ Result operationStatement(baseFunctionSig) {
     return result;
 }
 
-Result addOperation(baseFunctionSig) {
+Result addOperation(INTER_FUNCTIONSIG) {
     Result left, right, result;
     setResult(&result, true, inter);
     getresult(left, left, inter);
@@ -56,7 +63,7 @@ Result addOperation(baseFunctionSig) {
     return result;
 }
 
-Result subOperation(baseFunctionSig) {
+Result subOperation(INTER_FUNCTIONSIG) {
     Result left, right, result;
     setResult(&result, true, inter);
     getresult(left, left, inter);
@@ -67,7 +74,7 @@ Result subOperation(baseFunctionSig) {
     return result;
 }
 
-Result mulOperation(baseFunctionSig) {
+Result mulOperation(INTER_FUNCTIONSIG) {
     Result left, right, result;
     setResult(&result, true, inter);
     getresult(left, left, inter);
@@ -92,7 +99,7 @@ Result mulOperation(baseFunctionSig) {
     return result;
 }
 
-Result divOperation(baseFunctionSig) {
+Result divOperation(INTER_FUNCTIONSIG) {
     Result left, right, result;
     setResult(&result, true, inter);
     getresult(left, left, inter);
@@ -103,7 +110,7 @@ Result divOperation(baseFunctionSig) {
     return result;
 }
 
-Result assOperation(baseFunctionSig) {
+Result assOperation(INTER_FUNCTIONSIG) {
     Result times, right;
     int int_times;
     getresult(right, right, inter);
@@ -112,7 +119,7 @@ Result assOperation(baseFunctionSig) {
             int_times = 0;
             goto not_times;
         }
-        times = iterStatement(callFunctionSig(st->u.operation.left->u.base_var.times, var_list));
+        times = iterStatement(CALL_INTER_FUNCTIONSIG(st->u.operation.left->u.base_var.times, var_list));
         int_times = (int)times.value->value->data.num.num;
         not_times:
         addFromVarList(st->u.operation.left->u.base_var.name, var_list, int_times, right.value);

+ 27 - 8
src/run.c

@@ -13,14 +13,14 @@ switch (result.value->value->type){ \
 } \
 } while(0) \
 
-Result getBaseVar(baseFunctionSig) {
+Result getBaseVar(INTER_FUNCTIONSIG) {
     Result times, result;
     int int_times;
     if (st->u.base_var.times == NULL){
         int_times = 0;
         goto not_times;
     }
-    times = iterStatement(callFunctionSig(st->u.base_var.times, var_list));
+    times = iterStatement(CALL_INTER_FUNCTIONSIG(st->u.base_var.times, var_list));
     int_times = (int)times.value->value->data.num.num;
 
     not_times:
@@ -31,7 +31,14 @@ Result getBaseVar(baseFunctionSig) {
     return result;
 }
 
-Result runStatement(baseFunctionSig) {
+/**
+ * 运行但个statement
+ * @param st
+ * @param inter
+ * @param var_list
+ * @return
+ */
+Result runStatement(INTER_FUNCTIONSIG) {
     Result result;
     setResult(&result, true, inter);
     switch (st->type) {
@@ -39,10 +46,10 @@ Result runStatement(baseFunctionSig) {
             result.value->value = st->u.base_value.value;
             break;
         case base_var:
-            result = getBaseVar(callFunctionSig(st, var_list));
+            result = getBaseVar(CALL_INTER_FUNCTIONSIG(st, var_list));
             break;
         case operation:
-            result = operationStatement(callFunctionSig(st, var_list));
+            result = operationStatement(CALL_INTER_FUNCTIONSIG(st, var_list));
             printResult(result, "operation result = ", "");
             break;
         default:
@@ -51,23 +58,35 @@ Result runStatement(baseFunctionSig) {
     return result;
 }
 
-Result iterStatement(baseFunctionSig) {
+/**
+ * 局部程序运行statement
+ * @param st
+ * @param inter
+ * @param var_list
+ * @return
+ */
+Result iterStatement(INTER_FUNCTIONSIG) {
     Result result;
     Statement *base_st = st;
     VarList *new_var_list = var_list;
     while(base_st != NULL){
-        result = runStatement(callFunctionSig(base_st, new_var_list));
+        result = runStatement(CALL_INTER_FUNCTIONSIG(base_st, new_var_list));
         base_st = base_st->next;
     }
     return result;
 }
 
+/**
+ * 全局程序运行statement
+ * @param inter
+ * @return
+ */
 Result globalIterStatement(Inter *inter) {
     Result result;
     Statement *base_st = inter->statement;
     VarList *new_var_list = inter->var_list;
     while(base_st != NULL){
-        result = runStatement(callFunctionSig(base_st, new_var_list));
+        result = runStatement(CALL_INTER_FUNCTIONSIG(base_st, new_var_list));
         base_st = base_st->next;
     }
     return result;

+ 1 - 1
src/value.c

@@ -22,7 +22,7 @@ Value *makeValue(Inter *inter) {
     return tmp;
 }
 
-Value *makeNumberValue(number_type num, Inter *inter) {
+Value *makeNumberValue(NUMBER_TYPE num, Inter *inter) {
     Value *tmp;
     tmp = makeValue(inter);
     tmp->type = number;

+ 15 - 10
src/var.c

@@ -78,17 +78,22 @@ VarList *freeVarList(VarList *vl, bool self){
 
 }
 
-unsigned int time33(char *key){ // hash function
-    unsigned int hash = 5381;
+/**
+ * hashTable使用time33算法
+ * @param key
+ * @return
+ */
+HASH_INDEX time33(char *key){ // hash function
+    HASH_INDEX hash = 5381;
     while(*key){
-        hash += (hash << (unsigned int)5) + (*key++);
+        hash += (hash << (HASH_INDEX)5) + (*key++);
     }
-    return (hash & (unsigned int)0x7FFFFFFF) % MAX_SIZE;
+    return (hash & (HASH_INDEX)0x7FFFFFFF) % MAX_SIZE;
 }
 
 
 void addVar(char *name, LinkValue *value, VarList *var_list){
-    unsigned int index = time33(name);
+    HASH_INDEX index = time33(name);
     Var *base = var_list->hashtable->hashtable[index];
     if (base == NULL){
         var_list->hashtable->hashtable[index] = makeVar(name, value);
@@ -104,7 +109,7 @@ void addVar(char *name, LinkValue *value, VarList *var_list){
 
 LinkValue *findVar(char *name, VarList *var_list){
     LinkValue *tmp = NULL;
-    unsigned int index = time33(name);
+    HASH_INDEX index = time33(name);
     Var *base = var_list->hashtable->hashtable[index];
     if (base == NULL){
         goto return_;
@@ -120,9 +125,9 @@ LinkValue *findVar(char *name, VarList *var_list){
     return tmp;
 }
 
-LinkValue *findFromVarList(char *name, VarList *var_list, number_type times){
+LinkValue *findFromVarList(char *name, VarList *var_list, NUMBER_TYPE times){
     LinkValue *tmp = NULL;
-    for (number_type i=0;i<times && var_list->next != NULL;i++){
+    for (NUMBER_TYPE i=0; i < times && var_list->next != NULL; i++){
         var_list = var_list->next;
     }
     while (var_list != NULL){
@@ -136,8 +141,8 @@ LinkValue *findFromVarList(char *name, VarList *var_list, number_type times){
     return tmp;
 }
 
-void addFromVarList(char *name, VarList *var_list, number_type times, LinkValue *value){
-    for (number_type i=0;i<times && var_list->next != NULL;i++){
+void addFromVarList(char *name, VarList *var_list, NUMBER_TYPE times, LinkValue *value){
+    for (NUMBER_TYPE i=0; i < times && var_list->next != NULL; i++){
         var_list = var_list->next;
     }
     addVar(name, value, var_list);