-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_monty.c
179 lines (157 loc) · 3.46 KB
/
run_monty.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* File: run_monty.c
* Auth: Faith Nyaberi
* Peter Nyaberi
*/
#include "monty.h"
#include <string.h>
void free_tokens(void);
unsigned int token_arr_len(void);
int is_empty_line(char *line, char *delims);
void (*get_op_func(char *opcode))(stack_t**, unsigned int);
int run_monty(FILE *script_fd);
/**
* free_tokens - Frees the global op_toks array of strings.
*/
void free_tokens(void)
{
size_t i = 0;
if (op_toks == NULL)
return;
for (i = 0; op_toks[i]; i++)
free(op_toks[i]);
free(op_toks);
}
/**
* token_arr_len - Gets the length of current op_toks.
*
* Return: Length of current op_toks (as int).
*/
unsigned int token_arr_len(void)
{
unsigned int toks_len = 0;
while (op_toks[toks_len])
toks_len++;
return (toks_len);
}
/**
* is_empty_line - Checks if a line read from getline only contains delimiters.
* @line: A pointer to the line.
* @delims: A string of delimiter characters.
*
* Return: If the line only contains delimiters - 1.
* Otherwise - 0.
*/
int is_empty_line(char *line, char *delims)
{
int i, j;
for (i = 0; line[i]; i++)
{
for (j = 0; delims[j]; j++)
{
if (line[i] == delims[j])
break;
}
if (delims[j] == '\0')
return (0);
}
return (1);
}
/**
* get_op_func - Matches an opcode with its corresponding function.
* @opcode: The opcode to match.
*
* Return: A pointer to the corresponding function.
*/
void (*get_op_func(char *opcode))(stack_t**, unsigned int)
{
instruction_t op_funcs[] = {
{"push", monty_push},
{"pall", monty_pall},
{"pint", monty_pint},
{"pop", monty_pop},
{"swap", monty_swap},
{"add", monty_add},
{"nop", monty_nop},
{"sub", monty_sub},
{"div", monty_div},
{"mul", monty_mul},
{"mod", monty_mod},
{"pchar", monty_pchar},
{"pstr", monty_pstr},
{"rotl", monty_rotl},
{"rotr", monty_rotr},
{"stack", monty_stack},
{"queue", monty_queue},
{NULL, NULL}
};
int i;
for (i = 0; op_funcs[i].opcode; i++)
{
if (strcmp(opcode, op_funcs[i].opcode) == 0)
return (op_funcs[i].f);
}
return (NULL);
}
/**
* run_monty - Primary function to execute a Monty bytecodes script.
* @script_fd: File descriptor for an open Monty bytecodes script.
*
* Return: EXIT_SUCCESS on success, respective error code on failure.
*/
int run_monty(FILE *script_fd)
{
stack_t *stack = NULL;
char *line = NULL;
size_t len = 0, exit_status = EXIT_SUCCESS;
unsigned int line_number = 0, prev_tok_len = 0;
ssize_t getline(char **line, size_t *n, FILE *stream);
void (*op_func)(stack_t**, unsigned int);
if (init_stack(&stack) == EXIT_FAILURE)
return (EXIT_FAILURE);
while (getline(&line, &len, script_fd) != -1)
{
line_number++;
op_toks = strtow(line, DELIMS);
if (op_toks == NULL)
{
if (is_empty_line(line, DELIMS))
continue;
free_stack(&stack);
return (malloc_error());
}
else if (op_toks[0][0] == '#') /* comment line */
{
free_tokens();
continue;
}
op_func = get_op_func(op_toks[0]);
if (op_func == NULL)
{
free_stack(&stack);
exit_status = unknown_op_error(op_toks[0], line_number);
free_tokens();
break;
}
prev_tok_len = token_arr_len();
op_func(&stack, line_number);
if (token_arr_len() != prev_tok_len)
{
if (op_toks && op_toks[prev_tok_len])
exit_status = atoi(op_toks[prev_tok_len]);
else
exit_status = EXIT_FAILURE;
free_tokens();
break;
}
free_tokens();
}
free_stack(&stack);
if (line && *line == 0)
{
free(line);
return (malloc_error());
}
free(line);
return (exit_status);
}