cleaner interface for defining functions
This commit is contained in:
parent
dacf01e83e
commit
e3c466e321
55
main.c
55
main.c
@ -54,11 +54,26 @@ enum {
|
|||||||
#define IS_FLOAT(c) ((c >= '0' && c <= '9') || c == '.')
|
#define IS_FLOAT(c) ((c >= '0' && c <= '9') || c == '.')
|
||||||
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
||||||
|
|
||||||
|
typedef struct Function {
|
||||||
|
const char *name;
|
||||||
|
real (*func)(real *args);
|
||||||
|
size_t n_args;
|
||||||
|
} Function;
|
||||||
|
|
||||||
|
#define FUNCTIONS_CAP 256
|
||||||
|
Function functions[FUNCTIONS_CAP];
|
||||||
|
size_t functions_size = 0;
|
||||||
|
|
||||||
void push_tok(Tok t) {
|
void push_tok(Tok t) {
|
||||||
if (toks_size+1 < TOKS_CAP)
|
if (toks_size+1 < TOKS_CAP)
|
||||||
toks[toks_size++] = t;
|
toks[toks_size++] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void add_func(const char *name, real (*func)(real *args), size_t n_args) {
|
||||||
|
if (functions_size+1 < FUNCTIONS_CAP)
|
||||||
|
functions[functions_size++] = (Function){.name = name, .func = func, .n_args = n_args};
|
||||||
|
}
|
||||||
|
|
||||||
void tokenize(char *expr) {
|
void tokenize(char *expr) {
|
||||||
push_tok((Tok){.kind = TokOp, .Char = '('});
|
push_tok((Tok){.kind = TokOp, .Char = '('});
|
||||||
|
|
||||||
@ -222,22 +237,23 @@ real eval(Tok *t) {
|
|||||||
t -= 2;
|
t -= 2;
|
||||||
|
|
||||||
real outer_res;
|
real outer_res;
|
||||||
if (strcmp(t[1].Str, "sqrt") == 0) {
|
bool func_found = false;
|
||||||
if (arg_results_size != 1) {
|
for (size_t i = 0; i < functions_size; i++) {
|
||||||
fprintf(stderr, "Error: function sqrt() requires exactly 1 argument\n");
|
if (strcmp(t[1].Str, functions[i].name) == 0) {
|
||||||
exit(1);
|
func_found = true;
|
||||||
|
if (arg_results_size != functions[i].n_args) {
|
||||||
|
const char *plural = functions[i].n_args == 1 ? "" : "s";
|
||||||
|
fprintf(stderr, "Error: function %s() requires exactly 1 argument%s\n", functions[i].name, plural);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
outer_res = functions[i].func(arg_results);
|
||||||
}
|
}
|
||||||
outer_res = sqrt(arg_results[0]);
|
}
|
||||||
} else if (strcmp(t[1].Str, "pow") == 0) {
|
if (!func_found) {
|
||||||
if (arg_results_size != 2) {
|
fprintf(stderr, "Error: unknown function: %s()\n", t[1].Str);
|
||||||
fprintf(stderr, "Error: function pow() requires exactly 2 arguments\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
outer_res = pow(arg_results[0], arg_results[1]);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "Error: unknown function name: %s\n", t[1].Str);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
t[1].kind = TokNum;
|
t[1].kind = TokNum;
|
||||||
t[1].Num = outer_res;
|
t[1].Num = outer_res;
|
||||||
}
|
}
|
||||||
@ -289,11 +305,24 @@ void cleanup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
real fn_sqrt(real *args) { return sqrt(args[0]); }
|
||||||
|
real fn_pow(real *args) { return pow(args[0], args[1]); }
|
||||||
|
real fn_mod(real *args) { return fmod(args[0], args[1]); }
|
||||||
|
real fn_round(real *args) { return round(args[0]); }
|
||||||
|
real fn_floor(real *args) { return floor(args[0]); }
|
||||||
|
real fn_ceil(real *args) { return ceil(args[0]); }
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc != 2 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
|
if (argc != 2 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
|
||||||
fprintf(stderr, "Usage: ./exp \"<expression>\"\n");
|
fprintf(stderr, "Usage: ./exp \"<expression>\"\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
add_func("sqrt", fn_sqrt, 1);
|
||||||
|
add_func("pow", fn_pow, 2);
|
||||||
|
add_func("mod", fn_mod, 2);
|
||||||
|
add_func("round", fn_round, 1);
|
||||||
|
add_func("floor", fn_floor, 1);
|
||||||
|
add_func("ceil", fn_ceil, 1);
|
||||||
tokenize(argv[1]);
|
tokenize(argv[1]);
|
||||||
print_toks();
|
print_toks();
|
||||||
real res = eval(toks);
|
real res = eval(toks);
|
||||||
|
Loading…
Reference in New Issue
Block a user