-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
129 lines (124 loc) · 3.79 KB
/
core.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
#include "monty.h"
/**
* parse_line - parses a line of Monty bytecode and updates program state
* @program_ptr: Pointer to the monty_program_t struct
*
* Description: this function takes a line from the Monty bytecode file,
* extracts the opcode and its argument (if present), and updates the
* program's state accordingly. If an error is found (e.g., invalid integer),
* the function will print an error message and exit the program.
*/
void parse_line(monty_program_t *program_ptr)
{
char *token;
char *endptr;
long arg;
char *line;
line = program_ptr->current_line;
while (*line == ' ')
{
line++;
}
if (*line == '#' || *line == '\0')
{
program_ptr->current_opcode = NULL;
return;
}
token = strtok(program_ptr->current_line, " \n\t");
if (token == NULL)
{
program_ptr->current_opcode = NULL;
return;
}
program_ptr->current_opcode = token;
if (strcmp(program_ptr->current_opcode, "push") == 0)
{
token = strtok(NULL, " \n\t");
if (token == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
arg = strtol(token, &endptr, 10);
if (*endptr != '\0' || token == endptr)
{
fprintf(stderr, "L%d: usage: push integer\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
program_ptr->current_arg = (int)arg;
}
}
/**
* execute_opcode - executes the current opcode
* @program_ptr: pointer to the monty_program_t struct
* Description: this function checks the current opcode and executes the
* corresponding function. If the opcode is NULL (indicating an empty line or
* just whitespace), or if the opcode is unknown, it handles these cases
* appropriately, including exiting the program with an error message if
* an unknown instruction is encountered.
*/
void execute_opcode(monty_program_t *program_ptr)
{
if (program_ptr->current_opcode == NULL)
return;
if (strcmp(program_ptr->current_opcode, "push") == 0)
push_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "pall") == 0)
pall_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "pint") == 0)
pint_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "pop") == 0)
pop_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "swap") == 0)
swap_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "add") == 0)
add_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "nop") == 0)
nop_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "sub") == 0)
sub_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "div") == 0)
div_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "mul") == 0)
mul_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "mod") == 0)
mod_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "pchar") == 0)
pchar_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "pstr") == 0)
pstr_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "rotl") == 0)
rotl_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "rotr") == 0)
rotr_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "stack") == 0)
stack_opcode(program_ptr);
else if (strcmp(program_ptr->current_opcode, "queue") == 0)
queue_opcode(program_ptr);
else
{
fprintf(stderr, "L%d: unknown instruction %s\n",
program_ptr->line_num, program_ptr->current_opcode);
exit(EXIT_FAILURE);
}
}
/**
* free_stack - frees a stack
* @stack: pointer to the top of the stack
*
* Description: this function iterates through stack and frees
* each element. the function continues until it traversed
* and freed the entire stack.
*/
void free_stack(stack_t *stack)
{
stack_t *temp;
while (stack != NULL)
{
temp = stack;
stack = stack->next;
free(temp);
}
}