-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_opcode_func.c
118 lines (105 loc) · 1.92 KB
/
get_opcode_func.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
#include "monty.h"
/**
* get_opcode_func - it handles the opcode function been passed
*
* description: checks if the opcode string is valid
*/
void get_opcode_func(void)
{
size_t i;
static instruction_t opcodes[] = {
{"push", push}, {"pall", pall},
{"pint", pint},
{"pop", pop},
{"add", add},
{"sub", sub},
{"div", _div},
{"mul", mul},
{"swap", swap},
{"nop", nop},
{"mod", mod},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{"rotr", rotr},
{NULL, NULL}
};
if (file_ptr->num_tokens == 0)
return;
if (file_ptr->tokens[0][0] == '#')
{
handle_hash();
return;
}
for (i = 0; opcodes[i].opcode != NULL; i++)
{
if (strcmp(opcodes[i].opcode, file_ptr->tokens[0]) == 0)
{
file_ptr->opcode_instruction->opcode = opcodes[i].opcode;
file_ptr->opcode_instruction->f = opcodes[i].f;
return;
}
}
invalid_instruction();
}
/**
* invalid_instruction - handles the error message
*
* description: freeing of invaild functions
*/
void invalid_instruction(void)
{
fprintf(stderr, "L%d: unknown instruction %s\n",
file_ptr->line_number, file_ptr->tokens[0]);
fclose_file();
free_tokens();
free_file_ptr();
exit(EXIT_FAILURE);
}
/**
* fclose_file - closes the file
*/
void fclose_file(void)
{
if (file_ptr->file != NULL)
{
fclose(file_ptr->file);
}
file_ptr->file = NULL;
}
/**
* free_tokens - frees all the tokenized string
*/
void free_tokens(void)
{
int i = 0;
if (file_ptr->tokens == NULL)
return;
while (file_ptr->tokens[i])
{
free(file_ptr->tokens[i]);
i++;
}
free(file_ptr->tokens);
file_ptr->tokens = NULL;
}
/**
* free_file_ptr - free the file_ptr, the pointer to the struct
*/
void free_file_ptr(void)
{
if (file_ptr == NULL)
return;
if (file_ptr->opcode_instruction)
{
free(file_ptr->opcode_instruction);
file_ptr->opcode_instruction = NULL;
}
free_head();
if (file_ptr->line)
{
free(file_ptr->line);
file_ptr->line = NULL;
}
free(file_ptr);
}