-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetfunc.c
64 lines (59 loc) · 1.27 KB
/
getfunc.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
#include "monty.h"
/**
* get_op_func - searches and matches text to opcode
* then returns corresponding function.
* @line: struct containing line content and number.
* @meta: struct containing all allocated memory.
*
* Return: pointer to relevant function.
*/
void (*get_op_func(line_t line, meta_t *meta))(stack_t **, unsigned int)
{
unsigned int i = 0;
instruction_t ops[] = {
{"push", push},
{"pall", pall},
{"pint", pint},
{"pop", pop},
{"swap", swap},
{"add", addop},
{"sub", subop},
{"mul", mulop},
{"div", divop},
{"mod", modop},
{"nop", nop},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotlop},
{"rotr", rotrop},
{"stack", addst},
{"queue", addqu},
{NULL, NULL}
};
if (comment_check(line))
return (nop);
while (ops[i].opcode)
{
if (strcmp(ops[i].opcode, line.content[0]) == 0)
{
push_check(line, meta, ops[i].opcode);
if (arg.flag == 1 && strcmp(ops[i].opcode, "push") == 0)
{
if (line.content)
free(line.content);
return (qpush);
}
free(line.content);
return (ops[i].f);
}
i++;
}
fprintf(stderr, "L%d: unknown instruction %s\n",
line.number, line.content[0]);
free(line.content);
free(meta->buf);
free_stack(&(meta->stack));
fclose(meta->file);
free(meta);
exit(EXIT_FAILURE);
}