mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

lispy.c (1674B)


      1 #include "../mpc.h"
      2 
      3 int main(int argc, char **argv) {
      4   
      5   mpc_result_t r;  
      6   
      7   mpc_parser_t* Number  = mpc_new("number");
      8   mpc_parser_t* Symbol  = mpc_new("symbol");
      9   mpc_parser_t* String  = mpc_new("string");
     10   mpc_parser_t* Comment = mpc_new("comment");
     11   mpc_parser_t* Sexpr   = mpc_new("sexpr");
     12   mpc_parser_t* Qexpr   = mpc_new("qexpr");
     13   mpc_parser_t* Expr    = mpc_new("expr");
     14   mpc_parser_t* Lispy   = mpc_new("lispy");
     15 
     16   mpca_lang(MPCA_LANG_PREDICTIVE,
     17     " number  \"number\"  : /[0-9]+/ ;                         "
     18     " symbol  \"symbol\"  : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; "
     19     " string  \"string\"  : /\"(\\\\.|[^\"])*\"/ ;             "
     20     " comment             : /;[^\\r\\n]*/ ;                    "
     21     " sexpr               : '(' <expr>* ')' ;                  "
     22     " qexpr               : '{' <expr>* '}' ;                  "
     23     " expr                : <number>  | <symbol> | <string>    "
     24     "                     | <comment> | <sexpr>  | <qexpr> ;   "
     25     " lispy               : /^/ <expr>* /$/ ;                  ",
     26     Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy, NULL);
     27   
     28   if (argc > 1) {
     29 
     30     if (mpc_parse_contents(argv[1], Lispy, &r)) {
     31       mpc_ast_print(r.output);
     32       mpc_ast_delete(r.output);
     33     } else {
     34       mpc_err_print(r.error);
     35       mpc_err_delete(r.error);
     36     }
     37         
     38   } else {
     39     
     40     if (mpc_parse_pipe("<stdin>", stdin, Lispy, &r)) {
     41       mpc_ast_print(r.output);
     42       mpc_ast_delete(r.output);
     43     } else {
     44       mpc_err_print(r.error);
     45       mpc_err_delete(r.error);
     46     }
     47   
     48   }
     49 
     50   mpc_cleanup(8, Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy);
     51   
     52   return 0;
     53   
     54 }
     55