X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Foberon.c;h=5816c2b3fb0a96f2076f0e6ff82fbeb2b87058d2;hb=51a1ab2543ec5c221d4a3a9ab89968ae7dd39981;hp=6cc5d1d193787867c68e74c0aec302d40a6022e0;hpb=36c1947b3e98abab25f16a0f1c2679125651fe95;p=dsw-obn.git diff --git a/src/oberon.c b/src/oberon.c index 6cc5d1d..5816c2b 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -60,7 +60,11 @@ enum { NIL, IMPORT, REAL, - CHAR + CHAR, + STRING, + IF, + THEN, + ELSE }; // ======================================================================= @@ -126,6 +130,15 @@ oberon_new_type_char(int 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 // ======================================================================= @@ -370,6 +383,18 @@ 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; + } } static void @@ -563,6 +588,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 @@ -676,6 +731,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; @@ -903,11 +964,29 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * // Допускается: // Если классы типов равны // Если INTEGER переводится в REAL + // Есди STRING переводится в ARRAY OF CHAR bool error = false; if(pref -> class != expr -> result -> class) { - if(expr -> result -> class == OBERON_TYPE_INTEGER) + printf("expr class %i\n", expr -> result -> class); + printf("pref class %i\n", pref -> class); + + if(expr -> result -> class == OBERON_TYPE_STRING) + { + if(pref -> class == OBERON_TYPE_ARRAY) + { + if(pref -> base -> class != OBERON_TYPE_CHAR) + { + error = true; + } + } + else + { + error = true; + } + } + else if(expr -> result -> class == OBERON_TYPE_INTEGER) { if(pref -> class != OBERON_TYPE_REAL) { @@ -922,7 +1001,7 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * if(error) { - oberon_error(ctx, "incompatible types"); + oberon_error(ctx, "oberon_autocast_to: incompatible types"); } if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL) @@ -939,6 +1018,7 @@ oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * else if(pref -> class == OBERON_TYPE_RECORD) { oberon_check_record_compatibility(ctx, expr -> result, pref); + expr = oberno_make_record_cast(ctx, expr, pref); } else if(pref -> class == OBERON_TYPE_POINTER) { @@ -1154,6 +1234,7 @@ oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_ar || ((x) == INTEGER) \ || ((x) == REAL) \ || ((x) == CHAR) \ + || ((x) == STRING) \ || ((x) == NIL) \ || ((x) == LPAREN) \ || ((x) == NOT) \ @@ -1478,16 +1559,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, 1); + 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); @@ -1495,12 +1582,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; @@ -1516,7 +1603,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"); @@ -1569,6 +1656,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"); } @@ -2773,6 +2864,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) { @@ -2805,6 +2899,36 @@ 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); + + if(ctx -> token == ELSE) + { + oberon_generate_label(ctx, els); + 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); @@ -2950,6 +3074,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);