-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
147 lines (130 loc) · 2.62 KB
/
parser.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
#include "monty.h"
/**
*line_parse - parse through individual lines from stream
*@buf: line from file
*@line: line number
*Return: Void
*/
int line_parse(char *buf, int line)
{
char *opcode = NULL, *opvalue = NULL, *delim = " \n";
int rt_val = 0;
if (buf == NULL)
{
fprintf(stderr, "Error: malloc failed");
exit(EXIT_FAILURE);
}
opcode = strtok(buf, delim);
if (opcode == NULL)
return (rt_val);
opvalue = strtok(NULL, delim);
if (strcmp(opcode, "stack") == 0)
return (0);
if (strcmp(opcode, "queue") == 0)
return (1);
opcode_mapping(opcode, opvalue, line, rt_val);
return (rt_val);
}
/**
*opcode_mapping - links an 'opcode' to corresponding function
*@opcode: operation code
*@opvalue: operation argument
*@line: line umber
*@rt_val: ...
*Return: Void
*/
void opcode_mapping(char *opcode, char *opvalue, unsigned int line, int rt_val)
{
int i = 0, flag;
instruction_t opsmap[] = {
{"push", push},
{"pall", pall},
{"pint", show_top},
{"add", add},
{"swap", swap},
{"pop", pop},
{"nop", nop},
{"sub", sub},
{"div", division},
{"mul", mul},
{"mod", mod},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{"rotr", rotr},
{NULL, NULL}
};
if (opcode[0] == '#')
return;
for (flag = 1, i = 0; opsmap[i].opcode != NULL; i++)
{
if (strcmp(opcode, opsmap[i].opcode) == 0)
{
execute(opsmap[i].f, opcode, opvalue, line, rt_val);
flag = 0;
/*return;*/
}
}
if (flag == 1)
{
fprintf(stderr, "L%u: unknown instruction %s\n", line, opcode);
exit(EXIT_FAILURE);
}
}
/**
*execute - executes an operation based on an opcode
*@index: function to be executed
*@opc: operation code
*@opv: operation argument
*@line: line number in file
*@rt_val: ....
*
*/
void execute(op_func index, char *opc, char *opv, unsigned int line, int rt_val)
{
stack_t *node;
int i, flag;
flag = 1;
if (strcmp(opc, "push") == 0)
{
if (opv != NULL && opv[0] == '-')
{
opv = opv + 1;
flag = -1;
}
if (opv == NULL)
{
fprintf(stderr, "L%u: usage: push integer\n", line);
exit(EXIT_FAILURE);
}
for (i = 0; opv[i] != '\0'; i++)
{
if (isdigit(opv[i]) == 0)
{
fprintf(stderr, "L%d: usage: push integer\n", line);
exit(EXIT_FAILURE);
}
}
node = newnode(atoi(opv) * flag);
if (rt_val == 0)
index(&node, line);
}
else
index(&head, line);
}
/**
*newnode - creates a new doubly linked list node
*@n: data to be inserted into node
*Return: pointer to new node
*/
stack_t *newnode(int n)
{
stack_t *node;
node = malloc(sizeof(stack_t));
if (node == NULL)
fprintf(stderr, "Error: Malloc failed\n");
node->next = NULL;
node->prev = NULL;
node->n = n;
return (node);
}