Compare commits

..

No commits in common. "dacf01e83e89c7295fd048aafaa2572f31da21e7" and "dfc515b7dfa3b6778cd7d17e933a606495f6dfaa" have entirely different histories.

38
main.c
View File

@ -35,7 +35,6 @@ uint8_t op_prec[256] = {
['/'] = 2,
['^'] = 3,
};
#define OP_PREC(tok_char) (op_prec[(size_t)tok_char])
enum {
OrderLtr,
@ -49,7 +48,6 @@ enum {
['/'] = OrderLtr,
['^'] = OrderRtl,
};
#define OP_ORDER(tok_char) (op_order[(size_t)tok_char])
#define IS_FLOAT(c) ((c >= '0' && c <= '9') || c == '.')
#define IS_ALPHA(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
@ -64,12 +62,15 @@ void tokenize(char *expr) {
size_t paren_depth = 0;
bool can_be_neg_num = true;
char *curr = expr;
for (char c = *curr; c != 0; c = *(++curr)) {
if (c == ' ')
continue;
if (IS_FLOAT(c)) {
if (IS_FLOAT(c) ||
(can_be_neg_num && c == '-' && IS_FLOAT(curr[1]))) {
char buf[16];
buf[0] = c;
size_t i = 1;
@ -83,6 +84,8 @@ void tokenize(char *expr) {
real num = strtod(buf, NULL);
push_tok((Tok){.kind = TokNum, .Num = num});
can_be_neg_num = false;
continue;
}
@ -98,6 +101,8 @@ void tokenize(char *expr) {
buf[i++] = 0;
push_tok((Tok){.kind = TokFunc, .Str = buf});
can_be_neg_num = false;
continue;
}
@ -127,6 +132,8 @@ void tokenize(char *expr) {
fprintf(stderr, "Error: unrecognized token at %zd: '%c'\n", curr - expr, c);
exit(1);
}
can_be_neg_num = c != ')';
}
if (paren_depth > 0) {
@ -161,34 +168,23 @@ void print_toks() {
printf("\n");
}
/* Delete tokens from begin to end (excluding end itself). */
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)) {
if (!(t[0].kind == TokOp && op_prec[(size_t)t[0].Char] == 0)) {
fprintf(stderr, "Error: expected delimiter at beginning of expression\n");
exit(1);
}
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++);
for (i = 2; !(t[i].kind == TokOp && op_prec[(size_t)t[i].Char] == 0); i++);
del_toks(t + 2, t + i + 1);
/* Put the newly evaluated value into place. */
t[1].kind = TokNum;
@ -210,7 +206,7 @@ real eval(Tok *t) {
while (1) {
arg_results[arg_results_size++] = eval(t); /* TODO: Overflow protection. */
size_t i = 1;
for (; !(t[i].kind == TokOp && OP_PREC(t[i].Char) == 0); i++);
for (; !(t[i].kind == TokOp && op_prec[(size_t)t[i].Char] == 0); i++);
bool end = t[i].Char == ')';
if (t[i].Char == ',')
del_toks(t, t + i);
@ -248,18 +244,18 @@ real eval(Tok *t) {
}
const char curr_op = t[0].Char;
const uint8_t curr_prec = OP_PREC(curr_op);
const uint8_t curr_prec = op_prec[(size_t)curr_op];
const char next_op = t[2].Char;
const uint8_t next_prec = OP_PREC(next_op);
const uint8_t next_prec = op_prec[(size_t)next_op];
/* Delimiters have a precedence of 0; if we have a number between two delimiters, we're done. */
if (curr_prec == 0 && next_prec == 0)
return t[1].Num;
if (next_prec > curr_prec || (next_prec == curr_prec && OP_ORDER(curr_op) == OrderRtl)) {
if (next_prec > curr_prec || (next_prec == curr_prec && op_order[(size_t)curr_op] == OrderRtl)) {
t += 2;
} else if (next_prec < curr_prec || (next_prec == curr_prec && OP_ORDER(curr_op) == OrderLtr)) {
} else if (next_prec < curr_prec || (next_prec == curr_prec && op_order[(size_t)curr_op] == OrderLtr)) {
real res;
real lhs = t[-1].Num, rhs = t[1].Num;
switch (curr_op) {