-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
92 lines (84 loc) · 1.51 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
#include "monty.h"
/**
* main - Entry point
* @argc: number of arguments passed
* @argv: array of arguments
* Return: 0 on success, -1 on failure
*/
int main(int argc, char *argv[])
{
char *buf;
int fd;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty filname\n");
exit(EXIT_FAILURE);
}
buf = malloc(sizeof(char) * 10000);
if (buf == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if (read(fd, buf, 10000) == -1)
{
fprintf(stderr, "Error: Can't read to buffer\n");
close(fd);
free(buf);
exit(EXIT_FAILURE);
}
run_code(buf);
free(buf);
close(fd);
return (0);
}
/**
* run_code - runs the byte code
* @buf: buffer containing bytecode
*/
void run_code(char *buf)
{
char *token, *delim;
unsigned int line, is_push;
stack_t *stack = NULL;
is_push = 0;
line = 1;
delim = "\n\t\v\r\a ;:";
token = strtok(buf, delim);
while (token)
{
if (is_push)
{
push(&stack, line, token);
is_push = 0;
}
else if (strcmp(token, "push") == 0)
{
is_push = 1;
token = strtok(NULL, delim);
continue;
}
else
{
if (get_op(token) != NULL)
get_op(token)(&stack, line);
else if (token[0] == '#')
nop(&stack, line);
else
{
free_stack(stack);
fprintf(stderr, "L%d: unknown instruction %s\n", line, token);
exit(EXIT_FAILURE);
}
}
line++;
token = strtok(NULL, delim);
}
free_stack(stack);
}