code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(90 lines)
1 // Compilation environments
3 #pragma once
5 #include "stdlib/datatypes.h"
6 #include "stdlib/print.h" // IWYU pragma: export
7 #include "stdlib/stdlib.h" // IWYU pragma: export
8 #include "types.h"
10 typedef struct {
11 Text_t local_typedefs;
12 Text_t staticdefs;
13 Text_t lambdas;
14 Text_t variable_initializers;
15 } compilation_unit_t;
17 typedef struct deferral_s {
18 struct deferral_s *next;
19 struct env_s *defer_env;
20 ast_t *block;
21 } deferral_t;
23 typedef struct loop_ctx_s {
24 struct loop_ctx_s *next;
25 const char *loop_name;
26 ast_list_t *loop_vars;
27 deferral_t *deferred;
28 Text_t skip_label, stop_label;
29 } loop_ctx_t;
31 typedef struct namespace_s {
32 const char *name;
33 List_t constructors;
34 struct namespace_s *parent;
35 } namespace_t;
37 typedef struct env_s {
38 Table_t *types, *globals, *namespace_bindings, *locals;
39 // Lookup table for env_t* where the key is:
40 // - Resolved path for local imports (so that `use ./foo.tm` is the same as `use ./baz/../foo.tm`)
41 // - Raw 'use' string for module imports
42 namespace_t *namespace;
43 Text_t id_suffix;
44 Table_t *imports;
45 compilation_unit_t *code;
46 ast_t *fn;
47 loop_ctx_t *loop_ctx;
48 deferral_t *deferred;
49 Closure_t *comprehension_action;
50 bool do_source_mapping : 1;
51 type_t *current_type;
52 } env_t;
54 typedef struct {
55 type_t *type;
56 Text_t code;
57 } binding_t;
59 env_t *global_env(bool source_mapping);
60 env_t *load_module_env(env_t *env, ast_t *ast);
61 env_t *get_namespace_by_type(env_t *env, type_t *t);
62 env_t *fresh_scope(env_t *env);
63 env_t *for_scope(env_t *env, ast_t *ast);
64 env_t *with_enum_scope(env_t *env, type_t *t);
65 env_t *namespace_env(env_t *env, const char *namespace_name);
66 #define compiler_err(f, start, end, ...) \
67 ({ \
68 file_t *_f = f; \
69 if (USE_COLOR) fputs("\x1b[31;7;1m ", stderr); \
70 if (_f && start && end) \
71 fprint_inline(stderr, _f->relative_filename, ":", get_line_number(_f, start), ".", \
72 get_line_column(_f, start), ": "); \
73 fprint_inline(stderr, __VA_ARGS__); \
74 if (USE_COLOR) fputs(" \x1b[m", stderr); \
75 fputs("\n\n", stderr); \
76 if (_f && start && end) highlight_error(_f, start, end, "\x1b[31;1m", 2, USE_COLOR); \
77 if (getenv("TOMO_STACKTRACE")) print_stacktrace(stderr, 1); \
78 raise(SIGABRT); \
79 exit(1); \
80 })
81 binding_t *get_binding(env_t *env, const char *name);
82 binding_t *get_constructor(env_t *env, type_t *t, arg_ast_t *args, bool allow_underscores);
83 PUREFUNC binding_t *get_metamethod_binding(env_t *env, ast_e tag, ast_t *lhs, ast_t *rhs, type_t *ret);
84 void set_binding(env_t *env, const char *name, type_t *type, Text_t code);
85 binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name);
86 #define code_err(ast, ...) compiler_err((ast)->file, (ast)->start, (ast)->end, __VA_ARGS__)
87 extern type_t *TEXT_TYPE;
88 extern type_t *PATH_TYPE;
89 extern type_t *PRESENT_TYPE;
90 extern type_t *RESULT_TYPE;