Compare commits
4 Commits
dacf01e83e
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
f9352abf06 | ||
|
fca5950f6c | ||
|
b4ea1650b7 | ||
|
e3c466e321 |
224
main.c
224
main.c
@@ -9,9 +9,10 @@ typedef double real;
|
||||
|
||||
typedef struct Tok {
|
||||
enum {
|
||||
TokNull,
|
||||
TokOp,
|
||||
TokNum,
|
||||
TokFunc,
|
||||
TokIdent,
|
||||
} kind;
|
||||
|
||||
union {
|
||||
@@ -22,10 +23,10 @@ typedef struct Tok {
|
||||
} Tok;
|
||||
|
||||
#define TOKS_CAP 65536
|
||||
Tok toks[TOKS_CAP];
|
||||
size_t toks_size = 0;
|
||||
static Tok toks[TOKS_CAP];
|
||||
static size_t toks_size = 0;
|
||||
|
||||
uint8_t op_prec[256] = {
|
||||
static uint8_t op_prec[256] = {
|
||||
['('] = 0, /* A precedence of 0 is reserved for delimiters. */
|
||||
[')'] = 0,
|
||||
[','] = 0,
|
||||
@@ -37,7 +38,7 @@ uint8_t op_prec[256] = {
|
||||
};
|
||||
#define OP_PREC(tok_char) (op_prec[(size_t)tok_char])
|
||||
|
||||
enum {
|
||||
static enum {
|
||||
OrderLtr,
|
||||
OrderRtl,
|
||||
} op_order[256] = {
|
||||
@@ -54,18 +55,69 @@ enum {
|
||||
#define IS_FLOAT(c) ((c >= '0' && c <= '9') || c == '.')
|
||||
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
|
||||
|
||||
void push_tok(Tok t) {
|
||||
if (toks_size+1 < TOKS_CAP)
|
||||
typedef struct Var {
|
||||
const char *name;
|
||||
real val;
|
||||
} Var;
|
||||
|
||||
#define VARS_CAP 256
|
||||
static Var vars[VARS_CAP];
|
||||
static size_t vars_size = 0;
|
||||
|
||||
static void set_var(const char *name, real val) {
|
||||
for (size_t i = 0; i < vars_size; i++) {
|
||||
if (strcmp(vars[i].name, name) == 0) {
|
||||
vars[i].val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
vars[vars_size++] = (Var){.name = name, .val = val};
|
||||
}
|
||||
|
||||
static void unset_var(const char *name) {
|
||||
for (size_t i = 0; i < vars_size; i++) {
|
||||
if (strcmp(vars[i].name, name) == 0) {
|
||||
memmove(vars + i, vars + i + 1, sizeof(Var) * (vars_size - (i + 1)));
|
||||
vars_size--;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct Function {
|
||||
const char *name;
|
||||
real (*func)(real *args);
|
||||
size_t n_args;
|
||||
} Function;
|
||||
|
||||
#define FUNCTIONS_CAP 256
|
||||
static Function functions[FUNCTIONS_CAP];
|
||||
static size_t functions_size = 0;
|
||||
|
||||
static void push_tok(Tok t) {
|
||||
if (toks_size < TOKS_CAP)
|
||||
toks[toks_size++] = t;
|
||||
}
|
||||
|
||||
void tokenize(char *expr) {
|
||||
static void add_func(const char *name, real (*func)(real *args), size_t n_args) {
|
||||
if (functions_size < FUNCTIONS_CAP)
|
||||
functions[functions_size++] = (Function){.name = name, .func = func, .n_args = n_args};
|
||||
}
|
||||
|
||||
static void tokenize(char *expr) {
|
||||
push_tok((Tok){.kind = TokOp, .Char = '('});
|
||||
|
||||
size_t paren_depth = 0;
|
||||
|
||||
Tok last;
|
||||
|
||||
char *curr = expr;
|
||||
for (char c = *curr; c != 0; c = *(++curr)) {
|
||||
if (toks_size > 0)
|
||||
last = toks[toks_size-1];
|
||||
else
|
||||
last = (Tok){.kind = TokNull};
|
||||
|
||||
if (c == ' ')
|
||||
continue;
|
||||
|
||||
@@ -82,6 +134,9 @@ void tokenize(char *expr) {
|
||||
|
||||
real num = strtod(buf, NULL);
|
||||
|
||||
if (last.kind == TokIdent || (last.kind == TokOp && last.Char == ')') || last.kind == TokNum)
|
||||
push_tok((Tok){.kind = TokOp, .Char = '*'});
|
||||
|
||||
push_tok((Tok){.kind = TokNum, .Num = num});
|
||||
continue;
|
||||
}
|
||||
@@ -97,7 +152,10 @@ void tokenize(char *expr) {
|
||||
curr += i - 1;
|
||||
buf[i++] = 0;
|
||||
|
||||
push_tok((Tok){.kind = TokFunc, .Str = buf});
|
||||
if (last.kind == TokIdent || (last.kind == TokOp && last.Char == ')') || last.kind == TokNum)
|
||||
push_tok((Tok){.kind = TokOp, .Char = '*'});
|
||||
|
||||
push_tok((Tok){.kind = TokIdent, .Str = buf});
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -120,6 +178,8 @@ void tokenize(char *expr) {
|
||||
case '*':
|
||||
case '/':
|
||||
case '^': {
|
||||
if (c == '(' && ((last.kind == TokOp && last.Char == ')') || last.kind == TokNum))
|
||||
push_tok((Tok){.kind = TokOp, .Char = '*'});
|
||||
push_tok((Tok){.kind = TokOp, .Char = c});
|
||||
break;
|
||||
}
|
||||
@@ -137,7 +197,7 @@ void tokenize(char *expr) {
|
||||
push_tok((Tok){.kind = TokOp, .Char = ')'});
|
||||
}
|
||||
|
||||
void print_toks() {
|
||||
static void print_toks() {
|
||||
for (size_t i = 0; i < toks_size; i++) {
|
||||
switch (toks[i].kind) {
|
||||
case TokOp:
|
||||
@@ -150,7 +210,7 @@ void print_toks() {
|
||||
case TokNum:
|
||||
printf("%.2f ", toks[i].Num);
|
||||
break;
|
||||
case TokFunc:
|
||||
case TokIdent:
|
||||
printf("%s ", toks[i].Str);
|
||||
break;
|
||||
default:
|
||||
@@ -162,53 +222,47 @@ void print_toks() {
|
||||
}
|
||||
|
||||
/* Delete tokens from begin to end (excluding end itself). */
|
||||
void del_toks(Tok *begin, Tok *end) {
|
||||
static void del_toks(Tok *begin, Tok *end) {
|
||||
memmove(begin, end, (toks_size - (end - toks)) * sizeof(Tok));
|
||||
toks_size -= end - begin;
|
||||
}
|
||||
|
||||
real eval(Tok *t) {
|
||||
if (!(t[0].kind == TokOp && OP_PREC(t[0].Char) == 0)) {
|
||||
fprintf(stderr, "Error: expected delimiter at beginning of expression\n");
|
||||
exit(1);
|
||||
static real eval(Tok *t);
|
||||
|
||||
static void collapse(Tok *t) {
|
||||
/* Collapse factor. */
|
||||
if (t[1].kind == TokOp && t[1].Char == '-') {
|
||||
collapse(t + 1);
|
||||
if (t[2].kind != TokNum) {
|
||||
fprintf(stderr, "Error: uncollapsable expression after minus factor\n");
|
||||
exit(1);
|
||||
}
|
||||
t[2].Num *= -1.0;
|
||||
del_toks(t + 1, t + 2);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
/* Collapse factor. */
|
||||
if (t[1].kind == TokOp && t[1].Char == '-') {
|
||||
if (t[2].kind != TokNum) {
|
||||
fprintf(stderr, "Error: expected number token after minus factor\n");
|
||||
exit(1);
|
||||
}
|
||||
t[2].Num *= -1.0;
|
||||
del_toks(t + 1, t + 2);
|
||||
}
|
||||
|
||||
/* Collapse parentheses. */
|
||||
if (t[1].kind == TokOp && t[1].Char == '(') {
|
||||
real res = eval(t + 1);
|
||||
size_t i;
|
||||
for (i = 2; !(t[i].kind == TokOp && OP_PREC(t[i].Char) == 0); i++);
|
||||
del_toks(t + 2, t + i + 1);
|
||||
/* Put the newly evaluated value into place. */
|
||||
t[1].kind = TokNum;
|
||||
t[1].Num = res;
|
||||
}
|
||||
/* Collapse parentheses. */
|
||||
if (t[1].kind == TokOp && t[1].Char == '(') {
|
||||
real res = eval(t + 1);
|
||||
size_t i;
|
||||
for (i = 2; !(t[i].kind == TokOp && OP_PREC(t[i].Char) == 0); i++);
|
||||
del_toks(t + 2, t + i + 1);
|
||||
/* Put the newly evaluated value into place. */
|
||||
t[1].kind = TokNum;
|
||||
t[1].Num = res;
|
||||
}
|
||||
|
||||
|
||||
/* Collapse function. */
|
||||
if (t[1].kind == TokFunc) {
|
||||
if (t + 2 >= toks + toks_size || !(t[2].kind == TokOp && t[2].Char == '(')) {
|
||||
fprintf(stderr, "Error: expected '(' token after function\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (t[1].kind == TokIdent) {
|
||||
if (t + 2 < toks + toks_size && (t[2].kind == TokOp && t[2].Char == '(')) {
|
||||
/* Collapse function. */
|
||||
real arg_results[16];
|
||||
size_t arg_results_size = 0;
|
||||
|
||||
t += 2;
|
||||
while (1) {
|
||||
arg_results[arg_results_size++] = eval(t); /* TODO: Overflow protection. */
|
||||
if (arg_results_size < 16)
|
||||
arg_results[arg_results_size++] = eval(t);
|
||||
size_t i = 1;
|
||||
for (; !(t[i].kind == TokOp && OP_PREC(t[i].Char) == 0); i++);
|
||||
bool end = t[i].Char == ')';
|
||||
@@ -222,26 +276,54 @@ real eval(Tok *t) {
|
||||
t -= 2;
|
||||
|
||||
real outer_res;
|
||||
if (strcmp(t[1].Str, "sqrt") == 0) {
|
||||
if (arg_results_size != 1) {
|
||||
fprintf(stderr, "Error: function sqrt() requires exactly 1 argument\n");
|
||||
exit(1);
|
||||
bool func_found = false;
|
||||
for (size_t i = 0; i < functions_size; i++) {
|
||||
if (strcmp(t[1].Str, functions[i].name) == 0) {
|
||||
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 (arg_results_size != 2) {
|
||||
fprintf(stderr, "Error: function pow() requires exactly 2 arguments\n");
|
||||
exit(1);
|
||||
}
|
||||
if (!func_found) {
|
||||
fprintf(stderr, "Error: unknown function: %s()\n", t[1].Str);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
t[1].kind = TokNum;
|
||||
t[1].Num = outer_res;
|
||||
} else {
|
||||
/* Collapse variable. */
|
||||
real res;
|
||||
bool found = false;
|
||||
for (size_t i = 0; i < vars_size; i++) {
|
||||
if (strcmp(t[1].Str, vars[i].name) == 0) {
|
||||
found = true;
|
||||
res = vars[i].val;
|
||||
}
|
||||
outer_res = pow(arg_results[0], arg_results[1]);
|
||||
} else {
|
||||
fprintf(stderr, "Error: unknown function name: %s\n", t[1].Str);
|
||||
}
|
||||
if (!found) {
|
||||
fprintf(stderr, "Error: unknown variable: %s\n", t[1].Str);
|
||||
exit(1);
|
||||
}
|
||||
t[1].kind = TokNum;
|
||||
t[1].Num = outer_res;
|
||||
t[1].Num = res;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static real eval(Tok *t) {
|
||||
if (!(t[0].kind == TokOp && OP_PREC(t[0].Char) == 0)) {
|
||||
fprintf(stderr, "Error: expected delimiter at beginning of expression\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
collapse(t);
|
||||
|
||||
if (!(t[0].kind == TokOp && t[1].kind == TokNum && t[2].kind == TokOp)) {
|
||||
fprintf(stderr, "Error: invalid token order\n");
|
||||
exit(1);
|
||||
@@ -282,18 +364,38 @@ real eval(Tok *t) {
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
static void cleanup() {
|
||||
for (size_t i = 0; i < toks_size; i++) {
|
||||
if (toks[i].kind == TokFunc)
|
||||
if (toks[i].kind == TokIdent)
|
||||
free(toks[i].Str);
|
||||
}
|
||||
}
|
||||
|
||||
static real fn_sqrt(real *args) { return sqrt(args[0]); }
|
||||
static real fn_pow(real *args) { return pow(args[0], args[1]); }
|
||||
static real fn_mod(real *args) { return fmod(args[0], args[1]); }
|
||||
static real fn_round(real *args) { return round(args[0]); }
|
||||
static real fn_floor(real *args) { return floor(args[0]); }
|
||||
static real fn_ceil(real *args) { return ceil(args[0]); }
|
||||
static real fn_sin(real *args) { return sin(args[0]); }
|
||||
static real fn_cos(real *args) { return cos(args[0]); }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
|
||||
fprintf(stderr, "Usage: ./exp \"<expression>\"\n");
|
||||
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);
|
||||
add_func("sin", fn_sin, 1);
|
||||
add_func("cos", fn_cos, 1);
|
||||
unset_var("x");
|
||||
set_var("pi", M_PI);
|
||||
set_var("e", M_E);
|
||||
tokenize(argv[1]);
|
||||
print_toks();
|
||||
real res = eval(toks);
|
||||
|
Reference in New Issue
Block a user