-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcodeOps.c
103 lines (92 loc) · 2.03 KB
/
opcodeOps.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
#include "monty.h"
void push(unsigned int line_number, data_t *data);
void pall(unsigned int line_number, data_t *data);
void pint(unsigned int line_number, data_t *data);
void pchar(unsigned int line_number, data_t *data);
void pstr(unsigned int line_number, data_t *data);
/**
* push - add an element to the stack
* @line_number: line number
* @data: data
*/
void push(unsigned int line_number, data_t *data)
{
if (data->cmdSize < 2 || !_isint(data->cmd[1]))
{
fprintf(stderr, "L%u: usage: push integer\n", line_number);
freeMemory(data, 1);
exit(EXIT_FAILURE);
}
add_top(atoi(data->cmd[1]), data, data->stackMode);
}
/**
* pall - print all elements in the stack
* @line_number: line number
* @data: data
*/
void pall(unsigned int line_number, data_t *data)
{
stack_t *h;
(void) line_number;
h = data->head_s;
while (h)
{
printf("%d\n", h->n);
h = h->next;
}
}
/**
* pint - print the value of the top element of the stack
* @line_number: line number
* @data: data
*/
void pint(unsigned int line_number, data_t *data)
{
stack_t *h;
if (!data->head_s)
{
fprintf(stderr, "L%u: can't pint, stack empty\n", line_number);
freeMemory(data, 1);
exit(EXIT_FAILURE);
}
h = data->head_s;
printf("%d\n", h->n);
}
/**
* pchar - print the char at the top of the stack
* @line_number: line number
* @data: data
*/
void pchar(unsigned int line_number, data_t *data)
{
if (!data->stackSize)
{
fprintf(stderr, "L%u: can't pchar, stack empty\n", line_number);
freeMemory(data, 1);
exit(EXIT_FAILURE);
}
if (data->head_s->n < 0 || data->head_s->n > 127)
{
fprintf(stderr, "L%u: can't pchar, value out of range\n", line_number);
freeMemory(data, 1);
exit(EXIT_FAILURE);
}
printf("%c\n", data->head_s->n);
}
/**
* pstr - print the string of the stack
* @line_number: line number
* @data: data
*/
void pstr(unsigned int line_number, data_t *data)
{
stack_t *h;
(void) line_number;
for (h = data->head_s; h; h = h->next)
{
if (h->n <= 0 || h->n > 127)
break;
printf("%c", h->n);
}
printf("\n");
}