Skip to content

Commit

Permalink
Task upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jekwupeter committed Jun 19, 2022
1 parent 9afbc12 commit bef7042
Show file tree
Hide file tree
Showing 18 changed files with 401 additions and 10 deletions.
15 changes: 15 additions & 0 deletions bytecodes/00.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@
push 2$
push 3$
pall$
push 0 Push 0 onto the stack$
push 1 Push 1 onto the stack$
$
push 2$
push 3$
pall $
$
$
$
push 4$
$
push 5 $
push 6 $
$
pall This is the end of our program. Monty is awesome!$
6 changes: 6 additions & 0 deletions bytecodes/06.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
push 1
pint
push 2
pint
push 3
pint
10 changes: 10 additions & 0 deletions bytecodes/07.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
push 1
push 2
push 3
pall
pop
pall
pop
pall
pop
pall
6 changes: 6 additions & 0 deletions bytecodes/09.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
push 1
push 2
push 3
pall
swap
pall
6 changes: 6 additions & 0 deletions bytecodes/12.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
push 1
push 2
push 3
pall
add
pall
14 changes: 14 additions & 0 deletions bytecodes/19.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
push 1
push 2
push 3
push 4
push 0
push 110
push 0
push 108
push 111
push 111
push 104
push 99
push 83
pstr
4 changes: 4 additions & 0 deletions bytecodes/28.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
push 72
pchar
push 100
pchar
14 changes: 14 additions & 0 deletions bytecodes/31.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
push 1
push 2
push 3
push 4
push 0
push 110
push 0
push 108
push 111
push 111
push 104
push 99
push 83
pstr
14 changes: 14 additions & 0 deletions bytecodes/35.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
push 1
push 2
push 3
push 4
push 5
push 6
push 7
push 8
push 9
push 0
pall
rotl
# pint
# pall
10 changes: 10 additions & 0 deletions bytecodes/test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

push 11
push 3
add
pall
pop
# pop
# push 1
# mod
# pall
36 changes: 31 additions & 5 deletions dlinked_list_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ stack_t *add_dnodeint(stack_t **stack_ptr, const int n)
return (new_node);
}

int delete_dnodeint(stack_t **stack_ptr)
int delete_dnodeint(stack_t **stack_ptr, unsigned int count)
{
stack_t *tmp;
tmp = (*stack_ptr);
if (*stack_ptr == NULL)
return (-1);
{
printf("L%d: can't pop an empty stack", count);
}
/* Move the head pointer to the next node */
(*stack_ptr) = tmp->next;
free(tmp);
Expand All @@ -43,14 +45,38 @@ int delete_dnodeint(stack_t **stack_ptr)
size_t print_dlistint(const stack_t *stack_ptr)
{
int i = 0;
while(stack_ptr){
printf("%d\n", stack_ptr->n);
stack_ptr = stack_ptr->next;
const stack_t *tmp;
tmp = stack_ptr;
while(tmp){
printf("%d\n", tmp->n);
tmp = tmp->next;
i++;
}
return (i);
}

size_t print_dlisthead(const stack_t *stack_ptr, unsigned int count)
{
if (stack_ptr == NULL)
{
printf("L%d: can't pint, stack empty\n", count);
exit(EXIT_FAILURE);
}
printf("%d\n", stack_ptr->n);
return (1);
}

size_t print_dlistheadch(const stack_t *stack_ptr, unsigned int count)
{
if (stack_ptr == NULL)
{
printf("L%d: can't pint, stack empty\n", count);
exit(EXIT_FAILURE);
}
printf("%c\n", stack_ptr->n);
return (1);
}

void free_stack(stack_t *stack_ptr)
{
stack_t *tmp;
Expand Down
13 changes: 13 additions & 0 deletions func1.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ void opcode(stack_t **stack_ptr, char *line, unsigned int count)
};*/
instruction_t op[] = {
{"pall", pall},
{"pint", pint},
{"pop", pop},
{"swap", swap},
{"add", _add},
{"nop", nop},
{"sub", _sub},
{"div", _div},
{"mod", _mod},
{"mul", _mul},
{"#", nop},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{NULL, NULL}
};
int i;
Expand Down
47 changes: 43 additions & 4 deletions func2.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,50 @@ void push(stack_t **stack_ptr, char *arg_data, unsigned int count)
}

/**
* pall - prints all the values on the stack, starting from the top of the stack.
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void pall(stack_t **stack_ptr, unsigned int count __attribute__((unused)))
void pop(stack_t **stack_ptr, unsigned int count)
{
print_dlistint(*stack_ptr);
}
if (stack_ptr != NULL)
delete_dnodeint(stack_ptr, count);
else
{
printf("L%d: can't pop an empty stack", count);
exit (EXIT_FAILURE);
}
}

/**
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void swap(stack_t **stack_ptr, unsigned int count)
{
int buff;

if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
buff = (*stack_ptr)->n;
(*stack_ptr)->n = (*stack_ptr)->next->n;
(*stack_ptr)->next->n = buff;
}

/**
* pall - prints top of stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void nop(stack_t **stack_ptr, unsigned int count)
{
(void)stack_ptr;
(void)count;
}



107 changes: 107 additions & 0 deletions math_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "monty.h"

/**
* _mul - multiplies first and second element in stack.
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void _mul(stack_t **stack_ptr, unsigned int count)
{
int num1, num2, result;

if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
num1 = (*stack_ptr)->n;
num2 = (*stack_ptr)->next->n;
result = num2 * num1;
(*stack_ptr)->next->n = result;
pop(stack_ptr, count);
}

/**
* _mod - finds modulus of first and second element
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void _mod(stack_t **stack_ptr, unsigned int count)
{
int num1, num2, result;
stack_t *tmp;
tmp = *stack_ptr;

if (tmp->next == NULL)
{
printf("L%d: can't mod, stack too short", count);
exit(EXIT_FAILURE);
}
num1 = tmp->n;
num2 = tmp->next->n;
result = num2 % num1;
(*stack_ptr)->next->n = result;
pop(stack_ptr, count);
}

/**
* _add - adds first and second element in stack
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void _add(stack_t **stack_ptr, unsigned int count)
{
int num1, num2, result;

if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
num1 = (*stack_ptr)->n;
num2 = (*stack_ptr)->next->n;
result = num1 + num2;
(*stack_ptr)->next->n = result;
pop(stack_ptr, count);
}
/**
* _div - divides second and first element in stack
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/
void _div(stack_t **stack_ptr, unsigned int count)
{
int num1, num2, result;

if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
num1 = (*stack_ptr)->n;
num2 = (*stack_ptr)->next->n;
result = num2 / num1;
(*stack_ptr)->next->n = result;
pop(stack_ptr, count);
}
/**
* _sub -finds the difference first and second element in stack
* @stack_ptr: pointer to dlinkedlist
* @count: line number
*/

void _sub(stack_t **stack_ptr, unsigned int count)
{
int num1, num2, result;

if ((*stack_ptr) == NULL || (*stack_ptr)->next == NULL)
{
printf("L%d: can't swap, stack too short", count);
exit(EXIT_FAILURE);
}
num1 = (*stack_ptr)->n;
num2 = (*stack_ptr)->next->n;
result = num2 - num1;
(*stack_ptr)->next->n = result;
pop(stack_ptr, count);
}
Binary file modified monty.exe
Binary file not shown.
16 changes: 15 additions & 1 deletion monty.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ typedef struct instruction_s
size_t getline(char **lineptr, size_t *n, FILE *stream);
void pall(stack_t **stack_ptr, unsigned int count __attribute__((unused)));
size_t print_dlistint(const stack_t *stack_ptr);
int delete_dnodeint(stack_t **stack_ptr);
int delete_dnodeint(stack_t **stack_ptr, unsigned int count);
stack_t *add_dnodeint(stack_t **stack_ptr, const int n);
void push(stack_t **stack_ptr, char *arg_data, unsigned int count);
void opcode(stack_t **stack_ptr, char *line, unsigned int count);
void free_stack(stack_t *stack_ptr);
size_t print_dlisthead(const stack_t *stack_ptr, unsigned int count);
void pint(stack_t **stack_ptr, unsigned int count __attribute__((unused)));
void pop(stack_t **stack_ptr, unsigned int count);
void swap(stack_t **stack_ptr, unsigned int count);
void _add(stack_t **stack_ptr, unsigned int count);
void _sub(stack_t **stack_ptr, unsigned int count);
void _div(stack_t **stack_ptr, unsigned int count);
void _mod(stack_t **stack_ptr, unsigned int count);
void _mul(stack_t **stack_ptr, unsigned int count);
void nop(stack_t **stack_ptr, unsigned int count);
void rotl(stack_t **stack_ptr, unsigned int count);
void pchar(stack_t **stack_ptr, unsigned int count);
void pstr(stack_t **stack_ptr, unsigned int count);
size_t print_dlistheadch(const stack_t *stack_ptr, unsigned int count);
#endif
Loading

0 comments on commit bef7042

Please sign in to comment.