Quellcode durchsuchen

feat: 支持使用block内嵌代码块

SongZihuan vor 4 Jahren
Ursprung
Commit
c582945c9c
6 geänderte Dateien mit 50 neuen und 20 gelöschten Zeilen
  1. 3 1
      include/statement.h
  2. 3 2
      main.c
  3. 18 11
      parser/grammar.c
  4. 1 2
      src/run.c
  5. 14 0
      src/runoperation.c
  6. 11 4
      src/statement.c

+ 3 - 1
include/statement.h

@@ -67,6 +67,7 @@ struct Statement{
                 OPT_DIV = 4,
                 OPT_ASS = 5,
                 OPT_POINT = 6,
+                OPT_BLOCK = 7,
             } OperationType;
             struct Statement *left;
             struct Statement *right;
@@ -189,7 +190,8 @@ Statement *copyStatement(Statement *st);
 Statement *copyStatementCore(Statement *st);
 void connectStatement(Statement *base, Statement *new);
 
-Statement *makeOperationStatement(int type, long int line, char *file);
+Statement *makeOperationBaseStatement(enum OperationType type, long int line, char *file);
+Statement *makeOperationStatement(enum OperationType type, Statement *left, Statement *right);
 Statement *makeBaseLinkValueStatement(LinkValue *value, long int line, char *file);
 Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long int line, char *file);
 Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file);

+ 3 - 2
main.c

@@ -21,8 +21,6 @@ int main(int argc, char *argv[]) {
 
 
 /** TODO-szh List
- * 断言
- * 内嵌代码块
  * 字面量后缀
  * 官方函数
  * 官方类
@@ -33,4 +31,7 @@ int main(int argc, char *argv[]) {
  * goto 语句
  * label 标签
  * yield 语句
+ * 括号无视换行
+ * \ 无视换行
+ * # 注释设定
  */

+ 18 - 11
parser/grammar.c

@@ -644,18 +644,16 @@ void parserDef(PASERSSIGNATURE){
  */
 void parserCode(PASERSSIGNATURE) {
     long int line = 0;
-    Statement *st = NULL;
+    Statement *st = makeStatement(line, pm->file);
     while (true){
-        if (readBackToken(pm) == MATHER_LC){
-            line = delToken(pm);
+        if (readBackToken(pm) != MATHER_LC)
             goto again_;
-        }
+        line = delToken(pm);
         break;
         again_:
         if (!checkToken(pm, MATHER_ENTER))
             goto return_;
     }
-    st = makeStatement(line, pm->file);
     parserCommandList(CALLPASERSSIGNATURE, false, st);
     if (!call_success(pm))
         goto error_;
@@ -699,7 +697,7 @@ void parserOperation(PASERSSIGNATURE){
 bool switchAssignment(PASERSSIGNATURE, int symbol, Statement **st){
     switch (symbol) {
         case MATHER_ASSIGNMENT:
-            *st = makeOperationStatement(OPT_ASS, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_ASS, 0, pm->file);
             break;
         default:
             return false;
@@ -761,10 +759,10 @@ void parserTuple(PASERSSIGNATURE){
 bool switchPolynomial(PASERSSIGNATURE, int symbol, Statement **st){
     switch (symbol) {
         case MATHER_ADD:
-            *st = makeOperationStatement(OPT_ADD, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_ADD, 0, pm->file);
             break;
         case MATHER_SUB:
-            *st = makeOperationStatement(OPT_SUB, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_SUB, 0, pm->file);
             break;
         default:
             return false;
@@ -786,10 +784,10 @@ void parserPolynomial(PASERSSIGNATURE){
 bool switchFactor(PASERSSIGNATURE, int symbol, Statement **st){
     switch (symbol) {
         case MATHER_MUL:
-            *st = makeOperationStatement(OPT_MUL, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_MUL, 0, pm->file);
             break;
         case MATHER_DIV:
-            *st = makeOperationStatement(OPT_DIV, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_DIV, 0, pm->file);
             break;
         default:
             return false;
@@ -844,7 +842,7 @@ void parserCallBack(PASERSSIGNATURE){
 bool switchPoint(PASERSSIGNATURE, int symbol, Statement **st){
     switch (symbol) {
         case MATHER_POINT:
-            *st = makeOperationStatement(OPT_POINT, 0, pm->file);
+            *st = makeOperationBaseStatement(OPT_POINT, 0, pm->file);
             break;
         default:
             return false;
@@ -961,6 +959,15 @@ void parserBaseValue(PASERSSIGNATURE){
         }
         st = makeBaseDictStatement(pt, value_token->line, pm->file);
     }
+    else if (MATHER_BLOCK == value_token->token_type){
+        Statement *block = NULL;
+        if (!callParserCode(CALLPASERSSIGNATURE, &block, "Don't get a while code", value_token->line)) {
+            syntaxError(pm, syntax_error, value_token->line, 1, "Don't get block command");
+            freeToken(value_token, true, true);
+            goto return_;
+        }
+        st = makeOperationStatement(OPT_BLOCK, block, NULL);
+    }
     else if (MATHER_PROTECT == value_token->token_type || MATHER_PRIVATE == value_token->token_type || MATHER_PUBLIC == value_token->token_type){
         if (MATHER_COLON != readBackToken(pm)){
             syntaxError(pm, syntax_error, value_token->line, 1, "Don't get a : after aut token");

+ 1 - 2
src/run.c

@@ -165,9 +165,8 @@ bool operationSafeInterStatement(INTER_FUNCTIONSIG){
 bool ifBranchSafeInterStatement(INTER_FUNCTIONSIG){
     ResultType type;
     type = iterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, father));
-    if (run_continue_type(type)){
+    if (run_continue_type(type))
         return false;
-    }
     if (type == rego_return){
         result->times--;
         if (result->times < 0)

+ 14 - 0
src/runoperation.c

@@ -6,6 +6,7 @@ ResultType mulOperation(INTER_FUNCTIONSIG);
 ResultType divOperation(INTER_FUNCTIONSIG);
 ResultType assOperation(INTER_FUNCTIONSIG);
 ResultType pointOperation(INTER_FUNCTIONSIG);
+ResultType blockOperation(INTER_FUNCTIONSIG);
 
 /**
  * operation的整体操作
@@ -35,6 +36,9 @@ ResultType operationStatement(INTER_FUNCTIONSIG) {
         case OPT_POINT:
             pointOperation(CALL_INTER_FUNCTIONSIG(st, var_list, result, father));
             break;
+        case OPT_BLOCK:
+            blockOperation(CALL_INTER_FUNCTIONSIG(st, var_list, result, father));
+            break;
         default:
             setResult(result, inter, father);
             break;
@@ -150,6 +154,16 @@ ResultType divOperation(INTER_FUNCTIONSIG) {
     return result->type;
 }
 
+ResultType blockOperation(INTER_FUNCTIONSIG) {
+    ResultType type;
+    var_list = pushVarList(var_list, inter);
+    type = operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.operation.left, var_list, result, father));
+    if (run_continue_type(type) && st->aut != auto_aut)
+        result->value->aut = st->aut;
+    popVarList(var_list);
+    return type;
+}
+
 ResultType pointOperation(INTER_FUNCTIONSIG) {
     LinkValue *left;
 

+ 11 - 4
src/statement.c

@@ -46,7 +46,7 @@ Statement *makeBaseLinkValueStatement(LinkValue *value, long int line, char *fil
     return tmp;
 }
 
-Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long int line, char *file) {
+Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long int line, char *file){
     Statement *tmp = makeStatement(line, file);
     tmp->type = base_value;
     tmp->u.base_value.type = type;
@@ -55,7 +55,7 @@ Statement *makeBaseStrValueStatement(char *value, enum BaseValueType type, long
     return tmp;
 }
 
-Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file) {
+Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file){
     Statement *tmp = makeStatement(line, file);
     tmp->type = base_var;
     tmp->u.base_var.name = memStrcpy(name);
@@ -71,14 +71,14 @@ Statement *makeBaseSVarStatement(Statement *name, Statement *times){
     return tmp;
 }
 
-Statement *makeBaseDictStatement(Parameter *pt, long int line, char *file) {
+Statement *makeBaseDictStatement(Parameter *pt, long int line, char *file){
     Statement *tmp = makeStatement(line, file);
     tmp->type = base_dict;
     tmp->u.base_dict.dict = pt;
     return tmp;
 }
 
-Statement *makeOperationStatement(int type, long int line, char *file) {
+Statement *makeOperationBaseStatement(enum OperationType type, long int line, char *file){
     Statement *tmp = makeStatement(line, file);
     tmp->type = operation;
     tmp->u.operation.OperationType = type;
@@ -87,6 +87,13 @@ Statement *makeOperationStatement(int type, long int line, char *file) {
     return tmp;
 }
 
+Statement *makeOperationStatement(enum OperationType type, Statement *left, Statement *right){
+    Statement *tmp = makeOperationBaseStatement(type, left->line, left->code_file);
+    tmp->u.operation.left = left;
+    tmp->u.operation.right = right;
+    return tmp;
+}
+
 Statement *makeTupleStatement(Parameter *pt, enum ListType type, long int line, char *file) {
     Statement *tmp = makeStatement(line, file);
     tmp->type = base_list;