summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/evalexpr/src/shunting_yard.c
blob: 2db5fc8b8db80e72c8bc3e7519261dc8f0284dc5 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>

#include "evalexpr.h"

enum assoc
{
    NONE,
    RIGHT,
    LEFT,
};

int opcmp(struct token *op1, struct token *op2)
{
    return op1->type - op2->type;
}

enum assoc get_assoc(struct token *op)
{
    if (op->type == EXP)
    {
        return RIGHT;
    }
    return LEFT;
}

enum token_type get_type(char op)
{
    switch (op)
    {
    case '*':
        return MUL;
    case '/':
        return DIV;
    case '-':
        return SUB;
    case '+':
        return ADD;
    case '%':
        return MOD;
    case '^':
        return EXP;
    case '(':
        return PAR_LEFT;
    case ')':
        return PAR_RIGHT;
    default:
        return WRONG_TOKEN;
    }
}

char *fifo_to_expr(struct fifo *output, size_t size_offset)
{
    char *res = malloc((2 * fifo_size(output) + size_offset) * sizeof(char));
    if (!res)
    {
        fifo_destroy(output);
        return NULL;
    }
    res[2 * fifo_size(output) + size_offset - 1] = '\0';
    size_t i = 0;
    while (fifo_size(output) > 0)
    {
        if (fifo_head(output)->type == NUMBER)
        {
            i += sprintf(res + i, "%d", fifo_head(output)->value) - 1;
        }
        else
        {
            res[i] = fifo_head(output)->value;
        }
        fifo_pop(output);
        i++;
        if (fifo_size(output) != 0)
        {
            res[i] = ' ';
            i++;
        }
    }
    fifo_destroy(output);
    return res;
}

int handle_rpar(struct tstack **opstack, struct fifo *output, struct token **t)
{
    while (*opstack)
    {
        struct token *o2 = tstack_peek(*opstack);
        if (o2->value == '(')
        {
            free(o2);
            break;
        }
        *opstack = tstack_pop(*opstack);
        fifo_push(output, o2);
    }
    if (*opstack == NULL)
    {
        free(*t);
        fifo_destroy(output);
        return 2;
    }
    free(*t);
    *opstack = tstack_pop(*opstack);
    return 0;
}

int empty_opstack(struct tstack **opstack, struct fifo *output)
{
    while (*opstack)
    {
        struct token *t = tstack_peek(*opstack);
        if (t->value == '(' || t->value == ')')
        {
            fifo_destroy(output);
            return 1;
        }
        *opstack = tstack_pop(*opstack);
        fifo_push(output, t);
    }
    return 0;
}

void pop_ops(struct tstack **opstack, struct fifo *output, struct token *t)
{
    while (*opstack)
    {
        struct token *o2 = tstack_peek(*opstack);
        if (!(o2->value != '('
              && (opcmp(t, o2) < 0
                  || (opcmp(t, o2) == 0 && get_assoc(t) == LEFT))))
        {
            break;
        }

        *opstack = tstack_pop(*opstack);
        fifo_push(output, o2);
    }
}

void handle_numbers(struct fifo *output, size_t *i, int *size_offset,
                    const char *expr)
{
    size_t offset;
    int val = parse_number(expr + *i, &offset);
    struct token *t = malloc(sizeof(struct token));
    t->type = NUMBER;
    t->value = val;
    fifo_push(output, t);
    *i += offset;
    *size_offset += offset;
}

int shunting_yard(const char *expr, char **rpn)
{
    struct fifo *output = fifo_init();
    struct tstack *opstack = NULL;
    int size_offset = 0;
    for (size_t i = 0; expr[i]; i++)
    {
        if (expr[i] >= '0' && expr[i] <= '9')
        {
            handle_numbers(output, &i, &size_offset, expr);
        }
        else if (expr[i] != ' ')
        {
            struct token *t = malloc(sizeof(struct token));
            t->value = expr[i];
            if ((t->type = get_type(expr[i])) == WRONG_TOKEN)
            {
                free(t);
                return 1;
            }
            if (t->value == '(')
            {
                opstack = tstack_push(opstack, t);
                continue;
            }
            else if (t->value == ')')
            {
                if (handle_rpar(&opstack, output, &t) != 0)
                    return 2;
                continue;
            }
            pop_ops(&opstack, output, t);
            opstack = tstack_push(opstack, t);
        }
    }

    if (empty_opstack(&opstack, output) != 0)
    {
        return 1;
    }
    *rpn = fifo_to_expr(output, size_offset);
    if (!*rpn)
        return 4;

    return 0;
}