-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
133 lines (130 loc) · 2.65 KB
/
parser.c
File metadata and controls
133 lines (130 loc) · 2.65 KB
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
#include "monty.h"
/**
* parse_line - parses a line into tokens. tokens separated by " " and "\n".
* only first two tokens will be returned if successful.
* @line: buffer from getline in readfile() in monty.c.
*
* Return: null-terminated array of tokens, or NULL
*/
char **parse_line(char *line)
{
char *token, **tokens;
unsigned int i;
tokens = malloc(sizeof(char *) * 3);
if (tokens == NULL)
{
printf("Error: malloc failed");
exit(EXIT_FAILURE);
}
token = strtok(line, " '\n'");
if (token == NULL)
{
free(tokens);
return (NULL);
}
i = 0;
while (token != NULL && i < 2)
{
tokens[i] = token;
token = strtok(NULL, " '\n'");
i++;
}
tokens[i] = NULL;
return (tokens);
}
int arg = 0; /* global (extern) variable defined in header */
/**
* get_op_func - selects the correct function to perform from tokens
* @tokens: array of strings
* @ln: line number in monty code file
*
* Return: pointer to the appropriate function, or NULL if not valid
*/
void (*get_op_func(char **tokens, unsigned int ln))(stack_t **, unsigned int)
{
instruction_t ops[] = {
{"push", op_push},
{"pall", op_pall},
{"pop", op_pop},
{"pint", op_pint},
{"swap", op_swap},
{"add", op_add},
{"sub", op_sub},
{"mul", op_mul},
{"nop", op_nop},
{"div", op_div},
{"mod", op_mod},
{"pchar", op_pchar},
{"pstr", op_pstr},
{NULL, NULL}
};
unsigned int i = 0;
if (tokens[0][0] == '#')
{
free(tokens);
return (op_nop);
}
while (ops[i].opcode != NULL)
{
if ((strcmp(ops[i].opcode, tokens[0]) == 0))
{
if ((strcmp(ops[i].opcode, "push") == 0) &&
(tokens[1] == NULL || (!(valid_arg(tokens[1])))))
{
free(tokens);
printf("L%d: usage: push integer\n", ln);
exit(EXIT_FAILURE);
}
else if ((strcmp(ops[i].opcode, "push") == 0))
arg = atoi(tokens[1]);
free(tokens);
return (ops[i].f);
}
i++;
}
printf("L%d: unknown instruction %s\n", ln, tokens[0]);
free(tokens);
exit(EXIT_FAILURE);
}
/**
* valid_arg - determines if a token is a valid argument
* @token: argument token from valid_op.
*
* Return: 1 if valid argument, 0 if not
*/
int valid_arg(char *token)
{
unsigned int i;
/* if token is NULL its an op with no argument, so its valid */
if (token == NULL)
return (1);
i = 0;
while (token[i] != '\0')
{
if (token[0] == '-')
{
if ((!(token[1] >= '0' && token[1] <= '9')) || token[1] == '\0')
return (0);
i = 1;
while (token[i] >= '0' && token[i] <= '9')
{
i++;
if (token[i] == '\0')
return (1);
}
return (0);
}
else
{
i = 0;
while (token[i] >= '0' && token[i] <= '9')
{
i++;
if (token[i] == '\0')
return (1);
}
return (0);
}
}
return (0);
}