semos/parser.h

197 lines
3.6 KiB
C
Raw Normal View History

2024-04-02 16:21:48 +01:00
#ifndef PARSER_H
#define PARSER_H
2024-04-02 18:47:16 +01:00
#include <stdbool.h>
2024-04-02 16:21:48 +01:00
#include <stddef.h>
2024-04-02 18:47:16 +01:00
typedef struct {
size_t index;
int line;
int col;
} Pos;
2024-04-02 16:21:48 +01:00
typedef enum {
TokenType_Error,
2024-04-02 18:47:16 +01:00
TokenType_EOF,
2024-04-02 16:21:48 +01:00
TokenType_Id,
TokenType_Int,
TokenType_Not,
TokenType_And,
TokenType_Or,
TokenType_Loop,
TokenType_Fn,
2024-04-02 18:47:16 +01:00
TokenType_Return,
2024-04-02 16:21:48 +01:00
TokenType_Break,
TokenType_LParen,
TokenType_RParen,
TokenType_LBrace,
TokenType_RBrace,
TokenType_LBracket,
TokenType_RBracket,
TokenType_Comma,
TokenType_Semicolon,
TokenType_Plus,
2024-04-02 18:47:16 +01:00
TokenType_PlusEqual,
2024-04-02 16:21:48 +01:00
TokenType_Minus,
2024-04-02 18:47:16 +01:00
TokenType_MinusEqual,
2024-04-02 16:21:48 +01:00
TokenType_Asterisk,
2024-04-02 18:47:16 +01:00
TokenType_AsteriskEqual,
TokenType_EqualEqual,
TokenType_Exclamation,
TokenType_ExclamationEqual,
2024-04-02 16:21:48 +01:00
TokenType_LT,
TokenType_LTE,
2024-04-02 18:47:16 +01:00
TokenType_GT,
2024-04-02 16:21:48 +01:00
TokenType_GTE,
2024-04-02 18:47:16 +01:00
TokenType_Pipe,
TokenType_PipeGT,
2024-04-02 16:21:48 +01:00
} TokenType;
typedef struct {
2024-04-02 18:47:16 +01:00
TokenType token_type;
Pos pos;
2024-04-02 16:21:48 +01:00
size_t length;
} Token;
2024-04-02 18:47:16 +01:00
typedef struct {
const char* text;
size_t text_length;
size_t index;
int line;
int col;
} Lexer;
void lexer_construct(Lexer* lexer, const char* text, size_t text_length);
Token lexer_next(Lexer* lexer);
Token lexer_token(Lexer* lexer, TokenType token_type, Pos pos);
void lexer_step(Lexer* lexer);
bool lexer_done(const Lexer* lexer);
char lexer_current(const Lexer* lexer);
Pos lexer_pos(const Lexer* lexer);
2024-04-02 16:21:48 +01:00
typedef enum {
ASTNodeType_Error,
ASTNodeType_Id,
ASTNodeType_Int,
ASTNodeType_Block,
ASTNodeType_If,
ASTNodeType_Loop,
ASTNodeType_Call,
ASTNodeType_Index,
ASTNodeType_Unary,
ASTNodeType_Binary,
ASTNodeType_Assign,
ASTNodeType_Let,
ASTNodeType_Break,
ASTNodeType_Fn,
} ASTNodeType;
typedef struct ASTNode ASTNode;
typedef struct {
ASTNode** data;
size_t length;
size_t capacity;
} ASTNodeVec;
2024-04-02 18:47:16 +01:00
int ast_node_vec_construct(ASTNodeVec* vec);
2024-04-02 16:21:48 +01:00
void ast_node_vec_destroy(ASTNodeVec* vec);
2024-04-02 18:47:16 +01:00
int ast_node_vec_push(ASTNodeVec* vec, ASTNode* item);
2024-04-02 16:21:48 +01:00
typedef struct {
ASTNode* condition;
ASTNode* truthy;
ASTNode* falsy;
} ASTIfNode;
typedef struct {
ASTNode* body;
} ASTLoopNode;
typedef struct {
ASTNode* subject;
ASTNodeVec args;
} ASTCallNode;
typedef struct {
ASTNode* subject;
ASTNode* value;
} ASTIndexNode;
typedef enum {
UnaryType_Not,
UnaryType_Negate,
} UnaryType;
typedef struct {
UnaryType unary_type;
ASTNode* subject;
} ASTUnaryNode;
typedef enum {
BinaryType_And,
BinaryType_Or,
BinaryType_Add,
BinaryType_Subtract,
BinaryType_Multiply,
BinaryType_EE,
BinaryType_NE,
BinaryType_LT,
BinaryType_GT,
BinaryType_LTE,
BinaryType_GTE,
} BinaryType;
typedef struct {
BinaryType binary_type;
ASTNode* left;
ASTNode* right;
} ASTBinaryNode;
typedef enum {
AssignType_Assign,
AssignType_Add,
AssignType_Subtract,
AssignType_Multiply,
} AssignType;
typedef struct {
AssignType assign_type;
ASTNode* subject;
ASTNode* value;
} ASTAssignNode;
typedef struct {
char* id;
ASTNode* value;
} ASTLetNode;
typedef struct {
char* id;
ASTNodeVec params;
ASTNode* body;
} ASTFnNode;
struct ASTNode {
ASTNodeType node_type;
int line;
union {
char* id_value;
int int_value;
ASTNodeVec statements;
ASTIfNode if_node;
ASTLoopNode loop_node;
ASTCallNode call_node;
ASTIndexNode index_node;
ASTUnaryNode unary_node;
ASTBinaryNode binary_node;
ASTAssignNode assign_node;
ASTLetNode let_node;
ASTFnNode fn_node;
};
};
void ast_node_destroy(ASTNode* node);
#endif