Просмотр исходного кода

ncd: move parsing of the program out of NCDInterpreter

ambrop7 13 лет назад
Родитель
Сommit
16243308ac
4 измененных файлов с 54 добавлено и 36 удалено
  1. 10 24
      ncd/NCDInterpreter.c
  2. 5 3
      ncd/NCDInterpreter.h
  3. 21 6
      ncd/emncd.c
  4. 18 3
      ncd/ncd.c

+ 10 - 24
ncd/NCDInterpreter.c

@@ -37,7 +37,6 @@
 #include <misc/balloc.h>
 #include <misc/expstring.h>
 #include <base/BLog.h>
-#include <ncd/NCDConfigParser.h>
 #include <ncd/NCDSugar.h>
 #include <ncd/modules/modules.h>
 
@@ -124,9 +123,10 @@ static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t
 
 #define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
 
-int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params)
+int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterpreter_params params)
 {
-    ASSERT(program);
+    ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE));
+    ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD));
     ASSERT(params.handler_finished);
     ASSERT(params.num_extra_args >= 0);
     ASSERT(params.reactor);
@@ -146,6 +146,9 @@ int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_
     // set not terminating
     o->terminating = 0;
     
+    // set program
+    o->program = program;
+    
     // init string index
     if (!NCDStringIndex_Init(&o->string_index)) {
         BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
@@ -175,32 +178,16 @@ int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_
         }
     }
     
-    // parse config file
-    if (!NCDConfigParser_Parse((char *)program, program_len, &o->program)) {
-        BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
-        goto fail3;
-    }
-    
-    // include commands are not implemented currently
-    if (NCDProgram_ContainsElemType(&o->program, NCDPROGRAMELEM_INCLUDE)) {
-        BLog(BLOG_ERROR, "TODO include not implemented");
-        goto fail4;
-    }
-    if (NCDProgram_ContainsElemType(&o->program, NCDPROGRAMELEM_INCLUDE_GUARD)) {
-        BLog(BLOG_ERROR, "TODO include_guard not implemented");
-        goto fail4;
-    }
-    
     // desugar
     if (!NCDSugar_Desugar(&o->program)) {
         BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
-        goto fail4;
+        goto fail3;
     }
     
     // init placeholder database
     if (!NCDPlaceholderDb_Init(&o->placeholder_db, &o->string_index)) {
         BLog(BLOG_ERROR, "NCDPlaceholderDb_Init failed");
-        goto fail4;
+        goto fail3;
     }
     
     // init interp program
@@ -307,9 +294,6 @@ fail6:
 fail5:
     // free placeholder database
     NCDPlaceholderDb_Free(&o->placeholder_db);
-fail4:
-    // free program AST
-    NCDProgram_Free(&o->program);
 fail3:
     // free module index
     NCDModuleIndex_Free(&o->mindex);
@@ -320,6 +304,8 @@ fail1:
     // free string index
     NCDStringIndex_Free(&o->string_index);
 fail0:
+    // free program AST
+    NCDProgram_Free(&o->program);
     return 0;
 }
 

+ 5 - 3
ncd/NCDInterpreter.h

@@ -130,12 +130,14 @@ typedef struct {
  * Initializes and starts the interpreter.
  * 
  * @param o the interpreter
- * @param program pointer to NCD program text
- * @param program_len number of characters in program
+ * @param program the program to execute in AST format. The program must
+ *                not contain any 'include' or 'include_guard' directives.
+ *                The interpreter takes ownership of the program, regardless
+ *                of the success of this function.
  * @param params other parameters
  * @return 1 on success, 0 on failure
  */
-int NCDInterpreter_Init (NCDInterpreter *o, const char *program, size_t program_len, struct NCDInterpreter_params params) WARN_UNUSED;
+int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterpreter_params params) WARN_UNUSED;
 
 /**
  * Frees the interpreter.

+ 21 - 6
ncd/emncd.c

@@ -36,6 +36,7 @@
 #include <system/BTime.h>
 #include <system/BReactor.h>
 #include <ncd/NCDInterpreter.h>
+#include <ncd/NCDConfigParser.h>
 
 #include <generated/blog_channel_ncd.h>
 
@@ -71,9 +72,9 @@ int main ()
 }
 
 __attribute__((used))
-void emncd_start (const char *program, int loglevel)
+void emncd_start (const char *program_text, int loglevel)
 {
-    ASSERT(program);
+    ASSERT(program_text);
     ASSERT(loglevel >= 0);
     ASSERT(loglevel <= BLOG_DEBUG);
     
@@ -82,12 +83,26 @@ void emncd_start (const char *program, int loglevel)
         return;
     }
     
-    fprintf(stderr, "--- starting interpreter ---\n");
-    
     for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
         BLog_SetChannelLoglevel(i, loglevel);
     }
     
+    // parse program
+    NCDProgram program;
+    if (!NCDConfigParser_Parse((char *)program_text ,strlen(program_text), &program)) {
+        fprintf(stderr, "--- error: failed to parse the program ---\n");
+        return;
+    }
+    
+    // include commands are not implemented currently
+    if (NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE) || NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD)) {
+        fprintf(stderr, "--- error: include mechanism is not supported in emncd ---\n");
+        NCDProgram_Free(&program);
+        return;
+    }
+    
+    fprintf(stderr, "--- starting interpreter ---\n");
+    
     struct NCDInterpreter_params params;
     params.handler_finished = interpreter_handler_finished;
     params.user = NULL;
@@ -96,8 +111,8 @@ void emncd_start (const char *program, int loglevel)
     params.num_extra_args = 0;
     params.reactor = &reactor;
     
-    if (!NCDInterpreter_Init(&interpreter, program, strlen(program), params)) {
-        fprintf(stderr, "--- failed to initialize the interpreter (syntax error?) ---\n");
+    if (!NCDInterpreter_Init(&interpreter, program, params)) {
+        fprintf(stderr, "--- failed to initialize the interpreter ---\n");
         return;
     }
     

+ 18 - 3
ncd/ncd.c

@@ -44,6 +44,7 @@
 #include <udevmonitor/NCDUdevManager.h>
 #include <random/BRandom2.h>
 #include <ncd/NCDInterpreter.h>
+#include <ncd/NCDConfigParser.h>
 
 #ifdef BADVPN_USE_SYSLOG
 #include <base/BLog_syslog.h>
@@ -204,6 +205,22 @@ int main (int argc, char **argv)
         goto fail5;
     }
     
+    // parse program
+    NCDProgram program;
+    int res = NCDConfigParser_Parse((char *)file, file_len, &program);
+    free(file);
+    if (!res) {
+        BLog(BLOG_ERROR, "failed to parse program");
+        goto fail5;
+    }
+    
+    // include commands are not implemented currently
+    if (NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE) || NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD)) {
+        BLog(BLOG_ERROR, "TODO include not implemented");
+        NCDProgram_Free(&program);
+        goto fail5;
+    }
+    
     // setup interpreter parameters
     struct NCDInterpreter_params params;
     params.handler_finished = interpreter_handler_finished;
@@ -217,9 +234,7 @@ int main (int argc, char **argv)
     params.random2 = &random2;
     
     // initialize interpreter
-    int res = NCDInterpreter_Init(&interpreter, (const char *)file, file_len, params);
-    free(file);
-    if (!res) {
+    if (!NCDInterpreter_Init(&interpreter, program, params)) {
         goto fail5;
     }