forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.h
60 lines (52 loc) · 1.47 KB
/
defs.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Structure and enum definitions
// Copyright (c) 2019 Warren Toomey, GPL3
#define TEXTLEN 512 // Length of symbols in input
#define NSYMBOLS 1024 // Number of symbol table entries
// Token types
enum {
T_EOF,
T_PLUS, T_MINUS,
T_STAR, T_SLASH,
T_EQ, T_NE,
T_LT, T_GT, T_LE, T_GE,
T_INTLIT, T_SEMI, T_ASSIGN, T_IDENT,
T_LBRACE, T_RBRACE, T_LPAREN, T_RPAREN,
// Keywords
T_PRINT, T_INT, T_IF, T_ELSE, T_WHILE,
T_FOR, T_VOID
};
// Token structure
struct token {
int token; // Token type, from the enum list above
int intvalue; // For T_INTLIT, the integer value
};
// AST node types. The first few line up
// with the related tokens
enum {
A_ADD = 1, A_SUBTRACT, A_MULTIPLY, A_DIVIDE,
A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE,
A_INTLIT,
A_IDENT, A_LVIDENT, A_ASSIGN, A_PRINT, A_GLUE,
A_IF, A_WHILE, A_FUNCTION
};
// Abstract Syntax Tree structure
struct ASTnode {
int op; // "Operation" to be performed on this tree
struct ASTnode *left; // Left, middle and right child trees
struct ASTnode *mid;
struct ASTnode *right;
union {
int intvalue; // For A_INTLIT, the integer value
int id; // For A_IDENT, the symbol slot number
} v; // For A_FUNCTION, the symbol slot number
};
#define NOREG -1 // Use NOREG when the AST generation
// functions have no register to return
// Symbol table structure
struct symtable {
char *name; // Name of a symbol
};