-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_monty.c
94 lines (85 loc) · 2 KB
/
parse_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
#include "monty.h"
/**
* parse_monty - the entry point of the Monty Bytecode Interpreter
* @fp: a pointer to the file stream of a bytecode file
*
* Return: EXIT_SUCCESS on success or EXIT_FAILURE on error.
*/
int parse_monty(FILE *fp)
{
char *line = NULL;
size_t len = 0;
ssize_t read;
int exit_code = EXIT_SUCCESS;
unsigned int line_number = 0;
stack_t *stack = NULL;
void (*op_func)(stack_t**, unsigned int);
if (initialize_stack(&stack) != EXIT_SUCCESS)
return (EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) /* read line from file*/
{
line_number++;
op_tokens = op_seperater(line); /* separate line into tokens */
if (op_tokens == NULL || (line_is_space(line, DELIMS) == 1))
continue;
else if (op_tokens[0][0] == '#') /* if line is comments */
{
free_tokens();
continue;
}
op_func = get_op_func(op_tokens[0]);
if (op_func == NULL)
{
free_stack(&stack);
exit_code = unknown_instruct(line_number, op_tokens[0]);
free_tokens();
break;
}
op_func(&stack, line_number);
if (strcmp(op_tokens[0], "FAIL") == 0)
{
exit_code = EXIT_FAILURE;
break; /* set func to set exit_code from op_funcs */
}
free_tokens();
}
free_stack(&stack), free(line);
return (exit_code);
}
/**
* free_tokens - frees the tokens in the lines read from bytecode file.
*
* Return: no return value.
*/
void free_tokens(void)
{
size_t i;
if (op_tokens == NULL)
return;
for (i = 0; op_tokens[i]; i++)
free(op_tokens[i]);
free(op_tokens);
}
/**
* line_is_space - checks if the character is a whitespace char or not
* @str: a pointer to the line read from filestream
* @delims: a pointer to a string of delimeters
*
* Return: 1 if the string only contains delimiters or 0 if not.
*/
int line_is_space(char *str, char *delims)
{
int alpha, bravo;
alpha = bravo = 0;
for (alpha = 0; str[alpha]; alpha++)
{
for (bravo = 0; delims[bravo]; bravo++)
{
if (str[alpha] == delims[bravo])
break;
}
if (delims[bravo] == '\0')
return (0);
}
return (1);
}