Decaf Compiler
Loading...
Searching...
No Matches
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
66typedef enum DecafType{
67 UNKNOWN, INT, BOOL, VOID, STR
69
76const char* DecafType_to_string(DecafType type);
77
84void print_escaped_string(const char* string, FILE* output);
85
94void print_doubly_escaped_string(const char* string, FILE* output);
95
110void Error_throw_printf (const char* format, ...);
111
117#define CHECK_MALLOC_PTR(P) \
118 if (P == NULL) { \
119 printf("Out of memory!\n"); \
120 exit(EXIT_FAILURE); \
121 }
122
132#define DECL_LIST_TYPE(NAME, ELEMTYPE) \
133 \
134 typedef struct NAME ## List { \
135 ELEMTYPE head; \
136 ELEMTYPE tail; \
137 int size; \
138 } NAME ## List; \
139 \
140 \
141 NAME ## List* NAME ## List_new (void); \
142 \
143 \
144 void NAME ## List_add (NAME ## List* list, ELEMTYPE item); \
145 \
146 \
147 int NAME ## List_size (NAME ## List* list); \
148 \
149 \
150 bool NAME ## List_is_empty (NAME ## List* list); \
151 \
152 \
153 void NAME ## List_free (NAME ## List* list);
154
168#define DEF_LIST_IMPL(NAME, ELEMTYPE, FREEFUNC) \
169 NAME ## List* NAME ## List_new (void) \
170 { \
171 NAME ## List* list = (NAME ## List*)calloc(1, sizeof(NAME ## List)); \
172 CHECK_MALLOC_PTR(list); \
173 list->head = NULL; \
174 list->tail = NULL; \
175 list->size = 0; \
176 return list; \
177 } \
178 void NAME ## List_add (NAME ## List* list, ELEMTYPE item) \
179 { \
180 if (list->head == NULL) { \
181 list->head = item; \
182 list->tail = item; \
183 } else { \
184 list->tail->next = item; \
185 list->tail = item; \
186 } \
187 list->size++; \
188 } \
189 int NAME ## List_size (NAME ## List* list) \
190 { \
191 return list->size; \
192 } \
193 bool NAME ## List_is_empty (NAME ## List* list) \
194 { \
195 return (list->size == 0); \
196 } \
197 void NAME ## List_free (NAME ## List* list) \
198 { \
199 ELEMTYPE next = list->head; \
200 while (next != NULL) { \
201 ELEMTYPE cur = next; \
202 next = cur->next; \
203 FREEFUNC(cur); \
204 } \
205 free(list); \
206 }
207
214#define FOR_EACH(TYPE, VARIABLE, CONTAINER) \
215 for (TYPE VARIABLE = (CONTAINER)->head; \
216 VARIABLE != NULL; \
217 VARIABLE = VARIABLE->next)
218
219#endif
void Error_throw_printf(const char *format,...)
Throw an exception with an error message using printf syntax.
Definition main.c:34
void print_doubly_escaped_string(const char *string, FILE *output)
Print a Decaf string literal, inserting double escape codes as necessary.
Definition common.c:29
void print_escaped_string(const char *string, FILE *output)
Print a Decaf string literal, inserting escape codes as necessary.
Definition common.c:15
const char * DecafType_to_string(DecafType type)
Convert a Decaf type to a string for output.
Definition common.c:3
DecafType
Valid Decaf types.
Definition common.h:66