-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.c
100 lines (89 loc) · 2.52 KB
/
logic.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
/** @file Ficheiro que contém as funcionalidades relativas à lógica */
#include "string.h"
#include "stack.h"
#include "math.h"
void Is_equal(STACK *s) {
if (has_type(top(s), STRING)) {
compare_strings(s);
}
else {
double x = pop_operand(s),
y = pop_operand(s);
push_INT(s, y == x);
}
}
void Is_lower(STACK *s) {
if (has_type(top(s), STRING)) {
compare_less_than_strings(s);
}
else {
double x = pop_operand(s),
y = pop_operand(s);
push_INT(s, y < x);
}
}
void Is_greater(STACK *s) {
if (has_type(top(s), STRING)) {
compare_greater_than_strings(s);
}
else {
double x = pop_operand(s),
y = pop_operand(s);
push_INT(s, y > x);
}
}
void negate(STACK*s) {
double x = pop_operand(s);
push_INT(s, !x);
}
void get_lower(STACK *s) {
if (has_type(top(s), STRING)) {
get_lower_string(s);
}
else {
double x = pop_operand(s),
y = pop_operand(s);
if (x < y) {push(s, s->stack[s->pos + 2]);return;}
push(s, s->stack[s->pos + 1]);
}
}
void get_greater(STACK *s) {
if (has_type(top(s), STRING)) {
get_greater_string(s);
}
else {
double x = pop_operand(s),
y = pop_operand(s);
if (x > y) {push(s, s->stack[s->pos + 2]);return;}
push(s, s->stack[s->pos + 1]);
}
}
void or_logic(STACK *s) {
double x = pop_operand(s),
y = pop_operand(s);
if (y) {push(s, s->stack[s->pos + 1]);return;}
if (x) {push(s, s->stack[s->pos + 2]);return;}
push_INT(s, 0);
}
void and_logic(STACK *s) {
double x = pop_operand(s),
y = pop_operand(s);
if (x == 0 || y == 0) {push_INT(s, 0); return;}
push(s, s->stack[s->pos + 2]);
}
void if_then_else (STACK *s) {
DATA ELSE = pop(s),
THEN = pop(s);
double IF = pop_operand(s);
if (IF) {push(s, THEN); return;}
push(s, ELSE);
}
void logic_shortcut (STACK *s, char *token) {
token++;
switch(*token) {
case '&': and_logic(s); break;
case '|': or_logic(s); break;
case '>': get_greater(s); break;
case '<': get_lower(s); break;
}
}