Skip to content

Instantly share code, notes, and snippets.

@dunn
Created November 16, 2015 00:54
Show Gist options
  • Save dunn/1176c4ed7b2ba6c5b68e to your computer and use it in GitHub Desktop.
Save dunn/1176c4ed7b2ba6c5b68e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <mpc.h>
#include <readline/readline.h>
#include <readline/history.h>
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(MPCA_LANG_DEFAULT, " \
number : /-?[0-9]*\\.?[0-9]+/ ; \
operator: '+' | '-' | '*' | '/'; \
expr : <number> | '(' <operator> <expr>+ ')' ;\
lispy : /^/ <operator> <expr>+ /$/; \
", Number, Operator, Expr, Lispy);
puts ("Maybe a Lisp 0.1.0");
puts ("ctrl+c to exit\n");
while (1) {
char* input = readline("honk> ");
add_history(input);
mpc_result_t r;
if (mpc_parse("<stdin>", input, Lispy, &r)) {
mpc_ast_print(r.output);
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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment