hyc

Hydrogen Compiler written in C
git clone git://git.kocotian.pl/hyc.git
Log | Files | Refs | README | LICENSE

ast.h (5690B)


      1 /*
      2    hyc - Hydrogen Compiler written in C
      3    Copyright (C) 2021  Kacper Kocot <kocotian@kocotian.pl>
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
     18 
     19 */
     20 
     21 #ifndef _AST_H
     22 #define _AST_H
     23 
     24 #include <str.h>
     25 #include <tokenize.h>
     26 
     27 /* Expressions */
     28 struct ASTExpressionAny;
     29 struct ASTExpressionLiteral;
     30 union ASTExpression;
     31 
     32 /* Statements */
     33 struct ASTStatementAny;
     34 struct ASTStatementReturn;
     35 struct ASTStatementCompound;
     36 union ASTStatement;
     37 
     38 /* Global parts */
     39 struct ASTAny;
     40 struct ASTGlobalFunction;
     41 union ASTGlobal;
     42 
     43 /**/
     44 
     45 typedef struct ASTStructAny {
     46 	Token *inittoken;
     47 } ASTStructAny;
     48 
     49 typedef enum ASTExpressionType {
     50 	ASTExpressionNULL_T = 0,
     51 	ASTExpressionLiteralIdentifier_T,
     52 	ASTExpressionLiteralInteger_T,
     53 	ASTExpressionLiteralString_T,
     54 	ASTExpressionUnarySignChange_T,
     55 	ASTExpressionUnaryAddressof_T,
     56 	ASTExpressionUnaryValuefrom_T,
     57 	ASTExpressionUnaryPreincrement_T,
     58 	ASTExpressionUnaryPredecrement_T,
     59 	ASTExpressionUnaryLogicalNot_T,
     60 	ASTExpressionFunctionArgumentList_T,
     61 	ASTExpressionFunctionCall_T,
     62 	ASTExpressionBinaryAssignment_T,
     63 	ASTExpression_Count,
     64 } ASTExpressionType;
     65 
     66 typedef struct ASTExpressionAny {
     67 	enum ASTExpressionType type;
     68 	struct ASTStructAny any;
     69 } ASTExpressionAny;
     70 
     71 typedef struct ASTExpressionLiteral {
     72 	struct ASTExpressionAny any;
     73 	char *value;
     74 } ASTExpressionLiteral;
     75 
     76 typedef struct ASTExpressionUnary {
     77 	struct ASTExpressionAny any;
     78 	union ASTExpression *expr;
     79 } ASTExpressionUnary;
     80 
     81 typedef struct ASTExpressionBinary {
     82 	struct ASTExpressionAny any;
     83 	union ASTExpression *left;
     84 	union ASTExpression *right;
     85 } ASTExpressionBinary;
     86 
     87 typedef struct ASTExpressionFunctionArgumentList {
     88 	struct ASTExpressionAny any;
     89 	union ASTExpression *data;
     90 	size_t len;
     91 } ASTExpressionFunctionArgumentList;
     92 
     93 typedef struct ASTExpressionFunctionCall {
     94 	struct ASTExpressionAny any;
     95 	union ASTExpression *callexpr;
     96 	union ASTExpression *argv;
     97 } ASTExpressionFunctionCall;
     98 
     99 typedef union ASTExpression {
    100 	enum ASTExpressionType type;
    101 	struct ASTExpressionAny Any;
    102 	struct ASTExpressionLiteral Literal;
    103 	struct ASTExpressionUnary Unary;
    104 	struct ASTExpressionUnary UnarySignChange;
    105 	struct ASTExpressionUnary UnaryAddressof;
    106 	struct ASTExpressionUnary UnaryValuefrom;
    107 	struct ASTExpressionUnary UnaryPreincrement;
    108 	struct ASTExpressionUnary UnaryPredecrement;
    109 	struct ASTExpressionUnary UnaryLogicalNot;
    110 	struct ASTExpressionFunctionArgumentList FunctionArgumentList;
    111 	struct ASTExpressionFunctionCall FunctionCall;
    112 	struct ASTExpressionBinary Binary;
    113 	struct ASTExpressionBinary BinaryAssignment;
    114 } ASTExpression;
    115 
    116 /**/
    117 
    118 typedef enum ASTStatementType {
    119 	ASTStatementNoOp_T = 0,
    120 	ASTStatementCompound_T,
    121 	ASTStatementConditional_T,
    122 	ASTStatementReturn_T,
    123 	ASTStatementExpression_T,
    124 	ASTStatementInlineAssembly_T,
    125 	ASTStatementVariableDeclaration_T,
    126 	ASTStatement_Count,
    127 } ASTStatementType;
    128 
    129 typedef struct ASTStatementAny {
    130 	enum ASTStatementType type;
    131 	struct ASTStructAny any;
    132 } ASTStatementAny;
    133 
    134 typedef struct ASTStatementCompound {
    135 	struct ASTStatementAny any;
    136 	union ASTStatement *data;
    137 	size_t len;
    138 } ASTStatementCompound;
    139 
    140 typedef struct ASTStatementConditional {
    141 	struct ASTStatementAny any;
    142 	union ASTExpression *condition;
    143 	union ASTStatement *body, *elsebody;
    144 } ASTStatementConditional;
    145 
    146 typedef struct ASTStatementReturn {
    147 	struct ASTStatementAny any;
    148 	union ASTExpression *expr;
    149 } ASTStatementReturn;
    150 
    151 typedef struct ASTStatementExpression {
    152 	struct ASTStatementAny any;
    153 	union ASTExpression *expr;
    154 } ASTStatementExpression;
    155 
    156 typedef struct ASTStatementInlineAssembly {
    157 	struct ASTStatementAny any;
    158 	struct ASTExpressionLiteral expr;
    159 } ASTStatementInlineAssembly;
    160 
    161 typedef struct ASTStatementVariableDeclaration {
    162 	struct ASTStatementAny any;
    163 	struct ASTExpressionLiteral name;
    164 } ASTStatementVariableDeclaration;
    165 
    166 typedef union ASTStatement {
    167 	enum ASTStatementType type;
    168 	struct ASTStatementAny Any;
    169 	struct ASTStatementCompound Compound;
    170 	struct ASTStatementConditional Conditional;
    171 	struct ASTStatementReturn Return;
    172 	struct ASTStatementExpression Expression;
    173 	struct ASTStatementInlineAssembly InlineAssembly;
    174 	struct ASTStatementVariableDeclaration VariableDeclaration;
    175 } ASTStatement;
    176 
    177 /**/
    178 
    179 typedef enum ASTGlobalType {
    180 	ASTGlobalNothing_T = 0,
    181 	ASTGlobalFunction_T,
    182 	ASTGlobalExport_T,
    183 	ASTGlobal_Count,
    184 } ASTGlobalType;
    185 
    186 typedef struct ASTGlobalAny {
    187 	enum ASTGlobalType type;
    188 	struct ASTStructAny any;
    189 } ASTGlobalAny;
    190 
    191 typedef struct ASTGlobalFunction {
    192 	struct ASTGlobalAny any;
    193 	struct ASTExpressionLiteral name;
    194 	struct {
    195 		struct ASTStatementVariableDeclaration *data;
    196 		size_t len;
    197 	} parameters;
    198 	union ASTStatement *body;
    199 } ASTGlobalFunction;
    200 
    201 typedef struct ASTGlobalExport {
    202 	struct ASTGlobalAny any;
    203 	struct ASTExpressionLiteral name;
    204 } ASTGlobalExport;
    205 
    206 typedef union ASTGlobal {
    207 	enum ASTGlobalType type;
    208 	struct ASTGlobalAny Any;
    209 	struct ASTGlobalFunction Function;
    210 	struct ASTGlobalExport Export;
    211 } ASTGlobal;
    212 
    213 /**/
    214 
    215 typedef struct ASTModule {
    216 	union ASTGlobal *data;
    217 	size_t len;
    218 } ASTModule;
    219 
    220 /**/
    221 
    222 ASTModule tokenstoASTModule(Token *tdata, size_t tlen);
    223 
    224 #endif