Example #1
0
void init(void) {
    parser_init();
    lispy = parser_get();
    env = lenv_new();
    builtins_init(env);

    _parse("(def {nil} {})");
    _parse("(def {true} 1)");
    _parse("(def {otherwise} true)");
    _parse(
        "(def {function} (lambda {args body} {\
            def (head args) (lambda (tail args) body)\
        }))"
Example #2
0
int init_tests( void ) {
	ASSERT_EQUAL(NO_FAILURE, builtins_init());

	return 0;
}
Example #3
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;
}