154 lines
3.5 KiB
C
154 lines
3.5 KiB
C
#ifndef PARSER_H
|
|
#define PARSER_H
|
|
|
|
#include "lexer.h"
|
|
#include "utils.h"
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
typedef enum {
|
|
ParsedExprTypeError,
|
|
ParsedExprTypeId,
|
|
ParsedExprTypeInt,
|
|
ParsedExprTypeFloat,
|
|
ParsedExprTypeChar,
|
|
ParsedExprTypeString,
|
|
ParsedExprTypeBool,
|
|
ParsedExprTypeArray,
|
|
ParsedExprTypeDict,
|
|
ParsedExprTypeIf,
|
|
ParsedExprTypeWhile,
|
|
ParsedExprTypeLoop,
|
|
ParsedExprTypeFor,
|
|
ParsedExprTypeCall,
|
|
ParsedExprTypeAccess,
|
|
ParsedExprTypeIndex,
|
|
ParsedExprTypeUnary,
|
|
ParsedExprTypeBinary,
|
|
ParsedExprTypeAssign,
|
|
ParsedExprTypeRangeFrom,
|
|
ParsedExprTypeRangeTo,
|
|
ParsedExprTypeRangeFromTo,
|
|
} ParsedExprType;
|
|
|
|
typedef enum {
|
|
ParsedBinaryTypeAdd,
|
|
ParsedBinaryTypeSubtract,
|
|
ParsedBinaryTypeMultiply,
|
|
ParsedBinaryTypeDivide,
|
|
ParsedBinaryTypeModulus,
|
|
ParsedBinaryTypeAnd,
|
|
ParsedBinaryTypeOr,
|
|
ParsedBinaryTypeContains,
|
|
ParsedBinaryTypeNotContains,
|
|
} ParsedBinaryType;
|
|
|
|
typedef enum {
|
|
ParsedUnaryTypeNot,
|
|
ParsedUnaryTypeReference,
|
|
ParsedUnaryTypeDereference,
|
|
ParsedUnaryTypeNegate,
|
|
} ParsedUnaryType;
|
|
|
|
typedef enum {
|
|
ParsedAssignTypeAssign,
|
|
ParsedAssignTypeAdd,
|
|
ParsedAssignTypeSubtract,
|
|
ParsedAssignTypeMultiply,
|
|
ParsedAssignTypeDivide,
|
|
ParsedAssignTypeModulus,
|
|
} ParsedAssignType;
|
|
|
|
typedef struct KeyValuePair KeyValuePair;
|
|
typedef struct ParsedExpr ParsedExpr;
|
|
|
|
struct ParsedExpr {
|
|
ParsedExprType type;
|
|
union {
|
|
struct {
|
|
Position position;
|
|
char* message;
|
|
} error;
|
|
struct {
|
|
char* value;
|
|
size_t length;
|
|
} id;
|
|
int64_t int_value;
|
|
double float_value;
|
|
char char_value;
|
|
struct {
|
|
char* value;
|
|
size_t length;
|
|
} string;
|
|
bool bool_value;
|
|
struct {
|
|
ParsedExpr* values;
|
|
size_t length;
|
|
} array;
|
|
struct {
|
|
KeyValuePair* pairs;
|
|
size_t length;
|
|
} dict;
|
|
struct {
|
|
ParsedExpr* condition;
|
|
ParsedExpr* truthy;
|
|
ParsedExpr* falsy;
|
|
} if_node;
|
|
struct {
|
|
ParsedExpr* condition;
|
|
ParsedExpr* body;
|
|
} while_node;
|
|
struct {
|
|
ParsedExpr* body;
|
|
} loop;
|
|
struct {
|
|
ParsedExpr* subject;
|
|
ParsedExpr* value;
|
|
ParsedExpr* body;
|
|
} for_node;
|
|
struct {
|
|
ParsedExpr* subject;
|
|
ParsedExpr* arguments;
|
|
size_t arguments_length;
|
|
} call;
|
|
struct {
|
|
ParsedExpr* subject;
|
|
ParsedExpr* value;
|
|
} access;
|
|
struct {
|
|
ParsedExpr* subject;
|
|
ParsedExpr* value;
|
|
} index;
|
|
};
|
|
};
|
|
|
|
ParsedExpr* parsed_expr_alloc(const ParsedExpr source);
|
|
void parsed_expr_free(ParsedExpr* expr);
|
|
ParsedExpr* parsed_error_expr(Position position, const char* message);
|
|
|
|
struct KeyValuePair {
|
|
char* key;
|
|
size_t key_length;
|
|
ParsedExpr* value;
|
|
};
|
|
|
|
typedef struct {
|
|
const char* text;
|
|
Lexer* lexer;
|
|
Token current;
|
|
} Parser;
|
|
|
|
void parser_create(Parser* parser, const char* text, Lexer* lexer);
|
|
ParsedExpr* parser_parse_expression(Parser* parser);
|
|
ParsedExpr* parser_parse_operand(Parser* parser);
|
|
|
|
bool parser_current_is(const Parser* parser, TokenType type);
|
|
bool parser_done(const Parser* parser);
|
|
Token parser_current(const Parser* parser);
|
|
void parser_step(Parser* parser);
|
|
|
|
AllocatedString alloc_token_string(Token token, const char* text);
|
|
|
|
#endif
|