Browse Source

Implement some bonus marks

master
T. Meissner 5 years ago
parent
commit
7affd2b284
1 changed files with 26 additions and 3 deletions
  1. +26
    -3
      chapter_07/lispy.c

+ 26
- 3
chapter_07/lispy.c View File

@ -31,12 +31,34 @@ void add_history(char* unused) {}
#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 */
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 pow(x, y);}
if (strcmp(op, "min") == 0) {return min(x, y);}
if (strcmp(op, "max") == 0) {return max(x, y);}
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 */
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Operator = mpc_new("operator");
@ -77,7 +99,8 @@ int main(int argc, char const *argv[])
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
operator : '+' | '-' | '*' | '/' ; \
operator : '+' | '-' | '*' | '/' | '%' | '^' | \
\"min\" | \"max\" ; \
expr : <number> | '(' <operator> <expr>+ ')' ; \
lispy : /^/ <operator> <expr>+ /$/ ; \
",


Loading…
Cancel
Save