tomo/environment.h

88 lines
2.5 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"
2024-09-13 17:18:08 -07:00
#include "stdlib/tables.h"
2024-02-15 10:43:46 -08:00
typedef struct {
CORD local_typedefs;
2024-02-17 13:56:19 -08:00
CORD staticdefs;
CORD funcs;
2024-02-17 18:04:35 -08:00
CORD typeinfos;
CORD variable_initializers;
CORD function_naming;
2024-02-17 13:56:19 -08:00
} compilation_unit_t;
2024-07-14 11:13:23 -07:00
typedef struct fn_ctx_s {
struct fn_ctx_s *parent;
2024-03-09 13:03:38 -08:00
type_t *return_type;
2024-09-05 11:57:31 -07:00
Table_t *closure_scope;
Table_t *closed_vars;
2024-03-15 10:35:30 -07:00
} fn_ctx_t;
2024-07-04 15:00:01 -07:00
typedef struct deferral_s {
struct deferral_s *next;
struct env_s *defer_env;
ast_t *block;
} deferral_t;
2024-03-15 10:35:30 -07:00
typedef struct loop_ctx_s {
struct loop_ctx_s *next;
2024-07-13 14:17:58 -07:00
const char *loop_name;
ast_list_t *loop_vars;
2024-07-04 15:00:01 -07:00
deferral_t *deferred;
2024-03-15 10:35:30 -07:00
CORD skip_label, stop_label;
} loop_ctx_t;
2024-03-09 13:03:38 -08:00
typedef struct namespace_s {
const char *name;
struct namespace_s *parent;
} namespace_t;
2024-03-21 10:33:10 -07:00
typedef struct env_s {
Table_t *types, *globals, *namespace_bindings, *locals;
// Lookup table for env_t* where the key is:
// - Resolved path for local imports (so that `use ./foo.tm` is the same as `use ./baz/../foo.tm`)
// - Raw 'use' string for module imports
2024-09-05 11:57:31 -07:00
Table_t *imports;
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-07-04 15:00:01 -07:00
deferral_t *deferred;
CORD *libname; // Pointer to currently compiling library name (if any)
namespace_t *namespace;
Closure_t *comprehension_action;
2024-02-15 10:43:46 -08:00
} env_t;
typedef struct {
2024-02-17 13:56:19 -08:00
type_t *type;
union {
CORD code;
2024-03-30 09:14:24 -07:00
void *value;
};
2024-02-15 10:43:46 -08:00
} binding_t;
env_t *new_compilation_unit(CORD *libname);
env_t *load_module_env(env_t *env, ast_t *ast);
CORD namespace_prefix(CORD *libname, namespace_t *ns);
2024-03-09 13:03:38 -08:00
env_t *global_scope(env_t *env);
env_t *namespace_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);
__attribute__((format(printf, 4, 5)))
_Noreturn void compiler_err(file_t *f, const char *start, const char *end, const char *fmt, ...);
2024-02-17 13:56:19 -08:00
binding_t *get_binding(env_t *env, const char *name);
binding_t *get_lang_escape_function(env_t *env, const char *lang_name, type_t *type_to_escape);
2024-02-17 13:56:19 -08:00
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__)
extern type_t *TEXT_TYPE;
extern type_t *RANGE_TYPE;
2024-08-11 11:47:34 -07:00
extern type_t *THREAD_TYPE;
2024-02-15 10:43:46 -08:00
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0