forked from GraceBuri/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontyfunc2.c
77 lines (72 loc) · 1.74 KB
/
montyfunc2.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
#include "monty.h"
/**
* pop - removes top value of the stack
* @stack: pointer to the head of the list
* @line_number: current line number
*
*/
void pop(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
(void) line_number;
if (*stack == NULL)
{
fprintf(stderr, "L%u: can't pop an empty stack\n", line_number);
exit(EXIT_FAILURE);
}
temp = *stack;
*stack = (*stack)->next;
if (*stack != NULL)
(*stack)->prev = NULL;
free(temp);
}
/**
* swap - swaps the top two value of the stack
* @stack: a pointer to the top node of stack
* @line_number: the current line number of file
*
*/
void swap(stack_t **stack, unsigned int line_number)
{
int temp;
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't swap, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
temp = (*stack)->n;
(*stack)->n = (*stack)->next->n;
(*stack)->next->n = temp;
}
/**
* add - a function that adds the previuos two stack members
* @head: pointer to the head of the stack
* @line_number: line in which bytecode is being executed
*/
void add(stack_t **head, unsigned int line_number)
{
if (*head == NULL || (*head)->next == NULL)
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
(void) line_number;
(*head)->next->n += (*head)->n;
pop(head, 0);
}
/**
* sub - a function that subtracts the previous two stack members
* @head: pointer to the head of the stack
* @line_number: line in which bytecode is being executed
*/
void sub(stack_t **head, unsigned int line_number)
{
if (*head == NULL || (*head)->next == NULL)
{
fprintf(stderr, "L%d: can't sub, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
(void) line_number;
(*head)->next->n -= (*head)->n;
pop(head, 0);
}