X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Foberon.c;h=e95a9bef396271f68b2dff5a6af06147cc530c09;hb=5b29102a5440d6511087356b9d9579b7501ca1ec;hp=6cc5d1d193787867c68e74c0aec302d40a6022e0;hpb=36c1947b3e98abab25f16a0f1c2679125651fe95;p=dsw-obn.git diff --git a/src/oberon.c b/src/oberon.c index 6cc5d1d..e95a9be 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -60,7 +60,20 @@ enum { NIL, IMPORT, REAL, - CHAR + CHAR, + STRING, + IF, + THEN, + ELSE, + ELSIF, + WHILE, + DO, + REPEAT, + UNTIL, + FOR, + BY, + LOOP, + EXIT }; // ======================================================================= @@ -126,6 +139,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 // ======================================================================= @@ -145,6 +167,7 @@ oberon_open_scope(oberon_context_t * ctx) scope -> local = scope -> up -> local; scope -> parent = scope -> up -> parent; scope -> parent_type = scope -> up -> parent_type; + scope -> exit_label = scope -> up -> exit_label; } ctx -> decl = scope; @@ -189,6 +212,22 @@ oberon_find_object(oberon_scope_t * scope, char * name, bool check_it) return result; } +static oberon_object_t * +oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only) +{ + oberon_object_t * newvar = malloc(sizeof *newvar); + memset(newvar, 0, sizeof *newvar); + newvar -> name = name; + newvar -> class = class; + newvar -> export = export; + newvar -> read_only = read_only; + newvar -> local = scope -> local; + newvar -> parent = scope -> parent; + newvar -> parent_type = scope -> parent_type; + newvar -> module = scope -> ctx -> mod; + return newvar; +} + static oberon_object_t * oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope) { @@ -211,17 +250,8 @@ oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export oberon_error(scope -> ctx, "already defined"); } - oberon_object_t * newvar = malloc(sizeof *newvar); - memset(newvar, 0, sizeof *newvar); - newvar -> name = name; - newvar -> class = class; - newvar -> export = export; - newvar -> read_only = read_only; - newvar -> local = scope -> local; - newvar -> parent = scope -> parent; - newvar -> parent_type = scope -> parent_type; - newvar -> module = scope -> ctx -> mod; - + oberon_object_t * newvar; + newvar = oberon_create_object(scope, name, class, export, read_only); x -> next = newvar; return newvar; @@ -370,6 +400,54 @@ 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; + } + else if(strcmp(ident, "WHILE") == 0) + { + ctx -> token = WHILE; + } + else if(strcmp(ident, "DO") == 0) + { + ctx -> token = DO; + } + else if(strcmp(ident, "REPEAT") == 0) + { + ctx -> token = REPEAT; + } + else if(strcmp(ident, "UNTIL") == 0) + { + ctx -> token = UNTIL; + } + else if(strcmp(ident, "FOR") == 0) + { + ctx -> token = FOR; + } + else if(strcmp(ident, "BY") == 0) + { + ctx -> token = BY; + } + else if(strcmp(ident, "LOOP") == 0) + { + ctx -> token = LOOP; + } + else if(strcmp(ident, "EXIT") == 0) + { + ctx -> token = EXIT; + } } static void @@ -563,6 +641,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 +784,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 +1017,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 +1054,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 +1071,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 +1287,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) \ @@ -1311,6 +1445,26 @@ oberon_qualident(oberon_context_t * ctx, char ** xname, int check) return x; } +static oberon_expr_t * +oberon_ident_item(oberon_context_t * ctx, char * name) +{ + bool read_only; + oberon_object_t * x; + oberon_expr_t * expr; + + x = oberon_find_object(ctx -> decl, name, true); + + read_only = false; + if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC) + { + read_only = true; + } + + expr = oberon_new_item(MODE_VAR, x -> type, read_only); + expr -> item.var = x; + return expr; +} + static oberon_expr_t * oberon_designator(oberon_context_t * ctx) { @@ -1341,7 +1495,7 @@ oberon_designator(oberon_context_t * ctx) expr = oberon_new_item(MODE_VAR, var -> type, read_only); break; case OBERON_CLASS_PROC: - expr = oberon_new_item(MODE_VAR, var -> type, 1); + expr = oberon_new_item(MODE_VAR, var -> type, true); break; default: oberon_error(ctx, "invalid designator"); @@ -1464,6 +1618,17 @@ oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i) } } +static oberon_expr_t * +oberon_integer_item(oberon_context_t * ctx, int64_t i) +{ + oberon_expr_t * expr; + oberon_type_t * result; + result = oberon_get_type_of_int_value(ctx, i); + expr = oberon_new_item(MODE_INTEGER, result, true); + expr -> item.integer = i; + return expr; +} + static oberon_expr_t * oberon_factor(oberon_context_t * ctx) { @@ -1477,17 +1642,21 @@ oberon_factor(oberon_context_t * ctx) expr = oberon_opt_func_parens(ctx, expr); break; case INTEGER: - result = oberon_get_type_of_int_value(ctx, ctx -> integer); - expr = oberon_new_item(MODE_INTEGER, result, 1); - expr -> item.integer = ctx -> integer; + expr = oberon_integer_item(ctx, 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 +1664,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 +1685,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 +1738,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"); } @@ -1761,6 +1934,21 @@ oberon_const_expr(oberon_context_t * ctx) oberon_error(ctx, "const expression are required"); } + switch(expr -> item.mode) + { + case MODE_INTEGER: + case MODE_BOOLEAN: + case MODE_NIL: + case MODE_REAL: + case MODE_CHAR: + case MODE_STRING: + /* accept */ + break; + default: + oberon_error(ctx, "const expression are required"); + break; + } + return (oberon_item_t *) expr; } @@ -2773,6 +2961,25 @@ oberon_decl_seq(oberon_context_t * ctx) oberon_prevent_undeclarated_procedures(ctx); } +static oberon_expr_t * +oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type) +{ + oberon_object_t * x; + oberon_expr_t * expr; + + x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false); + x -> local = true; + x -> type = type; + oberon_generator_init_temp_var(ctx, x); + + expr = oberon_new_item(MODE_VAR, type, false); + expr -> item.var = x; + return expr; +} + +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 +3012,192 @@ 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 == WHILE) + { + gen_label_t * begin; + gen_label_t * end; + oberon_expr_t * cond; + + begin = oberon_generator_reserve_label(ctx); + end = oberon_generator_reserve_label(ctx); + + oberon_assert_token(ctx, WHILE); + oberon_generate_label(ctx, begin); + cond = oberon_expr(ctx); + if(cond -> result -> class != OBERON_TYPE_BOOLEAN) + { + oberon_error(ctx, "condition must be boolean"); + } + oberon_generate_branch(ctx, cond, false, end); + + oberon_assert_token(ctx, DO); + oberon_statement_seq(ctx); + oberon_generate_goto(ctx, begin); + + oberon_assert_token(ctx, END); + oberon_generate_label(ctx, end); + } + else if(ctx -> token == REPEAT) + { + gen_label_t * begin; + oberon_expr_t * cond; + + begin = oberon_generator_reserve_label(ctx); + oberon_generate_label(ctx, begin); + oberon_assert_token(ctx, REPEAT); + + oberon_statement_seq(ctx); + + oberon_assert_token(ctx, UNTIL); + + cond = oberon_expr(ctx); + if(cond -> result -> class != OBERON_TYPE_BOOLEAN) + { + oberon_error(ctx, "condition must be boolean"); + } + + oberon_generate_branch(ctx, cond, true, begin); + } + else if(ctx -> token == FOR) + { + oberon_expr_t * from; + oberon_expr_t * index; + oberon_expr_t * to; + oberon_expr_t * bound; + oberon_expr_t * by; + oberon_expr_t * cond; + oberon_expr_t * count; + gen_label_t * begin; + gen_label_t * end; + char * iname; + int op; + + begin = oberon_generator_reserve_label(ctx); + end = oberon_generator_reserve_label(ctx); + + oberon_assert_token(ctx, FOR); + iname = oberon_assert_ident(ctx); + 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); + if(ctx -> token == BY) + { + oberon_assert_token(ctx, BY); + by = (oberon_expr_t *) oberon_const_expr(ctx); + } + else + { + by = oberon_integer_item(ctx, 1); + } + + if(by -> result -> class != OBERON_TYPE_INTEGER) + { + oberon_error(ctx, "must be integer"); + } + + if(by -> item.integer > 0) + { + op = LEQ; + } + else if(by -> item.integer < 0) + { + op = GEQ; + } + else + { + oberon_error(ctx, "zero step not allowed"); + } + + oberon_assert_token(ctx, DO); + oberon_generate_label(ctx, begin); + cond = oberon_make_bin_op(ctx, op, index, bound); + oberon_generate_branch(ctx, cond, false, end); + oberon_statement_seq(ctx); + count = oberon_make_bin_op(ctx, PLUS, index, by); + oberon_assign(ctx, count, index); + oberon_generate_goto(ctx, begin); + oberon_generate_label(ctx, end); + oberon_assert_token(ctx, END); + } + else if(ctx -> token == LOOP) + { + gen_label_t * begin; + gen_label_t * end; + + begin = oberon_generator_reserve_label(ctx); + end = oberon_generator_reserve_label(ctx); + + oberon_open_scope(ctx); + oberon_assert_token(ctx, LOOP); + oberon_generate_label(ctx, begin); + ctx -> decl -> exit_label = end; + oberon_statement_seq(ctx); + oberon_generate_goto(ctx, begin); + oberon_generate_label(ctx, end); + oberon_assert_token(ctx, END); + oberon_close_scope(ctx -> decl); + } + else if(ctx -> token == EXIT) + { + oberon_assert_token(ctx, EXIT); + if(ctx -> decl -> exit_label == NULL) + { + oberon_error(ctx, "not in LOOP-END"); + } + oberon_generate_goto(ctx, ctx -> decl -> exit_label); + } else if(ctx -> token == RETURN) { oberon_assert_token(ctx, RETURN); @@ -2950,6 +3343,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);