summaryrefslogtreecommitdiff
path: root/lispy/parsing.c
diff options
context:
space:
mode:
Diffstat (limited to 'lispy/parsing.c')
-rw-r--r--lispy/parsing.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/lispy/parsing.c b/lispy/parsing.c
new file mode 100644
index 0000000..68f2658
--- /dev/null
+++ b/lispy/parsing.c
@@ -0,0 +1,70 @@
+#include "mpc.h"
+
+#ifdef _WIN32
+
+static char buffer[2048];
+
+char* readline(char* prompt) {
+ fputs(prompt, stdout);
+ fgets(buffer, 2048, stdin);
+ char* cpy = malloc(strlen(buffer)+1);
+ strcpy(cpy, buffer);
+ cpy[strlen(cpy)-1] = '\0';
+ return cpy;
+}
+
+void add_history(char* unused) {}
+
+#else
+#include <editline/readline.h>
+#include <histedit.h>
+#endif
+
+int main() {
+
+ /* 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);
+
+ puts("Lispy Version 0.0.0.0.2");
+ puts("Press Ctrl+c to Exit\n");
+
+ while (1) {
+
+ char* input = readline("lispy> ");
+ 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);
+ mpc_ast_delete(r.output);
+ } else {
+ /* Otherwise print and delete the Error */
+ mpc_err_print(r.error);
+ mpc_err_delete(r.error);
+ }
+
+ free(input);
+ }
+
+ /* Undefine and delete our parsers */
+ mpc_cleanup(4, Number, Operator, Expr, Lispy);
+
+ return 0;
+}
+
+