s502 assembler
A very simple assembler for the 6502 line of processors written in C
Public Member Functions | Data Fields
istack_el Class Reference

very simple int stack More...

#include <istack.h>

Public Member Functions

istack_ptr istack_new ()
 create an empty istack More...
 
int istack_empty (istack_ptr istack)
 check if the istack is empty or not More...
 
int istack_push (istack_ptr istack, int val)
 push one element to the istack More...
 
int istack_pop (istack_ptr istack)
 pop the top of the istack More...
 
int istack_top (istack_ptr istack, int def)
 get the top element of the istack More...
 
void istack_free (istack_ptr istack)
 free all memory associated with a istack More...
 

Data Fields

int val
 stored value More...
 
struct _istack * next
 pointer to next element or NULL More...
 

Detailed Description

very simple int stack

Simple stack for storing integers
Implemented with a one-sentinel singly linked list

Definition at line 16 of file istack.h.

Member Function Documentation

◆ istack_empty()

int istack_empty ( istack_ptr  istack)

check if the istack is empty or not

Parameters
istackthe istack to check
Returns
1 if empty, 0 if not

Simple check of the emptiness of the stack.
Simply looks at the next pointer

Definition at line 26 of file istack.c.

26  {
27  return (!istack->next);
28 };

Referenced by istack_free(), istack_pop(), and istack_top().

◆ istack_free()

void istack_free ( istack_ptr  istack)

free all memory associated with a istack

Parameters
istackistack pointer

Destructor of istack.
Frees all memory, including head (sentinel) node.
Invalidates all references to any elements (including head), so they all should be NULLed!

Definition at line 59 of file istack.c.

59  {
60  while (!istack_empty(istack)) istack_pop(istack);
61  free(istack);
62 }

Referenced by pass_one().

◆ istack_new()

istack_ptr istack_new ( )

create an empty istack

Returns
istack pointer or NULL on error

Constructor for istack.
Returns head, which will be unchanged since it's a sentinel node.

Definition at line 15 of file istack.c.

15  {
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 }

Referenced by pass_one().

◆ istack_pop()

int istack_pop ( istack_ptr  istack)

pop the top of the istack

Parameters
istackthe istack to pop from
Returns
0 on success, -1 on error (popping from an empty istack)

Definition at line 44 of file istack.c.

44  {
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 }

Referenced by istack_free(), and pass_one().

◆ istack_push()

int istack_push ( istack_ptr  istack,
int  val 
)

push one element to the istack

Parameters
istackistack to push to
valvalue to push
Returns
0 on success, -1 on error

Push one element on the stack.
Does not modify head pointer.

Definition at line 31 of file istack.c.

31  {
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 }

Referenced by pass_one().

◆ istack_top()

int istack_top ( istack_ptr  istack,
int  def 
)

get the top element of the istack

Parameters
istackthe istack to get the top of
defdefault value if the istack is empty
Returns
top of the istack or def

Definition at line 53 of file istack.c.

53  {
54  if (istack_empty(istack)) return def;
55  return istack->next->val;
56 };

Referenced by pass_one().

Field Documentation

◆ next

struct _istack* istack_el::next

pointer to next element or NULL

Definition at line 20 of file istack.h.

Referenced by istack_empty(), istack_new(), istack_pop(), istack_push(), and istack_top().

◆ val

int istack_el::val

stored value

Definition at line 18 of file istack.h.

Referenced by istack_push().


The documentation for this class was generated from the following file:
istack_el::val
int val
stored value
Definition: istack.h:18
istack_el
very simple int stack
Definition: istack.h:16
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
istack_el::istack_empty
int istack_empty(istack_ptr istack)
check if the istack is empty or not
Definition: istack.c:26
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40