-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
65 lines (60 loc) · 1.38 KB
/
execute.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
#include "monty.h"
/**
* execute_file - reads and executes opcodes from a file
* @stack: pointer to the top of the stack
*/
void execute_file(stack_t **stack)
{
char *opcode = NULL;
size_t len = 0;
ssize_t nread;
unsigned int line_number = 0;
while ((nread = getline(&glob.line, &len, glob.file)) != -1)
{
line_number++;
opcode = strtok(glob.line, " \t\r\n\a");
glob.arg = strtok(NULL, " \n\t");
if (opcode == NULL || *opcode == '#')
continue;
execute_opcode(opcode, stack, line_number);
}
}
/**
* execute_opcode - executes a single opcode
* @opcode: opcode to execute
* @stack: pointer to the top of the stack
* @line_number: line number of the opcode
*/
void execute_opcode(char *opcode, stack_t **stack, unsigned int line_number)
{
instruction_t instructions[] = {
{"push", op_push},
{"pall", op_pall},
{"pint", op_pint},
{"pop", op_pop},
{"swap", op_swap},
{"nop", op_nop},
{"add", op_add},
{"sub", op_sub},
{"div", op_div},
{"mul", op_mul},
{"mod", op_mod},
{"pchar", op_pchar},
{"pstr", op_pstr},
{NULL, NULL}
};
int i;
for (i = 0; instructions[i].opcode != NULL; i++)
{
if (strcmp(opcode, instructions[i].opcode) == 0)
{
instructions[i].f(stack, line_number);
return;
}
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, opcode);
free_stack(*stack);
fclose(glob.file);
free(glob.line);
exit(EXIT_FAILURE);
}