-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcodes3.c
70 lines (61 loc) · 1.44 KB
/
opcodes3.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
#include "monty.h"
/**
* _nop - Doesn’t do anything.
* @stack: A pointer to the node at the top of the stack.
* @line_number: The line number in the monty file.
* Return: void
*/
void _nop(stack_t **stack, unsigned int line_number)
{
(void)stack;
(void)line_number;
}
/**
* _pchar - Prints the char at the top of the stack, followed by a new line.
* @stack: A pointer to the node at the top of the stack.
* @line_number: The line number in the monty file.
* Return: void
*/
void _pchar(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL)
{
fprintf(stderr, "L%d: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n < 0 || (*stack)->n > 127)
{
fprintf(stderr, "L%d: can't pchar, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
printf("%c\n", (*stack)->n);
}
/**
* _pstr - Prints the string starting at the top of the stack,
* followed by a new line.
* @stack: A pointer to the node at the top of the stack.
* @line_number: The line number in the monty file.
* Return: void
*/
void _pstr(stack_t **stack, unsigned int line_number)
{
stack_t *temp = *stack;
if (temp == NULL)
{
printf("\n");
return;
}
while (temp != NULL)
{
if (temp->n == 0)
break;
if (temp->n < 0 || temp->n > 127)
{
fprintf(stderr, "L%d: can't pstr, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
printf("%c", temp->n);
temp = temp->next;
}
printf("\n");
}