-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_op_code.c
94 lines (88 loc) · 1.67 KB
/
get_op_code.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
#include "monty.h"
#include <stdio.h>
/**
* get_op_code - functi to select operations
* Return: int
*/
void (*get_op_code())(stack_t **stack, unsigned int line)
{
int i = 0, j = 0, k = 0;
int val = 0;
int tok = 0;
char *tokenize[1024];
instruction_t opCodes[] = {
{"push", op_push},
{"pall", op_pall},
{"pop", op_pop},
{"pint", op_pint},
{"swap", op_swap},
{"add", op_add},
{"sub", op_sub},
{"div", op_div},
{"mul", op_mul},
{"mod", op_mod},
{"pchar", op_pchar},
{"pstr", op_pstr},
{"nop", op_none},
{"#", op_none},
{"rotl", op_rotl},
{NULL, NULL}};
tokenize_line(dataStruct.lineTokenized, tokenize);
while (i < 15)
{
while (tokenize[j] && j == 0)
{
if (tokenize[j + 1])
{
while (tokenize[j + 1][k])
{
if ((tokenize[j + 1][k] < 48 || tokenize[j + 1][k] > 57)
&& tokenize[j + 1][k] != 45)
{
tok = 1;
break;
}
k++;
}
}
if (strcmp(opCodes[i].opcode, tokenize[j]) == 0 ||
(*opCodes[i].opcode == '#' && *tokenize[0] == '#'))
{
if (tokenize[j + 1] && !tok)
{
val = atoi(tokenize[j + 1]);
dataStruct.dataToSave = val;
}
else
{
dataStruct.valDataToSave = 1;
}
tok = 0;
return (opCodes[i].f);
}
j++;
}
j = 0;
i++;
}
dataStruct.opCodeNoExist = tokenize[0];
return (NULL);
}
/**
* tokenize_line - get a line and save info to validate
* @line: character to validate symbol
* @tokenize: pointer array to save tokenized line
* Return: void
*/
void tokenize_line(char *line, char **tokenize)
{
int i = 0;
tokenize[i] = strtok(line, " ");
i++;
while (line != NULL)
{
line = strtok(NULL, " ");
tokenize[i] = line;
i++;
}
}