lval * ast_load_eval(char* fn, lenv *env) { lval *r; mpc_result_t parsed; lval *ast; lval *result; if (mpc_parse_contents(fn, Lispy, &parsed)) { ast = ast_read(parsed.output); mpc_ast_delete(parsed.output); while (ast->expr->count) { result = lval_eval(expr_pop(ast->expr, 0), env); if (result->type == LVAL_ERR) lval_println(result); lval_del(result); } lval_del(ast); r = lval_sexpr(); } else { char *err = mpc_err_string(parsed.error); mpc_err_delete(parsed.error); r = lval_err(err); free(err); } return r; }
ussval_t* builtin_load(ussenv_t* e, ussval_t* a) { UASSERT_NUM("load", a, 1); UASSERT_TYPE("load", a, 0, UVAL_STR); /* Parse File given by string name */ mpc_result_t r; parser_t *parser = parser_toplevel(); if (mpc_parse_contents(a->cell[0]->str, parser->uss, &r)) { fprintf(stderr, "DEBUG: loading: %s\n", a->cell[0]->str); ussenv_put(e, ussval_new_sym("file-path"), a->cell[0]); ussenv_put(e, ussval_new_sym("file-dir"), ussval_new_str(dirname(a->cell[0]->str))); /* Read contents */ ussval_t *expr = ussval_read(r.output); mpc_ast_delete(r.output); /* Evaluate each Expression */ while (expr->count) { ussval_t *x = ussval_eval(e, ussval_pop(expr, 0)); /* If Evaluation leads to error print it */ if (x->type == UVAL_ERR) { ussval_println(x); } ussval_del(x); } /* Delete expressions and arguments */ ussval_del(expr); ussval_del(a); parser_cleanup(parser); //lenv_put(e, lval_sym("file-path"), LVAL_NIL); //lenv_put(e, lval_sym("file-dir"), LVAL_NIL); /* Return empty list */ return ussval_new_sexpr(); } else { /* Get Parse Error as String */ char *err_msg = mpc_err_string(r.error); mpc_err_delete(r.error); /* Create new error message using it */ ussval_t *err = ussval_new_err("Could not load Library %s", err_msg); free(err_msg); ussval_del(a); parser_cleanup(parser); /* Cleanup and return error */ return err; } }
int main(int argc, char **argv) { mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* Symbol = mpc_new("symbol"); mpc_parser_t* String = mpc_new("string"); mpc_parser_t* Comment = mpc_new("comment"); mpc_parser_t* Sexpr = mpc_new("sexpr"); mpc_parser_t* Qexpr = mpc_new("qexpr"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Lispy = mpc_new("lispy"); mpca_lang(MPCA_LANG_PREDICTIVE, " number \"number\" : /[0-9]+/ ; " " symbol \"symbol\" : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; " " string \"string\" : /\"(\\\\.|[^\"])*\"/ ; " " comment : /;[^\\r\\n]*/ ; " " sexpr : '(' <expr>* ')' ; " " qexpr : '{' <expr>* '}' ; " " expr : <number> | <symbol> | <string> " " | <comment> | <sexpr> | <qexpr> ; " " lispy : /^/ <expr>* /$/ ; ", Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy); if (argc > 1) { mpc_result_t r; if (mpc_parse_contents(argv[1], Lispy, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } else { mpc_result_t r; if (mpc_parse_pipe("<stdin>", stdin, Lispy, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } mpc_cleanup(8, Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy); return 0; }
int main(int argc, char **argv) { mpc_parser_t *Expr = mpc_new("expression"); mpc_parser_t *Prod = mpc_new("product"); mpc_parser_t *Value = mpc_new("value"); mpc_parser_t *Maths = mpc_new("maths"); mpca_lang(MPCA_LANG_PREDICTIVE, " expression : <product> (('+' | '-') <product>)*; " " product : <value> (('*' | '/') <value>)*; " " value : /[0-9]+/ | '(' <expression> ')'; " " maths : /^/ <expression> /$/; ", Expr, Prod, Value, Maths, NULL); mpc_print(Expr); mpc_print(Prod); mpc_print(Value); mpc_print(Maths); if (argc > 1) { mpc_result_t r; if (mpc_parse_contents(argv[1], Maths, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } else { mpc_result_t r; if (mpc_parse_pipe("<stdin>", stdin, Maths, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } mpc_cleanup(4, Expr, Prod, Value, Maths); return 0; }
int main(int argc, char **argv) { mpc_parser_t* Adjective = mpc_new("adjective"); mpc_parser_t* Noun = mpc_new("noun"); mpc_parser_t* Phrase = mpc_new("phrase"); mpc_parser_t* Doge = mpc_new("doge"); mpca_lang(MPCA_LANG_DEFAULT, " adjective : \"wow\" | \"many\" | \"so\" | \"such\"; " " noun : \"lisp\" | \"language\" | \"c\" | \"book\" | \"build\"; " " phrase : <adjective> <noun>; " " doge : /^/ <phrase>* /$/; ", Adjective, Noun, Phrase, Doge, NULL); if (argc > 1) { mpc_result_t r; if (mpc_parse_contents(argv[1], Doge, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } else { mpc_result_t r; if (mpc_parse_pipe("<stdin>", stdin, Doge, &r)) { mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } mpc_cleanup(4, Adjective, Noun, Phrase, Doge); return 0; }
lval* builtin_load(lenv* e, lval* a) { LASSERT_NUM("load", a, 1); LASSERT_TYPE("load", a, 0, LVAL_STR); //Parse file given by string as filename mpc_result_t r; if (mpc_parse_contents(a->cell[0]->str, blisp, &r)) { //Read Contents lval* expr = lval_read(r.output); mpc_ast_delete(r.output); //Evaluate expressions on stack while (expr->count) { lval* x = lval_eval(e, lval_pop(expr, 0)); if (x->type == LVAL_ERR) { lval_println(x); } lval_del(x); } //Delete expression and args lval_del(expr); lval_del(a); //Return empty list return lval_sexpr(); } else { //Get error message as string and return as lval_err char* err_msg = mpc_err_string(r.error); mpc_err_delete(r.error); lval* err = lval_err("Could not load Library %s", err_msg); free(err_msg); lval_del(a); //Cleanup and return error return err; } }