comp.h (1498B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct Type Type; 5 typedef struct Func Func; 6 typedef struct Rhs Rhs; 7 typedef struct Expr Expr; 8 typedef struct Value Value; 9 typedef struct Arg Arg; 10 typedef struct Name Name; 11 12 typedef struct Func { 13 Type *type; 14 Name *name; 15 Arg *args; 16 int argc; 17 Expr *body; 18 int exprc; 19 } Func; 20 21 enum Types { 22 I8 = 1, 23 I16 = 2, 24 I32 = 4, 25 I64 = 8, 26 27 U8 = I8 | 16, 28 U16 = I16 | 16, 29 U32 = I32 | 16, 30 U64 = I64 | 16, 31 32 F32 = I32 | 32, 33 F64 = I64 | 32, 34 35 VOID = 64, 36 37 COMPLEX, 38 }; 39 40 typedef struct Type { 41 enum Types type; 42 } Type; 43 44 enum Exprs { 45 ASSIGN, 46 FCALL_EXPR, 47 }; 48 49 typedef struct Expr { 50 enum Exprs expr; 51 union { 52 struct { 53 Type *type; 54 Name *name; 55 Rhs *rhs; 56 } assign; 57 struct { 58 Name *name; 59 Rhs *args; 60 int argc; 61 } fcall; 62 }; 63 } Expr; 64 65 enum Rhss { 66 MATH, 67 FCALL_RHS, 68 VALUE, 69 BRACE, 70 }; 71 72 enum Op { 73 ADD, 74 SUB, 75 MUL, 76 DIV, 77 }; 78 79 typedef struct Rhs { 80 enum Rhss rhs; 81 union { 82 struct { 83 enum Op op; 84 Rhs *lhs; 85 Rhs *rhs; 86 } math; 87 struct { 88 Name *name; 89 Rhs **args; 90 int argc; 91 } fcall; 92 Rhs *brace; 93 Value *value; 94 }; 95 } Rhs; 96 97 typedef struct Value { 98 long long int lval; 99 double dval; 100 } Value; 101 102 typedef struct Name { 103 char *name; 104 } Name; 105 106 typedef struct List { 107 int count, size; 108 void *data; 109 } List; 110 111 typedef struct Arg { 112 Name *name; 113 Type *type; 114 } Arg; 115 116 #define APPEND(l, v) l->count++; \ 117 l->data = (l->data) ? realloc(l->data, l->size * l->count) : malloc(l->size); \ 118 memcpy(l->data + l->size * (l->count - 1), v, l->size);