Decaf Compiler
common.h
Go to the documentation of this file.
1 
10 #ifndef __COMMON_H
11 #define __COMMON_H
12 
20 /*
21  * DEPENDENCIES
22  */
23 
24 #include <inttypes.h>
25 #include <regex.h>
26 #include <setjmp.h>
27 #include <stdarg.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
37 #define MAX_FILE_SIZE 65536
38 
42 #define MAX_LINE_LEN 256
43 
47 #define MAX_TOKEN_LEN 256
48 
52 #define MAX_ERROR_LEN 256
53 
57 #define MAX_ID_LEN 256
58 
66 typedef enum DecafType{
67  UNKNOWN, INT, BOOL, VOID, STR
68 } DecafType;
69 
77 
84 void print_escaped_string(const char* string, FILE* output);
85 
100 void Error_throw_printf (const char* format, ...);
101 
107 #define CHECK_MALLOC_PTR(P) \
108  if (P == NULL) { \
109  printf("Out of memory!\n"); \
110  exit(EXIT_FAILURE); \
111  }
112 
122 #define DECL_LIST_TYPE(NAME, ELEMTYPE) \
123  \
124  typedef struct NAME ## List { \
125  ELEMTYPE head; \
126  ELEMTYPE tail; \
127  int size; \
128  } NAME ## List; \
129  \
130  \
131  NAME ## List* NAME ## List_new (); \
132  \
133  \
134  void NAME ## List_add (NAME ## List* list, ELEMTYPE item); \
135  \
136  \
137  int NAME ## List_size (NAME ## List* list); \
138  \
139  \
140  bool NAME ## List_is_empty (NAME ## List* list); \
141  \
142  \
143  void NAME ## List_free (NAME ## List* list);
144 
158 #define DEF_LIST_IMPL(NAME, ELEMTYPE, FREEFUNC) \
159  NAME ## List* NAME ## List_new () \
160  { \
161  NAME ## List* list = (NAME ## List*)calloc(1, sizeof(NAME ## List)); \
162  CHECK_MALLOC_PTR(list); \
163  list->head = NULL; \
164  list->tail = NULL; \
165  list->size = 0; \
166  return list; \
167  } \
168  void NAME ## List_add (NAME ## List* list, ELEMTYPE item) \
169  { \
170  if (list->head == NULL) { \
171  list->head = item; \
172  list->tail = item; \
173  } else { \
174  list->tail->next = item; \
175  list->tail = item; \
176  } \
177  list->size++; \
178  } \
179  int NAME ## List_size (NAME ## List* list) \
180  { \
181  return list->size; \
182  } \
183  bool NAME ## List_is_empty (NAME ## List* list) \
184  { \
185  return (list->size == 0); \
186  } \
187  void NAME ## List_free (NAME ## List* list) \
188  { \
189  ELEMTYPE next = list->head; \
190  while (next != NULL) { \
191  ELEMTYPE cur = next; \
192  next = cur->next; \
193  FREEFUNC(cur); \
194  } \
195  free(list); \
196  }
197 
204 #define FOR_EACH(TYPE, VARIABLE, CONTAINER) \
205  for (TYPE VARIABLE = (CONTAINER)->head; \
206  VARIABLE != NULL; \
207  VARIABLE = VARIABLE->next)
208 
209 #endif
DecafType
DecafType
Valid Decaf types.
Definition: common.h:66
DecafType_to_string
const char * DecafType_to_string(DecafType type)
Convert a Decaf type to a string for output.
Definition: common.c:3
LiteralNode::type
DecafType type
Literal type (discriminator/tag for the anonymous union)
Definition: ast.h:384
Error_throw_printf
void Error_throw_printf(const char *format,...)
Throw an exception with an error message using printf syntax.
Definition: main.c:25
print_escaped_string
void print_escaped_string(const char *string, FILE *output)
Print a Decaf string literal, inserting escape codes as necessary.
Definition: common.c:15