DEADSOFTWARE

Добавлена конструкция ELSIF-THEN
[dsw-obn.git] / src / oberon.c
index 570db39df1417efe4ffde397dc3f600ab2cc9e22..b428ecf73f6bda15c85365a8d49a5dd6aafb45ad 100644 (file)
@@ -59,7 +59,13 @@ enum {
        UPARROW,
        NIL,
        IMPORT,
-       REAL
+       REAL,
+       CHAR,
+       STRING,
+       IF,
+       THEN,
+       ELSE,
+       ELSIF
 };
 
 // =======================================================================
@@ -116,6 +122,24 @@ oberon_new_type_real(int size)
        return x;
 }
 
+static oberon_type_t *
+oberon_new_type_char(int size)
+{
+       oberon_type_t * x;
+       x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
+       x -> size = size;
+       return x;
+}
+
+static oberon_type_t *
+oberon_new_type_string(int size)
+{
+       oberon_type_t * x;
+       x = oberon_new_type_ptr(OBERON_TYPE_STRING);
+       x -> size = size;
+       return x;
+}
+
 // =======================================================================
 //   TABLE
 // ======================================================================= 
@@ -360,6 +384,22 @@ oberon_read_ident(oberon_context_t * ctx)
        {
                ctx -> token = IS;
        }
+       else if(strcmp(ident, "IF") == 0)
+       {
+               ctx -> token = IF;
+       }
+       else if(strcmp(ident, "THEN") == 0)
+       {
+               ctx -> token = THEN;
+       }
+       else if(strcmp(ident, "ELSE") == 0)
+       {
+               ctx -> token = ELSE;
+       }
+       else if(strcmp(ident, "ELSIF") == 0)
+       {
+               ctx -> token = ELSIF;
+       }
 }
 
 static void
@@ -377,6 +417,7 @@ oberon_read_number(oberon_context_t * ctx)
         * mode = 1 == HEX
         * mode = 2 == REAL
         * mode = 3 == LONGREAL
+        * mode = 4 == CHAR
         */
        int mode = 0;
        start_i = ctx -> code_index;
@@ -398,11 +439,20 @@ oberon_read_number(oberon_context_t * ctx)
 
                end_i = ctx -> code_index;
 
-               if(ctx -> c != 'H')
+               if(ctx -> c == 'H')
+               {
+                       mode = 1;
+                       oberon_get_char(ctx);
+               }
+               else if(ctx -> c == 'X')
+               {
+                       mode = 4;
+                       oberon_get_char(ctx);
+               }
+               else
                {
                        oberon_error(ctx, "invalid hex number");
                }
-               oberon_get_char(ctx);
        }
        else if(ctx -> c == '.')
        {
@@ -440,6 +490,20 @@ oberon_read_number(oberon_context_t * ctx)
                end_i = ctx -> code_index;
        }
 
+       if(mode == 0)
+       {
+               if(ctx -> c == 'H')
+               {
+                       mode = 1;
+                       oberon_get_char(ctx);
+               }
+               else if(ctx -> c == 'X')
+               {
+                       mode = 4;
+                       oberon_get_char(ctx);
+               }
+       }
+
        int len = end_i - start_i;
        ident = malloc(len + 1);
        memcpy(ident, &ctx -> code[start_i], len);
@@ -470,6 +534,11 @@ oberon_read_number(oberon_context_t * ctx)
                        sscanf(ident, "%lf", &real);
                        ctx -> token = REAL;
                        break;
+               case 4:
+                       sscanf(ident, "%lx", &integer);
+                       real = integer;
+                       ctx -> token = CHAR;
+                       break;
                default:
                        oberon_error(ctx, "oberon_read_number: wat");
                        break;
@@ -524,6 +593,36 @@ oberon_read_comment(oberon_context_t * ctx)
        }
 }
 
+static void oberon_read_string(oberon_context_t * ctx)
+{
+       int c = ctx -> c;
+       oberon_get_char(ctx);
+
+       int start = ctx -> code_index;
+
+       while(ctx -> c != 0 && ctx -> c != c)
+       {
+               oberon_get_char(ctx);
+       }
+
+       if(ctx -> c == 0)
+       {
+               oberon_error(ctx, "unterminated string");
+       }
+
+       int end = ctx -> code_index;
+
+       oberon_get_char(ctx);
+
+       char * string = calloc(1, end - start + 1);
+       strncpy(string, &ctx -> code[start], end - start);
+
+       ctx -> token = STRING;
+       ctx -> string = string;
+
+       printf("oberon_read_string: string ((%s))\n", string);
+}
+
 static void oberon_read_token(oberon_context_t * ctx);
 
 static void
@@ -637,6 +736,12 @@ oberon_read_symbol(oberon_context_t * ctx)
                        ctx -> token = UPARROW;
                        oberon_get_char(ctx);
                        break;
+               case '"':
+                       oberon_read_string(ctx);
+                       break;
+               case '\'':
+                       oberon_read_string(ctx);
+                       break;                  
                default:
                        oberon_error(ctx, "invalid char %c", ctx -> c);
                        break;
@@ -673,6 +778,7 @@ static void oberon_assert_token(oberon_context_t * ctx, int token);
 static char * oberon_assert_ident(oberon_context_t * ctx);
 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
+static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
 
 static oberon_expr_t *
 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
@@ -746,7 +852,14 @@ oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first,
        oberon_expr_t * last;
 
        *num_expr = 1;
-       *first = last = oberon_expr(ctx);
+       if(const_expr)
+       {
+               *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
+       }
+       else
+       {
+               *first = last = oberon_expr(ctx);
+       }
        while(ctx -> token == COMMA)
        {
                oberon_assert_token(ctx, COMMA);
@@ -776,8 +889,19 @@ oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * p
 static oberon_expr_t *
 oberno_make_record_cast(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * rec)
 {
-       if(expr -> result -> class != OBERON_TYPE_RECORD
-               || rec -> class != OBERON_TYPE_RECORD)
+       oberon_type_t * from = expr -> result;
+       oberon_type_t * to = rec;
+
+       printf("oberno_make_record_cast: from class %i to class %i\n", from -> class, to -> class);
+
+       if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
+       {
+               printf("oberno_make_record_cast: pointers\n");
+               from = from -> base;
+               to = to -> base;
+       }
+
+       if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
        {
                oberon_error(ctx, "must be record type");
        }
@@ -813,39 +937,78 @@ oberon_get_equal_expr_type(oberon_context_t * ctx, oberon_type_t * a, oberon_typ
        return result;
 }
 
+static void
+oberon_check_record_compatibility(oberon_context_t * ctx, oberon_type_t * from, oberon_type_t * to)
+{
+       if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
+       {
+               from = from -> base;
+               to = to -> base;
+       }
+
+       if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
+       {
+               oberon_error(ctx, "not a record");
+       }
+
+       oberon_type_t * t = from;
+       while(t != NULL && t != to)
+       {
+               t = t -> base;
+       }
+
+       if(t == NULL)
+       {
+               oberon_error(ctx, "incompatible record types");
+       }
+}
+
 static oberon_expr_t *
 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
 {
+       // Допускается:
+       //  Если классы типов равны
+       //  Если INTEGER переводится в REAL
+       //  Есди STRING переводится в ARRAY OF CHAR
+
+       bool error = false;
        if(pref -> class != expr -> result -> class)
        {
-               if(pref -> class == OBERON_TYPE_POINTER)
+               printf("expr class %i\n", expr -> result -> class);
+               printf("pref class %i\n", pref -> class);
+
+               if(expr -> result -> class == OBERON_TYPE_STRING)
                {
-                       if(expr -> result -> class == OBERON_TYPE_POINTER)
+                       if(pref -> class == OBERON_TYPE_ARRAY)
                        {
-                               // accept
+                               if(pref -> base -> class != OBERON_TYPE_CHAR)
+                               {
+                                       error = true;
+                               }
                        }
                        else
                        {
-                               oberon_error(ctx, "incompatible types");
+                               error = true;
                        }
                }
-               else if(pref -> class == OBERON_TYPE_REAL)
+               else if(expr -> result -> class == OBERON_TYPE_INTEGER)
                {
-                       if(expr -> result -> class == OBERON_TYPE_INTEGER)
-                       {
-                               // accept
-                       }
-                       else
+                       if(pref -> class != OBERON_TYPE_REAL)
                        {
-                               oberon_error(ctx, "incompatible types");
+                               error = true;
                        }
                }
                else
                {
-                       oberon_error(ctx, "incompatible types");
+                       error = true;
                }
        }
 
+       if(error)
+       {
+               oberon_error(ctx, "oberon_autocast_to: incompatible types");
+       }
+
        if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
        {
                if(expr -> result -> size > pref -> size)
@@ -859,24 +1022,18 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t *
        }
        else if(pref -> class == OBERON_TYPE_RECORD)
        {
-               oberon_type_t * t = expr -> result;
-               while(t != NULL && t != pref)
-               {
-                       t = t -> base;
-               }
-               if(t == NULL)
-               {
-                       printf("oberon_autocast_to: rec %p != %p\n", expr -> result, pref);
-                       oberon_error(ctx, "incompatible record types");
-               }
-               if(expr -> result != pref)
-               {
-                       expr = oberno_make_record_cast(ctx, expr, pref);
-               }
+               oberon_check_record_compatibility(ctx, expr -> result, pref);
+               expr = oberno_make_record_cast(ctx, expr, pref);
        }
        else if(pref -> class == OBERON_TYPE_POINTER)
        {
-               if(expr -> result -> base != pref -> base)
+               assert(pref -> base);
+               if(expr -> result -> base -> class == OBERON_TYPE_RECORD)
+               {
+                       oberon_check_record_compatibility(ctx, expr -> result, pref);
+                       expr = oberno_make_record_cast(ctx, expr, pref);
+               }
+               else if(expr -> result -> base != pref -> base)
                {
                        if(expr -> result -> base -> class != OBERON_TYPE_VOID)
                        {
@@ -1080,14 +1237,19 @@ oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_ar
        || ((x) == MINUS) \
        || ((x) == IDENT) \
        || ((x) == INTEGER) \
+       || ((x) == REAL) \
+       || ((x) == CHAR) \
+       || ((x) == STRING) \
+       || ((x) == NIL) \
        || ((x) == LPAREN) \
        || ((x) == NOT) \
        || ((x) == TRUE) \
-       || ((x) == FALSE))
+       || ((x) == FALSE)) 
 
 static oberon_expr_t *
 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
 {
+       printf("oberno_make_dereferencing\n");
        if(expr -> result -> class != OBERON_TYPE_POINTER)
        {
                oberon_error(ctx, "not a pointer");
@@ -1273,8 +1435,7 @@ oberon_designator(oberon_context_t * ctx)
        }
        expr -> item.var = var;
 
-       bool brk = false;
-       while(brk == false && ISSELECTOR(ctx -> token))
+       while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
        {
                switch(ctx -> token)
                {
@@ -1301,11 +1462,6 @@ oberon_designator(oberon_context_t * ctx)
                                expr = oberno_make_dereferencing(ctx, expr);
                                break;
                        case LPAREN:
-                               if(expr -> result -> class == OBERON_TYPE_PROCEDURE)
-                               {
-                                       brk = true;
-                                       break;
-                               }
                                oberon_assert_token(ctx, LPAREN);
                                oberon_object_t * objtype = oberon_qualident(ctx, NULL, 1);
                                if(objtype -> class != OBERON_CLASS_TYPE)
@@ -1320,14 +1476,13 @@ oberon_designator(oberon_context_t * ctx)
                                break;
                }
        }
+
        return expr;
 }
 
 static oberon_expr_t *
 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
 {
-       assert(expr -> is_item == 1);
-
        /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
        if(ctx -> token == LPAREN)
        {
@@ -1341,6 +1496,7 @@ oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
                        oberon_expr_list(ctx, &num_args, &arguments, 0);
                }
 
+               assert(expr -> is_item == 1);
                expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
 
                oberon_assert_token(ctx, RPAREN);
@@ -1352,7 +1508,7 @@ oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
 static void
 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
 {
-       assert(expr -> is_item == 1);
+       assert(expr -> is_item);
 
        int num_args = 0;
        oberon_expr_t * arguments = NULL;
@@ -1408,10 +1564,22 @@ oberon_factor(oberon_context_t * ctx)
                        break;
                case INTEGER:
                        result = oberon_get_type_of_int_value(ctx, ctx -> integer);
-                       expr = oberon_new_item(MODE_INTEGER, result, 1);
+                       expr = oberon_new_item(MODE_INTEGER, result, true);
                        expr -> item.integer = ctx -> integer;
                        oberon_assert_token(ctx, INTEGER);
                        break;
+               case CHAR:
+                       result = ctx -> char_type;
+                       expr = oberon_new_item(MODE_CHAR, result, true);
+                       expr -> item.integer = ctx -> integer;
+                       oberon_assert_token(ctx, CHAR);
+                       break;
+               case STRING:
+                       result = ctx -> string_type;
+                       expr = oberon_new_item(MODE_STRING, result, true);
+                       expr -> item.string = ctx -> string;
+                       oberon_assert_token(ctx, STRING);
+                       break;
                case REAL:
                        result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
                        expr = oberon_new_item(MODE_REAL, result, 1);
@@ -1419,12 +1587,12 @@ oberon_factor(oberon_context_t * ctx)
                        oberon_assert_token(ctx, REAL);
                        break;
                case TRUE:
-                       expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
+                       expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
                        expr -> item.boolean = true;
                        oberon_assert_token(ctx, TRUE);
                        break;
                case FALSE:
-                       expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
+                       expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
                        expr -> item.boolean = false;
                        oberon_assert_token(ctx, FALSE);
                        break;
@@ -1440,7 +1608,7 @@ oberon_factor(oberon_context_t * ctx)
                        break;
                case NIL:
                        oberon_assert_token(ctx, NIL);
-                       expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, 1);
+                       expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, true);
                        break;
                default:
                        oberon_error(ctx, "invalid expression");
@@ -1493,6 +1661,10 @@ oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_
                                || b -> result -> class == OBERON_TYPE_INTEGER
                                || a -> result -> class == OBERON_TYPE_REAL
                                || b -> result -> class == OBERON_TYPE_REAL)
+                       {
+                               // accept
+                       }
+                       else
                        {
                                oberon_error(ctx, "used only with numeric types");
                        }
@@ -2176,13 +2348,19 @@ oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
                        oberon_error(ctx, "base must be type");
                }
 
-               if(typeobj -> type -> class != OBERON_TYPE_RECORD)
+               oberon_type_t * base = typeobj -> type;
+               if(base -> class == OBERON_TYPE_POINTER)
+               {
+                       base = base -> base;
+               }
+
+               if(base -> class != OBERON_TYPE_RECORD)
                {
                        oberon_error(ctx, "base must be record type");
                }
 
-               rec -> base = typeobj -> type;
-               ctx -> decl = rec -> base -> scope;
+               rec -> base = base;
+               ctx -> decl = base -> scope;
 
                oberon_assert_token(ctx, RPAREN);
        }
@@ -2691,6 +2869,9 @@ oberon_decl_seq(oberon_context_t * ctx)
        oberon_prevent_undeclarated_procedures(ctx);
 }
 
+static void
+oberon_statement_seq(oberon_context_t * ctx);
+
 static void
 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
 {
@@ -2723,6 +2904,53 @@ oberon_statement(oberon_context_t * ctx)
                        oberon_opt_proc_parens(ctx, item1);
                }
        }
+       else if(ctx -> token == IF)
+       {
+               gen_label_t * end;
+               gen_label_t * els;
+               oberon_expr_t * cond;
+
+               els = oberon_generator_reserve_label(ctx);
+               end = oberon_generator_reserve_label(ctx);
+
+               oberon_assert_token(ctx, IF);
+               cond = oberon_expr(ctx);
+               if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
+               {
+                       oberon_error(ctx, "condition must be boolean");
+               }
+               oberon_assert_token(ctx, THEN);
+               oberon_generate_branch(ctx, cond, false, els);
+               oberon_statement_seq(ctx);
+               oberon_generate_goto(ctx, end);
+               oberon_generate_label(ctx, els);
+
+               while(ctx -> token == ELSIF)
+               {
+                       els = oberon_generator_reserve_label(ctx);
+
+                       oberon_assert_token(ctx, ELSIF);
+                       cond = oberon_expr(ctx);
+                       if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
+                       {
+                               oberon_error(ctx, "condition must be boolean");
+                       }
+                       oberon_assert_token(ctx, THEN);
+                       oberon_generate_branch(ctx, cond, false, els);
+                       oberon_statement_seq(ctx);
+                       oberon_generate_goto(ctx, end);
+                       oberon_generate_label(ctx, els);
+               }
+
+               if(ctx -> token == ELSE)
+               {
+                       oberon_assert_token(ctx, ELSE);
+                       oberon_statement_seq(ctx);
+               }
+
+               oberon_generate_label(ctx, end);
+               oberon_assert_token(ctx, END);          
+       }
        else if(ctx -> token == RETURN)
        {
                oberon_assert_token(ctx, RETURN);
@@ -2868,6 +3096,9 @@ register_default_types(oberon_context_t * ctx)
        ctx -> void_ptr_type -> base = ctx -> void_type;
        oberon_generator_init_type(ctx, ctx -> void_ptr_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);
 
@@ -2888,6 +3119,9 @@ register_default_types(oberon_context_t * ctx)
 
        ctx -> longreal_type = oberon_new_type_real(8);
        oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
+
+       ctx -> char_type = oberon_new_type_char(1);
+       oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
 }
 
 static void