Decaf Compiler
|
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. | |
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.
#define CHECK_MALLOC_PTR | ( | P | ) |
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.
#define DECL_LIST_TYPE | ( | NAME, | |
ELEMTYPE | |||
) |
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.
NAME | Prefix for the list struct name (actual name will be NAMEList ) |
ELEMTYPE | Type of the elements to be stored (usually a struct pointer) |
#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.
NAME | Prefix for the list struct name (actual name will be NAMEList ) |
ELEMTYPE | Type of the elements to be stored (must be a struct pointer) |
FREEFUNC | Name of the function to call to deallocate each element |
#define FOR_EACH | ( | TYPE, | |
VARIABLE, | |||
CONTAINER | |||
) |
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.
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
.
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
.
const char * DecafType_to_string | ( | DecafType | type | ) |
Convert a Decaf type to a string for output.
type | Type to convert |
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.
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.
string | String literal to print |
output | File stream to print the string to |
void print_escaped_string | ( | const char * | string, |
FILE * | output | ||
) |
Print a Decaf string literal, inserting escape codes as necessary.
string | String literal to print |
output | File stream to print the string to |