-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
63 lines (60 loc) · 1.29 KB
/
main.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
#include "monty.h"
gdata_t *globaldata;
/**
* main - main function for monty language interpreter
* @argc: arguments count
* @argv: value of arguments
* Return: exit status.
*/
int main(int argc, char const *argv[])
{
stack_t *stack = NULL;
size_t i = 0, j = 0, ins = 0;
char *token, *aux, *code, **instrucs;
gdata_t init = {"", NULL, "", 0, NULL, NULL, 1};
instruction_t opcodes[] = { {"push", push}, {"pall", pall}, {"pint", pint},
{"pop", pop}, {"swap", swap}, {"nop", nop}, {"sub", sub}, {"add", add},
{"div", _div}, {"mul", mul}, {"mod", mod}, {NULL, NULL}
};
debugUsage(argc);
globaldata = &init;
globaldata->filepath = strCat(globaldata->filepath, argv[1]);
instrucs = fixSpace(fileHandle(globaldata->filepath));
globaldata->top = stack;
for (j = 0; j < globaldata->linecount; j++)
{
aux = strCat("", instrucs[j]);
code = aux;
token = strtok(code, " ");
if (!token)
goto skip;
if (strcmp(token, "push") == 0)
{
token = strtok(NULL, " ");
globaldata->number = strCat("", token);
}
i = 0;
while (opcodes[i].opcode)
{
ins = 0;
if (strcmp(opcodes[i].opcode, aux) == 0)
{
opcodes[i].f(&globaldata->top, j + 1);
ins = 1;
break;
}
i++;
}
if (ins == 0)
{
fprintf(stderr, "L%ld: unknown instruction %s\n", j + 1, aux);
exit(EXIT_FAILURE);
}
skip:
free(aux);
}
freeArr(instrucs);
freeStack();
freeGlobal();
return (0);
}