DEADSOFTWARE

Иправлен(?) мемори коррапшн
[dsw-obn.git] / src / oberon.c
index 30f10ec49af929465b3dfa31885ae7bb3b150fbb..0fff8d7ba4c791846d662f1a6e728c6b2fbfe883 100644 (file)
@@ -6,6 +6,9 @@
 #include <assert.h>
 #include <stdbool.h>
 #include <math.h>
+#include <float.h>
+
+#include <gc.h>
 
 #include "../include/oberon.h"
 
@@ -24,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;
@@ -87,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;
@@ -104,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;
@@ -148,6 +151,16 @@ oberon_make_integer(oberon_context_t * ctx, int64_t i)
        return expr;
 }
 
+static oberon_expr_t *
+oberon_make_char(oberon_context_t * ctx, int64_t i)
+{
+       oberon_expr_t * expr;
+       expr = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
+       expr -> item.integer = i;
+       expr -> item.real = i;
+       return expr;
+}
+
 static oberon_expr_t *
 oberon_make_real_typed(oberon_context_t * ctx, double r, oberon_type_t * result)
 {
@@ -186,6 +199,16 @@ oberon_make_set(oberon_context_t * ctx, int64_t i)
        return expr;
 }
 
+static oberon_expr_t *
+oberon_make_set_index(oberon_context_t * ctx, int64_t i)
+{
+       oberon_expr_t * expr;
+       expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
+       expr -> item.integer = 1 << i;
+       expr -> item.real = 1 << i;
+       return expr;
+}
+
 static oberon_expr_t *
 oberon_make_set_range(oberon_context_t * ctx, int64_t x, int64_t y)
 {
@@ -203,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;
@@ -263,7 +289,7 @@ 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);
+       oberon_object_t * newvar = GC_MALLOC(sizeof *newvar);
        memset(newvar, 0, sizeof *newvar);
        newvar -> name = name;
        newvar -> class = class;
@@ -351,7 +377,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;
 
@@ -610,7 +636,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;
 
@@ -720,8 +746,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;
@@ -1227,7 +1254,7 @@ oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
        assert(expr -> is_item);
 
        oberon_expr_t * selector;
-       selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
+       selector = oberon_new_item(MODE_DEREF, expr -> result -> base, false);
        selector -> item.parent = (oberon_item_t *) expr;
 
        return selector;
@@ -1558,7 +1585,7 @@ oberon_element(oberon_context_t * ctx)
        oberon_expr_t * set;
        if(e2 == NULL && oberon_is_const(e1))
        {
-               set = oberon_make_set(ctx, e1 -> item.integer);
+               set = oberon_make_set_index(ctx, e1 -> item.integer);
        }
        else if(e2 != NULL && oberon_is_const(e1) && oberon_is_const(e2))
        {
@@ -1937,11 +1964,6 @@ oberon_simple_expr(oberon_context_t * ctx)
 
        expr = oberon_term_expr(ctx);
 
-       if(minus)
-       {
-               expr = oberon_make_unary_op(ctx, MINUS, expr);
-       }
-
        while(ISADDOP(ctx -> token))
        {
                int token = ctx -> token;
@@ -1951,6 +1973,11 @@ oberon_simple_expr(oberon_context_t * ctx)
                expr = oberon_make_bin_op(ctx, token, expr, inter);
        }
 
+       if(minus)
+       {
+               expr = oberon_make_unary_op(ctx, MINUS, expr);
+       }
+
        return expr;
 }
 
@@ -2919,9 +2946,10 @@ oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
                oberon_object_t * field = type -> decl;
                for(int i = 0; i < num_fields; i++)
                {
-                       oberon_initialize_object(ctx, field);
+                       //oberon_initialize_object(ctx, field);
+                       oberon_initialize_type(ctx, field -> type);
                        field = field -> next;
-               }               
+               }
 
                oberon_generator_init_type(ctx, type);
        }
@@ -3073,7 +3101,8 @@ oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
        oberon_check_dst(ctx, dst);
        oberon_check_assignment_compatible(ctx, src, dst -> result);
 
-       if(oberon_is_string_type(src -> result))
+       if(oberon_is_array_of_char_type(dst -> result)
+               && oberon_is_string_type(src -> result))
        {
                src -> next = dst;
                oberon_make_copy_call(ctx, 2, src);
@@ -3602,57 +3631,6 @@ oberon_parse_module(oberon_context_t * ctx)
 //   LIBRARY
 // =======================================================================
 
-static void
-register_default_types(oberon_context_t * ctx)
-{
-       ctx -> notype_type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
-       oberon_generator_init_type(ctx, ctx -> notype_type);
-
-       ctx -> nil_type = oberon_new_type_ptr(OBERON_TYPE_NIL);
-       oberon_generator_init_type(ctx, ctx -> nil_type);
-
-       ctx -> string_type = oberon_new_type_string(1);
-       oberon_generator_init_type(ctx, ctx -> string_type);
-
-       ctx -> bool_type = oberon_new_type_boolean();
-       oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
-
-       ctx -> char_type = oberon_new_type_char(1);
-       oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
-
-       ctx -> byte_type = oberon_new_type_integer(1);
-       oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> byte_type, 1);
-
-       ctx -> shortint_type = oberon_new_type_integer(2);
-       oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> shortint_type, 1);
-
-       ctx -> int_type = oberon_new_type_integer(4);
-       oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> int_type, 1);
-
-       ctx -> longint_type = oberon_new_type_integer(8);
-       oberon_define_type(ctx -> world_scope, "HUGEINT", ctx -> longint_type, 1);
-
-       ctx -> real_type = oberon_new_type_real(4);
-       oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
-
-       ctx -> longreal_type = oberon_new_type_real(8);
-       oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
-
-       ctx -> set_type = oberon_new_type_set(4);
-       oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
-}
-
-static void
-oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
-{
-       oberon_object_t * proc;
-       proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
-       proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
-       proc -> type -> sysproc = true;
-       proc -> type -> genfunc = f;
-       proc -> type -> genproc = p;
-}
-
 static oberon_expr_t *
 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
 {
@@ -3669,7 +3647,7 @@ oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
        oberon_expr_t * arg;
        arg = list_args;
 
-       if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
+       if(!oberon_is_type_expr(arg))
        {
                oberon_error(ctx, "MIN accept only type");
        }
@@ -3681,6 +3659,15 @@ oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
                case OBERON_TYPE_INTEGER:
                        expr = oberon_make_integer(ctx, -powl(2, bits - 1));
                        break;
+               case OBERON_TYPE_BOOLEAN:
+                       expr = oberon_make_boolean(ctx, false);
+                       break;
+               case OBERON_TYPE_CHAR:
+                       expr = oberon_make_char(ctx, 0);
+                       break;
+               case OBERON_TYPE_REAL:
+                       expr = oberon_make_real_typed(ctx, (bits <= 32) ? (-FLT_MAX) : (-DBL_MAX), arg -> result);
+                       break;
                case OBERON_TYPE_SET:
                        expr = oberon_make_integer(ctx, 0);
                        break;
@@ -3708,7 +3695,7 @@ oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
        oberon_expr_t * arg;
        arg = list_args;
 
-       if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
+       if(!oberon_is_type_expr(arg))
        {
                oberon_error(ctx, "MAX accept only type");
        }
@@ -3720,6 +3707,15 @@ oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
                case OBERON_TYPE_INTEGER:
                        expr = oberon_make_integer(ctx, powl(2, bits - 1) - 1);
                        break;
+               case OBERON_TYPE_BOOLEAN:
+                       expr = oberon_make_boolean(ctx, true);
+                       break;
+               case OBERON_TYPE_CHAR:
+                       expr = oberon_make_char(ctx, powl(2, bits) - 1);
+                       break;
+               case OBERON_TYPE_REAL:
+                       expr = oberon_make_real_typed(ctx, (bits <= 32) ? (FLT_MAX) : (DBL_MAX), arg -> result);
+                       break;
                case OBERON_TYPE_SET:
                        expr = oberon_make_integer(ctx, bits);
                        break;
@@ -3746,8 +3742,7 @@ oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list
 
        oberon_expr_t * arg;
        arg = list_args;
-
-       if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
+       if(!oberon_is_type_expr(arg))
        {
                oberon_error(ctx, "SIZE accept only type");
        }
@@ -3790,19 +3785,158 @@ oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
        arg = list_args;
        oberon_check_src(ctx, arg);
 
-       oberon_type_t * result_type;
-       result_type = arg -> result;
-       
-       if(result_type -> class != OBERON_TYPE_INTEGER)
+       if(oberon_is_number_type(arg -> result))
        {
-               oberon_error(ctx, "ABS accepts only integers");
+               oberon_error(ctx, "ABS accepts only numbers");
        }
 
        oberon_expr_t * expr;
-       expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
+       if(oberon_is_const(arg))
+       {
+               if(oberon_is_real_type(arg -> result))
+               {
+                       double x = arg -> item.real;
+                       expr = oberon_make_real(ctx, fabsl(x), arg -> result);
+               }
+               else
+               {
+                       int64_t x = arg -> item.integer;
+                       expr = oberon_make_integer(ctx, llabs(x));
+               }
+       }
+       else
+       {
+               expr = oberon_new_operator(OP_ABS, arg -> result, arg, NULL);
+       }
        return expr;
 }
 
+static void
+oberon_make_inc_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * dst;
+       dst = list_args;
+       oberon_check_dst(ctx, dst);
+
+       if(!oberon_is_integer_type(dst -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_make_bin_op(ctx, PLUS, dst, oberon_make_integer(ctx, 1));
+       oberon_assign(ctx, expr, dst);
+}
+
+static void
+oberon_make_incl_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 2)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 2)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * dst;
+       dst = list_args;
+       oberon_check_dst(ctx, dst);
+
+       if(!oberon_is_set_type(dst -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * x;
+       x = list_args -> next;
+       oberon_check_src(ctx, x);
+       
+       if(!oberon_is_integer_type(x -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_make_bin_op(ctx, PLUS, dst, oberon_new_operator(OP_RANGE, dst -> result, x, NULL));
+       oberon_assign(ctx, expr, dst);
+}
+
+static void
+oberon_make_excl_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 2)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 2)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * dst;
+       dst = list_args;
+       oberon_check_dst(ctx, dst);
+
+       if(!oberon_is_set_type(dst -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * x;
+       x = list_args -> next;
+       oberon_check_src(ctx, x);
+       
+       if(!oberon_is_integer_type(x -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_make_bin_op(ctx, MINUS, dst, oberon_new_operator(OP_RANGE, dst -> result, x, NULL));
+       oberon_assign(ctx, expr, dst);
+}
+
+static void
+oberon_make_dec_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * dst;
+       dst = list_args;
+       oberon_check_dst(ctx, dst);
+
+       if(!oberon_is_integer_type(dst -> result))
+       {
+               oberon_error(ctx, "expect integer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_make_bin_op(ctx, MINUS, dst, oberon_make_integer(ctx, 1));
+       oberon_assign(ctx, expr, dst);
+}
+
 static void
 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
 {
@@ -3937,7 +4071,7 @@ oberon_make_assert_call(oberon_context_t * ctx, int num_args, oberon_expr_t * li
        cond = list_args;
        oberon_check_src(ctx, cond);
 
-       if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
+       if(!oberon_is_boolean_type(cond -> result))
        {
                oberon_error(ctx, "expected boolean");
        }
@@ -3952,7 +4086,7 @@ oberon_make_assert_call(oberon_context_t * ctx, int num_args, oberon_expr_t * li
                num = list_args -> next;
                oberon_check_src(ctx, num);
 
-               if(num -> result -> class != OBERON_TYPE_INTEGER)
+               if(!oberon_is_integer_type(num -> result))
                {
                        oberon_error(ctx, "expected integer");
                }
@@ -3990,45 +4124,453 @@ oberon_make_halt_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list
        oberon_generate_halt(ctx, num -> item.integer);
 }
 
-static void
-oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
-{
-       oberon_object_t * constant;
-       constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
-       oberon_check_const(ctx, expr);
-       constant -> value = (oberon_item_t *) expr;
-}
-
-oberon_context_t *
-oberon_create_context(ModuleImportCallback import_module)
+static oberon_expr_t *
+oberon_make_ash_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
 {
-       oberon_context_t * ctx = calloc(1, sizeof *ctx);
+       if(num_args < 2)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
 
-       oberon_scope_t * world_scope;
-       world_scope = oberon_open_scope(ctx);
-       ctx -> world_scope = world_scope;
+       if(num_args > 2)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
 
-       ctx -> import_module = import_module;
+       oberon_expr_t * arg1;
+       arg1 = list_args;
+       oberon_check_src(ctx, arg1);
+       if(arg1 -> result -> class != OBERON_TYPE_INTEGER)
+       {
+               oberon_error(ctx, "expected integer");
+       }
 
-       oberon_generator_init_context(ctx);
+       oberon_expr_t * arg2;
+       arg2 = list_args -> next;
+       oberon_check_src(ctx, arg2);
+       if(arg2 -> result -> class != OBERON_TYPE_INTEGER)
+       {
+               oberon_error(ctx, "expected integer");
+       }
 
-       register_default_types(ctx);
+       oberon_expr_t * expr;
+       if(oberon_is_const(arg1) && oberon_is_const(arg2))
+       {
+               int64_t x = arg1 -> item.integer;
+               int64_t y = arg2 -> item.integer;
+               expr = oberon_make_integer(ctx, x * powl(2, y));
+       }
+       else
+       {
+               expr = oberon_new_operator(OP_ASH, arg1 -> result, arg1, arg2);
+       }
 
-       /* Constants */
-       oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
-       oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
+       return expr;
+}
 
-       /* Functions */
-       oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
-       oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
-       oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
-       oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
+static oberon_expr_t *
+oberon_make_cap_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
 
-       /* Procedures */
-       oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
-       oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(!oberon_is_char_type(arg -> result))
+       {
+               oberon_error(ctx, "expected char");
+       }
+
+       oberon_expr_t * expr;
+       if(oberon_is_const(arg))
+       {
+               expr = oberon_make_char(ctx, toupper(arg -> item.integer));
+       }
+       else
+       {
+               expr = oberon_new_operator(OP_CAP, arg -> result, arg, NULL);
+       }
+
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_chr_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(!oberon_is_integer_type(arg -> result))
+       {
+               oberon_error(ctx, "expected integer");
+       }
+
+       oberon_expr_t * expr;
+       if(oberon_is_const(arg))
+       {
+               expr = oberon_make_char(ctx, arg -> item.integer);
+       }
+       else
+       {
+               expr = oberon_cast_expr(ctx, arg, ctx -> char_type);
+       }
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_ord_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(!oberon_is_char_type(arg -> result))
+       {
+               oberon_error(ctx, "expected char");
+       }
+
+       oberon_expr_t * expr;
+       if(oberon_is_const(arg))
+       {
+               expr = oberon_make_integer(ctx, arg -> item.integer);
+       }
+       else
+       {
+               expr = oberon_cast_expr(ctx, arg, ctx -> int_type);
+       }
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_entier_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(!oberon_is_real_type(arg -> result))
+       {
+               oberon_error(ctx, "expected real");
+       }
+
+       oberon_expr_t * expr;
+       if(oberon_is_const(arg))
+       {
+               expr = oberon_make_integer(ctx, floor(arg -> item.real));
+       }
+       else
+       {
+               expr = oberon_new_operator(OP_ENTIER, ctx -> int_type, arg, NULL);
+       }
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_odd_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(!oberon_is_integer_type(arg -> result))
+       {
+               oberon_error(ctx, "expected integer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_make_bin_op(ctx, MOD, arg, oberon_make_integer(ctx, 2));
+       expr = oberon_make_bin_op(ctx, EQUAL, expr, oberon_make_integer(ctx, 1));
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_short_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(arg -> result -> shorter == NULL)
+       {
+               oberon_error(ctx, "already shorter");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_cast_expr(ctx, arg, arg -> result -> shorter);
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_long_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 1)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * arg;
+       arg = list_args;
+       oberon_check_src(ctx, arg);
+
+       if(arg -> result -> longer == NULL)
+       {
+               oberon_error(ctx, "already longer");
+       }
+
+       oberon_expr_t * expr;
+       expr = oberon_cast_expr(ctx, arg, arg -> result -> longer);
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_make_len_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
+{
+       if(num_args < 1)
+       {
+               oberon_error(ctx, "too few arguments");
+       }
+
+       if(num_args > 2)
+       {
+               oberon_error(ctx, "too mach arguments");
+       }
+
+       oberon_expr_t * v;
+       v = list_args;
+       oberon_check_src(ctx, v);
+
+       if(!oberon_is_array_type(v -> result))
+       {
+               oberon_error(ctx, "expected array");
+       }
+
+       int n = 0;
+       if(num_args == 2)
+       {
+               oberon_expr_t * num;
+               num = list_args -> next;
+               oberon_check_src(ctx, num);
+
+               if(!oberon_is_integer_type(num -> result))
+               {
+                       oberon_error(ctx, "expected integer");
+               }
+               oberon_check_const(ctx, num);
+
+               n = num -> item.integer;
+       }
+
+       int dim = 0;
+       oberon_type_t * arr = v -> result;
+       while(arr -> class == OBERON_TYPE_ARRAY)
+       {
+               dim += 1;
+               arr = arr -> base;
+       }
+
+       if(n < 0 || n > dim)
+       {
+               oberon_error(ctx, "not in range 0..%i", dim - 1);
+       }
+
+       assert(v -> is_item);
+
+       oberon_expr_t * expr;
+       expr = oberon_new_item(MODE_LEN, ctx -> int_type, true);
+       expr -> item.parent = (oberon_item_t *) v;
+       expr -> item.integer = n;
+       return expr;    
+}
+
+static void
+oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
+{
+       oberon_object_t * constant;
+       constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
+       oberon_check_const(ctx, expr);
+       constant -> value = (oberon_item_t *) expr;
+}
+
+static void
+register_default_types(oberon_context_t * ctx)
+{
+       ctx -> notype_type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
+       oberon_generator_init_type(ctx, ctx -> notype_type);
+
+       ctx -> nil_type = oberon_new_type_ptr(OBERON_TYPE_NIL);
+       oberon_generator_init_type(ctx, ctx -> nil_type);
+
+       ctx -> string_type = oberon_new_type_string(1);
+       oberon_generator_init_type(ctx, ctx -> string_type);
+
+       ctx -> bool_type = oberon_new_type_boolean();
+       oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
+
+       ctx -> char_type = oberon_new_type_char(1);
+       oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
+
+       ctx -> byte_type = oberon_new_type_integer(1);
+       oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> byte_type, 1);
+
+       ctx -> shortint_type = oberon_new_type_integer(2);
+       oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> shortint_type, 1);
+
+       ctx -> int_type = oberon_new_type_integer(4);
+       oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> int_type, 1);
+
+       ctx -> longint_type = oberon_new_type_integer(8);
+       oberon_define_type(ctx -> world_scope, "HUGEINT", ctx -> longint_type, 1);
+
+       ctx -> real_type = oberon_new_type_real(4);
+       oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
+
+       ctx -> longreal_type = oberon_new_type_real(8);
+       oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
+
+       ctx -> set_type = oberon_new_type_set(4);
+       oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
+
+
+
+       ctx -> byte_type -> shorter = NULL;
+       ctx -> byte_type -> longer = ctx -> shortint_type;
+
+       ctx -> shortint_type -> shorter = ctx -> byte_type;
+       ctx -> shortint_type -> longer = ctx -> int_type;
+
+       ctx -> int_type -> shorter = ctx -> shortint_type;
+       ctx -> int_type -> longer = ctx -> longint_type;
+
+       ctx -> longint_type -> shorter = ctx -> int_type;
+       ctx -> longint_type -> longer = NULL;
+
+       ctx -> real_type -> shorter = NULL;
+       ctx -> real_type -> longer = ctx -> longreal_type;
+
+       ctx -> longreal_type -> shorter = ctx -> real_type;
+       ctx -> longreal_type -> longer = NULL;
+}
+
+static void
+oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
+{
+       oberon_object_t * proc;
+       proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
+       proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
+       proc -> type -> sysproc = true;
+       proc -> type -> genfunc = f;
+       proc -> type -> genproc = p;
+}
+
+oberon_context_t *
+oberon_create_context(ModuleImportCallback import_module)
+{
+       oberon_context_t * ctx = GC_MALLOC(sizeof *ctx);
+       memset(ctx, 0, sizeof *ctx);
+
+       oberon_scope_t * world_scope;
+       world_scope = oberon_open_scope(ctx);
+       ctx -> world_scope = world_scope;
+
+       ctx -> import_module = import_module;
+
+       oberon_generator_init_context(ctx);
+
+       register_default_types(ctx);
+
+       /* Constants */
+       oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
+       oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
+
+       /* Functions */
+       oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
+       oberon_new_intrinsic(ctx, "ASH", oberon_make_ash_call, NULL);
+       oberon_new_intrinsic(ctx, "CAP", oberon_make_cap_call, NULL);
+       oberon_new_intrinsic(ctx, "CHR", oberon_make_chr_call, NULL);
+       oberon_new_intrinsic(ctx, "ENTIER", oberon_make_entier_call, NULL);
+       oberon_new_intrinsic(ctx, "LEN", oberon_make_len_call, NULL);
+       oberon_new_intrinsic(ctx, "LONG", oberon_make_long_call, NULL);
+       oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
+       oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
+       oberon_new_intrinsic(ctx, "ODD", oberon_make_odd_call, NULL);
+       oberon_new_intrinsic(ctx, "ORD", oberon_make_ord_call, NULL);
+       oberon_new_intrinsic(ctx, "SHORT", oberon_make_short_call, NULL);
+       oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
+
+       /* Procedures */
        oberon_new_intrinsic(ctx, "ASSERT", NULL, oberon_make_assert_call);
+       oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
+       oberon_new_intrinsic(ctx, "DEC", NULL, oberon_make_dec_call);
+       oberon_new_intrinsic(ctx, "EXCL", NULL, oberon_make_excl_call);
        oberon_new_intrinsic(ctx, "HALT", NULL, oberon_make_halt_call);
+       oberon_new_intrinsic(ctx, "INC", NULL, oberon_make_inc_call);
+       oberon_new_intrinsic(ctx, "INCL", NULL, oberon_make_incl_call);
+       oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
 
        return ctx;
 }
@@ -4037,7 +4579,6 @@ void
 oberon_destroy_context(oberon_context_t * ctx)
 {
        oberon_generator_destroy_context(ctx);
-       free(ctx);
 }
 
 oberon_module_t *
@@ -4058,7 +4599,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;