Commit 47d0f01b authored by Chris Müller's avatar Chris Müller
Browse files

Move runtime specific code in its own module

Move several runtime concepts (symbol mapping, exceptions, runtime
initialization) into its own file.
parent 5556ce5e
......@@ -89,6 +89,7 @@ struct org_cherry_value* org_cherry_fixnum(cy_fixnum_t value);
struct org_cherry_value* org_cherry_float(cy_float_t float_value);
struct org_cherry_value* org_cherry_string(const cy_byte_t* string_value);
struct org_cherry_value* org_cherry_char(cy_unicode_t char_value);
struct org_cherry_value* org_cherry_primitive(cy_primitive_t fun_value);
struct org_cherry_value* org_cherry_value_dup(struct org_cherry_value* value);
......
......@@ -5,7 +5,7 @@ set(CORE_SOURCES
value.c
unicode.c
tables.c
exception.c
runtime.c
primitives/core.c
primitives/io.c
primitives/system.c)
......
......@@ -17,8 +17,82 @@
*/
#include "cherry/runtime.h"
#include "cherry/primitives.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <gc.h>
// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------
struct org_cherry_symbollist* org_cherry_global_symbollist;
struct org_cherry_value* org_cherry_emptylist;
struct org_cherry_value* org_cherry_true;
struct org_cherry_value* org_cherry_false;
struct org_cherry_value* org_cherry_symbol_define;
struct org_cherry_value* org_cherry_symbol_let;
struct org_cherry_value* org_cherry_symbol_lambda;
struct org_cherry_value* org_cherry_symbol_if;
struct org_cherry_value* org_cherry_symbol_quote;
struct org_cherry_value* org_cherry_symbol_try;
struct org_cherry_value* org_cherry_symbol_begin;
struct org_cherry_value* org_cherry_symbol_catch;
void
org_cherry_initialize(struct org_cherry_pair* arguments)
{
GC_INIT();
org_cherry_global_symbollist = org_cherry_symbollist();
org_cherry_true = org_cherry_value_alloc();
org_cherry_true->meta.type = CY_BOOLEAN;
org_cherry_true->boolean_value = 1;
org_cherry_false = org_cherry_value_alloc();
org_cherry_false->meta.type = CY_BOOLEAN;
org_cherry_false->boolean_value = 0;
org_cherry_emptylist = org_cherry_value_alloc();
org_cherry_emptylist->meta.type = CY_EMPTYLIST;
org_cherry_symbol_quote = org_cherry_symbol("quote");
org_cherry_symbol_define = org_cherry_symbol("define");
org_cherry_symbol_let = org_cherry_symbol("let");
org_cherry_symbol_lambda = org_cherry_symbol("lambda");
org_cherry_symbol_if = org_cherry_symbol("if");
org_cherry_symbol_begin = org_cherry_symbol("begin");
org_cherry_symbol_catch = org_cherry_symbol("catch");
org_cherry_symbol_try = org_cherry_symbol("try");
}
struct org_cherry_environment*
org_cherry_environment(void)
{
struct org_cherry_environment* env = GC_MALLOC(sizeof(struct org_cherry_environment));
env->mapping = org_cherry_symbollist();
env->exception_stack = 0;
#define proc_to_env(ENV, STR, FUN) \
org_cherry_env_add(ENV, org_cherry_symbol(STR), org_cherry_primitive(FUN))
proc_to_env(env, "println", org_cherry_io_println);
proc_to_env(env, "raise", org_cherry_core_raise);
proc_to_env(env, "type", org_cherry_core_type);
proc_to_env(env, "+", org_cherry_core_add);
proc_to_env(env, "-", org_cherry_core_sub);
proc_to_env(env, "*", org_cherry_core_mul);
proc_to_env(env, "/", org_cherry_core_div);
proc_to_env(env, "exit", org_cherry_system_exit);
return env;
}
static struct org_cherry_exception*
org_cherry_exception(void)
......
......@@ -366,7 +366,7 @@ cy_unicode_t
org_cherry_utf8_get(const cy_byte_t* str)
{
assert(str != 0 && (UTF8_IS_SINGLE(*str) || UTF8_IS_LEAD(*str)));
cy_unicode_t ch = 0;
UTF8_GET_UNSAFE(str, ch);
......@@ -481,8 +481,10 @@ org_cherry_unicode_to_utf8(cy_byte_t* out, cy_unicode_t ch)
ch >>= 6;
}
out[0] = ch | first;
out[length] = '\0';
}
return length;
}
......
......@@ -23,24 +23,6 @@
#include <gc.h>
#include <assert.h>
// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------
struct org_cherry_symbollist* org_cherry_global_symbollist;
struct org_cherry_value* org_cherry_emptylist;
struct org_cherry_value* org_cherry_true;
struct org_cherry_value* org_cherry_false;
struct org_cherry_value* org_cherry_symbol_define;
struct org_cherry_value* org_cherry_symbol_let;
struct org_cherry_value* org_cherry_symbol_lambda;
struct org_cherry_value* org_cherry_symbol_if;
struct org_cherry_value* org_cherry_symbol_quote;
struct org_cherry_value* org_cherry_symbol_try;
struct org_cherry_value* org_cherry_symbol_begin;
struct org_cherry_value* org_cherry_symbol_catch;
// ----------------------------------------------------------------------------
// helper methods
// ----------------------------------------------------------------------------
......@@ -261,6 +243,7 @@ org_cherry_char(cy_unicode_t value)
{
struct org_cherry_value* cy_value = org_cherry_value_alloc();
cy_value->meta.type = CY_CHAR;
cy_value->char_value = value;
return cy_value;
}
......@@ -361,58 +344,6 @@ org_cherry_list_reverse(struct org_cherry_value* xs)
}
void
org_cherry_initialize(struct org_cherry_pair* arguments)
{
GC_INIT();
org_cherry_global_symbollist = org_cherry_symbollist();
org_cherry_true = org_cherry_value_alloc();
org_cherry_true->meta.type = CY_BOOLEAN;
org_cherry_true->boolean_value = 1;
org_cherry_false = org_cherry_value_alloc();
org_cherry_false->meta.type = CY_BOOLEAN;
org_cherry_false->boolean_value = 0;
org_cherry_emptylist = org_cherry_value_alloc();
org_cherry_emptylist->meta.type = CY_EMPTYLIST;
org_cherry_symbol_quote = org_cherry_symbol("quote");
org_cherry_symbol_define = org_cherry_symbol("define");
org_cherry_symbol_let = org_cherry_symbol("let");
org_cherry_symbol_lambda = org_cherry_symbol("lambda");
org_cherry_symbol_if = org_cherry_symbol("if");
org_cherry_symbol_begin = org_cherry_symbol("begin");
org_cherry_symbol_catch = org_cherry_symbol("catch");
org_cherry_symbol_try = org_cherry_symbol("try");
}
struct org_cherry_environment*
org_cherry_environment(void)
{
struct org_cherry_environment* env = GC_MALLOC(sizeof(struct org_cherry_environment));
env->mapping = org_cherry_symbollist();
env->exception_stack = 0;
#define proc_to_env(ENV, STR, FUN) \
org_cherry_env_add(ENV, org_cherry_symbol(STR), org_cherry_primitive(FUN))
proc_to_env(env, "println", org_cherry_io_println);
proc_to_env(env, "raise", org_cherry_core_raise);
proc_to_env(env, "type", org_cherry_core_type);
proc_to_env(env, "+", org_cherry_core_add);
proc_to_env(env, "-", org_cherry_core_sub);
proc_to_env(env, "*", org_cherry_core_mul);
proc_to_env(env, "/", org_cherry_core_div);
proc_to_env(env, "exit", org_cherry_system_exit);
return env;
}
// ----------------------------------------------------------------------------
......@@ -442,6 +373,7 @@ void
org_cherry_print(FILE* out, struct org_cherry_value* value)
{
const cy_byte_t* p;
cy_byte_t c[8];
switch(value->meta.type) {
case CY_EMPTYLIST:
......@@ -490,7 +422,8 @@ org_cherry_print(FILE* out, struct org_cherry_value* value)
fprintf(out, "vtab");
break;
default:
fprintf(out, "%c", value->char_value);
org_cherry_unicode_to_utf8(c, value->char_value);
fprintf(out, "%s", c);
}
break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment