simple-lisp/bootstrap/compile.h

114 lines
2.4 KiB
C

#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;
char **required;
size_t noptional;
char **optional;
size_t nkeys;
char **keys;
bool allow_other_keys;
bool has_rest;
char *rest;
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;
bool inherit;
} 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
uint32_t first_available_val;
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);
void load_toplevel_definition(CompileEnvironment *env, AstNode *form,
CompileError **err);
#endif