-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-opcodes.c
131 lines (122 loc) · 3.68 KB
/
2-opcodes.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
#include "monty.h"
/**
* add_opcode - adds top two elements of the stack
* @program_ptr: pointer to monty_program_t struct
*
* Description: this function adds the top two elements of the stack.
* If the stack does not have at least two elements, it prints an error
* message and exits the program. It performs the addition and then removes
* the top element from the stack.
*/
void add_opcode(monty_program_t *program_ptr)
{
stack_t *top, *second;
if (program_ptr->stack == NULL || program_ptr->stack->next == NULL)
{
fprintf(stderr, "L%d: can't add, stack too short\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
top = program_ptr->stack;
second = top->next;
second->n += top->n;
program_ptr->stack = second;
second->prev = NULL;
free(top);
}
/**
* nop_opcode - Does nothing
* @program_ptr: Pointer to the monty_program_t struct
*
* Description: This function is a no-operation function. It is designed
* to do nothing and is typically used as a placeholder or to avoid an
* unused parameter warning.
*/
void nop_opcode(monty_program_t *program_ptr)
{
(void)program_ptr;
}
/**
* sub_opcode - Subtracts top element from the
* second top element of the stack
* @program_ptr: Pointer to the monty_program_t struct
*
* Description: this function performs a subtraction between the top two
* elements of the stack. If the stack does not have at least two elements,
* it prints an error message and exits the program. After the subtraction,
* the top element is removed from the stack.
*/
void sub_opcode(monty_program_t *program_ptr)
{
stack_t *top, *second;
if (program_ptr->stack == NULL || program_ptr->stack->next == NULL)
{
fprintf(stderr, "L%d: can't sub, stack too short\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
top = program_ptr->stack;
second = top->next;
second->n -= top->n;
program_ptr->stack = second;
second->prev = NULL;
free(top);
}
/**
* div_opcode - Divides second top element by the top element of the stack
* @program_ptr: Pointer to the monty_program_t struct
*
* Description: This function performs a division of the second top element by
* the top element of the stack. It checks if the stack has at least two
* elements and if the top element (the divisor) is not zero. If any of these
* conditions are not met, it prints an error message and exits the program.
* After the division, the top element is removed from the stack.
*/
void div_opcode(monty_program_t *program_ptr)
{
stack_t *top, *second;
if (program_ptr->stack == NULL || program_ptr->stack->next == NULL)
{
fprintf(stderr, "L%d: can't div, stack too short\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
top = program_ptr->stack;
second = top->next;
if (top->n == 0)
{
fprintf(stderr, "L%d: division by zero\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
second->n /= top->n;
program_ptr->stack = second;
second->prev = NULL;
free(top);
}
/**
* mul_opcode - Multiplies second top element with
* the top element of the stack
* @program_ptr: Pointer to the monty_program_t struct
*
* Description: this function multiplies the second top element of the stack
* with the top element. If the stack does not have at least two elements, it
* prints an error message and exits the program. After performing the
* multiplication, the top element is removed from the stack.
*/
void mul_opcode(monty_program_t *program_ptr)
{
stack_t *top, *second;
if (program_ptr->stack == NULL || program_ptr->stack->next == NULL)
{
fprintf(stderr, "L%d: can't mul, stack too short\n",
program_ptr->line_num);
exit(EXIT_FAILURE);
}
top = program_ptr->stack;
second = top->next;
second->n *= top->n;
program_ptr->stack = second;
second->prev = NULL;
free(top);
}