-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
93 lines (84 loc) · 1.55 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
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
#include "monty.h"
/**
* exec_opcode - finds opcode that matches with command
* @cmd: the command para
* @head: head pointer
* @line_number: number of lines read/executed
*
* Return: void
*/
void exec_opcode(char *cmd, stack_t **head, unsigned int line_number)
{
int i = 0;
instruction_t opcodes[] = {
{"push", push},
{"pall", pall},
{"pop", pop},
{"pint", pint},
{"swap", swap},
{"nop", nop},
{"add", add},
{"sub", sub},
{"div", op_div},
{"mul", mul},
{"mod", mod},
{"pchar", pchar},
{"pstr", pstr},
{NULL, NULL}
};
if (*cmd == '#')
{
return;
}
while (opcodes[i].opcode != NULL)
{
if (strcmp(opcodes[i].opcode, cmd) == 0)
{
opcodes[i].f(head, line_number);
return;
}
i++;
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, cmd);
exit(EXIT_FAILURE);
}
/**
* main - Start the program
* @argc: Argument count
* @argv: Argument Value(s)
*
* Return: Error or success code
*/
int main(int argc, char **argv)
{
FILE *fp = NULL;
char chunk[MAX_SIZE];
unsigned int line_num = 1;
stack_t *head = NULL;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
while (fgets(chunk, MAX_SIZE, fp) != NULL)
{
char *token_s = strtok(chunk, "\t\r\n ");
if (token_s == NULL)
{
line_num++;
continue;
}
/* printf("%s\n", token_s); */
exec_opcode(token_s, &head, line_num);
line_num++;
}
free_head(head);
fclose(fp);
return (0);
}