summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/evalexpr/src/evalrpn.c
blob: db493eb4a507f227c346996edd03b4ef2a5c2be9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <ctype.h>

#include "evalexpr.h"

int parse_number(const char *expr, size_t *offset)
{
    for (*offset = 0;
         expr[*offset] && expr[*offset] >= '0' && expr[*offset] <= '9';
         (*offset)++)
    {
        continue;
    }
    (*offset)--;

    int res = 0;
    int pow = 1;
    for (size_t n = 0; n <= *offset; n++, pow *= 10)
    {
        res += (expr[*offset - n] - '0') * pow;
    }

    return res;
}

int get_operands(struct stack **s, int *op1, int *op2)
{
    if (*s == NULL)
    {
        return 2;
    }
    *op1 = stack_peek(*s);
    *s = stack_pop(*s);
    if (*s == NULL)
    {
        return 2;
    }
    *op2 = stack_peek(*s);
    *s = stack_pop(*s);
    return 0;
}

// Computes a to the bth
int my_pow(int a, int b)
{
    if (b == 0)
    {
        return 1;
    }
    if (a == 0)
    {
        return 0;
    }

    int res = 1;
    int c = b / 2;
    while (c != 0)
    {
        res *= a * a;
        c /= 2;
    }
    if (b % 2 != 0)
        res *= a;
    return res;
}

int dispatch(const char c, struct stack **s, int op1, int op2)
{
    switch (c)
    {
    case '*':
        *s = stack_push(*s, op1 * op2);
        break;
    case '+':
        *s = stack_push(*s, op1 + op2);
        break;
    case '-':
        *s = stack_push(*s, op2 - op1);
        break;
    case '/':
        if (op1 == 0)
        {
            return 3;
        }
        *s = stack_push(*s, op2 / op1);
        break;
    case '%':
        if (op1 == 0)
        {
            return 3;
        }
        *s = stack_push(*s, op2 % op1);
        break;
    case '^':
        if (op1 < 0)
        {
            return 3;
        }
        *s = stack_push(*s, my_pow(op2, op1));
        break;
    default:
        return 1;
    }
    return 0;
}

int evalrpn(const char *expr, int *retval)
{
    struct stack *s = NULL;

    for (size_t i = 0; expr[i]; i++)
    {
        if (expr[i] >= '0' && expr[i] <= '9')
        {
            size_t offset;
            int val = parse_number(expr + i, &offset);
            s = stack_push(s, val);
            i += offset;
        }
        else if (isspace(expr[i]))
            continue;
        else
        {
            int op1;
            int op2;
            if (get_operands(&s, &op1, &op2) == 2)
            {
                return 2;
            }

            int d = dispatch(expr[i], &s, op1, op2);
            if (d != 0)
            {
                return d;
            }
        }
    }

    if (s == NULL)
    {
        return 0;
    }
    *retval = stack_peek(s);
    s = stack_pop(s);
    return (!s) ? 0 : 2;
}