parser.h (2966B)
1 #ifndef COMPILER_PARSER_H_ 2 #define COMPILER_PARSER_H_ 3 4 #include "compiler.h" 5 #include "string.h" 6 #include "tokenizer.h" 7 8 typedef enum { 9 TypeVoid, 10 TypeInteger, 11 TypeReal, 12 TypeArray, 13 } TypeKind; 14 15 struct Type; 16 17 typedef struct { 18 struct Type* content; 19 long begin; 20 long end; 21 } Array; 22 23 typedef struct Type { 24 Array array; 25 TypeKind kind; 26 } Type; 27 28 struct Expression; 29 30 typedef enum { 31 BinaryPlus, 32 BinaryMinus, 33 BinaryMul, 34 BinaryDiv, 35 BinaryMod, 36 BinaryAnd, 37 BinaryOr, 38 BinaryEq, 39 BinaryGe, 40 BinaryGt, 41 BinaryLe, 42 BinaryLt, 43 BinaryAssign, 44 } BinaryOperator; 45 46 typedef enum { 47 UnaryPlus, 48 UnaryMinus, 49 UnaryNot, 50 } UnaryOperator; 51 52 typedef struct { 53 UnaryOperator operator; 54 struct Expression* operand; 55 } UnaryOperation; 56 57 typedef struct { 58 BinaryOperator operator; 59 struct Expression* left; 60 struct Expression* right; 61 } BinaryOperation; 62 63 typedef struct { 64 String name; 65 struct Expression* index; 66 } Index; 67 68 typedef struct { 69 Type type; 70 String content; 71 } Literal; 72 73 typedef struct Compound { 74 struct Expression* this; 75 struct Compound* next; 76 } Compound; 77 78 typedef struct { 79 String name; 80 Compound* params; 81 } Call; 82 83 typedef struct { 84 struct Expression* condition; 85 struct Expression* body; 86 struct Expression* elsee; 87 } If; 88 89 typedef struct { 90 struct Expression* condition; 91 struct Expression* body; 92 } While; 93 94 typedef enum { 95 ExpressionLiteral, 96 ExpressionName, 97 ExpressionIf, 98 ExpressionWhile, 99 ExpressionCompound, 100 ExpressionBinaryOperation, 101 ExpressionUnaryOperation, 102 ExpressionIndex, 103 ExpressionCall, 104 } ExpressionKind; 105 106 typedef struct Expression { 107 union { 108 Literal literal; 109 String name; 110 UnaryOperation unary; 111 BinaryOperation binary; 112 If iff; 113 While whilee; 114 Compound compound; 115 } this; 116 117 ExpressionKind kind; 118 } Expression; 119 120 typedef struct Names { 121 String this; 122 struct Names* next; 123 } Names; 124 125 typedef struct { 126 Names* names; 127 Type type; 128 } Declaration; 129 130 typedef struct Declarations { 131 Declaration this; 132 struct Declarations* next; 133 } Declarations; 134 135 typedef struct { 136 Declaration this; 137 bool reference; 138 } ParameterDeclaration; 139 140 typedef struct Parameters { 141 ParameterDeclaration this; 142 struct Parameters* next; 143 } Parameters; 144 145 typedef struct { 146 Parameters* parameters; 147 Declarations* locals; 148 Expression body; 149 Type returnType; 150 String name; 151 } Subprogram; 152 153 typedef struct Subprograms { 154 Subprogram* this; 155 struct Subprograms* next; 156 } Subprograms; 157 158 typedef struct Ast { 159 String name; 160 Declarations* declarations; 161 Subprograms* subprograms; 162 Expression main; 163 } Ast; 164 165 Ast* Parse(Arena* arena, const char* program, Tokens* tokens, Error* error); 166 167 #endif // COMPILER_PARSER_H_