浏览代码

实现内置函数 : input

SongZihuan 5 年之前
父节点
当前提交
b982e919d0
共有 4 个文件被更改,包括 36 次插入4 次删除
  1. 1 1
      README.md
  2. 2 0
      demo/8.gwf
  3. 29 3
      inter/cfunc.c
  4. 4 0
      inter/interpreter.h

+ 1 - 1
README.md

@@ -91,7 +91,7 @@ return x n  函数返回值x,返回n层
 * 魔法属性:``__name__,__id__``
 * 数据类型:``set(),link(),file()``
 * 内置类:``range,exp(列表推导试)``
-* 内置函数:``print(), input(), exit()[报出错误ExitException(Systemctl)], type(), isinherited()[A类是否继承B类], isinstance()[A object是否为B的实例(不检查继承关系)]``
+* 内置函数:``exit()[报出错误ExitException(Systemctl)], type(), isinherited()[A类是否继承B类], isinstance()[A object是否为B的实例(不检查继承关系)]``
 * @装饰器语法
 * 内存控制:控制``var``链表,控制``哈希表``,控制``var_list``
 * 多重继承优化:``super().__init__()``

+ 2 - 0
demo/8.gwf

@@ -0,0 +1,2 @@
+a = input("Please Enter Something")
+print("a = ", a)

+ 29 - 3
inter/cfunc.c

@@ -182,8 +182,8 @@ void login_official_func(int type, int is_class, var_list *the_var, char *name,
 }
 
 void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *,inter *), inter *global_inter){
-    int a[][2] = {{printf_func,0}};
-    char *name[] = {"print"};
+    int a[][2] = {{printf_func,0}, {input_func,0}};
+    char *name[] = {"print", "input"};
 
     int lenth = sizeof(a)/sizeof(a[0]);
     for(int i = 0;i < lenth;i+=1){
@@ -204,7 +204,7 @@ GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var,
             goto return_result;
         }
         char *str = malloc(0);
-        while(1){
+        while(true){
             GWARF_result tmp = traverse(tmp_s->u.value, out_var, false, global_inter);
             error_space(tmp, return_result, return_value);
             tmp = to_str(tmp.value, out_var, global_inter);
@@ -217,9 +217,35 @@ GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var,
             tmp_s = tmp_s->next;
         }
         printf("%s\n", str);  // 换行
+        free(str);
         return_value.u = statement_end;
         break;
     }
+    case input_func:{
+        if(tmp_s != NULL){  // 输出提示
+            GWARF_result tmp = traverse(tmp_s->u.value, out_var, false, global_inter);
+            error_space(tmp, return_result, return_value);
+            tmp = to_str(tmp.value, out_var, global_inter);
+            error_space(tmp, return_result, return_value);
+            printf("%s", tmp.value.value.string);
+        }
+        char p, *str = malloc(0);
+        int a = 0;
+        while(true){
+            a += 1;
+            p = getc(stdin);
+            if(p == '\n' || p == '\0'){
+                break;  // 遇到\n就跳出
+            }
+            str = realloc(str, a);
+            str[a - 1] = p;
+        }
+        str[a] = '\0';
+        return_value.value.type = STRING_value;
+        return_value.value.value.string = str;
+        return_value = to_object(return_value, global_inter);
+        break;
+    }
     default:
         break;
     }

+ 4 - 0
inter/interpreter.h

@@ -28,6 +28,9 @@ do{ \
 append_statement(tmp_statement,token.data.statement_value); \
 }while(0);
 
+// 日志文件
+extern FILE *debug, *status_log, *token_log, *token_info, *inter_info, *tree_info;
+
 // the type of data(GWARF_value)
 typedef enum{
     NUMBER_value = 1,  // [只允许系统使用] [1]
@@ -492,6 +495,7 @@ typedef enum{
 
 typedef enum{
     printf_func,
+    input_func,
     __init__func,
     __value__func,
     __add__func,