示例#1
0
文件: main.c 项目: alblang/alblang
int main(int argc, char** argv) {

  create_parsers();

  init();

  if (argc > 1) {

    parser_execute_file(argv[1]);

  } else {
    while(1) {
      char* input = readline(":>  ");

      add_history(input);

      if (strcmp(input, "") == 0) {
        continue;
      } else if (strcmp(input, "exit") == 0) {
        break;
      } else {
        parser_execute(input);
      }

      free(input);

    }
  }
  cleanup_parsers();

  return 0;
}
示例#2
0
文件: main.c 项目: sam159/klisp
int main(int argc, char** argv) {
    
    printf("KLisp Version %s\n", VERSION);
    
    //Init environment
    lenv* env = lenv_new();
    lenv_add_builtin_funcs(env);
    setup_parsers();
    
    //Attempt to import/run files specified on the command line
    if (argc > 1) {
        for(int i = 1; i < argc; i++) {
            lval* loadargs = lval_add(lval_s_expr(), lval_str(argv[i]));
            
            lval* result = builtin_load(env, loadargs);
            
            if (result->type == LVAL_ERR) {
                lval_println(result);
            }
            
            lval_delete(result);
        }
    }
    
    int exitcode = EXIT_SUCCESS;
    
    while(1) {
        char *input = readline("> ");
        if (NULL == input) {
            break;
        }
        add_history(input);
        
        mpc_ast_t* ast_result = tokenize(input);
        
        free(input);
        
        if (ast_result != NULL) {
            
            //Parse the ast
            lval* result = parse(ast_result);
            if (result == NULL) {
                result = lval_err(LERR_OTHER);
            }
            
            //Evaluate
            result = eval(env, result);
            
            BOOL exit = FALSE;
            if (result != NULL && result->type == LVAL_EXIT) {
                exit = TRUE;
                exitcode = result->data.exitcode;
            } else {
                //print the result
                lval_println(result);
            }
            
            //Cleanup
            lval_delete(result);
            mpc_ast_delete(ast_result);
            
            if (exit == TRUE) {;
                break;
            }
        }
        
    }
    
    lenv_delete(env);
    cleanup_parsers();
    
    return (exitcode);
}