s502 assembler
A very simple assembler for the 6502 line of processors written in C
Macros | Functions
tokenslist.c File Reference

implement TokensList methods More...

#include "debugmalloc.h"
#include <stdio.h>
#include <stdlib.h>
#include "tokenslist.h"
#include "tokenFunc.h"
#include "logging.h"

Go to the source code of this file.

Macros

#define __USE_MINGW_ANSI_STDIO   1
 

Functions

TokensListtokenslist_new ()
 
int tokenslist_add (TokensList *list, Token t)
 
TokensListElementtokenslist_remove (TokensList *list, TokensListElement *el)
 
void tokenslist_free (TokensList *list)
 
void tokenslist_debug_print (TokensList *list)
 
void tokenslist_insert (TokensList *list, TokensListElement *pos, TokensList *src)
 
int tokenslist_recognize (TokensList *t)
 

Detailed Description

implement TokensList methods

See also
TokensList

Definition in file tokenslist.c.

Macro Definition Documentation

◆ __USE_MINGW_ANSI_STDIO

#define __USE_MINGW_ANSI_STDIO   1

Definition at line 3 of file tokenslist.c.

Function Documentation

◆ tokenslist_add()

int tokenslist_add ( TokensList list,
Token  t 
)

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 }

References ERROR, TokensList::head, TokensListElement::next, TokensListElement::prev, TokensList::tail, and TokensListElement::token.

◆ tokenslist_debug_print()

void tokenslist_debug_print ( TokensList list)

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 }

References TokensList::head, LOG, TokensListElement::next, and Token::token_print().

◆ tokenslist_free()

void tokenslist_free ( TokensList list)

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 }

References TokensList::head, and TokensList::tokenslist_remove().

◆ tokenslist_insert()

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

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 }

References TokensList::head, TokensListElement::next, TokensListElement::prev, and TokensList::tail.

◆ tokenslist_new()

TokensList* tokenslist_new ( )

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 }

References ERROR, TokensList::head, and TokensList::tail.

◆ tokenslist_recognize()

int tokenslist_recognize ( TokensList t)

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 }

References FAIL, TokensList::head, TokensListElement::next, and Token::token_recognize().

◆ tokenslist_remove()

TokensListElement* tokenslist_remove ( TokensList list,
TokensListElement el 
)

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 }

References TokensList::head, TokensListElement::next, TokensListElement::prev, TokensList::tail, and TokensListElement::token.

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
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
MapEntry::next
struct MapEntry * next
pointer to the next entry or NULL
Definition: map.h:24
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
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