Decaf Compiler
Loading...
Searching...
No Matches
Macros | Typedefs | Enumerations | Functions
common.h File Reference

Includes, constants, declarations, and macros used across the compiler. More...

#include <inttypes.h>
#include <regex.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define MAX_FILE_SIZE   65536
 Maximum size (in bytes) of any Decaf source file.
 
#define MAX_LINE_LEN   256
 Maximum length (in characters) of any single line of input.
 
#define MAX_TOKEN_LEN   256
 Maximum length (in characters) of any single token.
 
#define MAX_ERROR_LEN   256
 Maximum length (in characters) of any error message.
 
#define MAX_ID_LEN   256
 Maximum length (in characters) of any identifier.
 
#define CHECK_MALLOC_PTR(P)
 Check a pointer for NULL and terminate with an out-of-memory error.
 
#define DECL_LIST_TYPE(NAME, ELEMTYPE)
 Declare a singly-linked list structure of the given type.
 
#define DEF_LIST_IMPL(NAME, ELEMTYPE, FREEFUNC)
 Define a list implementation.
 
#define FOR_EACH(TYPE, VARIABLE, CONTAINER)
 Set up a for-each style loop over a singly-linked list.
 

Typedefs

typedef enum DecafType DecafType
 Valid Decaf types.
 

Enumerations

enum  DecafType {
  UNKNOWN , INT , BOOL , VOID ,
  STR
}
 Valid Decaf types. More...
 

Functions

const char * DecafType_to_string (DecafType type)
 Convert a Decaf type to a string for output.
 
void print_escaped_string (const char *string, FILE *output)
 Print a Decaf string literal, inserting escape codes as necessary.
 
void print_doubly_escaped_string (const char *string, FILE *output)
 Print a Decaf string literal, inserting double escape codes as necessary.
 
void Error_throw_printf (const char *format,...)
 Throw an exception with an error message using printf syntax.
 

Detailed Description

Includes, constants, declarations, and macros used across the compiler.

This module declares a list of standard C library dependencies used by most modules, a list of constants used across the compiler, a couple of utility functions, and several useful macros.

Macro Definition Documentation

◆ CHECK_MALLOC_PTR

#define CHECK_MALLOC_PTR (   P)
Value:
if (P == NULL) { \
printf("Out of memory!\n"); \
exit(EXIT_FAILURE); \
}

Check a pointer for NULL and terminate with an out-of-memory error.

This is meant to be called immediately after a malloc/calloc call.

◆ DECL_LIST_TYPE

#define DECL_LIST_TYPE (   NAME,
  ELEMTYPE 
)
Value:
\
typedef struct NAME ## List { \
ELEMTYPE head; \
ELEMTYPE tail; \
int size; \
} NAME ## List; \
\
\
NAME ## List* NAME ## List_new (void); \
\
\
void NAME ## List_add (NAME ## List* list, ELEMTYPE item); \
\
\
int NAME ## List_size (NAME ## List* list); \
\
\
bool NAME ## List_is_empty (NAME ## List* list); \
\
\
void NAME ## List_free (NAME ## List* list);

Declare a singly-linked list structure of the given type.

This avoids having to declare a separate structure for every type that we need to be able to store in lists.

Parameters
NAMEPrefix for the list struct name (actual name will be NAMEList)
ELEMTYPEType of the elements to be stored (usually a struct pointer)

◆ DEF_LIST_IMPL

#define DEF_LIST_IMPL (   NAME,
  ELEMTYPE,
  FREEFUNC 
)

Define a list implementation.

This avoids having to implement a separate structure for every type that we need to be able to store in lists.

Requirement: ELEMTYPE must be a pointer to a struct with a self-referencing next pointer.

Parameters
NAMEPrefix for the list struct name (actual name will be NAMEList)
ELEMTYPEType of the elements to be stored (must be a struct pointer)
FREEFUNCName of the function to call to deallocate each element

◆ FOR_EACH

#define FOR_EACH (   TYPE,
  VARIABLE,
  CONTAINER 
)
Value:
for (TYPE VARIABLE = (CONTAINER)->head; \
VARIABLE != NULL; \
VARIABLE = VARIABLE->next)

Set up a for-each style loop over a singly-linked list.

Works for all structures declared and implemented with DECL_LIST_TYPE and DEF_LIST_IMPL.

Typedef Documentation

◆ DecafType

typedef enum DecafType DecafType

Valid Decaf types.

Variables can only be INT or BOOL; the others are included for tracking the return type of a void function or the type of a parameter to print_str.

Enumeration Type Documentation

◆ DecafType

enum DecafType

Valid Decaf types.

Variables can only be INT or BOOL; the others are included for tracking the return type of a void function or the type of a parameter to print_str.

Function Documentation

◆ DecafType_to_string()

const char * DecafType_to_string ( DecafType  type)

Convert a Decaf type to a string for output.

Parameters
typeType to convert
Returns
Static const string representation of the given type

◆ Error_throw_printf()

void Error_throw_printf ( const char *  format,
  ... 
)

Throw an exception with an error message using printf syntax.

This function uses the longjmp functionality in the standard C library to implement exception handling for the lexing and parsing phases. The code that might throw an exception must be wrapped in a setjmp block.

Because this function must have access to the jmp_buf used to connect the setjmp callsite with the longjmp, this function must be implemented in the compiler or test driver (where the setjmp call will be) in other to avoid having an extern static variable. Thus, there is no implementation in common.c, and it is only declared here so that it can be called in the various front end phases of compilation.

Throw an exception with an error message using printf syntax.

This function is declared in common.h but must be defined here in main.c because that's where the jmp_buf declaration is.

◆ print_doubly_escaped_string()

void print_doubly_escaped_string ( const char *  string,
FILE *  output 
)

Print a Decaf string literal, inserting double escape codes as necessary.

This is primarily useful for AST output w/ string literals and/or ILOC code.

Parameters
stringString literal to print
outputFile stream to print the string to

◆ print_escaped_string()

void print_escaped_string ( const char *  string,
FILE *  output 
)

Print a Decaf string literal, inserting escape codes as necessary.

Parameters
stringString literal to print
outputFile stream to print the string to