-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_operations.c
143 lines (133 loc) · 3.68 KB
/
handle_operations.c
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
/** @file Ficheiro que contém as funcionalidades relativas às operações do programa */
#include <string.h>
#include <stdio.h>
#include "handle_operations.h"
#include "stack.h"
#include "math.h"
#include "logic.h"
#include "stackManipulation.h"
#include "IO.h"
#include "array.h"
#include "string.h"
#include "block.h"
void handle_arithmetic_operation (STACK *s, char *token) {
char *possible_tokens = "+-*/#%|&^~()";
void (*operators[]) (STACK *s) = {
sum,
subtraction,
multiplication,
division,
power,
module,
or_bitwise,
and_bitwise,
xor_bitwise,
not_bitwise,
decrement,
increment
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}
void handle_logic_operation (STACK *s, char *token) {
if (*token == 'e') logic_shortcut(s, token);
char *possible_tokens = "=<>!?";
void (*operators[]) (STACK *s) = {
Is_equal,
Is_lower,
Is_greater,
negate,
if_then_else
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}
void handle_string_operation (STACK *s, char *token) {
char *possible_tokens = "+*,=<>()#/SN";
void (*operators[]) (STACK *s) = {
concatenate_string,
multiply_string,
string_length,
string_element_by_index,
get_string_elements_by_first,
get_string_elements_by_last,
remove_string_first_element,
remove_string_last_element,
search_substring,
separate_string_by_substring,
separate_string_by_whitespace,
separate_string_by_newline
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}
void handle_array_operation (STACK *s, char *token) {
char *possible_tokens = "~+*,=<>()";
void (*operators[]) (STACK *s) = {
array_to_stack,
concatenate_array,
multiply_array,
range_array,
array_element_by_index,
get_array_elements_by_first,
get_array_elements_by_last,
remove_array_first_element,
remove_array_last_element
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}
void handle_stack_operation (STACK *s, char *token) {
if (*token == ';') pop(s);
char *possible_tokens = "_\\@$ifcs";
void (*operators[]) (STACK *s) = {
duplicate_top,
swap_top,
rotate_top,
copy_nth_element,
convert_stack_to_int,
convert_stack_to_float,
convert_stack_to_char,
convert_stack_to_string
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}
void handle_input_output_operation (STACK *s, char *token) {
switch(*token) {
case 'l': read_line(s); break;
case 't': read_all_input(s); break;
case 'p': print_data(s, top(s)); putchar('\n'); break;
}
}
void handle_block_operation(STACK *s, char *token) {
char *possible_tokens = "~%*,$w";
void (*operators[]) (STACK *s) = {
execute_block,
apply_block_to,
fold_array,
filter_by_block,
sort_by_block,
execute_while_true
};
for (int i = 0; possible_tokens[i] != '\0'; i++) {
if (possible_tokens[i] == *token) {
operators[i](s); return;
}
}
}