DEADSOFTWARE

JVM: Реализован доступ к локальным переменным ровнем выше из локальных процедур
[dsw-obn.git] / src / oberon.c
index 3b4a3747b904d0dbef91c4bb3f2ee11960e19679..66a3c7025056b195a204386c126024933c4ebca3 100644 (file)
@@ -79,7 +79,8 @@ enum {
        RBRACE,
        DOTDOT,
        CASE,
-       BAR
+       BAR,
+       WITH
 };
 
 // =======================================================================
@@ -467,6 +468,10 @@ oberon_read_ident(oberon_context_t * ctx)
        {
                ctx -> token = CASE;
        }
+       else if(strcmp(ident, "WITH") == 0)
+       {
+               ctx -> token = WITH;
+       }
 }
 
 static void
@@ -1218,13 +1223,21 @@ oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
        {
                if(param -> class == OBERON_CLASS_VAR_PARAM)
                {
+                       if(arg -> result != param -> type)
+                       {
+                               oberon_error(ctx, "incompatible type");
+                       }
                        if(arg -> read_only)
                        {
                                oberon_error(ctx, "assign to read-only var");
                        }
+                       casted[i] = arg;
+               }
+               else
+               {
+                       casted[i] = oberon_autocast_to(ctx, arg, param -> type);
                }
 
-               casted[i] = oberon_autocast_to(ctx, arg, param -> type);
                arg = arg -> next;
                param = param -> next;
        }
@@ -1500,9 +1513,8 @@ oberon_ident_item(oberon_context_t * ctx, char * name)
 }
 
 static oberon_expr_t *
-oberon_designator(oberon_context_t * ctx)
+oberon_qualident_expr(oberon_context_t * ctx)
 {
-       char * name;
        oberon_object_t * var;
        oberon_expr_t * expr;
 
@@ -1538,8 +1550,20 @@ oberon_designator(oberon_context_t * ctx)
                        oberon_error(ctx, "invalid designator");
                        break;
        }
+
        expr -> item.var = var;
 
+       return expr;
+}
+
+static oberon_expr_t *
+oberon_designator(oberon_context_t * ctx)
+{
+       char * name;
+       oberon_expr_t * expr;
+
+       expr = oberon_qualident_expr(ctx);
+
        while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
        {
                switch(ctx -> token)
@@ -2445,13 +2469,20 @@ oberon_proc_decl(oberon_context_t * ctx)
        signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
        oberon_opt_formal_pars(ctx, &signature);        
 
-       oberon_initialize_decl(ctx);
+       //oberon_initialize_decl(ctx);
        oberon_generator_init_type(ctx, signature);
        oberon_close_scope(ctx -> decl);
 
        oberon_object_t * proc;
        proc = oberon_find_object(ctx -> decl, name, 0);
-       if(proc != NULL)
+       if(proc == NULL)
+       {
+               proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
+               proc -> type = signature;
+               proc -> scope = proc_scope;
+               oberon_generator_init_proc(ctx, proc);
+       }
+       else
        {
                if(proc -> class != OBERON_CLASS_PROC)
                {
@@ -2473,16 +2504,15 @@ oberon_proc_decl(oberon_context_t * ctx)
 
                oberon_compare_signatures(ctx, proc -> type, signature);
        }
-       else
+
+       proc_scope -> parent = proc;
+       oberon_object_t * param = proc_scope -> list -> next;
+       while(param)
        {
-               proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
-               proc -> type = signature;
-               proc -> scope = proc_scope;
-               oberon_generator_init_proc(ctx, proc);
+               param -> parent = proc;
+               param = param -> next;
        }
 
-       proc -> scope -> parent = proc;
-
        if(forward == 0)
        {
                proc -> linked = 1;
@@ -3276,6 +3306,72 @@ oberon_case_statement(oberon_context_t * ctx)
        oberon_assert_token(ctx, END);
 }
 
+static void
+oberon_with_guard_do(oberon_context_t * ctx, gen_label_t * end)
+{
+       oberon_expr_t * val;
+       oberon_expr_t * var;
+       oberon_expr_t * type;
+       oberon_expr_t * cond;
+       oberon_expr_t * cast;
+       oberon_type_t * old_type;
+       gen_var_t * old_var;
+       gen_label_t * this_end;
+
+       this_end = oberon_generator_reserve_label(ctx);
+
+       var = oberon_qualident_expr(ctx);
+       oberon_assert_token(ctx, COLON);
+       type = oberon_qualident_expr(ctx);
+       cond = oberon_make_bin_op(ctx, IS, var, type);
+
+       oberon_assert_token(ctx, DO);
+       oberon_generate_branch(ctx, cond, false, this_end);
+
+       /* Сохраняем ссылку во временной переменной */
+       val = oberon_make_temp_var_item(ctx, type -> result);
+       cast = oberno_make_record_cast(ctx, var, type -> result);
+       oberon_assign(ctx, cast, val);
+       /* Подменяем тип у оригинальной переменной */
+       old_type = var -> item.var -> type;
+       var -> item.var -> type = type -> result;
+       /* Подменяем ссылку на переменную */
+       old_var = var -> item.var -> gen_var;
+       var -> item.var -> gen_var = val -> item.var -> gen_var;
+
+       oberon_statement_seq(ctx);
+       oberon_generate_goto(ctx, end);
+       oberon_generate_label(ctx, this_end);
+
+       /* Возвращаем исходное состояние */
+       var -> item.var -> gen_var = old_var;
+       var -> item.var -> type = old_type;
+}
+
+static void
+oberon_with_statement(oberon_context_t * ctx)
+{
+       gen_label_t * end;
+       end = oberon_generator_reserve_label(ctx);
+
+       oberon_assert_token(ctx, WITH);
+       oberon_with_guard_do(ctx, end);
+       while(ctx -> token == BAR)
+       {
+               oberon_assert_token(ctx, BAR);
+               oberon_with_guard_do(ctx, end);
+       }
+
+       if(ctx -> token == ELSE)
+       {
+               oberon_assert_token(ctx, ELSE);
+               oberon_statement_seq(ctx);
+       }
+
+       oberon_generate_label(ctx, end);
+       oberon_assert_token(ctx, END);
+}
+
 static void
 oberon_statement(oberon_context_t * ctx)
 {
@@ -3486,6 +3582,10 @@ oberon_statement(oberon_context_t * ctx)
        {
                oberon_case_statement(ctx);
        }
+       else if(ctx -> token == WITH)
+       {
+               oberon_with_statement(ctx);
+       }
        else if(ctx -> token == RETURN)
        {
                oberon_assert_token(ctx, RETURN);