示例#1
0
文件: lispy.c 项目: nvdnkpr/mpc
int main(int argc, char **argv) {
  
  mpc_parser_t* Number  = mpc_new("number");
  mpc_parser_t* Symbol  = mpc_new("symbol");
  mpc_parser_t* String  = mpc_new("string");
  mpc_parser_t* Comment = mpc_new("comment");
  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");

  mpca_lang(MPCA_LANG_PREDICTIVE,
    " number  \"number\"  : /[0-9]+/ ;                         "
    " symbol  \"symbol\"  : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; "
    " string  \"string\"  : /\"(\\\\.|[^\"])*\"/ ;             "
    " comment             : /;[^\\r\\n]*/ ;                    "
    " sexpr               : '(' <expr>* ')' ;                  "
    " qexpr               : '{' <expr>* '}' ;                  "
    " expr                : <number>  | <symbol> | <string>    "
    "                     | <comment> | <sexpr>  | <qexpr> ;   "
    " lispy               : /^/ <expr>* /$/ ;                  ",
    Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy);

  if (argc > 1) {
    
    mpc_result_t r;
    if (mpc_parse_contents(argv[1], Lispy, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
    
  } else {
    
    mpc_result_t r;
    if (mpc_parse_pipe("<stdin>", stdin, Lispy, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
  
  }

  mpc_cleanup(8, Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy);
  
  return 0;
  
}
示例#2
0
文件: maths.c 项目: 01000001/mpc
int main(int argc, char **argv) {
  
  mpc_parser_t *Expr  = mpc_new("expression");
  mpc_parser_t *Prod  = mpc_new("product");
  mpc_parser_t *Value = mpc_new("value");
  mpc_parser_t *Maths = mpc_new("maths");
  
  mpca_lang(MPCA_LANG_PREDICTIVE,
    " expression : <product> (('+' | '-') <product>)*; "
    " product : <value>   (('*' | '/')   <value>)*;    "
    " value : /[0-9]+/ | '(' <expression> ')';         "
    " maths : /^/ <expression> /$/;                    ",
    Expr, Prod, Value, Maths, NULL);
  
  mpc_print(Expr);
  mpc_print(Prod);
  mpc_print(Value);
  mpc_print(Maths);
  
  if (argc > 1) {
    
    mpc_result_t r;
    if (mpc_parse_contents(argv[1], Maths, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
    
  } else {

    mpc_result_t r;
    if (mpc_parse_pipe("<stdin>", stdin, Maths, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
  
  }

  mpc_cleanup(4, Expr, Prod, Value, Maths);
  
  return 0;
  
}
示例#3
0
文件: main.c 项目: paulkarabilo/lishp
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;
}
示例#4
0
int test_empty_input()
{
	mpc_ast_t* ast = parse("  ");
	TEST_ASSERT(NULL == ast);
	mpc_ast_delete(ast);
	return 0;
}
示例#5
0
/* 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;
}
示例#6
0
文件: repl.c 项目: ikbenlike/Xenon
int repl(){
    char *a = calloc(1, 1000 * sizeof(char));
    xenon_stack_vector stack;
    stack.size = 1000;
    while(1){
        stack.vector = calloc(1, 1000 * sizeof(xenon_stack_item));
        printf("Xenon> ");
        input(a, 1000);
        if(strcmp(a, ".exit") == 0){
            return 0;
        }
        char *b = preprocessor(a);
        mpc_ast_t *ast = parse("stdin", b);
        free(b);
        if(ast != NULL){
            stack.cursor = 0;
            mpc_ast_print(ast);
            //vm_add_opcode_to_stack(&stack, HALT);
            tree_walker(ast, stack);
            vm_add_opcode_to_stack(&stack, CONST);
            vm_add_int_to_stack(&stack, 10);
            printf("%i\n", stack.cursor);
            VM *vm = vm_create(stack, 0);
            mpc_ast_delete(ast);
            vm_exec(vm, 0, false);
            vm_free(vm);
        }
        if(stack.vector != NULL){
            free(stack.vector);
        }
    }
    free(a);
    return 0;
}
示例#7
0
int main(int argc, char** argv) {
  char* input;
  lispy_parser* parser = create_lispy_parser();
  mpc_result_t parser_result;

  puts("Lispy version 0");
  puts("Exit with ^C, ^D or \\q");

  while(1) {
    if (input != NULL) {
      free(input);
    }
    input = readline("lispy> ");
    add_history(input);

    if (input == NULL || strcmp(input, "\\q") == 0) {
      printf("\nBye!\n");
      break;
    } else {
      // TODO: Hide all of this parser stuff behind a facade
      if (mpc_parse("<stdin>", input, parser->Lispy, &parser_result)) {
        mpc_ast_print(parser_result.output);
        mpc_ast_delete(parser_result.output);
      } else {
        mpc_err_print(parser_result.error);
        mpc_err_delete(parser_result.error);
      }
    }
  }
}
示例#8
0
文件: ast.c 项目: catwell/ownlisp
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;
}
示例#9
0
int test_ast_failure()
{
	mpc_ast_t* ast = parse("+ 4 (");
	TEST_ASSERT(NULL == ast);
	mpc_ast_delete(ast);
	return 0;
}
示例#10
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;
}
示例#11
0
文件: doge.c 项目: 01000001/mpc
int main(int argc, char **argv) {
  
  mpc_parser_t* Adjective = mpc_new("adjective");
  mpc_parser_t* Noun      = mpc_new("noun");
  mpc_parser_t* Phrase    = mpc_new("phrase");
  mpc_parser_t* Doge      = mpc_new("doge");

  mpca_lang(MPCA_LANG_DEFAULT,
    " adjective : \"wow\" | \"many\" | \"so\" | \"such\";                 "
    " noun      : \"lisp\" | \"language\" | \"c\" | \"book\" | \"build\"; "
    " phrase    : <adjective> <noun>;                                     "
    " doge      : /^/ <phrase>* /$/;                                      ",
    Adjective, Noun, Phrase, Doge, NULL);

  if (argc > 1) {
    
    mpc_result_t r;
    if (mpc_parse_contents(argv[1], Doge, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
    
  } else {

    mpc_result_t r;
    if (mpc_parse_pipe("<stdin>", stdin, Doge, &r)) {
      mpc_ast_print(r.output);
      mpc_ast_delete(r.output);
    } else {
      mpc_err_print(r.error);
      mpc_err_delete(r.error);
    }
  
  }

  mpc_cleanup(4, Adjective, Noun, Phrase, Doge);
  
  return 0;
  
}
示例#12
0
int test_ast_type()
{
	mpc_ast_t* ast = parse("+ 1.1 1");
	TEST_ASSERT(6 == ast_size(ast));
	TEST_ASSERT(strstr(ast->children[1]->tag, "symbol"));
	TEST_ASSERT(strstr(ast->children[2]->tag, "double"));
	TEST_ASSERT(strstr(ast->children[3]->tag, "long"));
	mpc_ast_delete(ast);
	return 0;
}
示例#13
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;
}
示例#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* 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;
}
示例#15
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;
}
示例#16
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;
}
示例#17
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;
}
示例#18
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;
}
示例#19
0
文件: builtin.c 项目: aktowns/USS
ussval_t* builtin_load(ussenv_t* e, ussval_t* a) {
    UASSERT_NUM("load", a, 1);
    UASSERT_TYPE("load", a, 0, UVAL_STR);

    /* Parse File given by string name */
    mpc_result_t r;
    parser_t *parser = parser_toplevel();

    if (mpc_parse_contents(a->cell[0]->str, parser->uss, &r)) {
        fprintf(stderr, "DEBUG: loading: %s\n", a->cell[0]->str);
        ussenv_put(e, ussval_new_sym("file-path"), a->cell[0]);
        ussenv_put(e, ussval_new_sym("file-dir"), ussval_new_str(dirname(a->cell[0]->str)));

        /* Read contents */
        ussval_t *expr = ussval_read(r.output);
        mpc_ast_delete(r.output);

        /* Evaluate each Expression */
        while (expr->count) {
            ussval_t *x = ussval_eval(e, ussval_pop(expr, 0));
            /* If Evaluation leads to error print it */
            if (x->type == UVAL_ERR) {
                ussval_println(x);
            }
            ussval_del(x);
        }

        /* Delete expressions and arguments */
        ussval_del(expr);
        ussval_del(a);

        parser_cleanup(parser);

        //lenv_put(e, lval_sym("file-path"), LVAL_NIL);
        //lenv_put(e, lval_sym("file-dir"), LVAL_NIL);

        /* Return empty list */
        return ussval_new_sexpr();

    } else {
        /* Get Parse Error as String */
        char *err_msg = mpc_err_string(r.error);
        mpc_err_delete(r.error);

        /* Create new error message using it */
        ussval_t *err = ussval_new_err("Could not load Library %s", err_msg);
        free(err_msg);
        ussval_del(a);

        parser_cleanup(parser);
        /* Cleanup and return error */
        return err;
    }
}
示例#20
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;
}
示例#21
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);
  }
}
示例#22
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;
}
示例#23
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;
}
示例#24
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;
}
示例#25
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;
}
示例#26
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;
}
示例#27
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;
}
示例#28
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;
}
示例#29
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;
}
示例#30
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;
}