-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_functions.c
49 lines (47 loc) · 1.23 KB
/
utility_functions.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
#include "monty.h"
/**
* _isdigit - checks if an argument is a digit
* @argument: the argument to be checked
* Return: 0 if it is a digit or 0 if not
*/
int _isdigit(char *argument)
{
int i = 0;
if (argument[0] == '-')
i = 1;
while (argument[i])
{
if (argument[i] < 48 || argument[i] > 57)
return (1);
i++;
}
return (0);
}
/**
* call_opcode - checks if an opcode is valid
* @opcode: the opcode to check
* @line_no: the line number
* @monty_stack: the stack to be freed
*/
void call_opcode(char *opcode, unsigned int line_no, stack_t **monty_stack)
{
instruction_t instructions[] = {{"push", push_op}, {"pall", pall_op},
{"div", div_op}, {"pint", pint_op}, {"pop", pop_op}, {"nop", nop_op},
{"swap", swap_op}, {"mul", mul_op}, {"add", add_op}, {"sub", sub_op},
{"mod", mod_op}, {"pchar", pchar_op}, {"pstr", pstr_op}, {"rotl", rotl_op},
{"rotr", rotr_op}, {"queue", queue_op}, {"stack", stack_op}, {NULL, NULL}};
int i = 0;
while (instructions[i].opcode != NULL)
{
if (strcmp(opcode, instructions[i].opcode) == 0)
{
instructions[i].f(monty_stack, line_no);
return;
}
i++;
}
fprintf(stderr, "L%u: unknown instruction %s\n", line_no, opcode);
free_stack(*monty_stack);
fclose(globals.file);
exit(EXIT_FAILURE);
}