-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.h
More file actions
91 lines (85 loc) · 2.74 KB
/
Header.h
File metadata and controls
91 lines (85 loc) · 2.74 KB
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
#include <stdio.h>
#include "Stack.h"
#include "Compilator.h"
void Processing ();
int FLength(FILE *fcode);
void Implementation (struct lexem *exe, struct stack *stack);
void Processing ()
{
struct lexem *exe = Compile();
struct stack *st;
st = Stack_Init ();
Implementation (exe, st);
Stack_Destraction (st);
}
void Implementation (struct lexem *exe, struct stack *stack) {
assert (exe);
assert(stack);
int nlexem = 0;
int reg_arr[4] = {0};
while (exe[nlexem].flagcmd != END)
{
switch (exe[nlexem].numcmd) {
case PUSH : {
if (exe[nlexem].flagnum == NUM)
Push (stack, exe[nlexem].num);
if (exe[nlexem].flagnum == REG)
reg_arr[exe[nlexem].num] = stack->data[stack->size];
break;
}
case POP:{
if (exe[nlexem].flagnum == ZERO)
{
int pop = Pop(stack);
if (pop != -1)
printf ("%d", pop);
}
if (exe[nlexem].flagnum == REG)
Push (stack, reg_arr[exe[nlexem].num]);
break;
}
case ADD:{
//printf ("add %d %d", stack->data[stack->size], stack->data[stack->size - 1]);
stack->data[stack->size - 1] = stack->data[stack->size - 2] + stack->data[stack->size - 1];
break;
}
case SUB:{
stack->data[stack->size - 1] = stack->data[stack->size - 2] - stack->data[stack->size - 1];
break;
}
case MULT:{
stack->data[stack->size - 1] = stack->data[stack->size - 2] * stack->data[stack->size - 1];
break;
}
case DIV:{
stack->data[stack->size - 1] = stack->data[stack->size - 2] / stack->data[stack->size - 1];
break;
}
case IN:{
int in = NAN;
printf (ANSI_COLOR_YELLOW "Enter the number:\n" ANSI_COLOR_WHITE);
if (scanf ("%d", &in))
{
Push(stack, in);
break;
}
printf ("Input error");
}
case OUT:{
if (stack->size > 0)
printf (ANSI_COLOR_GREEN "Result:%d\n", stack->data[stack->size - 1], ANSI_COLOR_WHITE);
else
{
printf("There is nothing to output.\n");
Dump(stack);
}
break;
}
case DUMP:{
Dump(stack);
break;
}
}
nlexem += 1;
}
}