s502 assembler
A very simple assembler for the 6502 line of processors written in C
istack.c
Go to the documentation of this file.
1 #include "debugmalloc.h"
2 
3 #include <stdlib.h>
4 
5 #include "istack.h"
6 #include "logging.h"
7 
16  istack_ptr head = malloc(sizeof(istack_el));
17  if (!head) {
18  ERROR("Memory allocation error in istack_new()!\n");
19  return NULL;
20  }
21  head->next = NULL;
22  return head;
23 }
24 
25 
26 int istack_empty(istack_ptr istack) {
27  return (!istack->next);
28 };
29 
30 
31 int istack_push(istack_ptr istack, int val) {
32  istack_ptr elem = malloc(sizeof(istack_el));
33  if (!elem) {
34  ERROR("Memory allocation error in istack_push()!\n");
35  return -1;
36  }
37  elem->val = val;
38  elem->next = istack->next;
39  istack->next = elem;
40  return 0;
41 }
42 
43 
44 int istack_pop(istack_ptr istack) {
45  if (istack_empty(istack)) return -1;
46  istack_ptr elem = istack->next;
47  istack->next = elem->next;
48  free(elem);
49  return 0;
50 }
51 
52 
53 int istack_top(istack_ptr istack, int def) {
54  if (istack_empty(istack)) return def;
55  return istack->next->val;
56 };
57 
58 
59 void istack_free(istack_ptr istack) {
60  while (!istack_empty(istack)) istack_pop(istack);
61  free(istack);
62 }
istack_el::val
int val
stored value
Definition: istack.h:18
istack_el
very simple int stack
Definition: istack.h:16
istack_el::istack_push
int istack_push(istack_ptr istack, int val)
push one element to the istack
Definition: istack.c:31
istack_el::istack_free
void istack_free(istack_ptr istack)
free all memory associated with a istack
Definition: istack.c:59
istack_el::next
struct _istack * next
pointer to next element or NULL
Definition: istack.h:20
istack_el::istack_pop
int istack_pop(istack_ptr istack)
pop the top of the istack
Definition: istack.c:44
logging.h
logging and fancy-printing
istack_el::istack_empty
int istack_empty(istack_ptr istack)
check if the istack is empty or not
Definition: istack.c:26
istack_el::istack_top
int istack_top(istack_ptr istack, int def)
get the top element of the istack
Definition: istack.c:53
istack_el::istack_new
istack_ptr istack_new()
create an empty istack
Definition: istack.c:15
istack.h
iStack class for storing non-negative integers
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40