浏览代码

feat: 更详细的用户反馈

SongZihuan 3 年之前
父节点
当前提交
28203f812d
共有 8 个文件被更改,包括 75 次插入33 次删除
  1. 6 2
      CMakeLists.txt
  2. 1 1
      deps/fflags
  3. 6 0
      src/CMakeLists.txt
  4. 18 0
      src/help-info
  5. 1 0
      src/help_info.c.in
  6. 36 28
      src/main.c
  7. 4 1
      src/main_build.c
  8. 3 1
      src/main_run.c

+ 6 - 2
CMakeLists.txt

@@ -51,8 +51,12 @@ set(CMAKE_INSTALL_RPATH
 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
 set(CMAKE_MACOSX_RPATH TRUE)
 include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR})  # 添加默认的include路径
-add_compile_definitions(aFunVersion="${aFunVersion}" aFunDescription="${aFunDescription}")
-
+set(base_compile_definitions
+    aFunVersion="${aFunVersion}"
+    aFunDescription="${aFunDescription}"
+    systemName="${CMAKE_SYSTEM_NAME}"
+    compilerID="${CMAKE_C_COMPILER_ID}")  # 默认的预定义宏
+add_compile_definitions(${base_compile_definitions})
 
 include(${CMAKE_CURRENT_LIST_DIR}/deps/deps.cmake)  # 安装依赖
 add_subdirectory(src)

+ 1 - 1
deps/fflags

@@ -1 +1 @@
-Subproject commit e649183be6140b125d58c6b833c6c24b391e0ade
+Subproject commit 4cb73963f77455c09bcfeaed8586775eee90cd36

+ 6 - 0
src/CMakeLists.txt

@@ -15,6 +15,11 @@ file(GLOB include_h
      LIST_DIRECTORIES FALSE
      "${PROJECT_SOURCE_DIR}/include/*.h")
 
+file(READ ${CMAKE_CURRENT_LIST_DIR}/help-info _help-info)
+string(REPLACE "\n" "\\n" help-info "${_help-info}")
+configure_file(${CMAKE_CURRENT_LIST_DIR}/help_info.c.in ${CMAKE_BINARY_DIR}/help_info.c @ONLY)
+list(APPEND source "${CMAKE_BINARY_DIR}/help_info.c")
+
 option(AFUN_BUILD_ALL "Build all types of aFun" ON)
 set(_build_all ${AFUN_BUILD_ALL})
 
@@ -31,6 +36,7 @@ endif()
 foreach(tgt IN LISTS aFunList)
     target_sources(${tgt} PRIVATE ${source} ${private_h} ${include_h})
     target_include_directories(${tgt} PRIVATE ${PROJECT_SOURCE_DIR}/include)
+    get_target_property(tmp ${tgt} COMPILE_DEFINITIONS)
     set_target_properties(${tgt} PROPERTIES PUBLIC_HEADER "${include_h}")
 endforeach()
 

+ 18 - 0
src/help-info

@@ -0,0 +1,18 @@
+[option]
+    help
+        -v Show    version info
+        -h Show    help page
+    run
+        -e/--eval    Run argument as code
+        -f/--file    Run file (.aun or .aub)
+        -s/--source    Run source file
+        -b/--byte    Run byte-code file
+        --no-aub    Do not save byte-code after run source file
+        --no-cl    Do not use command line
+    build
+        -o File output path
+        -p File output dir
+        -f Force build
+
+[Github page]
+    <https://github.com/aFun-org/aFunlang>

+ 1 - 0
src/help_info.c.in

@@ -0,0 +1 @@
+const char *help_info = "@help-info@";

+ 36 - 28
src/main.c

@@ -13,7 +13,7 @@ ff_defArg(run, true)
                 ff_argRule('f', file, must, 'f')
                 ff_argRule('s', source, must, 's')
                 ff_argRule('b', byte, must, 'b')
-                ff_argRule(NUL, no-afb, not, 'a')
+                ff_argRule(NUL, no-aub, not, 'a')
                 ff_argRule(NUL, no-cl, not, 'n')
 ff_endArg(run, true);
 
@@ -33,10 +33,11 @@ static void printVersion(void);
 static void printHelp(void);
 static int mainRun(ff_FFlags *ff);
 static int mainBuild(ff_FFlags *ff);
+extern const char *help_info;
 
 int main(int argc, char **argv) {
     int exit_code = EXIT_SUCCESS;
-    ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, aFunlang_exe);
+    ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, stderr, aFunlang_exe);
     if (ff == NULL)
         return EXIT_FAILURE;
     char *child = ff_getChild(ff);
@@ -59,9 +60,15 @@ static void printVersion(void) {
     printf(aFunDescription "\n");
 }
 
+static void printWelcomeInfo(void) {
+    printf("\naFunlang " aFunVersion " CommandLine (" __DATE__ ", " __TIME__ ")\n");
+    printf("["compilerID"] on "systemName"\n");
+    printf("(Enter the aFun code to run in the top activity)\n");
+}
+
 static void printHelp(void) {
     printf("aFunlang Usage:\n");
-    printf("Github: <https://github.com/aFun-org/aFunlang>\n");
+    printf("%s\n", help_info);
 }
 
 /*
@@ -69,7 +76,7 @@ static void printHelp(void) {
  * 目标: 打印参数错误信息
  */
 static void printError(ff_FFlags *ff) {
-    fprintf(stderr, "Command line argumenterror (%s)\n", ff_getChild(ff));
+    fprintf(stderr, "[aFunlang] Command line argument error (%s)\n", ff_getChild(ff));
     printHelp();
 }
 
@@ -106,14 +113,14 @@ out:
     return EXIT_SUCCESS;
 }
 
-static RunList *getRunList(ff_FFlags *ff, bool *command_line, bool *save_afb) {
+static RunList *getRunList(ff_FFlags *ff, bool *command_line, bool *save_aub) {
     char *text = NULL;
     RunList *run_list = NULL;
     RunList **prl = &run_list;
     int mark;
 
     *command_line = true;
-    *save_afb = true;
+    *save_aub = true;
 
     while (1) {
         mark = ff_getopt(&text, ff);
@@ -134,11 +141,12 @@ static RunList *getRunList(ff_FFlags *ff, bool *command_line, bool *save_afb) {
                 *command_line = false;
                 break;
             case 'a':
-                *save_afb = false;
+                *save_aub = false;
                 break;
             case -1:
                 goto out;
             default:
+                *command_line = false;  // 命令行也不启动
                 printError(ff);
                 freeAllRunList(run_list);
                 return NULL;
@@ -153,22 +161,25 @@ out:
 
 static int mainRun(ff_FFlags *ff) {
     bool command_line = true;
-    bool save_afb = true;
+    bool save_aub = true;
     int exit_code;
-    RunList *rl = getRunList(ff, &command_line, &save_afb);
+    RunList *rl = getRunList(ff, &command_line, &save_aub);
 
-    af_Environment *env = creatAFunEnviroment();
-    if (rl != NULL)
-        exit_code = runCodeFromRunList(rl, NULL, save_afb, env);
-    else if (!command_line) {
-        fprintf(stderr, "Not code to run.\n");
-        printHelp();
+    if (rl == NULL && !command_line) {
+        fprintf(stderr, "[aFunlang] There are not code to run.\n");
+        printError(ff);
         return EXIT_FAILURE;
     }
 
-    if (command_line) {
-        while (isCoreExit(env) != 1)
+    af_Environment *env = creatAFunEnviroment();
+    if (rl != NULL)
+        exit_code = runCodeFromRunList(rl, NULL, save_aub, env);
+
+    if (command_line && isCoreExit(env) != 1) {
+        printWelcomeInfo();
+        do
             exit_code = runCodeFromStdin("stdin", stderr, env);
+        while (isCoreExit(env) != 1);
     }
 
     destructAFunEnvironment(env);
@@ -189,14 +200,14 @@ static int mainBuild(ff_FFlags *ff) {
         switch (mark) {
             case 'o':
                 if (path != NULL) {
-                    fprintf(stderr, "Parameter conflict.\n");
+                    fprintf(stderr, "[aFunlang] Argument conflict (out, path).\n");
                     goto error;
                 }
                 out_put = text;
                 break;
             case 'p':
                 if (out_put != NULL) {
-                    fprintf(stderr, "Parameter conflict.\n");
+                    fprintf(stderr, "[aFunlang] Argument conflict (out, path).\n");
                     goto error;
                 }
                 path = text;
@@ -207,8 +218,7 @@ static int mainBuild(ff_FFlags *ff) {
             case -1:
                 goto out;
             default:
-                printError(ff);
-                return EXIT_FAILURE;
+                goto error;
         }
     }
 
@@ -218,17 +228,15 @@ out:
 
         /* 如果没有参数 */
         if (!ff_getopt_wild(&text, ff)) {
-            fprintf(stderr, "No source file to build.\n");
-            printHelp();
-            return EXIT_FAILURE;
+            fprintf(stderr, "[aFunlang] There are not source file to build.\n");
+            goto error;
         } else
             in = text;
 
         /* 如果还有第二个参数 */
         if (ff_getopt_wild(&text, ff)) {
-            fprintf(stderr, "Too many source file to build.\n");
-            printHelp();
-            return EXIT_FAILURE;
+            fprintf(stderr, "[aFunlang] There are too many source file to build. (Do not use --out option)\n");
+            goto error;
         }
 
         return buildFileOutput(out_put, in, force);
@@ -245,6 +253,6 @@ out:
     return exit_code;
 
 error:
-    printHelp();
+    printError(ff);
     return EXIT_FAILURE;
 }

+ 4 - 1
src/main_build.c

@@ -10,10 +10,13 @@ int buildFileOutput(FilePath out, FilePath in, bool force) {
             return -1;
         }
 
-        if (time_2 >= time_1)
+        if (time_2 >= time_1) {
+            fprintf(stdout, "[aFunlang] Source already build (%s), use --force to build again.\n", in);
             return 0;
+        }
     }
 
+    fprintf(stdout, "[aFunlang] File (%s) will be build. (%s)\n", in, out);
     return buildFile(out, in, stderr);
 }
 

+ 3 - 1
src/main_run.c

@@ -78,8 +78,10 @@ int runCodeFromRunList(RunList *run_list, RunList **bak, bool save_afb, af_Envir
                 break;
         }
 
-        if (exit_code != 0)
+        if (isCoreExit(env) == 1) {
+            fprintf((exit_code == 0) ? stdout : stderr, "[aFunlang] aFun core exit, exit_code = %d\n", exit_code);
             break;
+        }
     }
 
     if (bak != NULL)