|
@ -31,12 +31,34 @@ void add_history(char* unused) {} |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long min(long x, long y) { |
|
|
|
|
|
if (x <= y) { |
|
|
|
|
|
return x; |
|
|
|
|
|
} else { |
|
|
|
|
|
return y; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long max(long x, long y) { |
|
|
|
|
|
if (x >= y) { |
|
|
|
|
|
return x; |
|
|
|
|
|
} else { |
|
|
|
|
|
return y; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Use operator string to see which operation to perform */ |
|
|
/* Use operator string to see which operation to perform */ |
|
|
long eval_op(long x, char* op, long y) { |
|
|
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;} |
|
|
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;} |
|
|
|
|
|
if (strcmp(op, "%") == 0) {return x % y;} |
|
|
|
|
|
if (strcmp(op, "^") == 0) {return pow(x, y);} |
|
|
|
|
|
if (strcmp(op, "min") == 0) {return min(x, y);} |
|
|
|
|
|
if (strcmp(op, "max") == 0) {return max(x, y);} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -65,8 +87,8 @@ long eval(mpc_ast_t* t) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[]) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[]) { |
|
|
|
|
|
|
|
|
/* Create some parsers */ |
|
|
/* Create some parsers */ |
|
|
mpc_parser_t* Number = mpc_new("number"); |
|
|
mpc_parser_t* Number = mpc_new("number"); |
|
|
mpc_parser_t* Operator = mpc_new("operator"); |
|
|
mpc_parser_t* Operator = mpc_new("operator"); |
|
@ -77,7 +99,8 @@ int main(int argc, char const *argv[]) |
|
|
mpca_lang(MPCA_LANG_DEFAULT, |
|
|
mpca_lang(MPCA_LANG_DEFAULT, |
|
|
" \ |
|
|
" \ |
|
|
number : /-?[0-9]+/ ; \ |
|
|
number : /-?[0-9]+/ ; \ |
|
|
operator : '+' | '-' | '*' | '/' ; \ |
|
|
|
|
|
|
|
|
operator : '+' | '-' | '*' | '/' | '%' | '^' | \ |
|
|
|
|
|
\"min\" | \"max\" ; \ |
|
|
expr : <number> | '(' <operator> <expr>+ ')' ; \ |
|
|
expr : <number> | '(' <operator> <expr>+ ')' ; \ |
|
|
lispy : /^/ <operator> <expr>+ /$/ ; \ |
|
|
lispy : /^/ <operator> <expr>+ /$/ ; \ |
|
|
", |
|
|
", |
|
|