DEADSOFTWARE

Исправлены VAR-параметры, добавлены модули Files и Strings
[dsw-obn.git] / src / oberon.c
index 3b8a6b14846e593d9bbaf7b31385993475362a8c..365f47d6a107ac047dbe13e5f3293a5ea138123d 100644 (file)
@@ -8,6 +8,8 @@
 #include <math.h>
 #include <float.h>
 
+#include <gc.h>
+
 #include "../include/oberon.h"
 
 #include "oberon-internals.h"
@@ -25,7 +27,7 @@ oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list
 static oberon_type_t *
 oberon_new_type_ptr(int class)
 {
-       oberon_type_t * x = malloc(sizeof *x);
+       oberon_type_t * x = GC_MALLOC(sizeof *x);
        memset(x, 0, sizeof *x);
        x -> class = class;
        return x;
@@ -88,7 +90,7 @@ static oberon_expr_t *
 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
 {
        oberon_oper_t * operator;
-       operator = malloc(sizeof *operator);
+       operator = GC_MALLOC(sizeof *operator);
        memset(operator, 0, sizeof *operator);
 
        operator -> is_item = 0;
@@ -105,7 +107,7 @@ static oberon_expr_t *
 oberon_new_item(int mode, oberon_type_t * result, int read_only)
 {
        oberon_item_t * item;
-        item = malloc(sizeof *item);
+        item = GC_MALLOC(sizeof *item);
         memset(item, 0, sizeof *item);
 
        item -> is_item = 1;
@@ -224,8 +226,11 @@ oberon_make_set_range(oberon_context_t * ctx, int64_t x, int64_t y)
 static oberon_scope_t *
 oberon_open_scope(oberon_context_t * ctx)
 {
-       oberon_scope_t * scope = calloc(1, sizeof *scope);
-       oberon_object_t * list = calloc(1, sizeof *list);
+       oberon_scope_t * scope = GC_MALLOC(sizeof *scope);
+       memset(scope, 0, sizeof *scope);
+
+       oberon_object_t * list = GC_MALLOC(sizeof *list);
+       memset(list, 0, sizeof *list);
 
        scope -> ctx = ctx;
        scope -> list = list;
@@ -256,6 +261,7 @@ oberon_find_object_in_list(oberon_object_t * list, char * name)
        oberon_object_t * x = list;
        while(x -> next && strcmp(x -> next -> name, name) != 0)
        {
+               printf("inlist: '%s' != '%s'\n", x -> next -> name, name);
                x = x -> next;
        }
        return x -> next;
@@ -284,7 +290,8 @@ oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
 static oberon_object_t *
 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
 {
-       oberon_object_t * newvar = malloc(sizeof *newvar);
+       printf("oberon_create_object: '%s'\n", name);
+       oberon_object_t * newvar = GC_MALLOC(sizeof *newvar);
        memset(newvar, 0, sizeof *newvar);
        newvar -> name = name;
        newvar -> class = class;
@@ -311,6 +318,7 @@ oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export
        oberon_object_t * x = scope -> list;
        while(x -> next && strcmp(x -> next -> name, name) != 0)
        {
+               printf("inlist: '%s' != '%s'\n", x -> next -> name, name);
                x = x -> next;
        }
 
@@ -372,7 +380,7 @@ oberon_read_ident(oberon_context_t * ctx)
                c = ctx -> code[i];
        }
 
-       char * ident = malloc(len + 1);
+       char * ident = GC_MALLOC(len + 1);
        memcpy(ident, &ctx->code[ctx->code_index], len);
        ident[len] = 0;
 
@@ -631,7 +639,7 @@ oberon_read_number(oberon_context_t * ctx)
        }
 
        int len = end_i - start_i;
-       ident = malloc(len + 1);
+       ident = GC_MALLOC(len + 1);
        memcpy(ident, &ctx -> code[start_i], len);
        ident[len] = 0;
 
@@ -741,8 +749,9 @@ static void oberon_read_string(oberon_context_t * ctx)
 
        oberon_get_char(ctx);
 
-       char * string = calloc(1, end - start + 1);
+       char * string = GC_MALLOC(end - start + 1);
        strncpy(string, &ctx -> code[start], end - start);
+       string[end] = 0;
 
        ctx -> token = STRING;
        ctx -> string = string;
@@ -1041,10 +1050,14 @@ oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * p
                cast = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
                cast -> item.integer = expr -> item.string[0];
        }
-       else
+       else if(!oberon_is_some_types(expr -> result, pref))
        {
                cast = oberon_new_operator(OP_CAST, pref, expr, NULL);
        }
+       else
+       {
+               cast = expr;
+       }
 
        return cast;
 }
@@ -4524,7 +4537,8 @@ oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f
 oberon_context_t *
 oberon_create_context(ModuleImportCallback import_module)
 {
-       oberon_context_t * ctx = calloc(1, sizeof *ctx);
+       oberon_context_t * ctx = GC_MALLOC(sizeof *ctx);
+       memset(ctx, 0, sizeof *ctx);
 
        oberon_scope_t * world_scope;
        world_scope = oberon_open_scope(ctx);
@@ -4572,7 +4586,6 @@ void
 oberon_destroy_context(oberon_context_t * ctx)
 {
        oberon_generator_destroy_context(ctx);
-       free(ctx);
 }
 
 oberon_module_t *
@@ -4593,7 +4606,8 @@ oberon_compile_module(oberon_context_t * ctx, const char * newcode)
        module_scope = oberon_open_scope(ctx);
 
        oberon_module_t * module;
-       module = calloc(1, sizeof *module);
+       module = GC_MALLOC(sizeof *module);
+       memset(module, 0, sizeof *module);
        module -> decl = module_scope;
        module -> next = ctx -> module_list;