simple-lisp/bootstrap/compile.h

114 lines
2.4 KiB
C
Raw Normal View History

2024-09-24 07:03:52 -07:00
#ifndef INCLUDED_COMPILE_H
#define INCLUDED_COMPILE_H
#include "ast.h"
#include "constants.h"
#include <stdbool.h>
#include <sys/types.h>
typedef enum {
COMPILE_FORMAT_ASM,
COMPILE_FORMAT_BIN,
} CompileFormat;
extern CompileFormat COMPILE_FORMAT;
typedef enum {
COMPILE_WARNING,
COMPILE_ERROR,
} CompileErrorType;
typedef struct _CompileError {
struct _CompileError *next;
CompileErrorType type;
// this will be zero if there is no position information
size_t line;
size_t col;
char *message;
char *context;
} CompileError;
CompileError *compile_error_pop(CompileError **err);
void compile_error_free_one(CompileError *err);
void compile_error_free_all(CompileError **err);
void compile_format_error(CompileError *err, const char *file, FILE *stream);
typedef struct {
size_t line;
size_t col;
char *name;
size_t name_len;
size_t nrequired;
2024-09-24 22:24:02 -07:00
char **required;
2024-09-24 07:03:52 -07:00
size_t noptional;
2024-09-24 22:24:02 -07:00
char **optional;
2024-09-24 07:03:52 -07:00
size_t nkeys;
char **keys;
bool allow_other_keys;
bool has_rest;
2024-09-24 22:24:02 -07:00
char *rest;
2024-09-24 07:03:52 -07:00
char *doc;
size_t doc_len;
} FunctionEntry;
void destroy_function_entry(FunctionEntry *entry);
typedef struct {
size_t line;
size_t col;
char *name;
size_t name_len;
bool is_const;
char *doc;
size_t doc_len;
} VariableEntry;
void destroy_variable_entry(VariableEntry *entry);
typedef struct _CompileLexenv {
struct _CompileLexenv *next;
// local variables
struct {
char *name;
LispReg reg;
} *vars;
size_t nvars;
// symbols interned in this lexenv
struct {
char *name;
LispReg reg;
} *symbols;
uint32_t first_avaiable_saved;
size_t nsymbols;
2024-09-24 22:24:02 -07:00
bool inherit;
2024-09-24 07:03:52 -07:00
} CompileLexenv;
void destroy_compile_lexenv(CompileLexenv *lexenv);
typedef struct {
size_t nfuncs;
FunctionEntry *funcs;
size_t nvars;
VariableEntry *vars;
size_t nlexenv;
CompileLexenv *lexenv_stack;
// the minimum var register that is safe to use
2024-09-27 04:50:47 -07:00
uint32_t first_available_val;
2024-09-24 07:03:52 -07:00
AstQuoteType quote;
} CompileEnvironment;
CompileEnvironment *make_compile_environment(void);
void destroy_compile_environment(CompileEnvironment *env);
ssize_t byte_compile_form(CompileEnvironment *env, AstNode *form, FILE *stream,
CompileError **err);
2024-09-27 04:50:47 -07:00
void load_toplevel_definition(CompileEnvironment *env, AstNode *form,
CompileError **err);
2024-09-24 07:03:52 -07:00
#endif