tomo/environment.h

63 lines
1.6 KiB
C
Raw Normal View History

2024-02-15 10:43:46 -08:00
#pragma once
2024-03-18 09:49:38 -07:00
// Compilation environments
2024-02-15 10:43:46 -08:00
#include <gc/cord.h>
#include "types.h"
#include "builtins/table.h"
typedef struct {
2024-02-17 13:56:19 -08:00
CORD imports;
CORD typedefs;
CORD typecode;
CORD fndefs;
2024-02-17 13:56:19 -08:00
CORD staticdefs;
CORD funcs;
2024-02-17 18:04:35 -08:00
CORD typeinfos;
2024-02-17 13:56:19 -08:00
CORD main;
2024-03-19 11:22:03 -07:00
CORD object_files;
2024-02-17 13:56:19 -08:00
} compilation_unit_t;
2024-03-09 13:03:38 -08:00
typedef struct {
type_t *return_type;
table_t *closure_scope;
table_t *closed_vars;
2024-03-15 10:35:30 -07:00
} fn_ctx_t;
typedef struct loop_ctx_s {
struct loop_ctx_s *next;
2024-03-15 10:45:25 -07:00
const char *loop_name, *key_name, *value_name;
2024-03-15 10:35:30 -07:00
CORD skip_label, stop_label;
} loop_ctx_t;
2024-03-09 13:03:38 -08:00
2024-02-17 13:56:19 -08:00
typedef struct {
table_t *types, *globals, *locals;
2024-03-19 11:22:03 -07:00
table_t *imports; // Map of 'use' name -> env_t*
2024-02-29 10:28:39 -08:00
table_t *type_namespaces; // Map of type name -> namespace table
2024-02-17 13:56:19 -08:00
compilation_unit_t *code;
2024-03-15 10:35:30 -07:00
fn_ctx_t *fn_ctx;
loop_ctx_t *loop_ctx;
2024-03-17 17:40:40 -07:00
CORD file_prefix, scope_prefix;
2024-03-17 12:26:25 -07:00
const char *comprehension_var;
2024-02-15 10:43:46 -08:00
} env_t;
typedef struct {
2024-02-17 13:56:19 -08:00
CORD code;
type_t *type;
2024-02-15 10:43:46 -08:00
} binding_t;
2024-02-17 13:56:19 -08:00
env_t *new_compilation_unit(void);
2024-03-09 13:03:38 -08:00
env_t *global_scope(env_t *env);
2024-02-17 13:56:19 -08:00
env_t *fresh_scope(env_t *env);
2024-03-13 23:37:56 -07:00
env_t *for_scope(env_t *env, ast_t *ast);
env_t *namespace_env(env_t *env, const char *namespace_name);
2024-02-17 13:56:19 -08:00
__attribute__((noreturn))
void compiler_err(file_t *f, const char *start, const char *end, const char *fmt, ...);
binding_t *get_binding(env_t *env, const char *name);
void set_binding(env_t *env, const char *name, binding_t *binding);
2024-03-03 10:04:50 -08:00
binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name);
2024-02-17 13:56:19 -08:00
#define code_err(ast, ...) compiler_err((ast)->file, (ast)->start, (ast)->end, __VA_ARGS__)
2024-02-15 10:43:46 -08:00
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0