-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.h
132 lines (105 loc) · 2.98 KB
/
global.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*! \file
* This file contains the important global definitions for the CS24 Python
* interpreter.
*/
#ifndef GLOBAL_H
#define GLOBAL_H
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <setjmp.h>
/*! Maximum length of a single token. */
#define MAX_LENGTH 512
/*! Default initial size for realloc-growing arrays. */
#define INITIAL_SIZE 8
/* A handy macro to delineate an unreachable branch in switches. */
#define UNREACHABLE() \
{ fprintf(stderr, "THIS SHOULD BE UNREACHABLE!"); exit(-1); }
void *parse_alloc(size_t sz);
void parse_free_all();
char *parse_string_dup(const char *str);
//TODO: where do I put this???
void error(int pos, const char *fmt, ...) __attribute__((noreturn));
extern sigjmp_buf error_jmp;
/********************* PARSE TYPES *********************/
/*!
* An enumeration of the various types of tokens the parser can generate.
*/
typedef enum TokenType {
STREAM_END, /*!< EOF. */
LINE_END, /*!< Hit end of line. */
DEL, /*!< Deletion keyword. */
GC, /*!< GC "keyword". */
RPAREN, /*!< Right parenthesis. */
LPAREN, /*!< Left parenthesis. */
LBRACKET, /*!< Left bracket. */
RBRACKET, /*!< Right bracket. */
LBRACE, /*!< Left brace. (This character: { ) */
RBRACE, /*!< Right brace. (This character: } ) */
COLON, /*!< Colon. */
EQUAL, /*!< Equal sign. */
PLUS, /*!< Plus. */
MINUS, /*!< Minus. */
ASTERISK, /*!< Asterisk. */
SLASH, /*!< Forward slash. (This character: / ) */
DOT, /*!< Period. */
COMMA, /*!< Comma. */
FLOAT, /*!< A float number of some sort. */
STRING, /*!< A string-literal. Composed of any characters. */
IDENT, /*!< An identifier for a variable. */
} TokenType;
typedef struct Token {
TokenType type;
int pos;
char string[MAX_LENGTH];
int int_value;
float float_value;
} Token;
typedef enum StatementType {
STMT_DEL,
STMT_EXPR,
STMT_GC
} StatementType;
typedef struct ParseStatement {
enum StatementType type;
union {
struct ParseExpression *expr;
char *identifier;
};
} ParseStatement;
typedef enum ExpressionType {
EXPR_SUBSCRIPT,
EXPR_NEGATE,
EXPR_IDENT,
EXPR_STRING,
EXPR_FLOAT,
EXPR_LIST,
EXPR_DICT,
EXPR_ASSIGN,
EXPR_ADD,
EXPR_SUB,
EXPR_MULT,
EXPR_DIV
} ExpressionType;
typedef struct ParseExpression {
enum ExpressionType type;
union {
struct {
struct ParseExpression *lhs, *rhs;
};
char *string;
float float_value;
struct ParseListNode *list;
struct ParseDictNode *dict;
};
} ParseExpression;
typedef struct ParseListNode {
struct ParseListNode *next;
struct ParseExpression *expr;
} ParseListNode;
typedef struct ParseDictNode {
struct ParseDictNode *next;
struct ParseExpression *key, *value;
} ParseDictNode;
#endif /* GLOBAL_H */