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; }
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; }
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; }
/* Parsing Stuff */ int main(int argc, char** argv){ puts("Lispy Version 0.0.1"); puts("Press Ctrl+c to Exit\n"); while(1) { /* Prompt and readline */ char* input = readline("lispy> "); if(!input) { printf("\n"); continue; } if(input[0]){ add_history(input); } /* Try to parse stuff and print out the AST. Otherwise print the error */ mpc_result_t r; if(lispy_parse("<stdin>", input, &r)){ lval_println(eval(r.output)); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } free(input); } return 0; }
int main(int argc, char **argv) { mpc_parser_t *plisp = plisp_set_grammar(); puts("PLisp v.0.0.0.1."); //Pablo Lisp lol puts("Press Ctrl-C to exit."); global = new_lenv(128); lenv_add_builtins(global); while ((input = linenoise("mylsp> ")) != NULL) { if (input[0] != '\0') { mpc_result_t r; linenoiseHistoryAdd(input); if ((mpc_parse("<stdin>", input, plisp, &r))) { lval *val = lval_eval(global, lval_read(r.output)); lval_println(val); lval_del(val); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } } free(input); } lenv_del(global); plisp_cleanup_grammar(); return 0; }
int main(int argc, char** argv){ /* Create Some Parsers */ mpc_parser_t* number = mpc_new("number"); mpc_parser_t* operator = mpc_new("operator"); mpc_parser_t* expr = mpc_new("expr"); mpc_parser_t* lispy = mpc_new("lispy"); /* Define them with the following Language */ mpca_lang(MPCA_LANG_DEFAULT, " \ number : /-?[0-9]+/; \ operator: '+'|'-'|'*'|'/'; \ expr : <number> |'('<operator><expr>+')'; \ lispy : /^/ <operator> <expr>+ /$/; \ ", number,operator,expr,lispy ); /* Print Version and Exit Information */ puts("Lispy Version 0.0.0.0.3"); puts("Press Ctrl_c to Exit\n"); /* In a never ending loop */ while(1){ /* Output our prompt and get input*/ char * input = readline("lispy>"); /* add input to history*/ add_history(input); /* Attempt to parse the user input */ mpc_result_t r; if (mpc_parse("<stdin>",input,lispy,&r)){ lval result = eval(r.output); lval_println(result); mpc_ast_delete(r.output); }else{ /* otherwise print and delete the error */ mpc_err_print(r.error); mpc_err_delete(r.error); } /* Echo input bace to user */ // printf("No you're a %s",input); free(input); } mpc_cleanup(4,number,operator,expr,lispy); return 0; }
int main(int argc, char** argv) { /* Create some parsers */ mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* Operator = mpc_new("operator"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Lispy = mpc_new("lispy"); /* Define them with the following Language */ mpca_lang(MPCA_LANG_DEFAULT, " \ number: /-?[0-9]+/; \ operator: '+' | '-' | '*' | '/'; \ expr: <number> | '(' <operator> <expr>+ ')'; \ lispy: /^/ <operator> <expr>+ /$/; \ ", Number, Operator, Expr, Lispy); /* Print version and exit information */ puts("Lispy Version 0.0.2"); puts("Press Ctrl+c to Exit\n"); /* In a never ending loop */ while (1) { /* Output our prompt */ char* input = readline("lispy> "); /* Add input to history */ add_history(input); /* Attempt to parse the user input */ mpc_result_t r; if (mpc_parse("<stdin>", input, Lispy, &r)) { /* On success print and delete the AST */ mpc_ast_print(r.output); /* Eval */ lval result = eval(r.output); lval_println(result); mpc_ast_delete(r.output); } else { /* Otherwise print and delete the Error */ mpc_err_print(r.error); mpc_err_delete(r.error); } /* Free retrieved input */ free(input); } /* Undefine and delete our parsers */ mpc_cleanup(4, Number, Operator, Expr, Lispy); return 0; }
int main(int argc, char *argv[]) { /* Create types */ mpc_parser_t *Number = mpc_new("number"); mpc_parser_t *Bool = mpc_new("bool"); mpc_parser_t *Symbol = mpc_new("symbol"); mpc_parser_t *Sexp = mpc_new("sexp"); mpc_parser_t *Qexp = mpc_new("qexp"); mpc_parser_t *Expr = mpc_new("expr"); mpc_parser_t *Smallisp = mpc_new("Smallisp"); /* Definintions */ mpca_lang(MPC_LANG_DEFAULT, "number : /-?[0-9]+/ ; " "bool : \"true\" | \"false\" ; " "symbol : /[a-zA-Z0-9_+\\-*\\/=<>!&]+/; " "sexp : '(' <expr>* ')'; " "qexp : '{' <expr>* '}'; " "expr : <number> | <bool> | <char> | <symbol> | <sexp> | <qexp>; " "Smallisp : /^/ <sexp>* /$/ ; ", Number, Bool, Symbol, Sexp, Qexp, Expr, Smallisp, NULL); puts("Lispy version 0.0.0.7\n"); puts("Press Ctrl-C to exit\n"); lenv *env = lenv_new(); lenv_add_builtins(env); while(1) { char *input = readline("slisp> "); add_history(input); mpc_result_t r; if (mpc_parse("<stdin>", input, Smallisp, &r)) { /* On success print AST */ lval *result = lval_eval(env, lval_read(r.output)); lval_println(result); lval_del(result); mpc_ast_print(r.output); mpc_ast_delete(r.output); } else { /* Print error */ mpc_err_print(r.error); mpc_err_print(r.error); } free(input); } lenv_del(env); mpc_cleanup(7, Number, Bool, Symbol, Sexp, Qexp, Expr, Smallisp); return 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; }
void parse_input(char* input, mpc_parser_t* Lambo) { mpc_result_t r; if (mpc_parse("<stdin>", input, Lambo, &r)) { lval* x = lval_eval(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); } }
int main(int argc, char** argv) { /* Create some parsers */ mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* Operator = mpc_new("operator"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Lithp = mpc_new("lithp"); /* Define them with the following language */ mpca_lang(MPCA_LANG_DEFAULT, "\ number : /-?[0-9]+/ ; \ operator : '+' | '-' | '*' | '/' | '%' | '^' | \"min\" | \"max\" ; \ expr : <number> | '(' <operator> <expr>+ ')' ; \ lithp : /^/ <operator> <expr>+ /$/ ; \ ", Number, Operator, Expr, Lithp); /* Print version and exit */ puts("Lithp Version 0.0.0.0.1"); puts("Suitable for absolutely nothing!"); puts("Press Ctrl-C to exit\n"); /* never ending storrr-y! */ while(1) { /* output our prompt and get input */ char* input = readline("lithp> "); /* add to our history */ add_history(input); /* Attempt to parse the user input */ mpc_result_t r; if (mpc_parse("<stdin>", input, Lithp, &r)) { /* On success, print the AST */ //mpc_ast_print(r.output); /* Get a real result and print it */ lval result = eval(r.output); lval_println(result); mpc_ast_delete(r.output); } else { /* Otherwise, print the error */ mpc_err_print(r.error); mpc_err_delete(r.error); } /* free the malocs! */ free(input); } /* Undefine and Delete our Parsers. Or else... */ mpc_cleanup(4, Number, Operator, Expr, Lithp); return 0; }
int main(int argc, char** argv) { mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* Operator = mpc_new("operator"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Lispy = mpc_new("lispy"); mpca_lang(MPC_LANG_DEFAULT, " \ number : /-?[0-9]+/ ; \ operator : '+' | '-' | '*' | '/' ; \ expr : <number> | '(' <operator> <expr>+ ')' ; \ lispy : /^/ <operator> <expr>+ /$/ ; \ ", Number, Operator, Expr, Lispy); puts("Lispy Version 0.0.0.0.4"); puts("Press Ctrl+c to Exit\n"); while (1) { char* input = readline("lispy> "); add_history(input); mpc_result_t r; if (mpc_parse("<stdin>", input, Lispy, &r)) { lval result = eval(r.output); lval_println(result); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } free(input); } mpc_cleanup(4, Number, Operator, Expr, Lispy); return 0; }
int main(int argc, char** argv) { mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* Operator = mpc_new("operator"); mpc_parse_t* Sexpr = mpc_new("sexpr"); mpc_parse_t* Qexpr = mpc_new("qexpr"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Edward = mpc_new("edward"); mpca_lang(MPC_LANG_DEFAULT, " \ number : /-?[0-9]+/ ; \ operator : '+' | '-' | '*' | '/' | '%'; \ sexpr : '(' <expr> ')'; \ qexpr : '{' <expr> '}'; \ expr : <number> | '(' <operator> <expr>+ ')' ; \ edward : /^/ <operator> <expr>+ /$/ ; \ ", Number, Operator, Sexpr, Qexpr, Expr, Edward); puts("Edward Version 0.0.0.0.4"); puts("Press Ctrl+c to Exit\n"); while (1) { char* input = readline("edward> "); add_history(input); mpc_result_t r; if (mpc_parse("<stdin>", input, Edward, &r)) { lval result = eval(r.output); lval_println(result); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } free(input); } mpc_cleanup(6, Number, Operator, Sexpr, Qexpr, Expr, Edward); 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; } }
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); }
int main(int argc, char** argv) { /* Create some parsers */ mpc_parser_t *Number = mpc_new("number"); mpc_parser_t *Symbol = mpc_new("symbol"); 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"); /* Define them with the following language */ mpca_lang(MPCA_LANG_DEFAULT, "" "number : /-?[0-9]+/ ;" "symbol : '+' | '-' | '*' | '/' | '%' | '^' " " | \"len\" | \"cons\"" " | \"list\" | \"head\" | \"tail\" | \"join\" | \"eval\" ;" "sexpr : '(' <expr>* ')' ;" "qexpr : '{' <expr>* '}' ;" "expr : <number> | <symbol> | <sexpr> | <qexpr> ;" "lispy : /^/ <expr>* /$/ ; " "", Number, Symbol, Sexpr, Qexpr, Expr, Lispy); /* Print Version and Exit Information */ puts("Lispy Version 0.0.0.0.1"); puts("Press Ctrl+c or Ctrl+d to Exit\n"); /* In a never ending loop */ while (1) { /* Output our prompt and get input */ char* input = readline("lispy> "); if (NULL == input) { /* quit with Ctrl-d */ break; } /* Add input to history */ add_history(input); /* Try to parse the user input */ mpc_result_t r; if (mpc_parse("<stdin>", input, Lispy, &r)) { /* On success evaluate AST */ lval *result = lval_eval(lval_read(r.output)); lval_println(result); lval_del(result); mpc_ast_delete(r.output); } else { mpc_err_print(r.error); mpc_err_delete(r.error); } /* Free retrieved input */ free(input); } /* Undefine and delete the parsers */ mpc_cleanup(6, Number, Symbol, Sexpr, Qexpr, Expr, Lispy); return 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; }
int main(int argc, char** argv) { // {{{ // Create Parsers. mpc_parser_t* Number = mpc_new("number"); mpc_parser_t* OperatorSymbol = mpc_new("operator_symbol"); mpc_parser_t* OperatorText = mpc_new("operator_text"); mpc_parser_t* Operator = mpc_new("operator"); mpc_parser_t* Sexpr = mpc_new("sexpr"); mpc_parser_t* Expr = mpc_new("expr"); mpc_parser_t* Lispy = mpc_new("lispy"); // Define polish notation language rules. mpca_lang(MPCA_LANG_DEFAULT, " number : /-?[0-9]+(\\.[0-9]+)?/ ;" " operator_symbol : '+' | '-' | '*' | '/' | '%' | '^' ;" " operator_text : \"add\" | \"sub\" | \"mul\" | \"div\" | \"mod\" " " | \"max\" | \"min\" ;" " operator : <operator_symbol> | <operator_text> ;" " sexpr : '(' <expr>* ')' ;" " expr : <number> | <operator> | <sexpr> ;" " lispy : /^/ <expr>* /$/ ;", Number, OperatorSymbol, OperatorText, Operator, Sexpr, Expr, Lispy ); // Print version and exit information. puts("Lispy Version 0.0.0.0.1"); puts("Press Ctrl+c to Exit\n"); // Never ending loop. while (1) { // Output our prompt to get input. // The function strips the trailing newline character char* input = readline("lispy> "); // Add input to history. add_history(input); // Attempt to parse the user input. mpc_result_t r; if (mpc_parse("<stdin>", input, Lispy, &r)) { lval* result = lval_eval(lval_read(r.output)); lval_println(result); lval_del(result); mpc_ast_delete(r.output); } else { // Print the error. mpc_err_print(r.error); mpc_err_delete(r.error); } // Imported from stdlib.h, free retrived input. free(input); } // Delete Parsers. mpc_cleanup( 7, Number, OperatorSymbol, OperatorText, Operator, Sexpr, Expr, Lispy ); return 0; } // }}}