diff --git a/src/oberon.c b/src/oberon.c
index ece881fc44366290a324ac54b55def84b1c9804b..f67d3927dfdb0d72cbd620aeea0f4f606901376b 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");
return expr;
}
-static oberon_expr_t *
-oberon_make_type_guard(oberon_context_t * ctx, oberon_expr_t * expr, oberon_object_t * objtype)
+static void
+oberon_check_type_guard(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * type)
{
- oberon_type_t * type;
-
- if(objtype -> class != OBERON_CLASS_TYPE)
- {
- oberon_error(ctx, "must be type");
- }
- type = objtype -> type;
-
/* Охрана типа применима, если */
/* 1. v - параметр-переменная типа запись, или v - указатель, и если */
/* 2. T - расширение статического типа v */
@@ -1594,7 +1594,8 @@ oberon_make_type_guard(oberon_context_t * ctx, oberon_expr_t * expr, oberon_obje
{
// accept
}
- else if(expr -> result -> class == OBERON_TYPE_POINTER)
+ else if(expr -> result -> class == OBERON_TYPE_POINTER
+ || expr -> result -> class == OBERON_TYPE_RECORD)
{
// accept
}
@@ -1604,6 +1605,20 @@ oberon_make_type_guard(oberon_context_t * ctx, oberon_expr_t * expr, oberon_obje
}
oberon_check_record_compatibility(ctx, type, expr -> result);
+}
+
+static oberon_expr_t *
+oberon_make_type_guard(oberon_context_t * ctx, oberon_expr_t * expr, oberon_object_t * objtype)
+{
+ oberon_type_t * type;
+
+ if(objtype -> class != OBERON_CLASS_TYPE)
+ {
+ oberon_error(ctx, "must be type");
+ }
+ type = objtype -> type;
+
+ oberon_check_type_guard(ctx, expr, type);
return oberno_make_record_cast(ctx, expr, objtype -> type);
}
return expr;
}
-#define ITMAKESBOOLEAN(x) \
- (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
-
-#define ITUSEONLYINTEGER(x) \
- ((x) >= LESS && (x) <= GEQ)
-
-#define ITUSEONLYBOOLEAN(x) \
- (((x) == OR) || ((x) == AND))
-
static void
oberon_autocast_to_real(oberon_context_t * ctx, oberon_expr_t ** e)
{
}
}
+static bool
+oberon_is_numeric_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_INTEGER) || (t -> class == OBERON_TYPE_REAL);
+}
+
+static bool
+oberon_is_char_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_CHAR);
+}
+
+static bool
+oberon_is_string_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_STRING)
+ || (t -> class == OBERON_TYPE_ARRAY && t -> base -> class == OBERON_TYPE_CHAR);
+}
+
+static bool
+oberon_is_boolean_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_BOOLEAN);
+}
+
+static bool
+oberon_is_set_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_SET);
+}
+
+static bool
+oberon_is_pointer_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_POINTER) || (t -> class == OBERON_TYPE_NIL);
+}
+
+static bool
+oberon_is_procedure_type(oberon_type_t * t)
+{
+ return (t -> class == OBERON_TYPE_POINTER) || (t -> class == OBERON_TYPE_NIL);
+}
+
static oberon_expr_t *
oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
{
@@ -1912,66 +1961,82 @@ oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_
}
else if(token == IS)
{
- oberon_type_t * v = a -> result;
- if(v -> class == OBERON_TYPE_POINTER)
- {
- v = v -> base;
- if(v -> class != OBERON_TYPE_RECORD)
- {
- oberon_error(ctx, "must be record");
- }
- }
- else if(v -> class != OBERON_TYPE_RECORD)
- {
- oberon_error(ctx, "must be record");
- }
-
if(b -> is_item == false || b -> item.mode != MODE_TYPE)
{
oberon_error(ctx, "requires type");
}
- oberon_type_t * t = b -> result;
- if(t -> class == OBERON_TYPE_POINTER)
- {
- t = t -> base;
- if(t -> class != OBERON_TYPE_RECORD)
- {
- oberon_error(ctx, "must be record");
- }
- }
- else if(t -> class != OBERON_TYPE_RECORD)
- {
- oberon_error(ctx, "must be record");
- }
-
result = ctx -> bool_type;
+ oberon_check_type_guard(ctx, a, b -> result);
expr = oberon_new_operator(OP_IS, result, a, b);
}
- else if(ITMAKESBOOLEAN(token))
+ else if((token >= EQUAL && token <= GEQ) || token == OR || token == AND)
{
- if(ITUSEONLYINTEGER(token))
+ if(token >= LESS && token <= GEQ)
{
- if(a -> result -> class == OBERON_TYPE_INTEGER
- || b -> result -> class == OBERON_TYPE_INTEGER
- || a -> result -> class == OBERON_TYPE_REAL
- || b -> result -> class == OBERON_TYPE_REAL)
+ if(oberon_is_numeric_type(a -> result) && oberon_is_numeric_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_char_type(a -> result) && oberon_is_char_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_string_type(a -> result) && oberon_is_string_type(b -> result))
+ {
+ // accept
+ }
+ else
+ {
+ oberon_error(ctx, "invalid comparation");
+ }
+ }
+ else if(token == EQUAL || token == NEQ)
+ {
+ if(oberon_is_numeric_type(a -> result) && oberon_is_numeric_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_char_type(a -> result) && oberon_is_char_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_string_type(a -> result) && oberon_is_string_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_boolean_type(a -> result) && oberon_is_boolean_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_set_type(a -> result) && oberon_is_set_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_pointer_type(a -> result) && oberon_is_pointer_type(b -> result))
+ {
+ // accept
+ }
+ else if(oberon_is_procedure_type(a -> result) && oberon_is_procedure_type(b -> result))
{
// accept
}
else
{
- oberon_error(ctx, "used only with numeric types");
+ oberon_error(ctx, "invalid comparation");
}
}
- else if(ITUSEONLYBOOLEAN(token))
+ else if(token == AND || token == OR)
{
- if(a -> result -> class != OBERON_TYPE_BOOLEAN
- || b -> result -> class != OBERON_TYPE_BOOLEAN)
+ if(!oberon_is_boolean_type(a -> result) || !oberon_is_boolean_type(b -> result))
{
- oberon_error(ctx, "used only with boolean type");
+ oberon_error(ctx, "invalid comparation");
}
}
+ else
+ {
+ oberon_error(ctx, "wat");
+ }
oberon_autocast_binary_op(ctx, &a, &b);
result = ctx -> bool_type;
{
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 *
oberon_assert_token(ctx, ELSE);
oberon_statement_seq(ctx);
}
+ else
+ {
+ oberon_generate_trap(ctx, -1);
+ }
oberon_generate_label(ctx, end);
oberon_assert_token(ctx, END);
oberon_assert_token(ctx, ELSE);
oberon_statement_seq(ctx);
}
+ else
+ {
+ oberon_generate_trap(ctx, -2);
+ }
oberon_generate_label(ctx, end);
oberon_assert_token(ctx, END);
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);
@@ -3986,7 +4080,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);
@@ -4062,6 +4155,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;
}