示例#1
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;
}
示例#2
0
int main(int argc, char** argv) {
	
	/* Create some parser */
	mpc_parser_t* Number	= mpc_new("number");
	mpc_parser_t* Operator	= mpc_new("operator");
	mpc_parser_t* Expr	= mpc_new("expr");
	mpc_parser_t* Ark	= mpc_new("ark");

	/* Define them with the following language */
	mpca_lang(MPCA_LANG_DEFAULT,
		"										\
	   		number		:	/-?[0-9]+[.]?[0-9]*/ ;				\
	   		operator	:	'+' | '-' | '*' | '/' | '%' | \"add\" | \"sub\" | \"mul\" | \"div\" | \"mod\" | \"raise\" | \"min\" | \"max\" ;		\
	   		expr		:	<number> | '(' <operator> <expr>+ ')' ;		\
	   		ark		:	/^/ <operator> <expr>+ /$/ ;			\
		",
		Number, Operator, Expr, Ark);

	/* Print version and Exit Information  */
	puts("Ark Version 0.0.0.0.2");
	puts("Press Ctrl+c to Exit\n");
	puts("You can also type `exit` OR `q` OR `quit` to Exit\n");

	/* In never ending loop */
	while (1) {
		
		/* Now in either case readline will be correctly defined  */
		char* input = readline("ark> ");
		add_history(input);

		if (strcmp(input, "exit") == 0 || strcmp(input, "q") == 0 || strcmp(input, "quit") == 0) {
			exit(0);
		}

		/* Attempt to parse the user input */
		mpc_result_t r;
		if(mpc_parse("<stdin>", input, Ark, &r)) {
			
			long result = eval(r.output);
			printf("%li\n", result);
			mpc_ast_delete(r.output);
		} else {
			/* Otherwise Print 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, Ark);

	return 0;
}
示例#3
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");
    
    // Defint themm with this 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.1");
    puts("Press Ctrl+c to exit\n");
    
    //In a never ending loop
    while (1) {
        // Output prompt and get input
        char* input = readline("lispy> ");
        
        // add input to history
        add_history(input);
        
        mpc_result_t r;
        if(mpc_parse("<stdin>", input, Lispy, &r)) {
            // TESTING On success print the AST
           // mpc_ast_print(r.output); FOR TESTING ONLY
            
            // print the  actual result
            long result = eval(r.output);
            printf("%li\n", result);
            
            mpc_ast_delete(r.output);
        } else {
            // Otherwise print the error
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }
        
        //Free retrived input
        free(input);
    }
    
    // Undefine and delete our parsers
    mpc_cleanup(4, Number, Operator, Expr, Lispy);
    
    return 0;
}
示例#4
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;
}
示例#5
0
文件: prompt.c 项目: tevino/aroma
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;
}
示例#6
0
int main(int argc, char **argv) {

  char *input;
  int exit;
  mpc_result_t ast;
  long result;

  // Print version and exit information
  puts("Lispy version 0.0.0.0.1");
  puts("Press ctrl-c to exit\n");

  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]+/ ; \
     operator : '+' | '-' | '*' | '/' ; \
     expr : <number> | '(' <operator> <expr>+ ')' ; \
     lispy : /^/ <operator> <expr>+ /$/ ; \
    ", Number, Operator, Expr, Lispy);

  // In a never ending loop
  while(1) {
    // output our prompt
    input = readline("lispy> ");

    exit = strcmp(input, "exit");

    if (exit == 0) {
      puts("Bye!");
      break;
    }

    add_history(input);

    if (mpc_parse("<stdin>", input, Lispy, &ast)) {
      result = eval(ast.output);
      printf("%li\n", result);
      mpc_ast_delete(ast.output);
    } else {
      // Otherwise, print the error
      mpc_err_print(ast.error);
      mpc_err_delete(ast.error);
    }

    free(input);

  }

  mpc_cleanup(4, Number, Operator, Expr, Lispy);
  return 0;
}
示例#7
0
int main (int argc, char** argv) {

    /* 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* Awly     = mpc_new("awly");

    /* Grammar */
    mpca_lang(MPCA_LANG_DEFAULT,
        "                                                                       \
         number   : /-?[0-9]+\\.?[0-9]*/ ;                                      \
         operator : '+' | '-' | '*' | '/' | '%' | '^'                           \
                    | \"add\" | \"sub\" | \"mul\" | \"div\" | \"mod\" | \"exp\" \
                    | \"min\" | \"max\" ;                                       \
         expr     : <number> | '(' <operator> <expr>+ ')' ;                     \
         awly     : /^/ <operator> <expr>+ /$/ ;                                \
        ",
        Number, Operator, Expr, Awly);
   
    /* Print Prelude Header */
    puts("Awlyspian Version 0.0.5");
    puts("Press Ctrl-C to exit\n");

    /* Infinite REPL Loop */
    while (1) {
        /* Input Buffer */
        char* input = readline("awly> ");
        /* Store Input History*/
        add_history(input);
        /* ParseInput */
        mpc_result_t r;
        if (mpc_parse("<stdin>", input, Awly, &r)) {
            /* Success - Print AST */
            //mpc_ast_print(r.output);
            /* Print Results */
            double result = eval(r.output);
            printf("%g\n", result);
            /* Clean */
            mpc_ast_delete(r.output);
        } else {
            /* Error */
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }
        /* Free Input Buffer */
        free(input);
    }

    /* Clean-up Parsers */
    mpc_cleanup(4, Number, Operator, Expr, Awly);

    return 0;
}
示例#8
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;
}
示例#9
0
文件: eval.c 项目: oubiwann/lambo
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);
  }
}
示例#10
0
文件: parser.c 项目: mtunjic/lispy
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 : '+' | '-' | '*' | '/' | '%' | '^' | \"min\" | \"max\" ; \
      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);
      
      // Instead of printing the tree we now want to print the
      // result of the evaluation.
      lval result = eval(r.output);
      lval_printf(result);
      mpc_ast_delete(r.output);

    } else {
      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;
}
示例#11
0
int main(int argc, char** argv) {
    /* Create some basic 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* CLispy = mpc_new("clispy");

    /* Initialize a grammar for Reverse Polish Notation */
    mpca_lang(MPCA_LANG_DEFAULT,
    " \
        number : /-?[0-9]+/ ; \
        operator : '+' | '-' | '*' | '/' ; \
        expr : <number> | '(' <operator> <expr>+ ')' ; \
        clispy : /^/ <operator> <expr>+ /$/ ; \
    ",
    Number, Operator, Expr, CLispy);
    

    /* Print version infromation */
    puts("CLispy Version 0.0.2");
    puts("Press Ctrl+C to Exit\n");

    /* Forever */
    for (;;) {
        /* Display a prompt and read user input */
        char* input = readline("clispy> ");

        /* Add user input to history */
        add_history(input);

        /* Try to parse user input */
        mpc_result_t r;
        if (mpc_parse("<stdin>", input, CLispy, &r)) {
            /* On success print the AST */
            long result = eval(r.output);
            printf("%li\n", result);
            mpc_ast_delete(r.output);
        } else {
            /* Print the error */
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }
        
        /* Deallocate input */
        free(input);
    }

    /* Undefined and delete parsers */
    mpc_cleanup(4, Number, Operator, Expr, CLispy);

    return 0;
}
示例#12
0
文件: uss.c 项目: aktowns/USS
int main(int argc, char** argv) {
    GC_INIT();
    ussenv_t *e = ussenv_new();
    ussenv_common_builtins(e);

    char *prelude_path = join_path(STDLIB_PATH, true, "prelude.uss");

    ussval_t *prelude = ussval_eval_from_file(e, prelude_path);

    /* If the result is an error be sure to print it */
    if (prelude->type == UVAL_ERR) { ussval_println(prelude); }
    ussval_del(prelude);

    fprintf(stderr, "argv[0]=%s argv[1]=%s\n", argv[0], argv[1]);

    if (argc >= 2) {
        /* loop over each supplied filename (starting from 1) */
        for (int i = 1; i < argc; i++) {
            ussval_t *x = ussval_eval_from_file(e, argv[i]);

            /* If the result is an error be sure to print it */
            if (x->type == UVAL_ERR) {
                ussval_println(x);
            }

            ussval_del(x);
        }
    } else {
        char buf[4096];

        parser_t *parser = parser_toplevel();
        mpc_result_t r;

        ussval_t *x = UVAL_NIL;
        while(fgets(buf, sizeof(buf), stdin)) {
            if (mpc_parse("<stdin>", buf, parser->uss, &r)) {
                // mpc_ast_print(r.output);
                x = ussval_eval(e, ussval_read(r.output));
                mpc_ast_delete(r.output);
            } else {
                mpc_err_print(r.error);
                mpc_err_delete(r.error);
            }
        }

        ussval_println(x);
        parser_cleanup(parser);
    }

    ussenv_del(e);
    return 0;
}
示例#13
0
文件: parsing.c 项目: j4nu5/byol
int main() {
    /* Parsers */
    mpc_parser_t* Number        = mpc_new("number");
    mpc_parser_t* Operator      = mpc_new("operator");
    mpc_parser_t* Expression    = mpc_new("expression");
    mpc_parser_t* KLisp         = mpc_new("klisp");

    /* Language Definition */
    mpca_lang(MPCA_LANG_DEFAULT,
    "                                                               \
        number      : /-?[0-9]+(\\.[0-9]+)?/;                       \
        operator    : '+' | '-' | '*' | '/';                        \
        expression  : <number> | '(' <operator> <expression>+ ')';  \
        klisp       : /^/ <operator> <expression>+ /$/;             \
    ",
    Number, Operator, Expression, KLisp);

    printf("Welcome to KLisp v0.0.1\n");
    printf("To exit press Ctrl+c\n");

    while (1) {
        char *input = readline("klisp> ");

        if (!input) {
            printf("\n");
            break;
        }

        if (!strlen(input))
            continue;

        add_history(input);

        /* Process the input */
        mpc_result_t r;
        if (mpc_parse("<stdin>", input, KLisp, &r)) {
            mpc_ast_print(r.output);
            mpc_ast_delete(r.output);
        }
        else {
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }

        free(input);
    }

    /* Cleanup */
    mpc_cleanup(4, Number, Operator, Expression, KLisp);
    return 0;
}
示例#14
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* 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;
}
示例#15
0
int main(int argc, char** argv) {
  
  /* making the 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 the grammar */
  mpca_lang(MPCA_LANG_DEFAULT,
    "                                                     \
      number   : /-?[0-9]+/ ;                             \
      operator : '+' | '-' | '*' | '/' | '%' | '^' | \"add\" | \"exp\" | \"mul\" | \"sub\" | \"div\" | \"mod\" ;  \
      expr     : <number> | '(' <operator> <expr>+ ')' ;  \
      lispy    : /^/ <operator> <expr>+ /$/ ;             \
    ",
    Number, Operator, Expr, Lispy);
  
  puts("C-Lisp Version 0.0.0.0.4");
  puts("Press Ctrl+c to Exit\n");
  
  while (1) {
  
    char* input = readline("c-lisp> ");
    add_history(input);
    
    /* parse the input */
    mpc_result_t r;
    if (mpc_parse("<stdin>", input, Lispy, &r)) {
      /* On success print and delete the AST */
      long result = eval(r.output);
      printf("%li\n", result);
      mpc_ast_delete(r.output);
    } else {
      /* Otherwise print and delete the Error */
      mpc_err_print(r.error);
      printf("lol u broke it\n");
      printf("Try again.\n");
      printf("\n");
      mpc_err_delete(r.error);
    }
    
    free(input);
  }
  
  /* Undefine and delete our parsers */
  mpc_cleanup(4, Number, Operator, Expr, Lispy);
  
  return 0;
}
示例#16
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);
   
  puts("Lispy Version 0.0.0.0.1");
  puts("Press Ctrl+c to Exit\n");
   
  while (1) {
    
    /* Now in either case readline will be correctly defined */
    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 the AST */
    		mpc_ast_print(r.output);
    		mpc_ast_delete(r.output);
    } else {
    		/* Otherwise Print 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;
}
示例#17
0
文件: yafl.c 项目: djjolicoeur/yafl
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;
}
示例#18
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.1");
	puts("Press Ctrl-c to Exit\n");

	/* loop forever */
	while(1){		
		
		/* Output prompt and get input*/
		char* input = readline("lispy> ");

		/*Add input to history */
		add_history(input);

		/* Atempt to Parse user input */
		mpc_result_t r;
		if( mpc_parse("<stdin", input, Lispy, &r)){
			/* on sucess print the AST */
			mpc_ast_print(r.output);
			mpc_ast_delete(r.output);			
		}
		else {
			mpc_err_print(r.error);
			mpc_err_delete(r.error);
		}

		/* Free retrieved input */
		free(input);
	}
	return 0;
}
示例#19
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* Nilisp   = mpc_new("nilisp");

  /* Define them with the following Language */
  mpca_lang(MPCA_LANG_DEFAULT,
    "                                                    \
      number   : /-?[0-9]+(\\.[0-9]+)?/;                 \
      operator : '+' | '-' | '*' | '/' | '%' |           \
                \"add\" | \"sub\" | \"mul\" | \"div\";   \
      expr     : <number> | '(' <operator> <expr>+ ')' ; \
      nilisp   : /^/ <operator> <expr>+ /$/ ;            \
    ",
    Number, Operator, Expr, Nilisp);

  /* Print Version and Exit Information */
  puts("Nihilisp Version 0.0.0.0.1");
  puts("Created by @youfoundron");
  puts("Press Ctrl+c to Exit\n");

  while (1) {                           /* In a never ending loop */
    char* input = readline("nilisp> "); /* Output our prompt and get input */
    add_history(input);                 /* Add input to history */

    /* Atempt to Parse the user Input */
    mpc_result_t r;
    if (mpc_parse("<stdin>", input, Nilisp, &r)) {
      /* On Success Print the AST */
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      /* Otherwise Print the Error */
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }

    free(input);                        /* Free retrieved input */
  }

  /* Undefine and Delete our Parsers */
  mpc_cleanup(4, Number, Operator, Expr, Nilisp);
  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* Lclisp    = mpc_new("lclisp");


    mpca_lang(MPC_LANG_DEFAULT,
        "                                                   \
        number   : /-?[0-9]+/ ;                             \
        operator : '+' | '-' | '*' | '/' ;                  \
        expr     : <number> | '(' <operator> <expr>+ ')' ;  \
        lclisp   : /^/ <operator> <expr>+ /$/ ;             \
        ",
        Number, Operator, Expr, Lclisp);

    puts("Lonesome Crowded Lisp 0.0.0.0.2");
    puts("Press Ctrl+c to exit\n");

    while (1) {

        char* input = readline("(づ ̄ ³ ̄)づ ");
        add_history(input);

        /* attempt parsing user input */
        mpc_result_t r;
        if (mpc_parse("<stdin>", input, Lclisp, &r)) {
            /* on success, print the AST */
            long result = eval(r.output);
            printf("%li\n", result);
            mpc_ast_delete(r.output);
        } else {
            /* else, print the error */
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }

        free(input);

    }

    /* undefine and delete parsers */
    mpc_cleanup(4, Number, Operator, Expr, Lclisp);

    return 0;
}
示例#21
0
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 the parsers */
  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.2");
  puts("Press Ctrl+c to exit");

  while (1) {
    char* input = readline("lispy> ");
    add_history(input);

    /* Try to parse user input */
    mpc_result_t r;
    if (mpc_parse("<stdin>", input, Lispy, &r)) {
      /* Evaluate if successful */
      long result = eval(r.output);
      printf("%li\n", result);
      mpc_ast_delete(r.output);
    } else {
      /* Otherwise print the error */
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }

    free(input);
  }

  /* Undefine and delete the parsers */
  mpc_cleanup(4, Number, Operator, Expr, Lispy);

  return 0;
}
示例#22
0
int main(int argc, char** argv) {

    /* Create 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* Lispc     = mpc_new("lispc");

    /* Define the language */
    mpca_lang(MPC_LANG_DEFAULT,
            "                                                       \
             number     : /-?[0-9]+/  ;                             \
             operator   : '+' | '-' | '*' | '/'  ;                  \
             expr       : <number> | '(' <operator> <expr>+ ')'  ;  \
             lispc      : /^/ <operator> <expr>+ /$/  ;             \
            ",
            Number, Operator, Expr, Lispc);

    puts("Silang version 0.0.1");
    puts("Press Ctrl+C to exit\n");

    /* Do the main loop for REPL */
    while (1) {
        char* input = readline("lispc > ");
        add_history(input);

        /* Process the input */
        mpc_result_t result;
        if (mpc_parse("<stdin>", input, Lispc, &result)) {
            mpc_ast_print(result.output);
            mpc_ast_delete(result.output);
        }
        else {
            mpc_err_print(result.error);
            mpc_err_delete(result.error);
        }

        free(input);
    }

    /* Clean up the parsers */
    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_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;
}
示例#24
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(MPCA_LANG_DEFAULT,
           "                                                                \
                number      :   /-?[0-9]+/                            ;     \
                operator    :   '+' | '-' | '*' | '/' |  '%'          ;     \
                expr        :   <number> | '(' <operator> <expr>+ ')' ;     \
                lispy       :   /^/ <operator> <expr>+ /$/            ;     \
           ",
           Number, Operator, Expr, Lispy);
    
    puts("Leaver Version 0.0.0.0.3\n");
    puts("Press Ctrl+c to Exit\n");
    //puts("..or just kill yourself.. \n");
    
    while (1) {
    
        char *input = readline("leaver$ ");
        add_history(input);

        mpc_result_t r;
        if (mpc_parse("<stdin>", input, Lispy, &r)) {
        
            int result = leaves(r.output);
            printf("%i\n", 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;
}
示例#25
0
int main (int argc, char** argv) {
    //make 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 said parsers

    mpca_lang(MPCA_LANG_DEFAULT,
    "\
     number   : /-?[0-9]+/ ;\
     operator : '+' | '-' | '/'| '*' ;\
     expr     : <number> | '(' <operator> <expr>+ ')' ;\
     lispy    : /^/ <operator> <expr>+ /$/ ;\
    ",
    Number, Operator, Expr, Lispy);
    
    //print version number
    puts("Lispy version 0.0.0.0.3");
    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)) {
            long result = eval(r.output);
            printf("%li\n", 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;
}
示例#26
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(MPCA_LANG_DEFAULT,
        " \
            number      : /-?[0-9]+/ ;                              \
            operator    : '+' | '-' | '*' | '/' ;                   \
            expr        : <number> | '(' <operator> <expr>+ ')' ;   \
            lispy       : /^/ <operator> <expr>+ /$/ ;              \
        ",
        Number, Operator, Expr, Lispy);

    puts("Slight-Lisp Version 0.0.1");
    puts("Press Ctrl+c to Exit\n");

    while(1) {

        char* input = readline("slight> ");
        if (input && input[0] != '\0') {
            add_history(input);
        }
        
        mpc_result_t r;
        if (mpc_parse("<stdin>", input, Lispy, &r)) {
            /* On success print the AST */
            mpc_ast_print(r.output);
            mpc_ast_delete(r.output);
        } else {
            /* Otherwise print error */
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }

        free(input);
    }


    mpc_cleanup(4, Number, Operator, Expr, Lispy);
    return 0;
}
示例#27
0
文件: main.c 项目: ybur-yug/edward
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;
}
示例#28
0
int main(int argc, char **argv) {
  /* setup grammar */
  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.3");
  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)) {
      long result = eval(r.output);
      printf("%li\n", result);
      /* printf("Children nodes: %d\n", number_of_nodes(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;
}
示例#29
0
int main(int argc, char** argv) {
    /* Create parsers */
    mpc_parser_t* Number = mpc_new("number");
    mpc_parser_t* Operator = mpc_new("operator");
    mpc_parser_t* Expression = mpc_new("expression");
    mpc_parser_t* Lispy = mpc_new("lispy");
    
    linenoiseClearScreen();

    /* Define them with the language as follows */
    mpca_lang(MPCA_LANG_DEFAULT, 
            " number      : /-?[0-9]+/ ; "
             " operator    : '+' | '/' | '*' | '-' ; "
             " expression  : <number> | '(' <operator> <expression>+ ')' ; "
             " lispy        : /^/ <operator>* <expression> /$/ ; ", Number, Operator, Expression, Lispy);
    char *line = NULL;

    while((line = linenoise("Lispy> ")) != NULL ) {
        mpc_result_t result;

        if (mpc_parse("<stdin>" , line, Lispy, &result)) {
           mpc_ast_print(result.output);
           mpc_ast_delete(result.output);
        } else {
            mpc_err_print(result.output);
            mpc_err_delete(result.output);

        }

        free(line);
    }

    mpc_cleanup(4, Number, Operator, Expression, Lispy);

    return 0;
   
}
示例#30
0
int main(int argc, char** argv)
{
    mpc_parser_t* Bool = mpc_new("bool");
    mpc_parser_t* Number = mpc_new("number");
    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* Program = mpc_new("program");

    mpca_lang(MPCA_LANG_DEFAULT,
              "                                                       \
        bool     : /#[tf]/ ;                                \
        number   : /-?[0-9]+/ ;                             \
        symbol   : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&\\^?]+/ ;   \
        sexp     : '(' <expr>* ')' ;                        \
        qexp     : '{' <expr>* '}' ;                        \
        expr     : <bool> | <number> | <symbol> |           \
                   <sexp> | <qexp> ;                        \
        program  : /^/ <expr> /$/ ;                         \
    ",
              Bool, Number, Symbol, Sexp, Qexp, Expr, Program);

    puts("Welcome to gLenISP Version 0.0.0.1");
    puts("You have 1000 parentheses remaining");
    puts("Press Ctrl+c to Exit\n");

    struct lenv* e = lenv_new();
    lenv_add_builtins(e);

    while (1) {

        char* input = readline("glenisp> ");

        if (input == NULL) {
            break;
        }

        add_history(input);

        mpc_result_t r;
        if (mpc_parse("<stdin>", input, Program, &r)) {

            //mpc_ast_print(r.output);

            struct lval* x = lval_read(r.output);
            mpc_ast_delete(r.output);

            puts("Input:");
            lval_print(x);
            putchar('\n');

            struct lval* r = lval_eval(e, x);

            puts("Output:");
            lval_print(r);
            putchar('\n');

            lval_del(r);

        } else {
            mpc_err_print(r.error);
            mpc_err_delete(r.error);
        }

        free(input);

    }

    mpc_cleanup(6, Bool, Number, Symbol, Sexp, Qexp, Expr, Program);

    return 0;
}