simple-lisp/bootstrap/ast.h
2024-09-24 07:03:52 -07:00

111 lines
2.1 KiB
C

#ifndef INCLUDED_AST_H
#define INCLUDED_AST_H
#include "parse.h"
#include <stddef.h>
#include <stdint.h>
typedef enum {
AST_TYPE_LIST,
AST_TYPE_SYMBOL,
AST_TYPE_VECTOR,
AST_TYPE_INT,
AST_TYPE_FLOAT,
AST_TYPE_STRING,
AST_TYPE_QUOTE,
AST_TYPE_NULL,
} AstType;
typedef struct {
AstType type;
size_t line;
size_t col;
} AstNode;
typedef struct {
AstNode parent;
size_t nchildren;
AstNode **children;
} AstListNode;
typedef struct {
AstNode parent;
size_t nchildren;
AstNode **children;
} AstVectorNode;
typedef struct {
AstNode parent;
int64_t value;
} AstIntNode;
typedef struct {
AstNode parent;
double value;
} AstFloatNode;
typedef struct {
AstNode parent;
char *value;
size_t length;
} AstStringNode;
typedef struct {
AstNode parent;
bool is_property;
char *name;
size_t name_length;
bool skip_free;
} AstSymbolNode;
typedef enum {
AST_QUOTE_NONE = 0,
AST_QUOTE_COMMA,
AST_QUOTE_SPLICE,
AST_QUOTE_NORM,
AST_QUOTE_BACK,
} AstQuoteType;
typedef struct {
AstNode parent;
AstQuoteType type;
AstNode *form;
} AstQuoteNode;
typedef enum {
AST_ERROR_PARSE,
AST_ERROR_BUILD
} AstErrorType;
typedef struct _AstErrorList {
struct _AstErrorList *next;
AstErrorType type;
union {
ParseError *parse;
struct {
Token token;
size_t off; // from start of token
char *msg;
} build;
};
} AstErrorList;
void ast_init_parser(void);
void ast_deinit_parser(void);
AstNode *ast_next_toplevel(TokenStream *stream, AstErrorList **err);
void destroy_ast_node(void *node);
AstErrorList *ast_error_list_pop(AstErrorList **list);
void ast_error_list_free_one(AstErrorList *list);
void ast_error_list_free_all(AstErrorList *list);
char *ast_escape_string(const char *input, size_t input_len, size_t *out_len);
void ast_prin1_node(AstNode *node, FILE *stream);
char *ast_prin1_node_to_string(AstNode *node, size_t *out_len);
void ast_format_error(AstErrorList *err, const char *file_name, FILE *stream);
#endif