Hash :
32a9b516
Author :
Thomas de Grivel
Date :
2023-02-02T20:14:38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include "binding.h"
#include "buf.h"
#include "buf_file.h"
#include "buf_inspect.h"
#include "env.h"
#include "error_handler.h"
#include "facts.h"
#include "frame.h"
#include "list.h"
#include "module.h"
#include "str.h"
#include "sym.h"
#include "tag.h"
void env_clean (s_env *env)
{
assert(env);
frame_delete_all(env->frame);
error_handler_delete_all(env->error_handler);
facts_clean(&env->facts);
}
void env_error_f (s_env *env, const char *fmt, ...)
{
va_list ap;
s_tag tag;
assert(env);
assert(fmt);
va_start(ap, fmt);
tag.type.type = TAG_STR;
str_init_vf(&tag.data.str, fmt, ap);
va_end(ap);
env_error_tag(env, &tag);
}
void env_error_tag (s_env *env, const s_tag *tag)
{
s_error_handler *error_handler;
assert(env);
assert(tag);
error_handler = env->error_handler;
if (error_handler) {
tag_copy(tag, &error_handler->tag);
error_handler->backtrace = env->backtrace;
env_longjmp(env, &error_handler->jmp_buf);
/* never reached */
return;
}
if (buf_file_is_open(&env->err)) {
buf_write_1(&env->err, "error: ");
buf_inspect_tag(&env->err, tag);
buf_write_1(&env->err, "\n");
return;
}
}
s_tag * env_eval_call_fn (s_env *env, const s_call *call, s_tag *dest)
{
s_arg *args;
s_list *call_args;
s_frame frame;
s_fn *fn;
s_tag tmp;
assert(env);
assert(call);
assert(dest);
fn = call->fn;
assert(fn);
frame_init(&frame, env->frame);
frame.bindings = fn->bindings;
args = fn->args;
call_args = call->arguments;
while (args) {
if (! call_args) {
assert(! "env_eval_call_fn: missing argument");
errx(1, "env_eval_call_fn: missing argument");
return NULL;
}
/* TODO: check type */
env_eval_tag(env, &call_args->tag, &tmp);
frame.bindings = binding_new(args->name, &call_args->tag,
frame.bindings);
args = args->next;
call_args = list_next(call_args);
}
if (call_args) {
assert(! "env_eval_call_fn: too many arguments");
errx(1, "env_eval_call_fn: too many arguments");
return NULL;
}
env->frame = &frame;
env_eval_progn(env, fn->algo, dest);
env->frame = frame_clean(&frame);
return dest;
}
s_tag * env_eval_call_macro (s_env *env, const s_call *call, s_tag *dest)
{
s_tag *expanded;
assert(env);
assert(call);
assert(dest);
(void) env;
(void) call;
(void) expanded;
return dest;
}
const s_tag * env_eval_ident (s_env *env, const s_ident *ident)
{
const s_tag *tag;
assert(env);
assert(ident);
if (! (tag = frame_get(env->frame, ident->sym))) {
assert(! "env_eval_ident: unbound variable");
errx(1, "env_eval_ident: unbound variable");
}
return tag;
}
s_tag * env_eval_progn (s_env *env, const s_list *program, s_tag *dest)
{
s_tag tmp;
assert(env);
assert(program);
assert(dest);
while (program) {
env_eval_tag(env, &program->tag, &tmp);
program = list_next(program);
}
*dest = tmp;
return dest;
}
s_tag * env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
{
switch (tag->type.type) {
case TAG_VOID: return tag_init_void(dest);
case TAG_CALL:
assert(! "env_eval_tag: invalid tag type: TAG_CALL");
errx(1, "env_eval_tag: invalid tag type TAG_CALL");
return NULL;
case TAG_CALL_FN:
return env_eval_call_fn(env, &tag->data.call, dest);
case TAG_CALL_MACRO:
return env_eval_call_macro(env, &tag->data.call, dest);
case TAG_IDENT:
return tag_copy(env_eval_ident(env, &tag->data.ident), dest);
case TAG_BOOL:
case TAG_CHARACTER:
case TAG_F32:
case TAG_F64:
case TAG_FN:
case TAG_INTEGER:
case TAG_LIST:
case TAG_PTAG:
case TAG_QUOTE:
case TAG_S16:
case TAG_S32:
case TAG_S64:
case TAG_S8:
case TAG_STR:
case TAG_SYM:
case TAG_TUPLE:
case TAG_U16:
case TAG_U32:
case TAG_U64:
case TAG_U8:
case TAG_VAR:
return tag_copy(tag, dest);
}
assert(! "env_eval_tag: invalid tag");
errx(1, "env_eval_tag: invalid tag");
return NULL;
}
s_env * env_init (s_env *env)
{
assert(env);
env->error_handler = NULL;
env->frame = NULL;
buf_init_alloc(&env->in, BUF_SIZE);
buf_file_open_r(&env->in, stdin);
buf_init_alloc(&env->out, BUF_SIZE);
buf_file_open_w(&env->out, stdout);
buf_init_alloc(&env->err, BUF_SIZE);
buf_file_open_w(&env->err, stderr);
facts_init(&env->facts);
str_init_1(&env->module_path, NULL, "/home/dx/c/c3-lang/c3/lib");
if (! module_load(&env->c3_module, sym_1("C3"), &env->facts))
return NULL;
env->current_module = &env->c3_module;
return env;
}
void env_longjmp (s_env *env, jmp_buf *jmp_buf)
{
if (env->unwind_protect && *jmp_buf > env->unwind_protect->buf) {
s_unwind_protect *unwind_protect = env->unwind_protect;
while (unwind_protect->next && *jmp_buf > unwind_protect->next->buf) {
unwind_protect->jmp = &unwind_protect->next->buf;
unwind_protect = unwind_protect->next;
}
unwind_protect->jmp = jmp_buf;
longjmp(env->unwind_protect->buf, 1);
}
longjmp(*jmp_buf, 1);
}
s_module * env_module_load (s_env *env, s_module *module,
const s_sym *name, s_facts *facts)
{
s_str path;
assert(env);
assert(module);
assert(name);
assert(facts);
module->name = name;
module->facts = facts;
if (! module_name_path(&env->module_path, name, &path))
return 0;
buf_write_1(&env->out, "module_load ");
buf_write_str(&env->out, &name->str);
buf_write_1(&env->out, " -> ");
buf_write_str(&env->out, &path);
buf_write_s8(&env->out, '\n');
buf_flush(&env->out);
if (facts_load_file(facts, path.ptr.ps8) < 0)
return 0;
return module;
}
void env_pop_error_handler (s_env *env)
{
if (env->error_handler)
env->error_handler = error_handler_delete(env->error_handler);
}
void env_pop_unwind_protect (s_env *env)
{
if (env->unwind_protect)
env->unwind_protect = env->unwind_protect->next;
}
void env_push_error_handler (s_env *env, s_error_handler *error_handler)
{
tag_init_void(&error_handler->tag);
error_handler->next = env->error_handler;
env->error_handler = error_handler;
}
void env_push_unwind_protect (s_env *env,
s_unwind_protect *unwind_protect)
{
unwind_protect->next = env->unwind_protect;
env->unwind_protect = unwind_protect;
}
s_tag * env_unwind_protect (s_env *env, s_tag *protected, s_list *cleanup,
s_tag *dest)
{
s_tag tmp;
s_unwind_protect unwind_protect;
assert(env);
assert(protected);
if (setjmp(unwind_protect.buf)) {
env_pop_unwind_protect(env);
env_eval_progn(env, cleanup, &tmp);
longjmp(*unwind_protect.jmp, 1);
}
env_eval_tag(env, protected, dest);
env_pop_unwind_protect(env);
env_eval_progn(env, cleanup, &tmp);
return dest;
}