diff options
Diffstat (limited to 'parse.c')
-rw-r--r-- | parse.c | 242 |
1 files changed, 0 insertions, 242 deletions
@@ -1,245 +1,3 @@ -/* TODO: stmt_parse -> parse_stmt, etc. */ -typedef enum { - TYPE_VOID, - TYPE_UNKNOWN, - TYPE_BUILTIN, - TYPE_FN, - TYPE_TUPLE, - TYPE_ARR, - TYPE_PTR -} TypeKind; - -typedef enum { - BUILTIN_I8, - BUILTIN_I16, - BUILTIN_I32, - BUILTIN_I64, - BUILTIN_U8, - BUILTIN_U16, - BUILTIN_U32, - BUILTIN_U64, - BUILTIN_F32, - BUILTIN_F64, - BUILTIN_CHAR, - BUILTIN_BOOL, - BUILTIN_TYPE_COUNT -} BuiltinType; - -#define TYPE_FLAG_FLEXIBLE 0x01 -#define TYPE_FLAG_RESOLVED 0x02 - -typedef struct Type { - Location where; - TypeKind kind; - uint16_t flags; - union { - BuiltinType builtin; - struct { - Array types; /* [0] = ret_type, [1..] = param_types */ - } fn; - Array tuple; - struct { - struct Type *of; - union { - UInteger n; /* this is NOT set by parse_type; it will be handled by types.c */ - struct Expression *n_expr; - }; - } arr; - struct { - struct Type *of; - } ptr; - }; -} Type; - -#define BLOCK_FLAG_FN 0x01 - -typedef struct Block { - uint16_t flags; - Location start; - Location end; - Array stmts; - struct Block *parent; - struct Expression *ret_expr; /* the return expression of this block, e.g. {foo(); 3} => 3 NULL for no expression. */ -} Block; - -typedef enum { - EXPR_LITERAL_FLOAT, - EXPR_LITERAL_INT, - EXPR_LITERAL_STR, - EXPR_LITERAL_BOOL, - EXPR_LITERAL_CHAR, - EXPR_IDENT, /* variable or constant */ - EXPR_BINARY_OP, - EXPR_UNARY_OP, - EXPR_IF, - EXPR_WHILE, - EXPR_FN, - EXPR_CAST, - EXPR_NEW, - EXPR_CALL, - EXPR_BLOCK, - EXPR_DIRECT -} ExprKind; - -typedef enum { - UNARY_MINUS, - UNARY_ADDRESS, /* &x */ - UNARY_DEREF, /* *x */ - UNARY_NOT, /* !x */ - UNARY_DEL -} UnaryOp; - -typedef enum { - BINARY_SET, /* e.g. x = y */ - BINARY_ADD, - BINARY_SUB, - BINARY_MUL, - BINARY_DIV, - BINARY_COMMA, - BINARY_GT, - BINARY_LT, - BINARY_GE, - BINARY_LE, - BINARY_EQ, - BINARY_NE, - BINARY_AT_INDEX /* e.g. x[i] */ -} BinaryOp; - -typedef struct { - Directive which; - Array args; /* of Argument */ -} DirectExpr; - - -typedef struct { - struct Expression *fn; - Array args; /* of Argument; after typing, becomes of Expression */ -} CallExpr; - -typedef struct { - struct Expression *cond; /* NULL = this is an else */ - struct Expression *next_elif; /* next elif/else of this statement */ - Block body; -} IfExpr; - -typedef struct { - struct Expression *cond; - Block body; -} WhileExpr; - -typedef struct { - Array params; /* declarations of the parameters to this function */ - Array ret_decls; /* array of decls, if this has named return values. otherwise, len & data will be 0. */ - Type ret_type; - Block body; -} FnExpr; /* an expression such as fn(x: int) int { 2 * x } */ - -typedef struct { - Type type; - struct Expression *expr; -} CastExpr; - -#define EXPR_FLAG_FOUND_TYPE 0x01 - -typedef struct Expression { - Location where; - ExprKind kind; - uint16_t flags; - Type type; - union { - Floating floatl; - UInteger intl; - StrLiteral strl; - bool booll; - char charl; - struct { - UnaryOp op; - struct Expression *of; - } unary; - struct { - BinaryOp op; - struct Expression *lhs; - struct Expression *rhs; - } binary; - CallExpr call; - DirectExpr direct; - Identifier ident; - struct { - Type type; - } new; - struct { - Type type; - } del; - IfExpr if_; - WhileExpr while_; - FnExpr fn; - CastExpr cast; - Block block; - }; -} Expression; - -typedef struct { - Location where; - Identifier name; /* NULL = no name */ - Expression val; -} Argument; - -#define DECL_FLAG_ANNOTATES_TYPE 0x01 -#define DECL_FLAG_CONST 0x02 -#define DECL_FLAG_HAS_EXPR 0x04 -#define DECL_FLAG_FOUND_TYPE 0x08 -#define DECL_FLAG_ERRORED_ABOUT_SELF_REFERENCE 0x10 /* has there been an error about this decl referencing itself? */ - -/* OPTIM: Instead of using dynamic arrays, do two passes. */ -typedef struct Declaration { - Location where; - Array idents; - Type type; - uint16_t flags; - Expression expr; - union Value *val; /* only for constant decls. set to NULL here, and to actual value by types.c. */ -} Declaration; - -typedef enum { - STMT_DECL, - STMT_EXPR, - STMT_RET -} StatementKind; - -#define RET_FLAG_EXPR 0x01 -typedef struct { - uint16_t flags; - Expression expr; -} Return; - -#define STMT_FLAG_VOIDED_EXPR 0x01 /* the "4;" in fn () { 4; } is a voided expression, but the "4" in fn () int { 4 } is not */ -typedef struct { - Location where; - StatementKind kind; - unsigned short flags; - union { - Declaration decl; - Expression expr; - Return ret; - }; -} Statement; - -typedef struct { - Array stmts; -} ParsedFile; - -typedef struct { - Tokenizer *tokr; - Allocator allocr; - Block *block; /* which block are we in? NULL = file scope */ -} Parser; - -typedef enum { - DECL_END_SEMICOLON, - DECL_END_RPAREN_COMMA, - DECL_END_LBRACE_COMMA -} DeclEndKind; - static bool parse_expr(Parser *p, Expression *e, Token *end); #define PARSE_DECL_ALLOW_CONST_WITH_NO_EXPR 0x01 |