s502 assembler
A very simple assembler for the 6502 line of processors written in C
tokenslist.c
Go to the documentation of this file.
1 #include "debugmalloc.h"
2 
3 #define __USE_MINGW_ANSI_STDIO 1
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include "tokenslist.h"
8 #include "tokenFunc.h"
9 #include "logging.h"
10 
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 }
29 
30 
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 }
58 
59 
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  }
71  TokensListElement* next = el->next;
72  free(el->token);
73  free(el);
74  return next;
75 }
76 
77 
79  if (!list) return;
80  while (list->head != NULL) {
81  tokenslist_remove(list, list->head);
82  }
83  free(list);
84 }
85 
86 
87 
88 
90  LOG(0, "Dumping code:\n");
91 
92  for (TokensListElement* ptr = list->head; ptr != NULL; ptr = ptr->next)
93  token_print(ptr->token);
94 }
95 
96 
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 }
115 
116 
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 }
TokensList::tokenslist_free
void tokenslist_free(TokensList *list)
free ALL memory associated with the TokensList object
Definition: tokenslist.c:78
TokensList::tokenslist_add
int tokenslist_add(TokensList *list, Token t)
Append a token to the list.
Definition: tokenslist.c:31
TokensListElement::token
Token * token
Token value.
Definition: tokenslist.h:18
TokensList::tail
TokensListElement * tail
tail (last element) pointer
Definition: tokenslist.h:33
TokensList::tokenslist_debug_print
void tokenslist_debug_print(TokensList *list)
Pretty-print all tokens in a list.
Definition: tokenslist.c:89
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
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.h
Implement TokensList.
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::tokenslist_recognize
int tokenslist_recognize(TokensList *t)
Do token recognition on all tokens in a list.
Definition: tokenslist.c:117
tokenFunc.h
Token type member methods.
TokensList
A doubly-linked list for storing Tokens.
Definition: tokenslist.h:29
Token::token_print
void token_print(Token *token)
Pretty-print one token, with its source and length.
Definition: tokenFunc.c:20
Token::token_recognize
int token_recognize(Token *t)
Parse token - test if it's an opcode, a label or a directive.
Definition: tokenFunc.c:251
logging.h
logging and fancy-printing
TokensList::tokenslist_remove
TokensListElement * tokenslist_remove(TokensList *list, TokensListElement *el)
remove a token from the list
Definition: tokenslist.c:60
TokensList::tokenslist_insert
void tokenslist_insert(TokensList *list, TokensListElement *target, TokensList *src)
Insert the contents of SRC into another list after an element.
Definition: tokenslist.c:97
TokensList::tokenslist_new
TokensList * tokenslist_new()
Create a new (empty) TokensList object.
Definition: tokenslist.c:17
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