diff --git a/bytecodes/00.m b/bytecodes/00.m index 5fa87a9..72c05ef 100644 --- a/bytecodes/00.m +++ b/bytecodes/00.m @@ -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!$ \ No newline at end of file diff --git a/bytecodes/06.m b/bytecodes/06.m new file mode 100644 index 0000000..262ceb1 --- /dev/null +++ b/bytecodes/06.m @@ -0,0 +1,6 @@ +push 1 +pint +push 2 +pint +push 3 +pint diff --git a/bytecodes/07.m b/bytecodes/07.m new file mode 100644 index 0000000..30497b1 --- /dev/null +++ b/bytecodes/07.m @@ -0,0 +1,10 @@ +push 1 +push 2 +push 3 +pall +pop +pall +pop +pall +pop +pall \ No newline at end of file diff --git a/bytecodes/09.m b/bytecodes/09.m new file mode 100644 index 0000000..c860590 --- /dev/null +++ b/bytecodes/09.m @@ -0,0 +1,6 @@ +push 1 +push 2 +push 3 +pall +swap +pall \ No newline at end of file diff --git a/bytecodes/12.m b/bytecodes/12.m new file mode 100644 index 0000000..f85b5f8 --- /dev/null +++ b/bytecodes/12.m @@ -0,0 +1,6 @@ +push 1 +push 2 +push 3 +pall +add +pall \ No newline at end of file diff --git a/bytecodes/19.m b/bytecodes/19.m new file mode 100644 index 0000000..790b827 --- /dev/null +++ b/bytecodes/19.m @@ -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 \ No newline at end of file diff --git a/bytecodes/28.m b/bytecodes/28.m new file mode 100644 index 0000000..b5dc742 --- /dev/null +++ b/bytecodes/28.m @@ -0,0 +1,4 @@ +push 72 +pchar +push 100 +pchar \ No newline at end of file diff --git a/bytecodes/31.m b/bytecodes/31.m new file mode 100644 index 0000000..790b827 --- /dev/null +++ b/bytecodes/31.m @@ -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 \ No newline at end of file diff --git a/bytecodes/35.m b/bytecodes/35.m new file mode 100644 index 0000000..9c03770 --- /dev/null +++ b/bytecodes/35.m @@ -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 \ No newline at end of file diff --git a/bytecodes/test.m b/bytecodes/test.m new file mode 100644 index 0000000..3809d94 --- /dev/null +++ b/bytecodes/test.m @@ -0,0 +1,10 @@ + +push 11 +push 3 +add +pall +pop +# pop +# push 1 +# mod +# pall diff --git a/dlinked_list_functions.c b/dlinked_list_functions.c index d81f431..4a542a0 100644 --- a/dlinked_list_functions.c +++ b/dlinked_list_functions.c @@ -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); @@ -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; diff --git a/func1.c b/func1.c index 938b4ae..ba71d45 100644 --- a/func1.c +++ b/func1.c @@ -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; diff --git a/func2.c b/func2.c index 2feecc9..712408c 100644 --- a/func2.c +++ b/func2.c @@ -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); -} \ No newline at end of file + 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; +} + + + diff --git a/math_functions.c b/math_functions.c new file mode 100644 index 0000000..2c4bce3 --- /dev/null +++ b/math_functions.c @@ -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); +} \ No newline at end of file diff --git a/monty.exe b/monty.exe index cc4a97b..e626a7f 100644 Binary files a/monty.exe and b/monty.exe differ diff --git a/monty.h b/monty.h index 17159d7..41de8fd 100644 --- a/monty.h +++ b/monty.h @@ -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 diff --git a/movement_functions.c b/movement_functions.c new file mode 100644 index 0000000..fc78c36 --- /dev/null +++ b/movement_functions.c @@ -0,0 +1,24 @@ +#include "monty.h" + +void rotl(stack_t **stack_ptr, unsigned int count) +{ + stack_t *tmp, *node; + node = *stack_ptr; + tmp = (*stack_ptr); + (void)(count); + if (stack_ptr != NULL && (*stack_ptr)->next != NULL) + { + while(tmp) + tmp = tmp->next; + printf("%d\n", tmp->n); + pop(stack_ptr, count); + printf("TEST\n"); + printf("%d\n", tmp->n); + node->next = NULL; + tmp->next = node; + node->prev = tmp; + printf("%d\n", tmp->n); + printf("TEST-2\n"); + } + return; +} \ No newline at end of file diff --git a/print_functions.c b/print_functions.c new file mode 100644 index 0000000..3ba64a0 --- /dev/null +++ b/print_functions.c @@ -0,0 +1,69 @@ +#include "monty.h" + +/** + * pstr - prints string starting from top of stack. + * @stack_ptr: pointer to dlinkedlist + * @count: line number + */ +void pstr(stack_t **stack_ptr, unsigned int count) +{ + stack_t *tmp; + tmp = *stack_ptr; + (void)(count); + + if (stack_ptr != NULL) + { + while (tmp != NULL && tmp->n > 0 && tmp->n < 127) + { + printf("%c", tmp->n); + tmp = tmp->next; + } + } + putchar('\n'); +} + +/** + * pchar - prints char at top of stack. + * @stack_ptr: pointer to dlinkedlist + * @count: line number + */ +void pchar(stack_t **stack_ptr, unsigned int count) +{ + int num; + num = (*stack_ptr)->n; + + if (stack_ptr == NULL) + { + printf("L%d: can't pchar, stack empty", count); + exit(EXIT_FAILURE); + } + else if (num >= 0 && num <= 127) + { + print_dlistheadch(*stack_ptr, count); + return; + } + printf("L%d: can't pchar, value out of range", count); + exit(EXIT_FAILURE); +} + +/** + * pall - prints all the values on the stack, starting from the top of the stack. + * @stack_ptr: pointer to dlinkedlist + * @count: line number + */ +void pall(stack_t **stack_ptr, unsigned int count __attribute__((unused))) +{ + if (stack_ptr != NULL) + print_dlistint(*stack_ptr); + return; +} + +/** + * pall - prints top of stack. + * @stack_ptr: pointer to dlinkedlist + * @count: line number + */ +void pint(stack_t **stack_ptr, unsigned int count) +{ + print_dlisthead(*stack_ptr, count); +} \ No newline at end of file