Changeset 074444f in mainline
- Timestamp:
- 2010-04-10T11:15:33Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1ef0fc3, 38aaacc2
- Parents:
- 23de644
- Location:
- uspace
- Files:
-
- 6 added
- 28 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/Makefile
r23de644 r074444f 50 50 src/p_type.c \ 51 51 src/parse.c \ 52 src/program.c \ 52 53 src/rdata.c \ 53 54 src/run.c \ -
uspace/app/sbi/src/builtin/bi_fun.c
r23de644 r074444f 99 99 { 100 100 rdata_var_t *var; 101 int char_val; 102 int rc; 101 103 102 104 #ifdef DEBUG_RUN_TRACE … … 107 109 108 110 switch (var->vc) { 111 case vc_char: 112 rc = bigint_get_value_int(&var->u.char_v->value, &char_val); 113 if (rc == EOK) 114 printf("%lc\n", char_val); 115 else 116 printf("???\n"); 117 break; 109 118 case vc_int: 110 119 bigint_print(&var->u.int_v->value); -
uspace/app/sbi/src/imode.c
r23de644 r074444f 47 47 #include "list.h" 48 48 #include "mytypes.h" 49 #include "program.h" 49 50 #include "rdata.h" 50 51 #include "stree.h" … … 90 91 builtin_declare(program); 91 92 93 /* Process the library. */ 94 if (program_lib_process(program) != EOK) 95 exit(1); 96 92 97 /* Resolve ancestry. */ 93 98 ancr_module_process(program, program->module); … … 130 135 parse.error = b_false; 131 136 stype.error = b_false; 137 run.thread_ar->exc_payload = NULL; 138 run.thread_ar->bo_mode = bm_none; 132 139 133 140 input_new_interactive(&input); -
uspace/app/sbi/src/input.c
r23de644 r074444f 45 45 #define INPUT_BUFFER_SIZE 256 46 46 47 static int input_init_file(input_t *input, c har *fname);47 static int input_init_file(input_t *input, const char *fname); 48 48 static void input_init_interactive(input_t *input); 49 49 static void input_init_string(input_t *input, const char *str); … … 57 57 * ENOENT when opening file fails. 58 58 */ 59 int input_new_file(input_t **input, c har *fname)59 int input_new_file(input_t **input, const char *fname) 60 60 { 61 61 *input = malloc(sizeof(input_t)); … … 104 104 * @return EOK on success, ENOENT when opening file fails. 105 105 */ 106 static int input_init_file(input_t *input, c har *fname)106 static int input_init_file(input_t *input, const char *fname) 107 107 { 108 108 FILE *f; -
uspace/app/sbi/src/input.h
r23de644 r074444f 32 32 #include "mytypes.h" 33 33 34 int input_new_file(input_t **input, c har *fname);34 int input_new_file(input_t **input, const char *fname); 35 35 int input_new_interactive(input_t **input); 36 36 int input_new_string(input_t **input, const char *str); -
uspace/app/sbi/src/lex.c
r23de644 r074444f 53 53 static bool_t is_digit(char c); 54 54 static void lex_word(lex_t *lex); 55 static void lex_char(lex_t *lex); 55 56 static void lex_number(lex_t *lex); 56 57 static void lex_string(lex_t *lex); … … 74 75 static struct lc_name keywords[] = { 75 76 { lc_as, "as" }, 77 { lc_bool, "bool" }, 78 { lc_char, "char" }, 76 79 { lc_builtin, "builtin" }, 77 80 { lc_class, "class" }, … … 81 84 { lc_end, "end" }, 82 85 { lc_except, "except" }, 86 { lc_false, "false" }, 83 87 { lc_finally, "finally" }, 84 88 { lc_for, "for" }, … … 108 112 { lc_then, "then" }, 109 113 { lc_this, "this" }, 114 { lc_true, "true" }, 110 115 { lc_var, "var" }, 111 116 { lc_with, "with" }, … … 338 343 } 339 344 345 if (bp[0] == '\'') { 346 lex_char(lex); 347 return b_true; 348 } 349 340 350 if (is_digit(bp[0])) { 341 351 lex_number(lex); … … 460 470 lex->current.lclass = lc_ident; 461 471 lex->current.u.ident.sid = strtab_get_sid(ident_buf); 472 } 473 474 /** Lex a character literal. 475 * 476 * Reads in a character literal and stores it in the current lem in @a lex. 477 * 478 * @param lex Lexer object. 479 */ 480 static void lex_char(lex_t *lex) 481 { 482 char *bp; 483 int idx; 484 size_t len; 485 int char_val; 486 487 bp = lex->ibp + 1; 488 idx = 0; 489 490 while (bp[idx] != '\'') { 491 if (idx >= SLBUF_SIZE) { 492 printf("Error: Character literal too long.\n"); 493 exit(1); 494 } 495 496 if (bp[idx] == '\0') { 497 printf("Error: Unterminated character literal.\n"); 498 exit(1); 499 } 500 501 strlit_buf[idx] = bp[idx]; 502 ++idx; 503 } 504 505 lex->ibp = bp + idx + 1; 506 507 strlit_buf[idx] = '\0'; 508 len = os_str_length(strlit_buf); 509 if (len != 1) { 510 printf("Character literal should contain one character, " 511 "but contains %u characters instead.\n", (unsigned) len); 512 exit(1); 513 } 514 515 os_str_get_char(strlit_buf, 0, &char_val); 516 lex->current.lclass = lc_lit_char; 517 bigint_init(&lex->current.u.lit_char.value, char_val); 462 518 } 463 519 -
uspace/app/sbi/src/lex_t.h
r23de644 r074444f 38 38 39 39 lc_ident, 40 lc_lit_char, 40 41 lc_lit_int, 41 42 lc_lit_string, … … 43 44 /* Keywords */ 44 45 lc_as, 46 lc_bool, 45 47 lc_builtin, 48 lc_char, 46 49 lc_class, 47 50 lc_constructor, … … 50 53 lc_end, 51 54 lc_except, 55 lc_false, 52 56 lc_finally, 53 57 lc_for, … … 77 81 lc_then, 78 82 lc_this, 83 lc_true, 79 84 lc_var, 80 85 lc_with, … … 115 120 116 121 typedef struct { 122 /* Character value */ 123 bigint_t value; 124 } lem_lit_char_t; 125 126 typedef struct { 117 127 /* Integer value */ 118 128 bigint_t value; … … 131 141 union { 132 142 lem_ident_t ident; 143 lem_lit_char_t lit_char; 133 144 lem_lit_int_t lit_int; 134 145 lem_lit_string_t lit_string; -
uspace/app/sbi/src/main.c
r23de644 r074444f 37 37 #include <stdlib.h> 38 38 #include "ancr.h" 39 #include "os/os.h" 39 40 #include "builtin.h" 40 41 #include "imode.h" 41 42 #include "mytypes.h" 43 #include "program.h" 42 44 #include "strtab.h" 43 45 #include "stree.h" … … 48 50 #include "run.h" 49 51 50 void syntax_print(void);52 static void syntax_print(void); 51 53 52 54 /** Main entry point. … … 56 58 int main(int argc, char *argv[]) 57 59 { 58 input_t *input;59 lex_t lex;60 parse_t parse;61 60 stree_program_t *program; 62 61 stype_t stype; … … 64 63 int rc; 65 64 66 if (argc == 1) { 65 /* Store executable file path under which we have been invoked. */ 66 os_store_ef_path(*argv); 67 68 argv += 1; 69 argc -= 1; 70 71 if (argc == 0) { 67 72 /* Enter interactive mode */ 68 73 strtab_init(); … … 71 76 } 72 77 73 if ( argc != 2) {78 if (os_str_cmp(*argv, "-h") == 0) { 74 79 syntax_print(); 75 exit(1); 76 } 77 78 rc = input_new_file(&input, argv[1]); 79 if (rc != EOK) { 80 printf("Failed opening source file '%s'.\n", argv[1]); 81 exit(1); 80 return 0; 82 81 } 83 82 … … 89 88 builtin_declare(program); 90 89 91 /* Parse input file. */ 92 lex_init(&lex, input); 93 parse_init(&parse, program, &lex); 94 parse_module(&parse); 95 96 /* Check for parse errors. */ 97 if (parse.error) 90 /* Process source files in the library. */ 91 if (program_lib_process(program) != EOK) 98 92 return 1; 99 93 94 /* Process all source files specified in command-line arguments. */ 95 while (argc > 0) { 96 rc = program_file_process(program, *argv); 97 if (rc != EOK) 98 return 1; 99 100 argv += 1; 101 argc -= 1; 102 } 103 100 104 /* Resolve ancestry. */ 101 ancr_module_process(program, p arse.cur_mod);105 ancr_module_process(program, program->module); 102 106 103 107 /* Type program. */ … … 122 126 123 127 /** Print command-line syntax help. */ 124 void syntax_print(void)128 static void syntax_print(void) 125 129 { 126 printf("Missing or invalid arguments.\n");127 130 printf("Syntax: sbi <source_file.sy>\n"); 128 131 } -
uspace/app/sbi/src/os/helenos.c
r23de644 r074444f 38 38 #include "os.h" 39 39 40 /** Path to executable file via which we have been invoked. */ 41 static char *ef_path; 42 40 43 /* 41 44 * Using HelenOS-specific string API. … … 70 73 { 71 74 return str_cmp(a, b); 75 } 76 77 /** Return number of characters in string. */ 78 size_t os_str_length(const char *str) 79 { 80 return str_length(str); 72 81 } 73 82 … … 156 165 return EOK; 157 166 } 167 168 /** Store the executable file path via which we were executed. */ 169 void os_store_ef_path(char *path) 170 { 171 ef_path = path; 172 } 173 174 /** Return path to the Sysel library 175 * 176 * @return New string. Caller should deallocate it using @c free(). 177 */ 178 char *os_get_lib_path(void) 179 { 180 return os_str_dup("/src/sysel/lib"); 181 } -
uspace/app/sbi/src/os/os.h
r23de644 r074444f 33 33 int os_str_cmp(const char *a, const char *b); 34 34 char *os_str_dup(const char *str); 35 size_t os_str_length(const char *str); 35 36 int os_str_get_char(const char *str, int index, int *out_char); 36 37 void os_input_disp_help(void); … … 38 39 int os_exec(char *const cmd[]); 39 40 41 void os_store_ef_path(char *path); 42 char *os_get_lib_path(void); 40 43 41 44 #endif -
uspace/app/sbi/src/os/posix.c
r23de644 r074444f 29 29 /** @file POSIX-specific code. */ 30 30 31 #include <libgen.h> 31 32 #include <stdio.h> 32 33 #include <stdlib.h> … … 39 40 40 41 #include "os.h" 42 43 /** Path to executable file via which we have been invoked. */ 44 static char *ef_path; 41 45 42 46 /* … … 71 75 { 72 76 return strcmp(a, b); 77 } 78 79 /** Return number of characters in string. */ 80 size_t os_str_length(const char *str) 81 { 82 return strlen(str); 73 83 } 74 84 … … 146 156 return EOK; 147 157 } 158 159 /** Store the executable file path via which we were executed. */ 160 void os_store_ef_path(char *path) 161 { 162 ef_path = path; 163 } 164 165 /** Return path to the Sysel library 166 * 167 * @return New string. Caller should deallocate it using @c free(). 168 */ 169 char *os_get_lib_path(void) 170 { 171 return os_str_acat(dirname(ef_path), "/lib"); 172 } -
uspace/app/sbi/src/p_expr.c
r23de644 r074444f 56 56 static stree_expr_t *parse_primitive(parse_t *parse); 57 57 static stree_expr_t *parse_nameref(parse_t *parse); 58 static stree_expr_t *parse_lit_bool(parse_t *parse); 59 static stree_expr_t *parse_lit_char(parse_t *parse); 58 60 static stree_expr_t *parse_lit_int(parse_t *parse); 59 61 static stree_expr_t *parse_lit_ref(parse_t *parse); … … 494 496 expr = parse_nameref(parse); 495 497 break; 498 case lc_false: 499 case lc_true: 500 expr = parse_lit_bool(parse); 501 break; 502 case lc_lit_char: 503 expr = parse_lit_char(parse); 504 break; 496 505 case lc_lit_int: 497 506 expr = parse_lit_int(parse); … … 531 540 } 532 541 542 /** Parse boolean literal. 543 * 544 * @param parse Parser object. 545 */ 546 static stree_expr_t *parse_lit_bool(parse_t *parse) 547 { 548 stree_literal_t *literal; 549 stree_expr_t *expr; 550 bool_t value; 551 552 switch (lcur_lc(parse)) { 553 case lc_false: value = b_false; break; 554 case lc_true: value = b_true; break; 555 default: assert(b_false); 556 } 557 558 lskip(parse); 559 560 literal = stree_literal_new(ltc_bool); 561 literal->u.lit_bool.value = value; 562 563 expr = stree_expr_new(ec_literal); 564 expr->u.literal = literal; 565 566 return expr; 567 } 568 569 /** Parse character literal. 570 * 571 * @param parse Parser object. 572 */ 573 static stree_expr_t *parse_lit_char(parse_t *parse) 574 { 575 stree_literal_t *literal; 576 stree_expr_t *expr; 577 578 lcheck(parse, lc_lit_char); 579 580 literal = stree_literal_new(ltc_char); 581 bigint_clone(&lcur(parse)->u.lit_char.value, 582 &literal->u.lit_char.value); 583 584 lskip(parse); 585 586 expr = stree_expr_new(ec_literal); 587 expr->u.literal = literal; 588 589 return expr; 590 } 591 533 592 /** Parse integer literal. 534 593 * -
uspace/app/sbi/src/p_type.c
r23de644 r074444f 78 78 static stree_texpr_t *parse_tapply(parse_t *parse) 79 79 { 80 stree_texpr_t *a, *b, *tmp; 80 stree_texpr_t *gtype; 81 stree_texpr_t *aexpr; 82 stree_texpr_t *targ; 81 83 stree_tapply_t *tapply; 82 84 83 a = parse_tpostfix(parse); 85 gtype = parse_tpostfix(parse); 86 if (lcur_lc(parse) != lc_slash) 87 return gtype; 88 89 tapply = stree_tapply_new(); 90 tapply->gtype = gtype; 91 list_init(&tapply->targs); 92 84 93 while (lcur_lc(parse) == lc_slash) { 85 94 … … 88 97 89 98 lskip(parse); 90 b = parse_tpostfix(parse); 91 92 tapply = stree_tapply_new(); 93 tapply->gtype = a; 94 tapply->targ = b; 95 96 tmp = stree_texpr_new(tc_tapply); 97 tmp->u.tapply = tapply; 98 a = tmp; 99 } 100 101 return a; 99 targ = parse_tpostfix(parse); 100 101 list_append(&tapply->targs, targ); 102 } 103 104 aexpr = stree_texpr_new(tc_tapply); 105 aexpr->u.tapply = tapply; 106 return aexpr; 102 107 } 103 108 … … 224 229 texpr->u.tnameref = parse_tnameref(parse); 225 230 break; 231 case lc_bool: 232 case lc_char: 226 233 case lc_int: 227 234 case lc_string: … … 249 256 250 257 switch (lcur_lc(parse)) { 258 case lc_bool: 259 tlc = tlc_bool; 260 break; 261 case lc_char: 262 tlc = tlc_char; 263 break; 251 264 case lc_int: 252 265 tlc = tlc_int; -
uspace/app/sbi/src/parse.c
r23de644 r074444f 156 156 stree_csimbr_t *csimbr; 157 157 stree_symbol_t *symbol; 158 stree_ident_t *targ_name; 158 159 159 160 switch (dclass) { … … 168 169 csi = stree_csi_new(cc); 169 170 csi->name = parse_ident(parse); 171 172 list_init(&csi->targ_names); 173 174 while (lcur_lc(parse) == lc_slash) { 175 lskip(parse); 176 targ_name = parse_ident(parse); 177 list_append(&csi->targ_names, targ_name); 178 } 170 179 171 180 symbol = stree_symbol_new(sc_csi); -
uspace/app/sbi/src/rdata.c
r23de644 r074444f 51 51 #include "mytypes.h" 52 52 #include "stree.h" 53 #include "symbol.h" 53 54 54 55 #include "rdata.h" 55 56 57 static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest); 58 static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest); 56 59 static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest); 57 60 static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest); … … 287 290 } 288 291 292 /** Allocate new boolean. 293 * 294 * @return New boolean. 295 */ 296 rdata_bool_t *rdata_bool_new(void) 297 { 298 rdata_bool_t *bool_v; 299 300 bool_v = calloc(1, sizeof(rdata_bool_t)); 301 if (bool_v == NULL) { 302 printf("Memory allocation failed.\n"); 303 exit(1); 304 } 305 306 return bool_v; 307 } 308 309 /** Allocate new character. 310 * 311 * @return New character. 312 */ 313 rdata_char_t *rdata_char_new(void) 314 { 315 rdata_char_t *char_v; 316 317 char_v = calloc(1, sizeof(rdata_char_t)); 318 if (char_v == NULL) { 319 printf("Memory allocation failed.\n"); 320 exit(1); 321 } 322 323 return char_v; 324 } 325 289 326 /** Allocate new integer. 290 327 * … … 398 435 399 436 switch (src->vc) { 437 case vc_bool: 438 rdata_bool_copy(src->u.bool_v, &nvar->u.bool_v); 439 break; 440 case vc_char: 441 rdata_char_copy(src->u.char_v, &nvar->u.char_v); 442 break; 400 443 case vc_int: 401 444 rdata_int_copy(src->u.int_v, &nvar->u.int_v); … … 422 465 423 466 *dest = nvar; 467 } 468 469 /** Copy boolean. 470 * 471 * @param src Source boolean. 472 * @param dest Place to store pointer to new boolean. 473 */ 474 static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest) 475 { 476 *dest = rdata_bool_new(); 477 (*dest)->value = src->value; 478 } 479 480 /** Copy character. 481 * 482 * @param src Source character. 483 * @param dest Place to store pointer to new character. 484 */ 485 static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest) 486 { 487 *dest = rdata_char_new(); 488 bigint_clone(&src->value, &(*dest)->value); 424 489 } 425 490 … … 550 615 var->vc = nvar->vc; 551 616 switch (nvar->vc) { 617 case vc_bool: var->u.bool_v = nvar->u.bool_v; break; 618 case vc_char: var->u.char_v = nvar->u.char_v; break; 552 619 case vc_int: var->u.int_v = nvar->u.int_v; break; 553 620 case vc_string: var->u.string_v = nvar->u.string_v; break; … … 621 688 static void rdata_var_print(rdata_var_t *var) 622 689 { 690 int val; 691 623 692 switch (var->vc) { 693 case vc_bool: 694 printf("bool(%s)", var->u.bool_v->value ? "true" : "false"); 695 break; 696 case vc_char: 697 printf("char("); 698 if (bigint_get_value_int(&var->u.char_v->value, &val) == EOK) 699 printf("'%c'", val); 700 else 701 printf("???:x%x\n", (unsigned) val); 702 printf(")"); 703 break; 624 704 case vc_int: 625 705 printf("int("); … … 631 711 break; 632 712 case vc_ref: 633 printf("ref"); 713 printf("ref("); 714 rdata_var_print(var->u.ref_v->vref); 715 printf(")"); 634 716 break; 635 717 case vc_deleg: 636 printf("deleg"); 718 printf("deleg("); 719 if (var->u.deleg_v->obj != NULL) { 720 rdata_var_print(var->u.deleg_v->obj); 721 printf(","); 722 } 723 symbol_print_fqn(var->u.deleg_v->sym); 724 printf(")"); 725 break; 726 case vc_array: 727 printf("array"); 637 728 break; 638 729 case vc_object: 639 730 printf("object"); 640 731 break; 641 default:642 printf(" print(%d)\n", var->vc);643 assert(b_false);644 } 645 } 732 case vc_resource: 733 printf("resource(%p)", var->u.resource_v->data); 734 break; 735 } 736 } -
uspace/app/sbi/src/rdata.h
r23de644 r074444f 45 45 rdata_array_t *rdata_array_new(int rank); 46 46 rdata_object_t *rdata_object_new(void); 47 rdata_bool_t *rdata_bool_new(void); 48 rdata_char_t *rdata_char_new(void); 47 49 rdata_int_t *rdata_int_new(void); 48 50 rdata_string_t *rdata_string_new(void); -
uspace/app/sbi/src/rdata_t.h
r23de644 r074444f 35 35 #include "intmap_t.h" 36 36 37 /** Boolean variable. */ 38 typedef struct { 39 bool_t value; 40 } rdata_bool_t; 41 42 /** Character variable. 43 * 44 * Sysel character type should be able to store arbitrarily (or at least 45 * very) large character sets. 46 */ 47 typedef struct { 48 bigint_t value; 49 } rdata_char_t; 50 37 51 /** Integer variable. 38 52 * … … 43 57 bigint_t value; 44 58 } rdata_int_t; 45 46 59 47 60 /** String variable */ … … 104 117 105 118 typedef enum var_class { 119 /** Boolean */ 120 vc_bool, 121 122 /** Character **/ 123 vc_char, 124 106 125 /** Integer */ 107 126 vc_int, … … 136 155 137 156 union { 157 rdata_bool_t *bool_v; 158 rdata_char_t *char_v; 138 159 rdata_int_t *int_v; 139 160 rdata_string_t *string_v; -
uspace/app/sbi/src/run.c
r23de644 r074444f 657 657 void run_value_item_to_var(rdata_item_t *item, rdata_var_t **var) 658 658 { 659 rdata_char_t *char_v; 659 660 rdata_int_t *int_v; 660 661 rdata_string_t *string_v; … … 666 667 667 668 switch (in_var->vc) { 669 case vc_char: 670 *var = rdata_var_new(vc_char); 671 char_v = rdata_char_new(); 672 673 (*var)->u.char_v = char_v; 674 bigint_clone(&item->u.value->var->u.char_v->value, 675 &char_v->value); 676 break; 668 677 case vc_int: 669 678 *var = rdata_var_new(vc_int); -
uspace/app/sbi/src/run_expr.c
r23de644 r074444f 53 53 static void run_literal(run_t *run, stree_literal_t *literal, 54 54 rdata_item_t **res); 55 static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool, 56 rdata_item_t **res); 57 static void run_lit_char(run_t *run, stree_lit_char_t *lit_char, 58 rdata_item_t **res); 55 59 static void run_lit_int(run_t *run, stree_lit_int_t *lit_int, 56 60 rdata_item_t **res); … … 64 68 65 69 static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res); 70 static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 71 rdata_value_t *v2, rdata_item_t **res); 72 static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 73 rdata_value_t *v2, rdata_item_t **res); 66 74 static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 67 75 rdata_value_t *v2, rdata_item_t **res); … … 315 323 printf("Run literal.\n"); 316 324 #endif 317 318 325 switch (literal->ltc) { 326 case ltc_bool: 327 run_lit_bool(run, &literal->u.lit_bool, res); 328 break; 329 case ltc_char: 330 run_lit_char(run, &literal->u.lit_char, res); 331 break; 319 332 case ltc_int: 320 333 run_lit_int(run, &literal->u.lit_int, res); … … 326 339 run_lit_string(run, &literal->u.lit_string, res); 327 340 break; 328 default: 329 assert(b_false); 330 } 341 } 342 } 343 344 /** Evaluate Boolean literal. */ 345 static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool, 346 rdata_item_t **res) 347 { 348 rdata_item_t *item; 349 rdata_value_t *value; 350 rdata_var_t *var; 351 rdata_bool_t *bool_v; 352 353 #ifdef DEBUG_RUN_TRACE 354 printf("Run Boolean literal.\n"); 355 #endif 356 (void) run; 357 358 item = rdata_item_new(ic_value); 359 value = rdata_value_new(); 360 var = rdata_var_new(vc_bool); 361 bool_v = rdata_bool_new(); 362 363 item->u.value = value; 364 value->var = var; 365 var->u.bool_v = bool_v; 366 bool_v->value = lit_bool->value; 367 368 *res = item; 369 } 370 371 /** Evaluate character literal. */ 372 static void run_lit_char(run_t *run, stree_lit_char_t *lit_char, 373 rdata_item_t **res) 374 { 375 rdata_item_t *item; 376 rdata_value_t *value; 377 rdata_var_t *var; 378 rdata_char_t *char_v; 379 380 #ifdef DEBUG_RUN_TRACE 381 printf("Run character literal.\n"); 382 #endif 383 (void) run; 384 385 item = rdata_item_new(ic_value); 386 value = rdata_value_new(); 387 var = rdata_var_new(vc_char); 388 char_v = rdata_char_new(); 389 390 item->u.value = value; 391 value->var = var; 392 var->u.char_v = char_v; 393 bigint_clone(&lit_char->value, &char_v->value); 394 395 *res = item; 331 396 } 332 397 … … 486 551 487 552 switch (v1->var->vc) { 553 case vc_bool: 554 run_binop_bool(run, binop, v1, v2, res); 555 break; 556 case vc_char: 557 run_binop_char(run, binop, v1, v2, res); 558 break; 488 559 case vc_int: 489 560 run_binop_int(run, binop, v1, v2, res); … … 495 566 run_binop_ref(run, binop, v1, v2, res); 496 567 break; 568 case vc_deleg: 569 case vc_array: 570 case vc_object: 571 case vc_resource: 572 assert(b_false); 573 } 574 } 575 576 /** Evaluate binary operation on bool arguments. */ 577 static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 578 rdata_value_t *v2, rdata_item_t **res) 579 { 580 rdata_item_t *item; 581 rdata_value_t *value; 582 rdata_var_t *var; 583 rdata_bool_t *bool_v; 584 585 bool_t b1, b2; 586 587 (void) run; 588 589 item = rdata_item_new(ic_value); 590 value = rdata_value_new(); 591 var = rdata_var_new(vc_bool); 592 bool_v = rdata_bool_new(); 593 594 item->u.value = value; 595 value->var = var; 596 var->u.bool_v = bool_v; 597 598 b1 = v1->var->u.bool_v->value; 599 b2 = v2->var->u.bool_v->value; 600 601 switch (binop->bc) { 602 case bo_plus: 603 case bo_minus: 604 case bo_mult: 605 assert(b_false); 606 607 case bo_equal: 608 bool_v->value = (b1 == b2); 609 break; 610 case bo_notequal: 611 bool_v->value = (b1 != b2); 612 break; 613 case bo_lt: 614 bool_v->value = (b1 == b_false) && (b2 == b_true); 615 break; 616 case bo_gt: 617 bool_v->value = (b1 == b_true) && (b2 == b_false); 618 break; 619 case bo_lt_equal: 620 bool_v->value = (b1 == b_false) || (b2 == b_true); 621 break; 622 case bo_gt_equal: 623 bool_v->value = (b1 == b_true) || (b2 == b_false); 624 break; 625 } 626 627 *res = item; 628 } 629 630 /** Evaluate binary operation on char arguments. */ 631 static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1, 632 rdata_value_t *v2, rdata_item_t **res) 633 { 634 rdata_item_t *item; 635 rdata_value_t *value; 636 rdata_var_t *var; 637 rdata_bool_t *bool_v; 638 639 bigint_t *c1, *c2; 640 bigint_t diff; 641 bool_t zf, nf; 642 643 (void) run; 644 645 item = rdata_item_new(ic_value); 646 value = rdata_value_new(); 647 648 item->u.value = value; 649 650 c1 = &v1->var->u.char_v->value; 651 c2 = &v2->var->u.char_v->value; 652 653 var = rdata_var_new(vc_bool); 654 bool_v = rdata_bool_new(); 655 var->u.bool_v = bool_v; 656 value->var = var; 657 658 bigint_sub(c1, c2, &diff); 659 zf = bigint_is_zero(&diff); 660 nf = bigint_is_negative(&diff); 661 662 switch (binop->bc) { 663 case bo_plus: 664 case bo_minus: 665 case bo_mult: 666 assert(b_false); 667 668 case bo_equal: 669 bool_v->value = zf; 670 break; 671 case bo_notequal: 672 bool_v->value = !zf; 673 break; 674 case bo_lt: 675 bool_v->value = (!zf && nf); 676 break; 677 case bo_gt: 678 bool_v->value = (!zf && !nf); 679 break; 680 case bo_lt_equal: 681 bool_v->value = (zf || nf); 682 break; 683 case bo_gt_equal: 684 bool_v->value = !nf; 685 break; 497 686 default: 498 printf("Unimplemented: Binary operation arguments of "499 "type %d.\n", v1->var->vc);500 exit(1); 501 }687 assert(b_false); 688 } 689 690 *res = item; 502 691 } 503 692 … … 510 699 rdata_var_t *var; 511 700 rdata_int_t *int_v; 701 rdata_bool_t *bool_v; 512 702 513 703 bigint_t *i1, *i2; … … 520 710 item = rdata_item_new(ic_value); 521 711 value = rdata_value_new(); 522 var = rdata_var_new(vc_int);523 int_v = rdata_int_new();524 712 525 713 item->u.value = value; 526 value->var = var;527 var->u.int_v = int_v;528 714 529 715 i1 = &v1->var->u.int_v->value; … … 534 720 switch (binop->bc) { 535 721 case bo_plus: 722 int_v = rdata_int_new(); 536 723 bigint_add(i1, i2, &int_v->value); 537 724 break; 538 725 case bo_minus: 726 int_v = rdata_int_new(); 539 727 bigint_sub(i1, i2, &int_v->value); 540 728 break; 541 729 case bo_mult: 730 int_v = rdata_int_new(); 542 731 bigint_mul(i1, i2, &int_v->value); 543 732 break; … … 548 737 549 738 if (done) { 739 var = rdata_var_new(vc_int); 740 var->u.int_v = int_v; 741 value->var = var; 550 742 *res = item; 551 743 return; 552 744 } 745 746 var = rdata_var_new(vc_bool); 747 bool_v = rdata_bool_new(); 748 var->u.bool_v = bool_v; 749 value->var = var; 553 750 554 751 /* Relational operation. */ … … 558 755 nf = bigint_is_negative(&diff); 559 756 560 /* XXX We should have a real boolean type. */561 757 switch (binop->bc) { 562 758 case bo_equal: 563 b igint_init(&int_v->value, zf ? 1 : 0);759 bool_v->value = zf; 564 760 break; 565 761 case bo_notequal: 566 b igint_init(&int_v->value, !zf ? 1 : 0);762 bool_v->value = !zf; 567 763 break; 568 764 case bo_lt: 569 b igint_init(&int_v->value, (!zf && nf) ? 1 : 0);765 bool_v->value = (!zf && nf); 570 766 break; 571 767 case bo_gt: 572 b igint_init(&int_v->value, (!zf && !nf) ? 1 : 0);768 bool_v->value = (!zf && !nf); 573 769 break; 574 770 case bo_lt_equal: 575 b igint_init(&int_v->value, (zf || nf) ? 1 : 0);771 bool_v->value = (zf || nf); 576 772 break; 577 773 case bo_gt_equal: 578 b igint_init(&int_v->value, !nf ? 1 : 0);774 bool_v->value = !nf; 579 775 break; 580 776 default: … … 631 827 rdata_value_t *value; 632 828 rdata_var_t *var; 633 rdata_ int_t *int_v;829 rdata_bool_t *bool_v; 634 830 635 831 rdata_var_t *ref1, *ref2; … … 639 835 item = rdata_item_new(ic_value); 640 836 value = rdata_value_new(); 641 var = rdata_var_new(vc_ int);642 int_v = rdata_int_new();837 var = rdata_var_new(vc_bool); 838 bool_v = rdata_bool_new(); 643 839 644 840 item->u.value = value; 645 841 value->var = var; 646 var->u. int_v = int_v;842 var->u.bool_v = bool_v; 647 843 648 844 ref1 = v1->var->u.ref_v->vref; … … 650 846 651 847 switch (binop->bc) { 652 /* XXX We should have a real boolean type. */653 848 case bo_equal: 654 b igint_init(&int_v->value, (ref1 == ref2) ? 1 : 0);849 bool_v->value = (ref1 == ref2); 655 850 break; 656 851 case bo_notequal: 657 b igint_init(&int_v->value, (ref1 != ref2) ? 1 : 0);852 bool_v->value = (ref1 != ref2); 658 853 break; 659 854 default: … … 980 1175 access->member_name); 981 1176 982 if (member == NULL) { 983 printf("Error: CSI '"); 984 symbol_print_fqn(deleg_v->sym); 985 printf("' has no member named '%s'.\n", 986 strtab_get_str(access->member_name->sid)); 987 exit(1); 988 } 1177 /* Member existence should be ensured by static type checking. */ 1178 assert(member != NULL); 989 1179 990 1180 #ifdef DEBUG_RUN_TRACE … … 1475 1665 1476 1666 if (rc1 != EOK || rc2 != EOK) { 1667 #ifdef DEBUG_RUN_TRACE 1477 1668 printf("Error: String index (value: %d) is out of range.\n", 1478 1669 arg_val); 1479 exit(1); 1670 #endif 1671 /* Raise Error.OutOfBounds */ 1672 run_raise_exc(run, run->program->builtin->error_outofbounds); 1673 *res = run_recovery_item(run); 1674 return; 1480 1675 } 1481 1676 … … 1485 1680 ritem->u.value = value; 1486 1681 1487 cvar = rdata_var_new(vc_ int);1488 cvar->u. int_v = rdata_int_new();1489 bigint_init(&cvar->u. int_v->value, cval);1682 cvar = rdata_var_new(vc_char); 1683 cvar->u.char_v = rdata_char_new(); 1684 bigint_init(&cvar->u.char_v->value, cval); 1490 1685 value->var = cvar; 1491 1686 … … 1651 1846 * Tries to interpret @a item as a boolean value. If it is not a boolean 1652 1847 * value, this generates an error. 1653 *1654 * XXX Currently int supplants the role of a true boolean type.1655 1848 */ 1656 1849 bool_t run_item_boolean_value(run_t *run, rdata_item_t *item) … … 1665 1858 var = vitem->u.value->var; 1666 1859 1667 if (var->vc != vc_int) { 1668 printf("Error: Boolean (int) expression expected.\n"); 1669 exit(1); 1670 } 1671 1672 return !bigint_is_zero(&var->u.int_v->value); 1673 } 1860 assert(var->vc == vc_bool); 1861 return var->u.bool_v->value; 1862 } -
uspace/app/sbi/src/run_texpr.c
r23de644 r074444f 33 33 #include "list.h" 34 34 #include "mytypes.h" 35 #include "strtab.h" 35 36 #include "symbol.h" 36 37 #include "tdata.h" … … 46 47 static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx, 47 48 stree_tnameref_t *tnameref, tdata_item_t **res); 49 static void run_tapply(stree_program_t *prog, stree_csi_t *ctx, 50 stree_tapply_t *tapply, tdata_item_t **res); 48 51 49 52 void run_texpr(stree_program_t *prog, stree_csi_t *ctx, stree_texpr_t *texpr, … … 64 67 break; 65 68 case tc_tapply: 66 printf("Unimplemented: Evaluate type expression type %d.\n", 67 texpr->tc); 68 exit(1); 69 run_tapply(prog, ctx, texpr->u.tapply, res); 70 break; 69 71 } 70 72 } … … 85 87 run_texpr(prog, ctx, taccess->arg, &targ_i); 86 88 89 if (targ_i->tic == tic_ignore) { 90 *res = tdata_item_new(tic_ignore); 91 return; 92 } 93 87 94 if (targ_i->tic != tic_tobject) { 88 95 printf("Error: Using '.' with type which is not an object.\n"); 89 exit(1); 96 *res = tdata_item_new(tic_ignore); 97 return; 90 98 } 91 99 … … 94 102 95 103 sym = symbol_lookup_in_csi(prog, base_csi, taccess->member_name); 96 97 /* Existence should have been verified in type checking phase. */ 98 assert(sym != NULL); 104 if (sym == NULL) { 105 printf("Error: CSI '"); 106 symbol_print_fqn(csi_to_symbol(base_csi)); 107 printf("' has no member named '%s'.\n", 108 strtab_get_str(taccess->member_name->sid)); 109 *res = tdata_item_new(tic_ignore); 110 return; 111 } 99 112 100 113 if (sym->sc != sc_csi) { … … 102 115 symbol_print_fqn(sym); 103 116 printf("' is not a CSI.\n"); 104 exit(1); 117 *res = tdata_item_new(tic_ignore); 118 return; 105 119 } 106 120 … … 130 144 /* Evaluate base type. */ 131 145 run_texpr(prog, ctx, tindex->base_type, &base_ti); 146 147 if (base_ti->tic == tic_ignore) { 148 *res = tdata_item_new(tic_ignore); 149 return; 150 } 132 151 133 152 /* Construct type item. */ … … 167 186 168 187 switch (tliteral->tlc) { 188 case tlc_bool: tpc = tpc_bool; break; 189 case tlc_char: tpc = tpc_char; break; 169 190 case tlc_int: tpc = tpc_int; break; 170 191 case tlc_string: tpc = tpc_string; break; … … 191 212 #endif 192 213 sym = symbol_lookup_in_csi(prog, ctx, tnameref->name); 193 194 /* Existence should have been verified in type-checking phase. */ 195 assert(sym); 214 if (sym == NULL) { 215 printf("Error: Symbol '%s' not found.\n", 216 strtab_get_str(tnameref->name->sid)); 217 *res = tdata_item_new(tic_ignore); 218 return; 219 } 196 220 197 221 if (sym->sc != sc_csi) { … … 199 223 symbol_print_fqn(sym); 200 224 printf("' is not a CSI.\n"); 201 exit(1); 225 *res = tdata_item_new(tic_ignore); 226 return; 202 227 } 203 228 … … 212 237 *res = titem; 213 238 } 239 240 static void run_tapply(stree_program_t *prog, stree_csi_t *ctx, 241 stree_tapply_t *tapply, tdata_item_t **res) 242 { 243 tdata_item_t *base_ti; 244 tdata_item_t *arg_ti; 245 tdata_item_t *titem; 246 tdata_object_t *tobject; 247 248 list_node_t *arg_n; 249 stree_texpr_t *arg; 250 251 #ifdef DEBUG_RUN_TRACE 252 printf("Evaluating type apply operation.\n"); 253 #endif 254 /* Construct type item. */ 255 titem = tdata_item_new(tic_tobject); 256 tobject = tdata_object_new(); 257 titem->u.tobject = tobject; 258 259 /* Evaluate base (generic) type. */ 260 run_texpr(prog, ctx, tapply->gtype, &base_ti); 261 262 if (base_ti->tic != tic_tobject) { 263 printf("Error: Base type of generic application is not " 264 "a CSI.\n"); 265 *res = tdata_item_new(tic_ignore); 266 return; 267 } 268 269 tobject->static_ref = b_false; 270 tobject->csi = base_ti->u.tobject->csi; 271 list_init(&tobject->targs); 272 273 /* Evaluate type arguments. */ 274 arg_n = list_first(&tapply->targs); 275 while (arg_n != NULL) { 276 arg = list_node_data(arg_n, stree_texpr_t *); 277 run_texpr(prog, ctx, arg, &arg_ti); 278 279 if (arg_ti->tic == tic_ignore) { 280 *res = tdata_item_new(tic_ignore); 281 return; 282 } 283 284 list_append(&tobject->targs, arg_ti); 285 286 arg_n = list_next(&tapply->targs, arg_n); 287 } 288 289 *res = titem; 290 } -
uspace/app/sbi/src/stree_t.h
r23de644 r074444f 54 54 } stree_self_ref_t; 55 55 56 /** Boolean literal */ 57 typedef struct { 58 bool_t value; 59 } stree_lit_bool_t; 60 61 /** Character literal */ 62 typedef struct { 63 bigint_t value; 64 } stree_lit_char_t; 65 66 /** Integer literal */ 56 67 typedef struct { 57 68 bigint_t value; … … 62 73 } stree_lit_ref_t; 63 74 75 /** String literal */ 64 76 typedef struct { 65 77 char *value; … … 67 79 68 80 typedef enum { 81 ltc_bool, 82 ltc_char, 69 83 ltc_int, 70 84 ltc_ref, … … 76 90 literal_class_t ltc; 77 91 union { 92 stree_lit_bool_t lit_bool; 93 stree_lit_char_t lit_char; 78 94 stree_lit_int_t lit_int; 79 95 stree_lit_ref_t lit_ref; … … 214 230 /** Type literal class */ 215 231 typedef enum { 232 tlc_bool, 233 tlc_char, 216 234 tlc_int, 217 235 tlc_resource, … … 239 257 /** Type application operation */ 240 258 typedef struct { 241 /** Arguments */ 242 struct stree_texpr *gtype, *targ; 259 /* Base type */ 260 struct stree_texpr *gtype; 261 262 /** (Formal) type arguments */ 263 list_t targs; /* of stree_texpr_t */ 243 264 } stree_tapply_t; 244 265 … … 496 517 stree_ident_t *name; 497 518 519 /** List of type argument names */ 520 list_t targ_names; /* of stree_ident_t */ 521 498 522 /** Symbol for this CSI */ 499 523 struct stree_symbol *symbol; -
uspace/app/sbi/src/stype.c
r23de644 r074444f 64 64 static void stype_wef(stype_t *stype, stree_wef_t *wef_s); 65 65 66 static tdata_item_t *stype_boolean_titem(stype_t *stype);67 68 66 /** Type module */ 69 67 void stype_module(stype_t *stype, stree_module_t *module) … … 189 187 static void stype_var(stype_t *stype, stree_var_t *var) 190 188 { 189 tdata_item_t *titem; 190 191 191 (void) stype; 192 192 (void) var; 193 194 run_texpr(stype->program, stype->current_csi, var->type, 195 &titem); 196 if (titem->tic == tic_ignore) { 197 /* An error occured. */ 198 stype_note_error(stype); 199 return; 200 } 193 201 } 194 202 … … 283 291 stype_block_vr_t *block_vr; 284 292 stree_vdecl_t *old_vdecl; 293 tdata_item_t *titem; 285 294 286 295 #ifdef DEBUG_TYPE_TRACE … … 297 306 } 298 307 308 run_texpr(stype->program, stype->current_csi, vdecl_s->type, 309 &titem); 310 if (titem->tic == tic_ignore) { 311 /* An error occured. */ 312 stype_note_error(stype); 313 return; 314 } 315 299 316 intmap_set(&block_vr->vdecls, vdecl_s->name->sid, vdecl_s); 300 301 317 } 302 318 … … 524 540 goto failure; 525 541 break; 526 default:542 case tic_tfun: 527 543 printf("Error: Unimplemented: Converting '"); 528 544 tdata_item_print(src); … … 531 547 printf("'.\n"); 532 548 stype_note_error(stype); 549 break; 550 case tic_ignore: 551 assert(b_false); 533 552 } 534 553 … … 547 566 548 567 /** Return a boolean type item */ 549 statictdata_item_t *stype_boolean_titem(stype_t *stype)568 tdata_item_t *stype_boolean_titem(stype_t *stype) 550 569 { 551 570 tdata_item_t *titem; … … 554 573 (void) stype; 555 574 556 /* XXX Use a true boolean type */557 575 titem = tdata_item_new(tic_tprimitive); 558 tprimitive = tdata_primitive_new(tpc_ int);576 tprimitive = tdata_primitive_new(tpc_bool); 559 577 titem->u.tprimitive = tprimitive; 560 578 -
uspace/app/sbi/src/stype.h
r23de644 r074444f 41 41 tdata_item_t *dest); 42 42 43 tdata_item_t *stype_boolean_titem(stype_t *stype); 44 43 45 stree_vdecl_t *stype_local_vars_lookup(stype_t *stype, sid_t name); 44 46 stree_proc_arg_t *stype_proc_args_lookup(stype_t *stype, sid_t name); -
uspace/app/sbi/src/stype_expr.c
r23de644 r074444f 53 53 static void stype_binop(stype_t *stype, stree_binop_t *binop, 54 54 tdata_item_t **rtitem); 55 55 56 static void stype_binop_tprimitive(stype_t *stype, stree_binop_t *binop, 56 57 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem); 58 static void stype_binop_bool(stype_t *stype, stree_binop_t *binop, 59 tdata_item_t **rtitem); 60 static void stype_binop_char(stype_t *stype, stree_binop_t *binop, 61 tdata_item_t **rtitem); 62 static void stype_binop_int(stype_t *stype, stree_binop_t *binop, 63 tdata_item_t **rtitem); 64 static void stype_binop_nil(stype_t *stype, stree_binop_t *binop, 65 tdata_item_t **rtitem); 66 static void stype_binop_string(stype_t *stype, stree_binop_t *binop, 67 tdata_item_t **rtitem); 68 static void stype_binop_resource(stype_t *stype, stree_binop_t *binop, 69 tdata_item_t **rtitem); 70 57 71 static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop, 58 72 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem); … … 73 87 static void stype_access_tarray(stype_t *stype, stree_access_t *access, 74 88 tdata_item_t *arg_ti, tdata_item_t **rtitem); 75 static void stype_access_tgeneric(stype_t *stype, stree_access_t *access,76 tdata_item_t *arg_ti, tdata_item_t **rtitem);77 89 78 90 static void stype_call(stype_t *stype, stree_call_t *call, … … 86 98 tdata_item_t *base_ti, tdata_item_t **rtitem); 87 99 static void stype_index_tarray(stype_t *stype, stree_index_t *index, 88 tdata_item_t *base_ti, tdata_item_t **rtitem);89 static void stype_index_tgeneric(stype_t *stype, stree_index_t *index,90 100 tdata_item_t *base_ti, tdata_item_t **rtitem); 91 101 … … 248 258 249 259 switch (literal->ltc) { 260 case ltc_bool: tpc = tpc_bool; break; 261 case ltc_char: tpc = tpc_char; break; 250 262 case ltc_int: tpc = tpc_int; break; 251 263 case ltc_ref: tpc = tpc_nil; break; … … 333 345 } 334 346 335 /** Type a binary operation arguments of primitive type. */347 /** Type a binary operation with arguments of primitive type. */ 336 348 static void stype_binop_tprimitive(stype_t *stype, stree_binop_t *binop, 337 349 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem) 338 350 { 351 assert(ta->tic == tic_tprimitive); 352 assert(tb->tic == tic_tprimitive); 353 354 switch (ta->u.tprimitive->tpc) { 355 case tpc_bool: 356 stype_binop_bool(stype, binop, rtitem); 357 break; 358 case tpc_char: 359 stype_binop_char(stype, binop, rtitem); 360 break; 361 case tpc_int: 362 stype_binop_int(stype, binop, rtitem); 363 break; 364 case tpc_nil: 365 stype_binop_nil(stype, binop, rtitem); 366 break; 367 case tpc_string: 368 stype_binop_string(stype, binop, rtitem); 369 break; 370 case tpc_resource: 371 stype_binop_resource(stype, binop, rtitem); 372 break; 373 } 374 } 375 376 /** Type a binary operation with bool arguments. */ 377 static void stype_binop_bool(stype_t *stype, stree_binop_t *binop, 378 tdata_item_t **rtitem) 379 { 339 380 tprimitive_class_t rtpc; 340 381 tdata_item_t *res_ti; 341 382 342 (void) stype; 343 344 assert(ta->tic == tic_tprimitive); 345 assert(tb->tic == tic_tprimitive); 346 347 switch (ta->u.tprimitive->tpc) { 348 case tpc_int: 349 rtpc = tpc_int; 350 break; 351 case tpc_nil: 352 printf("Unimplemented; Binary operation on nil.\n"); 353 stype_note_error(stype); 354 rtpc = tpc_nil; 355 break; 356 case tpc_string: 357 if (binop->bc != bo_plus) { 358 printf("Unimplemented: Binary operation(%d) " 359 "on strings.\n", binop->bc); 360 stype_note_error(stype); 361 } 362 rtpc = tpc_string; 363 break; 364 case tpc_resource: 365 printf("Error: Cannot apply operator to resource type.\n"); 366 stype_note_error(stype); 367 rtpc = tpc_resource; 383 switch (binop->bc) { 384 case bo_equal: 385 case bo_notequal: 386 case bo_lt: 387 case bo_gt: 388 case bo_lt_equal: 389 case bo_gt_equal: 390 /* Comparison -> boolean type */ 391 rtpc = tpc_bool; 392 break; 393 case bo_plus: 394 case bo_minus: 395 case bo_mult: 396 /* Arithmetic -> error */ 397 printf("Error: Binary operation (%d) on booleans.\n", 398 binop->bc); 399 stype_note_error(stype); 400 *rtitem = stype_recovery_titem(stype); 401 return; 368 402 } 369 403 … … 374 408 } 375 409 376 /** Type a binary operation arguments of an object type. */ 410 /** Type a binary operation with char arguments. */ 411 static void stype_binop_char(stype_t *stype, stree_binop_t *binop, 412 tdata_item_t **rtitem) 413 { 414 tprimitive_class_t rtpc; 415 tdata_item_t *res_ti; 416 417 (void) stype; 418 419 switch (binop->bc) { 420 case bo_equal: 421 case bo_notequal: 422 case bo_lt: 423 case bo_gt: 424 case bo_lt_equal: 425 case bo_gt_equal: 426 /* Comparison -> boolean type */ 427 rtpc = tpc_bool; 428 break; 429 case bo_plus: 430 case bo_minus: 431 case bo_mult: 432 /* Arithmetic -> error */ 433 printf("Error: Binary operation (%d) on characters.\n", 434 binop->bc); 435 stype_note_error(stype); 436 rtpc = tpc_char; 437 break; 438 } 439 440 res_ti = tdata_item_new(tic_tprimitive); 441 res_ti->u.tprimitive = tdata_primitive_new(rtpc); 442 443 *rtitem = res_ti; 444 } 445 446 /** Type a binary operation with int arguments. */ 447 static void stype_binop_int(stype_t *stype, stree_binop_t *binop, 448 tdata_item_t **rtitem) 449 { 450 tprimitive_class_t rtpc; 451 tdata_item_t *res_ti; 452 453 (void) stype; 454 455 switch (binop->bc) { 456 case bo_equal: 457 case bo_notequal: 458 case bo_lt: 459 case bo_gt: 460 case bo_lt_equal: 461 case bo_gt_equal: 462 /* Comparison -> boolean type */ 463 rtpc = tpc_bool; 464 break; 465 case bo_plus: 466 case bo_minus: 467 case bo_mult: 468 /* Arithmetic -> int type */ 469 rtpc = tpc_int; 470 break; 471 } 472 473 res_ti = tdata_item_new(tic_tprimitive); 474 res_ti->u.tprimitive = tdata_primitive_new(rtpc); 475 476 *rtitem = res_ti; 477 } 478 479 /** Type a binary operation with nil arguments. */ 480 static void stype_binop_nil(stype_t *stype, stree_binop_t *binop, 481 tdata_item_t **rtitem) 482 { 483 (void) binop; 484 485 printf("Unimplemented; Binary operation on nil.\n"); 486 stype_note_error(stype); 487 *rtitem = stype_recovery_titem(stype); 488 } 489 490 /** Type a binary operation with string arguments. */ 491 static void stype_binop_string(stype_t *stype, stree_binop_t *binop, 492 tdata_item_t **rtitem) 493 { 494 tprimitive_class_t rtpc; 495 tdata_item_t *res_ti; 496 497 if (binop->bc != bo_plus) { 498 printf("Unimplemented: Binary operation(%d) " 499 "on strings.\n", binop->bc); 500 stype_note_error(stype); 501 *rtitem = stype_recovery_titem(stype); 502 return; 503 } 504 505 rtpc = tpc_string; 506 507 res_ti = tdata_item_new(tic_tprimitive); 508 res_ti->u.tprimitive = tdata_primitive_new(rtpc); 509 510 *rtitem = res_ti; 511 } 512 513 /** Type a binary operation with resource arguments. */ 514 static void stype_binop_resource(stype_t *stype, stree_binop_t *binop, 515 tdata_item_t **rtitem) 516 { 517 tprimitive_class_t rtpc; 518 tdata_item_t *res_ti; 519 520 (void) binop; 521 522 printf("Error: Cannot apply operator to resource type.\n"); 523 stype_note_error(stype); 524 rtpc = tpc_resource; 525 526 res_ti = tdata_item_new(tic_tprimitive); 527 res_ti->u.tprimitive = tdata_primitive_new(rtpc); 528 529 *rtitem = res_ti; 530 } 531 532 /** Type a binary operation with arguments of an object type. */ 377 533 static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop, 378 534 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem) … … 390 546 case bo_equal: 391 547 case bo_notequal: 392 /* Comparing objects -> boolean (XXX int for now) type */ 393 res_ti = tdata_item_new(tic_tprimitive); 394 res_ti->u.tprimitive = tdata_primitive_new(tpc_int); 548 /* Comparing objects -> boolean type */ 549 res_ti = stype_boolean_titem(stype); 395 550 break; 396 551 default: 397 552 printf("Error: Binary operation (%d) on objects.\n", 398 553 binop->bc); 399 res_ti = NULL; 554 stype_note_error(stype); 555 *rtitem = stype_recovery_titem(stype); 400 556 return; 401 557 } … … 451 607 452 608 switch (ta->u.tprimitive->tpc) { 609 case tpc_bool: 610 rtpc = tpc_bool; 611 break; 453 612 case tpc_int: 454 613 rtpc = tpc_int; … … 480 639 */ 481 640 run_texpr(stype->program, stype->current_csi, new_op->texpr, rtitem); 641 642 if ((*rtitem)->tic == tic_ignore) { 643 /* An error occured when evaluating the type expression. */ 644 stype_note_error(stype); 645 } 482 646 } 483 647 … … 510 674 case tic_tarray: 511 675 stype_access_tarray(stype, access, arg_ti, rtitem); 512 break;513 case tic_tgeneric:514 stype_access_tgeneric(stype, access, arg_ti, rtitem);515 676 break; 516 677 case tic_tfun: … … 566 727 printf("' has no member named '%s'.\n", 567 728 strtab_get_str(access->member_name->sid)); 729 stype_note_error(stype); 568 730 *rtitem = stype_recovery_titem(stype); 569 731 return; … … 615 777 616 778 printf("Error: Unimplemented: Accessing array type '"); 617 tdata_item_print(arg_ti);618 printf("'.\n");619 stype_note_error(stype);620 *rtitem = stype_recovery_titem(stype);621 }622 623 /** Type a generic access operation. */624 static void stype_access_tgeneric(stype_t *stype, stree_access_t *access,625 tdata_item_t *arg_ti, tdata_item_t **rtitem)626 {627 (void) stype;628 (void) access;629 (void) rtitem;630 631 printf("Error: Unimplemented: Accessing generic type '");632 779 tdata_item_print(arg_ti); 633 780 printf("'.\n"); … … 792 939 stype_index_tarray(stype, index, base_ti, rtitem); 793 940 break; 794 case tic_tgeneric:795 stype_index_tgeneric(stype, index, base_ti, rtitem);796 break;797 941 case tic_tfun: 798 942 printf("Error: Indexing a function.\n"); … … 821 965 if (tprimitive->tpc == tpc_string) { 822 966 titem = tdata_item_new(tic_tprimitive); 823 titem->u.tprimitive = tdata_primitive_new(tpc_ int);967 titem->u.tprimitive = tdata_primitive_new(tpc_char); 824 968 *rtitem = titem; 825 969 return; … … 914 1058 } 915 1059 916 /** Type a generic indexing operation. */917 static void stype_index_tgeneric(stype_t *stype, stree_index_t *index,918 tdata_item_t *base_ti, tdata_item_t **rtitem)919 {920 (void) stype;921 (void) index;922 (void) rtitem;923 924 printf("Error: Unimplemented: Indexing generic type '");925 tdata_item_print(base_ti);926 printf("'.\n");927 stype_note_error(stype);928 *rtitem = stype_recovery_titem(stype);929 }930 931 1060 /** Type an assignment. */ 932 1061 static void stype_assign(stype_t *stype, stree_assign_t *assign, -
uspace/app/sbi/src/symbol.c
r23de644 r074444f 68 68 return b; 69 69 case tc_tapply: 70 printf("Internal error: Generic types not implemented.\n");71 exit(1);70 return symbol_xlookup_in_csi(prog, scope, 71 texpr->u.tapply->gtype); 72 72 default: 73 73 assert(b_false); -
uspace/app/sbi/src/tdata.c
r23de644 r074444f 31 31 #include <stdlib.h> 32 32 #include <assert.h> 33 #include "list.h" 33 34 #include "mytypes.h" 34 35 #include "stree.h" … … 40 41 static void tdata_tobject_print(tdata_object_t *tobject); 41 42 static void tdata_tarray_print(tdata_array_t *tarray); 42 static void tdata_tgeneric_print(tdata_generic_t *tgeneric);43 43 static void tdata_tfun_print(tdata_fun_t *tfun); 44 44 … … 142 142 tdata_tarray_print(titem->u.tarray); 143 143 break; 144 case tic_tgeneric:145 tdata_tgeneric_print(titem->u.tgeneric);146 break;147 144 case tic_tfun: 148 145 tdata_tfun_print(titem->u.tfun); … … 157 154 { 158 155 switch (tprimitive->tpc) { 156 case tpc_bool: printf("bool"); break; 157 case tpc_char: printf("char"); break; 159 158 case tpc_int: printf("int"); break; 160 159 case tpc_nil: printf("nil"); break; … … 167 166 { 168 167 stree_symbol_t *csi_sym; 168 list_node_t *arg_n; 169 tdata_item_t *arg; 169 170 170 171 csi_sym = csi_to_symbol(tobject->csi); 171 172 assert(csi_sym != NULL); 172 173 symbol_print_fqn(csi_sym); 174 175 arg_n = list_first(&tobject->targs); 176 while (arg_n != NULL) { 177 arg = list_node_data(arg_n, tdata_item_t *); 178 putchar('/'); 179 tdata_item_print(arg); 180 arg_n = list_next(&tobject->targs, arg_n); 181 } 173 182 } 174 183 … … 185 194 } 186 195 187 static void tdata_tgeneric_print(tdata_generic_t *tgeneric)188 {189 (void) tgeneric;190 printf("unimplemented(generic)");191 }192 193 196 static void tdata_tfun_print(tdata_fun_t *tfun) 194 197 { -
uspace/app/sbi/src/tdata_t.h
r23de644 r074444f 34 34 /** Class of primitive type. */ 35 35 typedef enum { 36 /** Boolean type */ 37 tpc_bool, 38 /** Character type */ 39 tpc_char, 36 40 /** Integer type */ 37 41 tpc_int, … … 57 61 /** CSI definition */ 58 62 struct stree_csi *csi; 63 64 /** (Real) type arguments */ 65 list_t targs; /* of tdata_item_t */ 59 66 } tdata_object_t; 60 67 … … 70 77 list_t extents; /* of stree_expr_t */ 71 78 } tdata_array_t; 72 73 /** Generic type. */74 typedef struct {75 } tdata_generic_t;76 79 77 80 /** Functional type. */ … … 90 93 /** Array type item */ 91 94 tic_tarray, 92 /** Generic type item */93 tic_tgeneric,94 95 /** Function type item */ 95 96 tic_tfun, … … 106 107 tdata_object_t *tobject; 107 108 tdata_array_t *tarray; 108 tdata_generic_t *tgeneric;109 109 tdata_fun_t *tfun; 110 110 } u; -
uspace/dist/src/sysel/demos/list.sy
r23de644 r074444f 27 27 -- 28 28 29 -- Doubly-linked list implementation. 30 class List is 31 var head : ListNode; 32 33 -- Initialize list. 34 fun Init() is 35 head = new ListNode(); 36 head.prev = head; 37 head.next = head; 38 end 39 40 -- Append new entry at the end of the list. 41 fun Append(data : int) is 42 var n : ListNode; 43 var ntl : ListNode; 44 45 ntl = head.prev; 46 47 n = new ListNode(); 48 n.value = data; 49 50 n.prev = ntl; 51 n.next = head; 52 n.head = head; 53 54 ntl.next = n; 55 head.prev = n; 56 end 57 58 -- Return first node in the list or @c nil if there is none. 59 prop First : ListNode is 60 get is 61 return get_first(); 62 end 63 end 64 65 -- Return first node in the list or @c nil if there is none. 66 fun get_first() : ListNode is 67 if head.next == head then 68 return nil; 69 else 70 return head.next; 71 end 72 end 73 end 74 75 class ListNode is 76 var value : int; 77 78 var prev : ListNode; 79 var next : ListNode; 80 var head : ListNode; 81 82 -- Value stored in this node. 83 prop Value : int is 84 get is 85 return value; 86 end 87 end 88 89 -- Previous node in list. 90 prop Prev : ListNode is 91 get is 92 return get_prev(); 93 end 94 end 95 96 -- Next node in list. 97 prop Next : ListNode is 98 get is 99 return get_next(); 100 end 101 end 102 103 -- Get next node. 104 fun get_next() : ListNode is 105 if next != head then 106 return next; 107 else 108 return nil; 109 end 110 end 111 112 -- Get previous node. 113 fun get_prev() : ListNode is 114 if prev != head then 115 return next; 116 else 117 return nil; 118 end 119 end 120 121 end 122 29 -- Using the List class from the library. 123 30 class ListDemo is 124 31 fun Main() is 125 32 var list : List; 33 var i : Int; 126 34 127 35 list = new List(); 128 36 list.Init(); 129 37 130 list.Append(5); 131 list.Append(6); 132 list.Append(7); 133 list.Append(8); 38 -- We need autoboxing or generics to get rid of this mess. 39 i = new Int(); i.Value = 5; list.Append(i); 40 i = new Int(); i.Value = 6; list.Append(i); 41 i = new Int(); i.Value = 7; list.Append(i); 42 i = new Int(); i.Value = 8; list.Append(i); 134 43 135 44 var n : ListNode; … … 137 46 n = list.First; 138 47 while n != nil do 139 Builtin.WriteLine( n.value);48 Builtin.WriteLine((n.value as Int).Value); 140 49 n = n.Next; 141 50 end
Note:
See TracChangeset
for help on using the changeset viewer.