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

A doubly-linked list for storing Tokens. More...

#include <tokenslist.h>

Public Member Functions

TokensListtokenslist_new ()
 Create a new (empty) TokensList object. More...
 
int tokenslist_add (TokensList *list, Token t)
 Append a token to the list. More...
 
TokensListElementtokenslist_remove (TokensList *list, TokensListElement *el)
 remove a token from the list More...
 
void tokenslist_insert (TokensList *list, TokensListElement *target, TokensList *src)
 Insert the contents of SRC into another list after an element. More...
 
void tokenslist_free (TokensList *list)
 free ALL memory associated with the TokensList object More...
 
void tokenslist_debug_print (TokensList *list)
 Pretty-print all tokens in a list. More...
 
int tokenslist_recognize (TokensList *t)
 Do token recognition on all tokens in a list. More...
 

Data Fields

TokensListElementhead
 head (first element) of the list More...
 
TokensListElementtail
 tail (last element) pointer More...
 

Detailed Description

A doubly-linked list for storing Tokens.

Definition at line 29 of file tokenslist.h.

Member Function Documentation

◆ tokenslist_add()

int tokenslist_add ( TokensList list,
Token  t 
)

Append a token to the list.

Returns
0 on success or -1 on error

Definition at line 31 of file tokenslist.c.

31  {
32  TokensListElement* elem = (TokensListElement*)malloc(sizeof(TokensListElement));
33  if (elem == NULL) goto ERR_MEM;
34  elem->next = NULL;
35  elem->prev = NULL;
36  elem->token = malloc(sizeof(Token));
37  if (!elem->token) goto ERR_MEM;
38  *elem->token = t;
39  if (list->head == NULL) {
40  list->head = elem;
41  list->tail = elem;
42  return 0;
43  }
44  list->tail->next = elem;
45  elem->prev = list->tail;
46  list->tail = elem;
47  return 0;
48 
49 ERR_MEM:
50  if (elem) {
51  if (elem->token)
52  free(elem->token);
53  free(elem);
54  }
55  ERROR("Memory allocation error in tokenslist_add()!\n");
56  return -1;
57 }

Referenced by read_file().

◆ tokenslist_debug_print()

void tokenslist_debug_print ( TokensList list)

Pretty-print all tokens in a list.

Intended for debugging

Definition at line 89 of file tokenslist.c.

89  {
90  LOG(0, "Dumping code:\n");
91 
92  for (TokensListElement* ptr = list->head; ptr != NULL; ptr = ptr->next)
93  token_print(ptr->token);
94 }

Referenced by main().

◆ tokenslist_free()

void tokenslist_free ( TokensList list)

free ALL memory associated with the TokensList object

Pointer should be NULLed after this!

Definition at line 78 of file tokenslist.c.

78  {
79  if (!list) return;
80  while (list->head != NULL) {
81  tokenslist_remove(list, list->head);
82  }
83  free(list);
84 }

Referenced by load_file(), read_file(), and state_free().

◆ tokenslist_insert()

void tokenslist_insert ( TokensList list,
TokensListElement target,
TokensList src 
)

Insert the contents of SRC into another list after an element.

Keeps the original element in place

Definition at line 97 of file tokenslist.c.

97  {
98  TokensListElement* next = pos->next;
99 
100  if (src->head == NULL)
101  return;
102 
103  // doubly-link starts
104  pos->next = src->head;
105  src->head->prev = pos;
106 
107 
108  src->tail->next = next;
109  if (next != NULL)
110  next->prev = src->tail;
111 
112  src->head = NULL;
113  src->tail = NULL;
114 }

◆ tokenslist_new()

TokensList * tokenslist_new ( )

Create a new (empty) TokensList object.

Returns
the new TokensList* or NULL on error

Definition at line 17 of file tokenslist.c.

17  {
18  TokensList* ret = (TokensList*)malloc(sizeof(TokensList));
19 
20  if (ret == NULL) {
21  ERROR("Memory allocation error in tokenslist_new()!\n");
22  return NULL;
23  }
24 
25  ret->head = NULL;
26  ret->tail = NULL;
27  return ret;
28 }

Referenced by read_file().

◆ tokenslist_recognize()

int tokenslist_recognize ( TokensList t)

Do token recognition on all tokens in a list.

Parameters
ttokenslist to use
Returns
0 on success, -1 on error

Definition at line 117 of file tokenslist.c.

117  {
118  for (TokensListElement* ptr = t->head; ptr != NULL; ptr = ptr->next)
119  if (token_recognize(ptr->token) < 0) {
120  FAIL("Can not recognize token types!\n");
121  return -1;
122  }
123  return 0;
124 }

Referenced by load_file().

◆ tokenslist_remove()

TokensListElement * tokenslist_remove ( TokensList list,
TokensListElement el 
)

remove a token from the list

Returns
the next element of the list (can be NULL)

The reason why we return is that we use it in an iteration over the same list

Definition at line 60 of file tokenslist.c.

60  {
61  if (list->head == el)
62  list->head = el->next;
63  if (list->tail == el)
64  list->tail = el->prev;
65 
66  if (el->next != NULL)
67  el->next->prev = el->prev;
68  if (el->prev != NULL) {
69  el->prev->next = el->next;
70  }
72  free(el->token);
73  free(el);
74  return next;
75 }

Referenced by pass_one(), and tokenslist_free().

Field Documentation

◆ head

TokensListElement* TokensList::head

◆ tail

TokensListElement* TokensList::tail

tail (last element) pointer

Definition at line 33 of file tokenslist.h.

Referenced by tokenslist_add(), tokenslist_insert(), tokenslist_new(), and tokenslist_remove().


The documentation for this class was generated from the following file:
TokensListElement::token
Token * token
Token value.
Definition: tokenslist.h:18
TokensList::tail
TokensListElement * tail
tail (last element) pointer
Definition: tokenslist.h:33
LOG
#define LOG(LVL,...)
logging macro - works like printf
Definition: logging.h:28
TokensListElement::prev
struct TokensListElement * prev
prev element in list or NULL
Definition: tokenslist.h:22
token_print
void token_print(Token *token)
Definition: tokenFunc.c:20
TokensListElement::next
struct TokensListElement * next
next element in list or NULL
Definition: tokenslist.h:20
Token
Token type to store token information.
Definition: token_t.h:37
TokensList::head
TokensListElement * head
head (first element) of the list
Definition: tokenslist.h:31
TokensListElement
An element of a TokensList.
Definition: tokenslist.h:16
TokensList
A doubly-linked list for storing Tokens.
Definition: tokenslist.h:29
token_recognize
int token_recognize(Token *t)
Definition: tokenFunc.c:251
MapEntry::next
struct MapEntry * next
pointer to the next entry or NULL
Definition: map.h:24
TokensList::tokenslist_remove
TokensListElement * tokenslist_remove(TokensList *list, TokensListElement *el)
remove a token from the list
Definition: tokenslist.c:60
FAIL
#define FAIL(...)
Fancy-print a fail (failed step). Works like printf.
Definition: logging.h:45
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40