Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
SHEFOO10 committed Oct 22, 2023
1 parent 721126e commit 4bafef2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
6 changes: 5 additions & 1 deletion bytecodes/00.m
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
nop
push 1
push 2
push 3
add
pall
3 changes: 3 additions & 0 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void error(int error_code, ...)
case 8:
fprintf(stderr, "L%d: can't swap, stack too short\n", va_arg(list, int));
break;
case 9:
fprintf(stderr, "L%d: can't add, stack too short\n", va_arg(list, int));
break;
default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion helper_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void (*select_operation_func(char *op_code, int ln))(stack_t **, unsigned int)
{"pop", pop},
{"swap", swap},
{"nop", nop},
{"add", add},
{NULL, NULL},
};

Expand Down Expand Up @@ -116,7 +117,7 @@ void call_func(
if (
((head == NULL && strcmp(op_code, "pall") != 0)
&& strcmp(op_code, "nop") != 0)
|| (strcmp(op_code, "swap") == 0 && head->next == NULL))
|| ((strcmp(op_code, "swap") == 0 || strcmp(op_code, "add") == 0) && head->next == NULL))
*mode = 3;
func(&head, line_number);
}
Expand Down
Binary file modified monty
Binary file not shown.
1 change: 1 addition & 0 deletions monty.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void print_int(stack_t **stack, unsigned int line_number);
void pop(stack_t **stack, unsigned int line_number);
void swap(stack_t **stack, unsigned int line_number);
void nop(stack_t **stack, unsigned int line_number);
void add(stack_t **stack, unsigned int line_number);

/* helper functions */
void (*select_operation_func(char *op_code, int line_number))(stack_t **, unsigned int);
Expand Down
40 changes: 21 additions & 19 deletions stack_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,7 @@ void print_int(stack_t **stack, unsigned int line_number)
printf("%d\n", (*stack)->n);
}

/**
* pop - pop the item from the top of stack.
*
* @stack: stack pointer.
* @line_number: line number that called this funciton.
*/

void pop(stack_t **stack, unsigned int line_number)
{
stack_t *tmp;

if (*stack == NULL)
error(7, line_number);
else
{
tmp = *stack;
*stack = tmp->next;
free(tmp);
}
}


/**
Expand Down Expand Up @@ -96,3 +77,24 @@ void nop(stack_t **stack, unsigned int line_number)
(void)stack;
(void)line_number;
}

/**
* add - add n of top stack node to n of next node to it.
*
* @stack: the stack.
* @line_number: line number.
*/
void add(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;

if (*stack == NULL && (*stack)->next == NULL)
{
error(9, line_number);
return;
}
tmp->next->n += tmp->n;

*stack = tmp->next;
free(tmp);
}
21 changes: 21 additions & 0 deletions stack_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ void add_to_stack(stack_t **new_node, __attribute__((unused))unsigned int ln)
head->next = tmp;
tmp->prev = head;
}

/**
* pop - pop the item from the top of stack.
*
* @stack: stack pointer.
* @line_number: line number that called this funciton.
*/

void pop(stack_t **stack, unsigned int line_number)
{
stack_t *tmp;

if (*stack == NULL)
error(7, line_number);
else
{
tmp = *stack;
*stack = tmp->next;
free(tmp);
}
}

0 comments on commit 4bafef2

Please sign in to comment.