<commit_before>#include <stdio.h>
#include <stdlib.h>

#include "mpc.h"

#ifdef _WIN32
#include <string.h>

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 <editline/history.h>
#endif

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("Lispy Version 0.0.0.0.2");
	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))
		{
			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;    
}<commit_msg>Added evaluation<commit_after>#include <stdio.h>
#include <stdlib.h>

#include "mpc.h"

#ifdef _WIN32
#include <string.h>

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 <editline/history.h>
#endif

long eval_op(long x, char* op, long y) {
	if (strcmp(op, "+") == 0) { return x + y; }
	if (strcmp(op, "-") == 0) { return x - y; }
	if (strcmp(op, "*") == 0) { return x * y; }
	if (strcmp(op, "/") == 0) { return x / y; }
	return 0;
}

double eval(mpc_ast_t* t)
{
	if(strstr(t->tag, "number"))
	{
		return atof(t->contents);
	}
	
	char* op = t->children[1]->contents;
	
	double x = eval(t->children[2]);
	
	int i = 3;
	while (strstr(t->children[i]->tag, "expr"))
	{
		x = eval_op(x, op, eval(t->children[i]));
		i++;
	}
	
	return x;
	
}

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("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))
		{
			/*mpc_ast_print(r.output);
			mpc_ast_delete(r.output);*/
			
			double result = eval(r.output);
			printf("%f\n", result);
			
		}
		else
		{
			mpc_err_print(r.error);
			mpc_err_delete(r.error);
		}
		
		free(input);
	}
	
	mpc_cleanup(4, Number, Operator, Expr, Lispy);

	return 0;    
}
{'fraction_non_alphanumeric': 0.20637785800240674, 'fraction_numerical': 0.017448856799037304, 'mean_word_length': 3.3181818181818183, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2777070', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1452, 'n_words': 273}