diff --git a/src/oberon.c b/src/oberon.c
index 9738056b94124b347e771b1bb541364e83859ae6..59a5c3d5fa7f6f11dc9e65e6b1974f59fca12770 100644 (file)
--- a/src/oberon.c
+++ b/src/oberon.c
// UTILS
// =======================================================================
+static void
+oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args);
+
static void
oberon_error(oberon_context_t * ctx, const char * fmt, ...)
{
@@ -1055,6 +1058,11 @@ oberon_check_record_compatibility(oberon_context_t * ctx, oberon_type_t * from,
static void
oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
{
+ if(dst -> read_only)
+ {
+ oberon_error(ctx, "read-only destination");
+ }
+
if(dst -> is_item == false)
{
oberon_error(ctx, "not variable");
{
oberon_error(ctx, "function result is not type");
}
+ if(typeobj -> type -> class == OBERON_TYPE_RECORD
+ || typeobj -> type -> class == OBERON_TYPE_ARRAY)
+ {
+ oberon_error(ctx, "records or arrays could not be result of function");
+ }
signature -> base = typeobj -> type;
}
}
static void
oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
{
- if(dst -> read_only)
+ if(src -> is_item
+ && src -> item.mode == MODE_STRING
+ && src -> result -> class == OBERON_TYPE_STRING
+ && dst -> result -> class == OBERON_TYPE_ARRAY
+ && dst -> result -> base -> class == OBERON_TYPE_CHAR
+ && dst -> result -> size > 0)
{
- oberon_error(ctx, "read-only destination");
- }
- oberon_check_dst(ctx, dst);
- src = oberon_autocast_to(ctx, src, dst -> result);
- oberon_generate_assign(ctx, src, dst);
+ if(strlen(src -> item.string) < dst -> result -> size)
+ {
+ src -> next = dst;
+ oberon_make_copy_call(ctx, 2, src);
+ }
+ else
+ {
+ oberon_error(ctx, "string too long for destination");
+ }
+ }
+ else
+ {
+ oberon_check_dst(ctx, dst);
+ src = oberon_autocast_to(ctx, src, dst -> result);
+ oberon_generate_assign(ctx, src, dst);
+ }
}
static oberon_expr_t *
index = oberon_ident_item(ctx, iname);
oberon_assert_token(ctx, ASSIGN);
from = oberon_expr(ctx);
- oberon_assign(ctx, from, index);
oberon_assert_token(ctx, TO);
bound = oberon_make_temp_var_item(ctx, index -> result);
to = oberon_expr(ctx);
- oberon_assign(ctx, to, bound);
+ oberon_assign(ctx, to, bound); // сначала temp
+ oberon_assign(ctx, from, index); // потом i
if(ctx -> token == BY)
{
oberon_assert_token(ctx, BY);
@@ -4043,7 +4072,6 @@ oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
oberon_error(ctx, "too few arguments");
}
-
oberon_expr_t * dst;
dst = list_args;
oberon_check_dst(ctx, dst);
@@ -4119,6 +4147,110 @@ oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_
oberon_assign(ctx, src, dst);
}
+static void
+oberon_make_copy_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 * src;
+ src = list_args;
+ oberon_check_src(ctx, src);
+
+ oberon_expr_t * dst;
+ dst = list_args -> next;
+ oberon_check_dst(ctx, dst);
+
+ if(!oberon_is_string_type(src -> result))
+ {
+ oberon_error(ctx, "source must be string or array of char");
+ }
+
+ if(!oberon_is_string_type(dst -> result))
+ {
+ oberon_error(ctx, "dst must be array of char");
+ }
+
+ oberon_generate_copy(ctx, src, dst);
+}
+
+static void
+oberon_make_assert_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 * cond;
+ cond = list_args;
+ oberon_check_src(ctx, cond);
+
+ if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
+ {
+ oberon_error(ctx, "expected boolean");
+ }
+
+ if(num_args == 1)
+ {
+ oberon_generate_assert(ctx, cond);
+ }
+ else
+ {
+ oberon_expr_t * num;
+ num = list_args -> next;
+ oberon_check_src(ctx, num);
+
+ if(num -> result -> class != OBERON_TYPE_INTEGER)
+ {
+ oberon_error(ctx, "expected integer");
+ }
+
+ oberon_check_const(ctx, num);
+
+ oberon_generate_assert_n(ctx, cond, num -> item.integer);
+ }
+}
+
+static void
+oberon_make_halt_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 * num;
+ num = list_args;
+ oberon_check_src(ctx, num);
+
+ if(num -> result -> class != OBERON_TYPE_INTEGER)
+ {
+ oberon_error(ctx, "expected integer");
+ }
+
+ oberon_check_const(ctx, num);
+
+ oberon_generate_halt(ctx, num -> item.integer);
+}
+
static void
oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
{
/* Procedures */
oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
+ oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
+ oberon_new_intrinsic(ctx, "ASSERT", NULL, oberon_make_assert_call);
+ oberon_new_intrinsic(ctx, "HALT", NULL, oberon_make_halt_call);
return ctx;
}