-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4d5913
commit 961d63c
Showing
9 changed files
with
279 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"files.associations": { | ||
"monty.h": "c" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
push 1$ | ||
push 2$ | ||
push 3$ | ||
pall$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,64 @@ | ||
#include "monty.h" | ||
/** | ||
* add_dnodeint - adds a new node to beggining of dlinked list | ||
* @stack_ptr: pointer to top of dlinked list | ||
* @n: input data for new node | ||
* | ||
* Return: adress to new dnode added | ||
* | ||
* */ | ||
|
||
stack_t *add_dnodeint(stack_t **stack_ptr, const int n) | ||
{ | ||
stack_t *new_node; | ||
stack_t *new_node; | ||
|
||
/* allocate memory to node */ | ||
new_node = (stack_t *)malloc(sizeof(stack_t)); | ||
if (new_node == NULL) | ||
return (NULL); | ||
/* allocate memory to node */ | ||
new_node = (stack_t *)malloc(sizeof(stack_t)); | ||
if (new_node == NULL) | ||
return (NULL); | ||
|
||
new_node->n = n;/* put in data*/ | ||
/* make next of new node as head & prev as NULL */ | ||
new_node->next = n; | ||
new_node->prev = NULL; | ||
new_node->n = n;/* put in data*/ | ||
new_node->prev = NULL; | ||
/* Check if the list is empty */ | ||
if (*stack_ptr == NULL) | ||
{ | ||
new_node->next = NULL; | ||
(*stack_ptr) = new_node; | ||
return (new_node); | ||
} | ||
/* change the prev of stack_ptr node to new node */ | ||
(*stack_ptr)->prev = new_node; | ||
/* make next of new node as stack_ptr & prev as NULL */ | ||
new_node->next = (*stack_ptr); | ||
new_node->prev = NULL; | ||
(*stack_ptr) = new_node; | ||
|
||
/* change the prev of head node to new node */ | ||
if ((*stack_ptr) != NULL) | ||
(*stack_ptr)->prev = new_node | ||
return (new_node); | ||
} | ||
|
||
return (newnode); | ||
int delete_dnodeint(stack_t **stack_ptr) | ||
{ | ||
stack_t *tmp; | ||
tmp = (*stack_ptr); | ||
if (*stack_ptr == NULL) | ||
return (-1); | ||
/* Move the head pointer to the next node */ | ||
(*stack_ptr) = tmp->next; | ||
free(tmp); | ||
return (1); | ||
} | ||
|
||
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; | ||
i++; | ||
} | ||
return (i); | ||
} | ||
|
||
void free_stack(stack_t *stack_ptr) | ||
{ | ||
stack_t *tmp; | ||
|
||
while (stack_ptr != NULL) | ||
{ | ||
tmp = stack_ptr; | ||
stack_ptr = stack_ptr->next; | ||
free(tmp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "monty.h" | ||
|
||
/** | ||
* opcode - finds the functions matching the opcode | ||
* @stack_ptr: pointer to dlinkedlist | ||
* @str: string to compare | ||
* @count: line counts | ||
* @arg_data: data following the function | ||
*/ | ||
void opcode(stack_t **stack_ptr, char *line, unsigned int count) | ||
{ | ||
/* | ||
instruction_t op[] = { | ||
{"pall", pall}, | ||
{"swap", _add}, | ||
{"pop", pop}, | ||
{"pint", pint}, | ||
{"mod", _mod}, | ||
{"mul", _mul}, | ||
{"div", _div}, | ||
{"sub", _sub}, | ||
{"nop", nop}, | ||
{"add", _add}, | ||
{NULL, NULL} | ||
};*/ | ||
instruction_t op[] = { | ||
{"pall", pall}, | ||
{NULL, NULL} | ||
}; | ||
int i; | ||
char *str = NULL; | ||
char *arg_data; | ||
char delimit[] = " \t\r\n\f$"; | ||
|
||
str = strtok(line, delimit); | ||
arg_data = strtok(NULL, delimit); | ||
if (str == NULL) | ||
return; | ||
if (strcmp(str, "push") == 0) | ||
{ | ||
push(stack_ptr, arg_data, count); | ||
return; | ||
} | ||
for (i = 0; op[i].opcode; i++) | ||
{ | ||
if (strcmp(op[i].opcode, str) == 0) | ||
{ | ||
op[i].f(stack_ptr, count); | ||
return; | ||
} | ||
} | ||
fprintf(stderr, "L%d: unknown instruction %s\n", count, str); | ||
exit(EXIT_FAILURE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "monty.h" | ||
|
||
/** | ||
* push - add an element to the stack/queue | ||
* @stack_ptr: pointer to dlinked list | ||
* @arg_data: data of node to be pushed | ||
* @count: line number | ||
*/ | ||
void push(stack_t **stack_ptr, char *arg_data, unsigned int count) | ||
{ | ||
stack_t *node; | ||
|
||
if (arg_data == NULL) | ||
{ | ||
fprintf(stderr, "L%d: usage: push integer\n", count); | ||
free(*stack_ptr); | ||
*stack_ptr = NULL; | ||
exit(EXIT_FAILURE); | ||
}; | ||
|
||
node = add_dnodeint(stack_ptr, atoi(arg_data)); | ||
if (node == NULL) | ||
{ | ||
puts("Error: malloc failed"); | ||
free_stack(*stack_ptr); | ||
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))) | ||
{ | ||
print_dlistint(*stack_ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "monty.h" | ||
/** | ||
* main - entry point | ||
* @argv: argument vector | ||
* @argc: argument counter | ||
* | ||
* Return: Nothing | ||
*/ | ||
int main(int argc, char *argv[]) | ||
{ | ||
FILE *fp; | ||
char *line = NULL; | ||
size_t len = 0; | ||
stack_t *stack_ptr = NULL; | ||
unsigned int count = 1; | ||
ssize_t read; | ||
|
||
if (argc != 2) | ||
{ | ||
fprintf(stderr, "USAGE: monty file\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
fp = fopen(argv[1], "r"); | ||
|
||
if (fp == NULL) | ||
{ | ||
fprintf(stderr, "Error: Can't open file %s\n", argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
while ((read = getline(&line, &len, fp)) != -1) | ||
{ | ||
if (*line == '\n') | ||
{ | ||
count++; | ||
continue; | ||
} | ||
opcode(&stack_ptr, line, count); | ||
count++; | ||
} | ||
free(line); | ||
fclose(fp); | ||
free_stack(stack_ptr); | ||
exit(EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "monty.h" | ||
/** | ||
* This code is public domain -- Will Hartung 4/9/09 | ||
* getline - reads a full line from a stream | ||
* @lineptr: pointer to character array | ||
* @n: pointer to variable that keeps array size | ||
* stream: stdin is the stream from which the file is read | ||
* | ||
* Return: returns the number of char entered | ||
* | ||
* */ | ||
size_t getline(char **lineptr, size_t *n, FILE *stream) { | ||
char *bufptr = NULL; | ||
char *p = bufptr; | ||
size_t size; | ||
int c; | ||
|
||
if (lineptr == NULL) { | ||
return -1; | ||
} | ||
if (stream == NULL) { | ||
return -1; | ||
} | ||
if (n == NULL) { | ||
return -1; | ||
} | ||
bufptr = *lineptr; | ||
size = *n; | ||
|
||
c = fgetc(stream); | ||
if (c == EOF) { | ||
return -1; | ||
} | ||
if (bufptr == NULL) { | ||
bufptr = malloc(128); | ||
if (bufptr == NULL) { | ||
return -1; | ||
} | ||
size = 128; | ||
} | ||
p = bufptr; | ||
while(c != EOF) { | ||
if ((size_t)(p - bufptr) > (size - 1)) { | ||
size = size + 128; | ||
bufptr = realloc(bufptr, size); | ||
if (bufptr == NULL) { | ||
return -1; | ||
} | ||
} | ||
*p++ = c; | ||
if (c == '\n') { | ||
break; | ||
} | ||
c = fgetc(stream); | ||
} | ||
|
||
*p++ = '\0'; | ||
*lineptr = bufptr; | ||
*n = size; | ||
|
||
return p - bufptr - 1; | ||
} |