Exemple #1
0
int main(int argc, char* *argv){
    init_parser();
    lenv *e = lenv_new();
    lenv_add_builtins(e);
    if (access(STD_LIB, F_OK) != -1){
        lval *err = builtin_load(e, lval_add(lval_sexpr(), lval_str((char *)STD_LIB)));
        if (err->type == LVAL_ERR) lval_println(err);
        lval_del(err);
    } else {
        printf("Can't find stdlib at %s.\n", STD_LIB);
    }

    if (argc > 1){
        for (int i = 1; i < argc; i++){
            lval *args = lval_add(lval_sexpr(), lval_str(argv[i]));
            lval *x = builtin_load(e, args);

            if (x->type == LVAL_ERR) lval_println(x);
            lval_del(x);
        }
    } else {
        puts("Aroma Version v0.0.0.1");
        puts("Press Ctrl+C to Exit.");

        char *input = NULL;
        mpc_result_t r;

        while (true){
            input = readline(">>> ");
            if (strlen(input) < 1) {
                continue;
            }
            add_history(input);
            if (mpc_parse("<stdin>", input, Lispy, &r)){
                /* mpc_ast_print(r.output); */
                lval *x = lval_eval(e, lval_read(r.output));
                lval_println(x);
                lval_del(x);

                mpc_ast_delete(r.output);
            } else {
                mpc_err_print(r.error);
                mpc_err_delete(r.error);
            }
            /* printf("%s\n", input); */
            free(input);
        }
    }

    lenv_del(e);
    cleanup_parser();

    return 0;
}
Exemple #2
0
int main(int argc, char** argv){
    puts("yafl Version 0.0.1");
    puts("Press CTRL+C to Exit\n");

    gen_parsers();
    lenv* e = lenv_new();
    lenv_add_builtins(e);
    lval* core = lval_add(lval_sexpr(), lval_str("src/yfl/core.yfl"));
    //printf("gets here");
    lval* core_loaded = builtin_load(e, core);

    if(core_loaded->type == LVAL_ERR) {lval_println(core_loaded);}
    lval_del(core_loaded);

    if(argc >= 2){ //execute file
        for(int i = 1; i < argc; i++){

            lval* args = lval_add(lval_sexpr(), lval_str(argv[i]));

            lval* x = builtin_load(e, args);

            if(x->type == LVAL_ERR) { lval_println(x); }
            lval_del(x);
        }
    }
    else{ //Start REPL

        while(1){
            char* input = readline("yafl> ");
            add_history(input);
            mpc_result_t r;
            if(mpc_parse("<stdin>", input, Yafl, &r)){

                lval* x = lval_eval(e, lval_read(r.output));
                lval_println(x);
                lval_del(x);
                mpc_ast_delete(r.output);
            }else{
                mpc_err_print(r.error);
                mpc_err_delete(r.error);
            }

            free(input);
        }
    }

    teardown_parsers();
    return 0;
}
Exemple #3
0
lval* lval_read_str(mpc_ast_t* t) {
    t->contents[strlen(t->contents)-1] = '\0';
    char* unescaped = malloc(strlen(t->contents+1)+1);
    strcpy(unescaped, t->contents+1);
    unescaped = mpcf_unescape(unescaped);
    lval* str = lval_str(unescaped);
    free(unescaped);
    return str;
}
Exemple #4
0
lval * ast_read(mpc_ast_t *t) {
    int i;
    lval *x;

    if (strstr(t->tag, "number")) return ast_read_num(t);

    if (strstr(t->tag, "symbol")) {
        if (!strcmp(t->contents, "true")) {
            return lval_boolean(1);
        }
        else if (!strcmp(t->contents, "false")) {
            return lval_boolean(0);
        }
        else {
            return lval_sym(t->contents);
        }
    }

    if (strstr(t->tag, "string")) {
        ssize_t sz = strlen(t->contents) + 1 - 2 /* quotes */;
        char *unescaped = malloc(sz);
        memcpy(unescaped, t->contents+1, sz);
        unescaped[sz-1] = '\0';
        unescaped = mpcf_unescape(unescaped);
        x = lval_str(unescaped);
        free(unescaped);
        return x;
    }

    if (strstr(t->tag, "comment")) return NULL;

    if (strstr(t->tag, "qexpr")) {
        x = lval_qexpr();
    }
    else {
        assert(
            (strcmp(t->tag, ">") == 0) ||
            strstr(t->tag, "sexpr")
        );
        x = lval_sexpr();
    }

    for (i = 0; i < t->children_num; ++i) {
        lval *v;
        if (
            (strlen(t->children[i]->contents) == 1) &&
            strstr("(){}", t->children[i]->contents)
        ) continue;
        if (!strcmp(t->children[i]->tag, "regex")) continue;
        v = ast_read(t->children[i]);
        if(v) lval_append(x, v);
    }

    return x;
}
Exemple #5
0
int main(int argc, char** argv) {

    init_parser();

    /* initialize environment */
    lenv* e = lenv_new();
    lenv_add_builtins(e);

    /* load standard library functions */
    lval* x = builtin_load(e, lval_add(lval_sexpr(), lval_str("prelude.lsp")));
    if (x->type == LVAL_ERR) { lval_println(x); }
    lval_del(x);

    if (argc >= 2) {

        /* read from command line */
        parse_args(e, argc, argv);

    } else {

        /* interactive prompt */
        say_hello();
        while (1) {
            char* input = prompt();
            lval* x = parse(input);
            if (x != NULL) {
                x = lval_eval(e, x);
                lval_println(x);
                lval_del(x);
            }
            free(input);
        }

    }

    /* cleanup */
    lenv_del(e);
    free_parser();

    return 0;
}
Exemple #6
0
int main(int argc, char** argv) {
    // Initialization
    parser_init();
    lenv* env = lenv_new();
    builtins_init(env);

    if (argc >= 2) {
        // Loop over file names
        for (int i = 1; i < argc; i++) {
            lval* args   = lval_add(lval_sexpr(), lval_str(argv[i]));
            lval* result = builtin_load(env, args);

            if (result->type == LVAL_ERR) {
                lval_println(env, result);
            }
            lval_del(result);
        }
    } else {
        // Welcome message
        puts("MLisp Version 0.1dev");
        puts("Enter 'quit' to exit\n");

        while (1) {
            char* input = read_input();
            add_history(input);

            if (input == NULL || strstr(input, "exit") || strstr(input, "quit")) {
                puts("Bye!");
                if (input != NULL) {
                    xfree(input);
                }
                break;
            }

            lval* result = NULL;
            mpc_err_t* parser_error = NULL;

            if (parse("<stdin>", input, env, &result, &parser_error)) {
                if (!(result->type == LVAL_SEXPR && result->count == 0)) {
                    char* repr = lval_repr(env, result);
                    printf("%s\n", repr);
                    xfree(repr);
                }

                lval_del(result);
            } else {
                mpc_err_print(parser_error);
                mpc_err_delete(parser_error);
            }

            xfree(input);
        }
    }

    lenv_del(env);

    // Undefine and delete our parsers
    parser_cleanup();

    return 0;
}
Exemple #7
0
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);
}