Changeset 37f527b in mainline
- Timestamp:
- 2010-03-26T21:55:23Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4204ad9
- Parents:
- b535aeb
- Location:
- uspace
- Files:
-
- 4 added
- 40 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/Makefile
rb535aeb r37f527b 34 34 35 35 SOURCES = \ 36 src/builtin/bi_fun.c \ 37 src/builtin/bi_textfile.c \ 36 38 src/os/helenos.c \ 37 39 src/ancr.c \ 38 40 src/builtin.c \ 41 src/imode.c \ 39 42 src/input.c \ 40 43 src/intmap.c \ -
uspace/app/sbi/src/ancr.c
rb535aeb r37f527b 50 50 #include <stdlib.h> 51 51 #include <assert.h> 52 #include "builtin.h" 52 53 #include "list.h" 53 54 #include "mytypes.h" … … 120 121 stree_symbol_t *base_sym; 121 122 stree_csi_t *base_csi, *outer_csi; 123 stree_csi_t *gf_class; 122 124 123 125 if (node->ancr_state == ws_visited) { … … 137 139 138 140 outer_csi = csi_to_symbol(node)->outer_csi; 141 gf_class = builtin_get_gf_class(prog->builtin); 139 142 140 143 /* Process outer CSI */ … … 151 154 /* Process base CSI. */ 152 155 ancr_csi_process(prog, base_csi); 156 } else if (node != gf_class) { 157 /* Implicit inheritance from grandfather class. */ 158 base_csi = gf_class; 153 159 } else { 160 /* Grandfather class has no base class. */ 154 161 base_csi = NULL; 155 162 } -
uspace/app/sbi/src/builtin.c
rb535aeb r37f527b 27 27 */ 28 28 29 /** @file Builtin procedures. */29 /** @file Builtin symbol binding. */ 30 30 31 31 #include <stdio.h> 32 32 #include <stdlib.h> 33 33 #include <assert.h> 34 #include "ancr.h" 35 #include "builtin/bi_fun.h" 36 #include "builtin/bi_textfile.h" 37 #include "input.h" 38 #include "intmap.h" 39 #include "lex.h" 34 40 #include "list.h" 35 41 #include "mytypes.h" 36 42 #include "os/os.h" 43 #include "parse.h" 37 44 #include "run.h" 38 45 #include "stree.h" … … 42 49 #include "builtin.h" 43 50 44 static stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name); 45 static void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name); 46 static void builtin_fun_add_vararg(stree_symbol_t *fun_sym, const char *name, 47 stree_texpr_t *type); 48 49 static stree_texpr_t *builtin_mktype_string_array(void); 50 51 static void builtin_write_line(run_t *run); 52 static void builtin_exec(run_t *run); 53 54 static stree_symbol_t *bi_write_line; 55 static stree_symbol_t *bi_exec; 51 static builtin_t *builtin_new(void); 56 52 57 53 /** Declare builtin symbols in the program. … … 61 57 void builtin_declare(stree_program_t *program) 62 58 { 63 stree_modm_t *modm; 59 builtin_t *bi; 60 61 bi = builtin_new(); 62 bi->program = program; 63 program->builtin = bi; 64 65 /* 66 * Declare grandfather class. 67 */ 68 69 builtin_code_snippet(bi, 70 "class Object is\n" 71 "end\n"); 72 bi->gf_class = builtin_find_lvl0(bi, "Object"); 73 74 /* 75 * Declare other builtin classes/functions. 76 */ 77 78 bi_fun_declare(bi); 79 bi_textfile_declare(bi); 80 81 /* Need to process ancestry so that symbol lookups work. */ 82 ancr_module_process(program, program->module); 83 84 bi_fun_bind(bi); 85 bi_textfile_bind(bi); 86 } 87 88 /** Get grandfather class. */ 89 stree_csi_t *builtin_get_gf_class(builtin_t *builtin) 90 { 91 if (builtin->gf_class == NULL) 92 return NULL; 93 94 return symbol_to_csi(builtin->gf_class); 95 } 96 97 static builtin_t *builtin_new(void) 98 { 99 builtin_t *builtin; 100 101 builtin = calloc(1, sizeof(builtin_t)); 102 if (builtin == NULL) { 103 printf("Memory allocation failed.\n"); 104 exit(1); 105 } 106 107 return builtin; 108 } 109 110 /** Parse a declaration code snippet. 111 * 112 * Parses a piece of code from a string at the module level. This can be 113 * used to declare builtin symbols easily and without need for an external 114 * file. 115 */ 116 void builtin_code_snippet(builtin_t *bi, const char *snippet) 117 { 118 input_t *input; 119 lex_t lex; 120 parse_t parse; 121 122 input_new_string(&input, snippet); 123 lex_init(&lex, input); 124 parse_init(&parse, bi->program, &lex); 125 parse_module(&parse); 126 } 127 128 /** Simplifed search for a global symbol. */ 129 stree_symbol_t *builtin_find_lvl0(builtin_t *bi, const char *sym_name) 130 { 131 stree_symbol_t *sym; 132 stree_ident_t *ident; 133 134 ident = stree_ident_new(); 135 136 ident->sid = strtab_get_sid(sym_name); 137 sym = symbol_lookup_in_csi(bi->program, NULL, ident); 138 139 return sym; 140 } 141 142 /** Simplifed search for a level 1 symbol. */ 143 stree_symbol_t *builtin_find_lvl1(builtin_t *bi, const char *csi_name, 144 const char *sym_name) 145 { 146 stree_symbol_t *csi_sym; 64 147 stree_csi_t *csi; 148 149 stree_symbol_t *mbr_sym; 65 150 stree_ident_t *ident; 66 stree_symbol_t *symbol;67 151 68 152 ident = stree_ident_new(); 69 ident->sid = strtab_get_sid("Builtin"); 70 71 csi = stree_csi_new(csi_class);72 csi ->name = ident;73 list_init(&csi->members);74 75 modm = stree_modm_new(mc_csi);76 m odm->u.csi = csi;77 78 symbol = stree_symbol_new(sc_csi);79 symbol->u.csi = csi; 80 symbol->outer_csi = NULL; 81 csi->symbol = symbol; 82 83 list_append(&program->module->members, modm); 84 85 /* Declare builtin procedures. */86 87 bi_write_line = builtin_declare_fun(csi, "WriteLine");88 builtin_fun_add_arg(bi_write_line, "arg");89 90 bi_exec = builtin_declare_fun(csi, "Exec");91 builtin_fun_add_vararg(bi_exec, "args", 92 builtin_mktype_string_array());153 154 ident->sid = strtab_get_sid(csi_name); 155 csi_sym = symbol_lookup_in_csi(bi->program, NULL, ident); 156 csi = symbol_to_csi(csi_sym); 157 assert(csi != NULL); 158 159 ident->sid = strtab_get_sid(sym_name); 160 mbr_sym = symbol_lookup_in_csi(bi->program, csi, ident); 161 162 return mbr_sym; 163 } 164 165 void builtin_fun_bind(builtin_t *bi, const char *csi_name, 166 const char *sym_name, builtin_proc_t bproc) 167 { 168 stree_symbol_t *fun_sym; 169 stree_fun_t *fun; 170 171 fun_sym = builtin_find_lvl1(bi, csi_name, sym_name); 172 assert(fun_sym != NULL); 173 fun = symbol_to_fun(fun_sym); 174 assert(fun != NULL); 175 176 fun->proc->bi_handler = bproc; 93 177 } 94 178 … … 96 180 { 97 181 stree_symbol_t *fun_sym; 182 builtin_t *bi; 183 builtin_proc_t bproc; 98 184 99 185 #ifdef DEBUG_RUN_TRACE … … 101 187 #endif 102 188 fun_sym = proc->outer_symbol; 103 104 if (fun_sym == bi_write_line) { 105 builtin_write_line(run); 106 } else if (fun_sym == bi_exec) { 107 builtin_exec(run); 108 } else { 109 assert(b_false); 189 bi = run->program->builtin; 190 191 bproc = proc->bi_handler; 192 if (bproc == NULL) { 193 printf("Error: Unrecognized builtin function '"); 194 symbol_print_fqn(fun_sym); 195 printf("'.\n"); 196 exit(1); 110 197 } 198 199 /* Run the builtin procedure handler. */ 200 (*bproc)(run); 201 } 202 203 /** Get pointer to member var of current object. */ 204 rdata_var_t *builtin_get_self_mbr_var(run_t *run, const char *mbr_name) 205 { 206 run_proc_ar_t *proc_ar; 207 rdata_object_t *object; 208 sid_t mbr_name_sid; 209 rdata_var_t *mbr_var; 210 211 proc_ar = run_get_current_proc_ar(run); 212 assert(proc_ar->obj->vc == vc_object); 213 object = proc_ar->obj->u.object_v; 214 215 mbr_name_sid = strtab_get_sid(mbr_name); 216 mbr_var = intmap_get(&object->fields, mbr_name_sid); 217 assert(mbr_var != NULL); 218 219 return mbr_var; 111 220 } 112 221 113 222 /** Declare a builtin function in @a csi. */ 114 st atic stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name)223 stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name) 115 224 { 116 225 stree_ident_t *ident; … … 143 252 144 253 /** Add one formal parameter to function. */ 145 staticvoid builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name)254 void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name) 146 255 { 147 256 stree_proc_arg_t *proc_arg; … … 158 267 list_append(&fun->args, proc_arg); 159 268 } 160 161 /** Add variadic formal parameter to function. */162 static void builtin_fun_add_vararg(stree_symbol_t *fun_sym, const char *name,163 stree_texpr_t *type)164 {165 stree_proc_arg_t *proc_arg;166 stree_fun_t *fun;167 168 fun = symbol_to_fun(fun_sym);169 assert(fun != NULL);170 171 proc_arg = stree_proc_arg_new();172 proc_arg->name = stree_ident_new();173 proc_arg->name->sid = strtab_get_sid(name);174 proc_arg->type = type;175 176 fun->varg = proc_arg;177 }178 179 /** Construct a @c string[] type expression. */180 static stree_texpr_t *builtin_mktype_string_array(void)181 {182 stree_texpr_t *tstring;183 stree_texpr_t *tsarray;184 stree_tliteral_t *tliteral;185 stree_tindex_t *tindex;186 187 /* Construct @c string */188 tstring = stree_texpr_new(tc_tliteral);189 tliteral = stree_tliteral_new(tlc_string);190 tstring->u.tliteral = tliteral;191 192 /* Construct the indexing node */193 tsarray = stree_texpr_new(tc_tindex);194 tindex = stree_tindex_new();195 tsarray->u.tindex = tindex;196 197 tindex->base_type = tstring;198 tindex->n_args = 1;199 list_init(&tindex->args);200 201 return tsarray;202 }203 204 static void builtin_write_line(run_t *run)205 {206 rdata_var_t *var;207 208 #ifdef DEBUG_RUN_TRACE209 printf("Called Builtin.WriteLine()\n");210 #endif211 var = run_local_vars_lookup(run, strtab_get_sid("arg"));212 assert(var);213 214 switch (var->vc) {215 case vc_int:216 printf("%d\n", var->u.int_v->value);217 break;218 case vc_string:219 printf("%s\n", var->u.string_v->value);220 break;221 default:222 printf("Unimplemented: writeLine() with unsupported type.\n");223 exit(1);224 }225 }226 227 /** Start an executable and wait for it to finish. */228 static void builtin_exec(run_t *run)229 {230 rdata_var_t *args;231 rdata_var_t *var;232 rdata_array_t *array;233 rdata_var_t *arg;234 int idx, dim;235 char **cmd;236 237 #ifdef DEBUG_RUN_TRACE238 printf("Called Builtin.Exec()\n");239 #endif240 args = run_local_vars_lookup(run, strtab_get_sid("args"));241 242 assert(args);243 assert(args->vc == vc_ref);244 245 var = args->u.ref_v->vref;246 assert(var->vc == vc_array);247 248 array = var->u.array_v;249 assert(array->rank == 1);250 dim = array->extent[0];251 252 if (dim == 0) {253 printf("Error: Builtin.Exec() expects at least one argument.\n");254 exit(1);255 }256 257 cmd = calloc(dim + 1, sizeof(char *));258 if (cmd == NULL) {259 printf("Memory allocation failed.\n");260 exit(1);261 }262 263 for (idx = 0; idx < dim; ++idx) {264 arg = array->element[idx];265 if (arg->vc != vc_string) {266 printf("Error: Argument to Builtin.Exec() must be "267 "string (found %d).\n", arg->vc);268 exit(1);269 }270 271 cmd[idx] = arg->u.string_v->value;272 }273 274 cmd[dim] = '\0';275 276 if (os_exec(cmd) != EOK) {277 printf("Error: Exec failed.\n");278 exit(1);279 }280 } -
uspace/app/sbi/src/builtin.h
rb535aeb r37f527b 33 33 34 34 void builtin_declare(stree_program_t *program); 35 void builtin_code_snippet(builtin_t *bi, const char *snippet); 36 37 stree_csi_t *builtin_get_gf_class(builtin_t *builtin); 35 38 void builtin_run_proc(run_t *run, stree_proc_t *proc); 36 39 40 rdata_var_t *builtin_get_self_mbr_var(run_t *run, const char *mbr_name); 41 42 stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name); 43 void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name); 44 45 stree_symbol_t *builtin_find_lvl0(builtin_t *bi, const char *sym_name); 46 stree_symbol_t *builtin_find_lvl1(builtin_t *bi, const char *csi_name, 47 const char *sym_name); 48 49 void builtin_fun_bind(builtin_t *bi, const char *csi_name, 50 const char *sym_name, builtin_proc_t bproc); 51 37 52 #endif -
uspace/app/sbi/src/input.c
rb535aeb r37f527b 42 42 #define INPUT_BUFFER_SIZE 256 43 43 44 static int input_init(input_t *input, char *fname); 44 static int input_init_file(input_t *input, char *fname); 45 static void input_init_interactive(input_t *input); 46 static void input_init_string(input_t *input, const char *str); 45 47 46 /** Create new input object . */47 int input_new (input_t **input, char *fname)48 /** Create new input object for reading from file. */ 49 int input_new_file(input_t **input, char *fname) 48 50 { 49 51 *input = malloc(sizeof(input_t)); … … 51 53 return ENOMEM; 52 54 53 return input_init (*input, fname);55 return input_init_file(*input, fname); 54 56 } 55 57 56 /** Initialize input object. */ 57 static int input_init(input_t *input, char *fname) 58 /** Create new input object for reading from interactive input. */ 59 int input_new_interactive(input_t **input) 60 { 61 *input = malloc(sizeof(input_t)); 62 if (*input == NULL) 63 return ENOMEM; 64 65 input_init_interactive(*input); 66 return EOK; 67 } 68 69 /** Create new input object for reading from string. */ 70 int input_new_string(input_t **input, const char *str) 71 { 72 *input = malloc(sizeof(input_t)); 73 if (*input == NULL) 74 return ENOMEM; 75 76 input_init_string(*input, str); 77 return EOK; 78 } 79 80 /** Initialize input object for reading from file. */ 81 static int input_init_file(input_t *input, char *fname) 58 82 { 59 83 FILE *f; … … 74 98 } 75 99 100 /** Initialize input object for reading from interactive input. */ 101 static void input_init_interactive(input_t *input) 102 { 103 input->buffer = malloc(INPUT_BUFFER_SIZE); 104 if (input->buffer == NULL) { 105 printf("Memory allocation failed.\n"); 106 exit(1); 107 } 108 109 input->line_no = 0; 110 input->fin = NULL; 111 } 112 113 /** Initialize input object for reading from string. */ 114 static void input_init_string(input_t *input, const char *str) 115 { 116 input->buffer = malloc(INPUT_BUFFER_SIZE); 117 if (input->buffer == NULL) { 118 printf("Memory allocation failed.\n"); 119 exit(1); 120 } 121 122 input->str = str; 123 input->line_no = 0; 124 input->fin = NULL; 125 } 126 76 127 /** Get next line of input. */ 77 128 int input_get_line(input_t *input, char **line) 78 129 { 79 if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL) 80 input->buffer[0] = '\0'; 130 const char *sp; 131 char *dp; 132 size_t cnt; 81 133 82 if (ferror(input->fin)) 83 return EIO; 134 if (input->fin != NULL) { 135 /* Reading from file. */ 136 if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL) 137 input->buffer[0] = '\0'; 84 138 85 *line = input->buffer; 139 if (ferror(input->fin)) 140 return EIO; 141 142 *line = input->buffer; 143 } else if (input->str != NULL) { 144 /* Reading from a string constant. */ 145 146 /* Copy one line. */ 147 sp = input->str; 148 dp = input->buffer; 149 cnt = 0; 150 while (*sp != '\n' && *sp != '\0' && 151 cnt < INPUT_BUFFER_SIZE - 2) { 152 *dp++ = *sp++; 153 } 154 155 /* Advance to start of next line. */ 156 if (*sp == '\n') 157 *dp++ = *sp++; 158 159 *dp++ = '\0'; 160 input->str = sp; 161 *line = input->buffer; 162 } else { 163 /* Interactive mode */ 164 if (input->line_no == 0) 165 printf("sbi> "); 166 else 167 printf("... "); 168 169 if (fgets(input->buffer, INPUT_BUFFER_SIZE, stdin) == NULL) 170 input->buffer[0] = '\0'; 171 172 if (ferror(stdin)) 173 return EIO; 174 175 *line = input->buffer; 176 } 177 86 178 ++input->line_no; 87 179 return EOK; -
uspace/app/sbi/src/input.h
rb535aeb r37f527b 32 32 #include "mytypes.h" 33 33 34 int input_new(input_t **input, char *fname); 34 int input_new_file(input_t **input, char *fname); 35 int input_new_interactive(input_t **input); 36 int input_new_string(input_t **input, const char *str); 37 35 38 int input_get_line(input_t *input, char **line); 36 39 int input_get_line_no(input_t *input); -
uspace/app/sbi/src/input_t.h
rb535aeb r37f527b 34 34 /** Input state object */ 35 35 typedef struct input { 36 /** Input file */36 /** Input file if reading from file. */ 37 37 FILE *fin; 38 38 39 /** Buffer holding current line */ 39 /** Input string if reading from string. */ 40 const char *str; 41 42 /** Buffer holding current line. */ 40 43 char *buffer; 41 44 -
uspace/app/sbi/src/lex.c
rb535aeb r37f527b 44 44 #define TAB_WIDTH 8 45 45 46 static bool_t lex_next_try(lex_t *lex); 46 static void lex_touch(lex_t *lex); 47 static bool_t lex_read_try(lex_t *lex); 47 48 48 49 static void lex_skip_comment(lex_t *lex); … … 72 73 /** Keyword names. Used both for printing and recognition. */ 73 74 static struct lc_name keywords[] = { 75 { lc_as, "as" }, 76 { lc_builtin, "builtin" }, 74 77 { lc_class, "class" }, 75 78 { lc_constructor, "constructor" }, … … 96 99 { lc_public, "public" }, 97 100 { lc_raise, "raise" }, 101 { lc_resource, "resource" }, 98 102 { lc_return, "return" }, 99 103 { lc_self, "self" }, … … 221 225 lex->ibp = lex->inbuf; 222 226 lex->col_adj = 0; 223 } 224 225 /** Read next lexical element. */ 227 lex->current_valid = b_true; 228 } 229 230 /** Advance to next lexical element. 231 * 232 * The new element be read in lazily then it is actually accessed. 233 */ 226 234 void lex_next(lex_t *lex) 227 235 { 236 /* Make sure the current lem has already been read in. */ 237 lex_touch(lex); 238 239 /* Force a new lem to be read on next access. */ 240 lex->current_valid = b_false; 241 } 242 243 /** Get current lem. 244 * 245 * The returned pointer is invalidated by next call to lex_next() 246 */ 247 lem_t *lex_get_current(lex_t *lex) 248 { 249 lex_touch(lex); 250 return &lex->current; 251 } 252 253 /** Read in the current lexical element (unless already read in). */ 254 static void lex_touch(lex_t *lex) 255 { 228 256 bool_t got_lem; 229 257 258 if (lex->current_valid == b_true) 259 return; 260 230 261 do { 231 got_lem = lex_ next_try(lex);262 got_lem = lex_read_try(lex); 232 263 } while (got_lem == b_false); 264 265 lex->current_valid = b_true; 233 266 } 234 267 … … 237 270 * @return @c b_true on success or @c b_false if it needs restarting. 238 271 */ 239 static bool_t lex_ next_try(lex_t *lex)272 static bool_t lex_read_try(lex_t *lex) 240 273 { 241 274 char *bp; -
uspace/app/sbi/src/lex.h
rb535aeb r37f527b 38 38 void lex_init(lex_t *lex, struct input *input); 39 39 void lex_next(lex_t *lex); 40 lem_t *lex_get_current(lex_t *lex); 40 41 41 42 #endif -
uspace/app/sbi/src/lex_t.h
rb535aeb r37f527b 40 40 41 41 /* Keywords */ 42 lc_as, 43 lc_builtin, 42 44 lc_class, 43 45 lc_constructor, … … 64 66 lc_public, 65 67 lc_raise, 68 lc_resource, 66 69 lc_return, 67 70 lc_self, … … 149 152 int col_adj; 150 153 151 /** Curent lem */ 154 /** @c b_true if we have the next lem in @c current */ 155 bool_t current_valid; 156 157 /** Curent lem (only valid if @c current_valid is true) */ 152 158 lem_t current; 153 159 } lex_t; -
uspace/app/sbi/src/main.c
rb535aeb r37f527b 33 33 #include "ancr.h" 34 34 #include "builtin.h" 35 #include "imode.h" 35 36 #include "mytypes.h" 36 37 #include "strtab.h" … … 54 55 int rc; 55 56 57 if (argc == 1) { 58 /* Enter interactive mode */ 59 strtab_init(); 60 imode_run(); 61 return 0; 62 } 63 56 64 if (argc != 2) { 57 65 syntax_print(); … … 59 67 } 60 68 61 rc = input_new (&input, argv[1]);69 rc = input_new_file(&input, argv[1]); 62 70 if (rc != EOK) { 63 71 printf("Failed opening source file '%s'.\n", argv[1]); -
uspace/app/sbi/src/mytypes.h
rb535aeb r37f527b 47 47 #define EOK 0 48 48 49 #include "builtin_t.h" 49 50 #include "input_t.h" 50 51 #include "intmap_t.h" -
uspace/app/sbi/src/p_expr.c
rb535aeb r37f527b 49 49 static stree_expr_t *parse_pf_call(parse_t *parse, stree_expr_t *a); 50 50 static stree_expr_t *parse_pf_index(parse_t *parse, stree_expr_t *a); 51 static stree_expr_t *parse_pf_as(parse_t *parse, stree_expr_t *a); 51 52 static stree_expr_t *parse_primitive(parse_t *parse); 52 53 static stree_expr_t *parse_nameref(parse_t *parse); … … 206 207 207 208 while (lcur_lc(parse) == lc_period || lcur_lc(parse) == lc_lparen || 208 lcur_lc(parse) == lc_lsbr ) {209 lcur_lc(parse) == lc_lsbr || lcur_lc(parse) == lc_as) { 209 210 210 211 switch (lcur_lc(parse)) { … … 218 219 tmp = parse_pf_index(parse, a); 219 220 break; 221 case lc_as: 222 tmp = parse_pf_as(parse, a); 223 break; 220 224 default: 221 225 assert(b_false); … … 316 320 } 317 321 322 /** Parse @c as operator. */ 323 static stree_expr_t *parse_pf_as(parse_t *parse, stree_expr_t *a) 324 { 325 stree_expr_t *expr; 326 stree_texpr_t *texpr; 327 stree_as_t *as_op; 328 329 lmatch(parse, lc_as); 330 texpr = parse_texpr(parse); 331 332 as_op = stree_as_new(); 333 as_op->arg = a; 334 as_op->dtype = texpr; 335 expr = stree_expr_new(ec_as); 336 expr->u.as_op = as_op; 337 338 return expr; 339 } 340 318 341 /** Parse primitive expression. */ 319 342 static stree_expr_t *parse_primitive(parse_t *parse) -
uspace/app/sbi/src/p_type.c
rb535aeb r37f527b 179 179 case lc_int: 180 180 case lc_string: 181 case lc_resource: 181 182 texpr = stree_texpr_new(tc_tliteral); 182 183 texpr->u.tliteral = parse_tliteral(parse); … … 203 204 tlc = tlc_string; 204 205 break; 206 case lc_resource: 207 tlc = tlc_resource; 208 break; 205 209 default: 206 210 assert(b_false); -
uspace/app/sbi/src/parse.c
rb535aeb r37f527b 43 43 #include "stree.h" 44 44 #include "strtab.h" 45 #include "symbol.h" 45 46 46 47 #include "parse.h" … … 49 50 * Module members 50 51 */ 51 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass); 52 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass, 53 stree_csi_t *outer_csi); 52 54 static stree_csimbr_t *parse_csimbr(parse_t *parse, stree_csi_t *outer_csi); 53 55 54 static stree_fun_t *parse_fun(parse_t *parse); 55 static stree_var_t *parse_var(parse_t *parse); 56 static stree_prop_t *parse_prop(parse_t *parse); 56 static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi); 57 static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi); 58 static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi); 59 60 static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse); 57 61 58 62 static stree_proc_arg_t *parse_proc_arg(parse_t *parse); … … 63 67 */ 64 68 static stree_block_t *parse_block(parse_t *parse); 65 static stree_stat_t *parse_stat(parse_t *parse);66 69 67 70 static stree_vdecl_t *parse_vdecl(parse_t *parse); … … 89 92 stree_csi_t *csi; 90 93 stree_modm_t *modm; 91 stree_symbol_t *symbol;92 94 93 95 while (lcur_lc(parse) != lc_eof) { … … 96 98 case lc_struct: 97 99 case lc_interface: 98 csi = parse_csi(parse, lcur_lc(parse) );100 csi = parse_csi(parse, lcur_lc(parse), NULL); 99 101 modm = stree_modm_new(mc_csi); 100 102 modm->u.csi = csi; 101 102 symbol = stree_symbol_new(sc_csi);103 symbol->u.csi = csi;104 symbol->outer_csi = NULL;105 csi->symbol = symbol;106 103 107 104 list_append(&parse->cur_mod->members, modm); … … 117 114 118 115 /** Parse class, struct or interface declaration. */ 119 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass) 116 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass, 117 stree_csi_t *outer_csi) 120 118 { 121 119 stree_csi_t *csi; 122 120 csi_class_t cc; 123 121 stree_csimbr_t *csimbr; 122 stree_symbol_t *symbol; 124 123 125 124 switch (dclass) { … … 134 133 csi = stree_csi_new(cc); 135 134 csi->name = parse_ident(parse); 135 136 symbol = stree_symbol_new(sc_csi); 137 symbol->u.csi = csi; 138 symbol->outer_csi = outer_csi; 139 csi->symbol = symbol; 136 140 137 141 #ifdef DEBUG_PARSE_TRACE … … 171 175 stree_prop_t *prop; 172 176 173 stree_symbol_t *symbol;174 175 177 switch (lcur_lc(parse)) { 176 178 case lc_class: 177 179 case lc_struct: 178 180 case lc_interface: 179 csi = parse_csi(parse, lcur_lc(parse) );181 csi = parse_csi(parse, lcur_lc(parse), outer_csi); 180 182 csimbr = stree_csimbr_new(csimbr_csi); 181 183 csimbr->u.csi = csi; 182 183 symbol = stree_symbol_new(sc_csi);184 symbol->u.csi = csi;185 symbol->outer_csi = outer_csi;186 csi->symbol = symbol;187 184 break; 188 185 case lc_fun: 189 fun = parse_fun(parse );186 fun = parse_fun(parse, outer_csi); 190 187 csimbr = stree_csimbr_new(csimbr_fun); 191 188 csimbr->u.fun = fun; 192 193 symbol = stree_symbol_new(sc_fun);194 symbol->u.fun = fun;195 symbol->outer_csi = outer_csi;196 fun->symbol = symbol;197 fun->proc->outer_symbol = symbol;198 189 break; 199 190 case lc_var: 200 var = parse_var(parse );191 var = parse_var(parse, outer_csi); 201 192 csimbr = stree_csimbr_new(csimbr_var); 202 193 csimbr->u.var = var; 203 204 symbol = stree_symbol_new(sc_var);205 symbol->u.var = var;206 symbol->outer_csi = outer_csi;207 var->symbol = symbol;208 194 break; 209 195 case lc_prop: 210 prop = parse_prop(parse );196 prop = parse_prop(parse, outer_csi); 211 197 csimbr = stree_csimbr_new(csimbr_prop); 212 198 csimbr->u.prop = prop; 213 214 symbol = stree_symbol_new(sc_prop);215 symbol->u.prop = prop;216 symbol->outer_csi = outer_csi;217 prop->symbol = symbol;218 if (prop->getter)219 prop->getter->outer_symbol = symbol;220 if (prop->setter)221 prop->setter->outer_symbol = symbol;222 199 break; 223 200 default: … … 231 208 232 209 /** Parse member function. */ 233 static stree_fun_t *parse_fun(parse_t *parse )210 static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi) 234 211 { 235 212 stree_fun_t *fun; 236 213 stree_proc_arg_t *arg; 214 stree_symbol_t *symbol; 215 stree_symbol_attr_t *attr; 237 216 238 217 fun = stree_fun_new(); 218 symbol = stree_symbol_new(sc_fun); 219 220 symbol->u.fun = fun; 221 symbol->outer_csi = outer_csi; 222 fun->symbol = symbol; 239 223 240 224 lmatch(parse, lc_fun); … … 277 261 } 278 262 279 lmatch(parse, lc_is); 263 list_init(&symbol->attr); 264 265 /* Parse attributes. */ 266 while (lcur_lc(parse) == lc_comma) { 267 lskip(parse); 268 attr = parse_symbol_attr(parse); 269 list_append(&symbol->attr, attr); 270 } 271 280 272 fun->proc = stree_proc_new(); 281 fun->proc->body = parse_block(parse); 282 lmatch(parse, lc_end); 273 fun->proc->outer_symbol = symbol; 274 275 if (lcur_lc(parse) == lc_scolon) { 276 lskip(parse); 277 278 /* This function has no body. */ 279 if (!stree_symbol_has_attr(symbol, sac_builtin)) { 280 printf("Error: Function '"); 281 symbol_print_fqn(symbol); 282 printf("' has no body.\n"); 283 exit(1); 284 } 285 fun->proc->body = NULL; 286 } else { 287 lmatch(parse, lc_is); 288 fun->proc->body = parse_block(parse); 289 lmatch(parse, lc_end); 290 } 283 291 284 292 return fun; … … 286 294 287 295 /** Parse member variable. */ 288 static stree_var_t *parse_var(parse_t *parse )296 static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi) 289 297 { 290 298 stree_var_t *var; 299 stree_symbol_t *symbol; 291 300 292 301 var = stree_var_new(); 302 symbol = stree_symbol_new(sc_var); 303 symbol->u.var = var; 304 symbol->outer_csi = outer_csi; 305 var->symbol = symbol; 293 306 294 307 lmatch(parse, lc_var); … … 302 315 303 316 /** Parse member property. */ 304 static stree_prop_t *parse_prop(parse_t *parse )317 static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi) 305 318 { 306 319 stree_prop_t *prop; 320 stree_symbol_t *symbol; 321 307 322 stree_ident_t *ident; 308 323 stree_proc_arg_t *arg; … … 310 325 prop = stree_prop_new(); 311 326 list_init(&prop->args); 327 328 symbol = stree_symbol_new(sc_prop); 329 symbol->u.prop = prop; 330 symbol->outer_csi = outer_csi; 331 prop->symbol = symbol; 312 332 313 333 lmatch(parse, lc_prop); … … 363 383 prop->getter = stree_proc_new(); 364 384 prop->getter->body = parse_block(parse); 385 prop->getter->outer_symbol = symbol; 365 386 366 387 lmatch(parse, lc_end); … … 380 401 prop->setter = stree_proc_new(); 381 402 prop->setter->body = parse_block(parse); 403 prop->setter->outer_symbol = symbol; 382 404 383 405 lmatch(parse, lc_end); … … 393 415 } 394 416 417 /** Parse symbol attribute. */ 418 static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse) 419 { 420 stree_symbol_attr_t *attr; 421 422 if (lcur_lc(parse) != lc_builtin) { 423 printf("Error: Unexpected attribute '"); 424 lem_print(lcur(parse)); 425 printf("'.\n"); 426 exit(1); 427 } 428 429 lskip(parse); 430 431 attr = stree_symbol_attr_new(sac_builtin); 432 return attr; 433 } 434 395 435 /** Parse formal function argument. */ 396 436 static stree_proc_arg_t *parse_proc_arg(parse_t *parse) … … 455 495 456 496 /** Parse statement. */ 457 st atic stree_stat_t *parse_stat(parse_t *parse)497 stree_stat_t *parse_stat(parse_t *parse) 458 498 { 459 499 stree_stat_t *stat; … … 719 759 lem_t *lcur(parse_t *parse) 720 760 { 721 return &parse->lex->current;761 return lex_get_current(parse->lex); 722 762 } 723 763 … … 725 765 lclass_t lcur_lc(parse_t *parse) 726 766 { 727 return parse->lex->current.lclass; 767 lem_t *lem; 768 769 lem = lcur(parse); 770 return lem->lclass; 728 771 } 729 772 -
uspace/app/sbi/src/parse.h
rb535aeb r37f527b 35 35 void parse_module(parse_t *parse); 36 36 37 stree_stat_t *parse_stat(parse_t *parse); 37 38 stree_ident_t *parse_ident(parse_t *parse); 38 39 -
uspace/app/sbi/src/rdata.c
rb535aeb r37f527b 42 42 static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest); 43 43 static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest); 44 static void rdata_resource_copy(rdata_resource_t *src, 45 rdata_resource_t **dest); 44 46 45 47 static int rdata_array_get_dim(rdata_array_t *array); … … 241 243 242 244 return string_v; 245 } 246 247 rdata_resource_t *rdata_resource_new(void) 248 { 249 rdata_resource_t *resource_v; 250 251 resource_v = calloc(1, sizeof(rdata_resource_t)); 252 if (resource_v == NULL) { 253 printf("Memory allocation failed.\n"); 254 exit(1); 255 } 256 257 return resource_v; 243 258 } 244 259 … … 306 321 rdata_object_copy(src->u.object_v, &nvar->u.object_v); 307 322 break; 323 case vc_resource: 324 rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v); 325 break; 308 326 } 309 327 … … 348 366 printf("Unimplemented: Copy object.\n"); 349 367 exit(1); 368 } 369 370 static void rdata_resource_copy(rdata_resource_t *src, rdata_resource_t **dest) 371 { 372 *dest = rdata_resource_new(); 373 (*dest)->data = src->data; 350 374 } 351 375 … … 389 413 case vc_array: var->u.array_v = nvar->u.array_v; break; 390 414 case vc_object: var->u.object_v = nvar->u.object_v; break; 415 case vc_resource: var->u.resource_v = nvar->u.resource_v; break; 391 416 } 392 417 -
uspace/app/sbi/src/rdata.h
rb535aeb r37f527b 47 47 rdata_int_t *rdata_int_new(void); 48 48 rdata_string_t *rdata_string_new(void); 49 rdata_resource_t *rdata_resource_new(void); 49 50 50 51 void rdata_array_alloc_element(rdata_array_t *array); -
uspace/app/sbi/src/rdata_t.h
rb535aeb r37f527b 44 44 } rdata_int_t; 45 45 46 46 47 /** String variable */ 47 48 typedef struct { … … 92 93 } rdata_object_t; 93 94 95 /** Resource handle 96 * 97 * Binding to external data. This type can be used to refer to data used 98 * by builtin functions (such as files). 99 */ 100 typedef struct { 101 /** Only understood by the right builtin function. */ 102 void *data; 103 } rdata_resource_t; 104 94 105 typedef enum var_class { 95 106 /** Integer */ … … 109 120 110 121 /** Object */ 111 vc_object 122 vc_object, 123 124 /** Interpreter builtin resource */ 125 vc_resource 112 126 } var_class_t; 113 127 … … 128 142 rdata_array_t *array_v; 129 143 rdata_object_t *object_v; 144 rdata_resource_t *resource_v; 130 145 } u; 131 146 } rdata_var_t; -
uspace/app/sbi/src/run.c
rb535aeb r37f527b 48 48 49 49 static void run_block(run_t *run, stree_block_t *block); 50 static void run_stat(run_t *run, stree_stat_t *stat);51 50 static void run_exps(run_t *run, stree_exps_t *exps); 52 51 static void run_vdecl(run_t *run, stree_vdecl_t *vdecl); … … 132 131 #ifdef DEBUG_RUN_TRACE 133 132 printf("Start executing function '"); 134 symbol_print_fqn(proc _sym);133 symbol_print_fqn(proc->outer_symbol); 135 134 printf("'.\n"); 136 135 #endif … … 159 158 #ifdef DEBUG_RUN_TRACE 160 159 printf("Done executing procedure '"); 161 symbol_print_fqn(proc );160 symbol_print_fqn(proc->outer_symbol); 162 161 printf("'.\n"); 163 162 … … 216 215 217 216 /** Run statement. */ 218 staticvoid run_stat(run_t *run, stree_stat_t *stat)217 void run_stat(run_t *run, stree_stat_t *stat) 219 218 { 220 219 #ifdef DEBUG_RUN_TRACE -
uspace/app/sbi/src/run.h
rb535aeb r37f527b 35 35 void run_program(run_t *run, stree_program_t *prog); 36 36 void run_proc(run_t *run, run_proc_ar_t *proc_ar, rdata_item_t **res); 37 void run_stat(run_t *run, stree_stat_t *stat); 37 38 38 39 void run_print_fun_bt(run_t *run); -
uspace/app/sbi/src/run_expr.c
rb535aeb r37f527b 96 96 rdata_item_t *base, list_t *args, rdata_item_t **res); 97 97 static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res); 98 static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res); 98 99 99 100 /** Evaluate expression. */ … … 134 135 case ec_assign: 135 136 run_assign(run, expr->u.assign, res); 137 break; 138 case ec_as: 139 run_as(run, expr->u.as_op, res); 136 140 break; 137 141 } … … 1360 1364 } 1361 1365 1362 1363 1366 /** Execute assignment. */ 1364 1367 static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res) … … 1387 1390 1388 1391 *res = NULL; 1392 } 1393 1394 /** Execute @c as conversion. */ 1395 static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res) 1396 { 1397 rdata_item_t *rarg_i; 1398 rdata_item_t *rarg_vi; 1399 rdata_item_t *rarg_di; 1400 rdata_var_t *arg_vref; 1401 tdata_item_t *dtype; 1402 run_proc_ar_t *proc_ar; 1403 1404 stree_symbol_t *obj_csi_sym; 1405 stree_csi_t *obj_csi; 1406 1407 #ifdef DEBUG_RUN_TRACE 1408 printf("Run @c as conversion operation.\n"); 1409 #endif 1410 run_expr(run, as_op->arg, &rarg_i); 1411 1412 /* 1413 * This should always be a reference if the argument is indeed 1414 * a class instance. 1415 */ 1416 assert(run_item_get_vc(run, rarg_i) == vc_ref); 1417 run_cvt_value_item(run, rarg_i, &rarg_vi); 1418 assert(rarg_vi->ic == ic_value); 1419 1420 if (rarg_vi->u.value->var->u.ref_v->vref == NULL) { 1421 /* Nil reference is always okay. */ 1422 *res = rarg_vi; 1423 return; 1424 } 1425 1426 run_dereference(run, rarg_vi, &rarg_di); 1427 1428 /* Now we should have a variable address. */ 1429 assert(rarg_di->ic == ic_address); 1430 assert(rarg_di->u.address->ac == ac_var); 1431 1432 arg_vref = rarg_di->u.address->u.var_a->vref; 1433 1434 proc_ar = run_get_current_proc_ar(run); 1435 /* XXX Memoize to avoid recomputing. */ 1436 run_texpr(run->program, proc_ar->proc->outer_symbol->outer_csi, 1437 as_op->dtype, &dtype); 1438 1439 assert(arg_vref->vc == vc_object); 1440 obj_csi_sym = arg_vref->u.object_v->class_sym; 1441 obj_csi = symbol_to_csi(obj_csi_sym); 1442 assert(obj_csi != NULL); 1443 1444 if (tdata_is_csi_derived_from_ti(obj_csi, dtype) != b_true) { 1445 printf("Error: Run-time type conversion error. Object is " 1446 "of type '"); 1447 symbol_print_fqn(obj_csi_sym); 1448 printf("' which is not derived from '"); 1449 tdata_item_print(dtype); 1450 printf("'.\n"); 1451 exit(1); 1452 } 1453 1454 *res = rarg_vi; 1389 1455 } 1390 1456 -
uspace/app/sbi/src/run_t.h
rb535aeb r37f527b 30 30 #define RUN_T_H_ 31 31 32 #include "intmap_t.h" 32 33 #include "list_t.h" 33 34 -
uspace/app/sbi/src/run_texpr.c
rb535aeb r37f527b 165 165 case tlc_int: tpc = tpc_int; break; 166 166 case tlc_string: tpc = tpc_string; break; 167 case tlc_resource: tpc = tpc_resource; break; 167 168 } 168 169 -
uspace/app/sbi/src/stree.c
rb535aeb r37f527b 136 136 } 137 137 138 stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac) 139 { 140 stree_symbol_attr_t *symbol_attr; 141 142 symbol_attr = calloc(1, sizeof(stree_symbol_attr_t)); 143 if (symbol_attr == NULL) { 144 printf("Memory allocation failed.\n"); 145 exit(1); 146 } 147 148 symbol_attr->sac = sac; 149 return symbol_attr; 150 } 151 138 152 stree_proc_t *stree_proc_new(void) 139 153 { … … 414 428 } 415 429 430 stree_as_t *stree_as_new(void) 431 { 432 stree_as_t *as_expr; 433 434 as_expr = calloc(1, sizeof(stree_as_t)); 435 if (as_expr == NULL) { 436 printf("Memory allocation failed.\n"); 437 exit(1); 438 } 439 440 return as_expr; 441 } 442 416 443 stree_nameref_t *stree_nameref_new(void) 417 444 { … … 572 599 573 600 return program; 601 } 602 603 /** Determine if @a symbol has attribute of class @a sac. */ 604 bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac) 605 { 606 list_node_t *node; 607 stree_symbol_attr_t *attr; 608 609 node = list_first(&symbol->attr); 610 while (node != NULL) { 611 attr = list_node_data(node, stree_symbol_attr_t *); 612 if (attr->sac == sac) 613 return b_true; 614 615 node = list_next(&symbol->attr, node); 616 } 617 618 return b_false; 574 619 } 575 620 -
uspace/app/sbi/src/stree.h
rb535aeb r37f527b 40 40 stree_prop_t *stree_prop_new(void); 41 41 42 stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac); 43 42 44 stree_proc_t *stree_proc_new(void); 43 45 stree_proc_arg_t *stree_proc_arg_new(void); … … 64 66 stree_call_t *stree_call_new(void); 65 67 stree_index_t *stree_index_new(void); 68 stree_as_t *stree_as_new(void); 66 69 stree_nameref_t *stree_nameref_new(void); 67 70 … … 80 83 stree_program_t *stree_program_new(void); 81 84 85 bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac); 82 86 bool_t stree_arg_has_attr(stree_proc_arg_t *arg, arg_attr_class_t aac); 83 87 bool_t stree_is_csi_derived_from_csi(stree_csi_t *a, stree_csi_t *b); -
uspace/app/sbi/src/stree_t.h
rb535aeb r37f527b 31 31 32 32 #include "list_t.h" 33 #include "builtin_t.h" 33 34 34 35 /* … … 156 157 list_t args; /* of stree_expr_t */ 157 158 } stree_index_t; 159 160 /** @c as conversion operation */ 161 typedef struct { 162 /** Expression to convert */ 163 struct stree_expr *arg; 164 /** Destination type of conversion. */ 165 struct stree_texpr *dtype; 166 } stree_as_t; 158 167 159 168 /** Arithmetic expression class */ … … 168 177 ec_call, 169 178 ec_assign, 170 ec_index 179 ec_index, 180 ec_as 171 181 } expr_class_t; 172 182 … … 188 198 stree_index_t *index; 189 199 stree_assign_t *assign; 200 stree_as_t *as_op; 190 201 } u; 191 202 } stree_expr_t; … … 200 211 typedef enum { 201 212 tlc_int, 213 tlc_resource, 202 214 tlc_string 203 215 } tliteral_class_t; … … 388 400 struct stree_symbol *outer_symbol; 389 401 390 /** Main block */402 /** Main block for regular procedures */ 391 403 stree_block_t *body; 404 405 /** Builtin handler for builtin procedures */ 406 builtin_proc_t bi_handler; 392 407 } stree_proc_t; 393 408 … … 512 527 } stree_module_t; 513 528 529 /** Symbol attribute class */ 530 typedef enum { 531 /** Builtin symbol (interpreter hook) */ 532 sac_builtin 533 } symbol_attr_class_t; 534 535 /** Symbol atribute */ 536 typedef struct { 537 symbol_attr_class_t sac; 538 } stree_symbol_attr_t; 539 540 514 541 typedef enum { 515 542 sc_csi, … … 539 566 /** Containing block (for block-level symbols) */ 540 567 stree_block_t *outer_block; 568 569 /** Symbol attributes. */ 570 list_t attr; /* of stree_symbol_attr_t */ 541 571 } stree_symbol_t; 542 572 … … 545 575 /** The one and only module in the program */ 546 576 stree_module_t *module; 577 578 /** Builtin symbols binding. */ 579 struct builtin *builtin; 547 580 } stree_program_t; 548 581 -
uspace/app/sbi/src/stype.c
rb535aeb r37f527b 54 54 55 55 static void stype_block(stype_t *stype, stree_block_t *block); 56 static void stype_stat(stype_t *stype, stree_stat_t *stat);57 56 58 57 static void stype_vdecl(stype_t *stype, stree_vdecl_t *vdecl_s); … … 253 252 254 253 /** Type statement */ 255 staticvoid stype_stat(stype_t *stype, stree_stat_t *stat)254 void stype_stat(stype_t *stype, stree_stat_t *stat) 256 255 { 257 256 #ifdef DEBUG_TYPE_TRACE -
uspace/app/sbi/src/stype.h
rb535aeb r37f527b 33 33 34 34 void stype_module(stype_t *stype, stree_module_t *module); 35 void stype_stat(stype_t *stype, stree_stat_t *stat); 35 36 36 37 stree_expr_t *stype_convert(stype_t *stype, stree_expr_t *expr, -
uspace/app/sbi/src/stype_expr.c
rb535aeb r37f527b 90 90 static void stype_assign(stype_t *stype, stree_assign_t *assign, 91 91 tdata_item_t **rtitem); 92 static void stype_as(stype_t *stype, stree_as_t *as_op, tdata_item_t **rtitem); 92 93 93 94 … … 96 97 { 97 98 tdata_item_t *et; 99 98 100 #ifdef DEBUG_TYPE_TRACE 99 101 printf("Type expression.\n"); 100 102 #endif 103 /* Silence warning. */ 104 et = NULL; 105 101 106 switch (expr->ec) { 102 107 case ec_nameref: stype_nameref(stype, expr->u.nameref, &et); break; … … 110 115 case ec_index: stype_index(stype, expr->u.index, &et); break; 111 116 case ec_assign: stype_assign(stype, expr->u.assign, &et); break; 117 case ec_as: stype_as(stype, expr->u.as_op, &et); break; 112 118 } 113 119 … … 337 343 rtpc = tpc_string; 338 344 break; 345 case tpc_resource: 346 printf("Error: Cannot apply operator to resource type.\n"); 347 exit(1); 339 348 } 340 349 … … 829 838 *rtitem = NULL; 830 839 } 840 841 /** Type @c as conversion. */ 842 static void stype_as(stype_t *stype, stree_as_t *as_op, tdata_item_t **rtitem) 843 { 844 tdata_item_t *titem; 845 846 #ifdef DEBUG_TYPE_TRACE 847 printf("Evaluate type of @c as conversion.\n"); 848 #endif 849 stype_expr(stype, as_op->arg); 850 run_texpr(stype->program, stype->current_csi, as_op->dtype, &titem); 851 852 /* Check that target type is derived from argument type. */ 853 if (tdata_is_ti_derived_from_ti(titem, as_op->arg->titem) != b_true) { 854 printf("Error: Target of 'as' operator '"); 855 tdata_item_print(titem); 856 printf("' is not derived from '"); 857 tdata_item_print(as_op->arg->titem); 858 printf("'.\n"); 859 exit(1); 860 } 861 862 *rtitem = titem; 863 } -
uspace/app/sbi/src/symbol.c
rb535aeb r37f527b 329 329 if (symbol->outer_csi != NULL) { 330 330 outer_sym = csi_to_symbol(symbol->outer_csi); 331 symbol_print_fqn( 331 symbol_print_fqn(outer_sym); 332 332 printf("."); 333 333 } -
uspace/app/sbi/src/tdata.c
rb535aeb r37f527b 60 60 } 61 61 62 /** 63 * Determine if CSI described by type item @a a is derived from CSI described 64 * by type item @a tb. 65 */ 66 bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb) 67 { 68 bool_t res; 69 70 switch (ta->tic) { 71 case tic_tobject: 72 res = tdata_is_csi_derived_from_ti(ta->u.tobject->csi, tb); 73 break; 74 default: 75 printf("Error: Derived type is not a CSI.\n"); 76 exit(1); 77 } 78 79 return res; 80 } 81 62 82 /** Determine if two type items are equal (i.e. describe the same type). */ 63 83 bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b) … … 137 157 case tpc_nil: printf("nil"); break; 138 158 case tpc_string: printf("string"); break; 159 case tpc_resource: printf("resource"); break; 139 160 } 140 161 } -
uspace/app/sbi/src/tdata.h
rb535aeb r37f527b 39 39 40 40 bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb); 41 bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb); 41 42 bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b); 42 43 void tdata_item_print(tdata_item_t *titem); -
uspace/app/sbi/src/tdata_t.h
rb535aeb r37f527b 39 39 tpc_nil, 40 40 /** String type */ 41 tpc_string 41 tpc_string, 42 /** Resource type */ 43 tpc_resource 42 44 } tprimitive_class_t; 43 45 -
uspace/dist/sysel/except.sy
rb535aeb r37f527b 28 28 29 29 class ExceptionDemo is 30 fun foo() : intis30 fun foo() is 31 31 Builtin.WriteLine("Entered foo()."); 32 32 raise new BaseException(); -
uspace/dist/sysel/hexec.sy
rb535aeb r37f527b 29 29 class HelenOSExecDemo is 30 30 fun Main() is 31 Builtin.Exec("/app/tester");32 Builtin.Exec("/app/tester", "print1");31 Task.Exec("/app/tester"); 32 Task.Exec("/app/tester", "print1"); 33 33 end 34 34 end -
uspace/dist/sysel/inherit.sy
rb535aeb r37f527b 45 45 fun Main() is 46 46 var a : A; 47 var c : C; 47 48 49 -- Construct and assign object. 48 50 a = new A(); 49 51 a.Foo(); 50 52 53 -- Implicit conversion to base type. 51 54 a = new B(); 52 55 a.Foo(); … … 54 57 a = new C(); 55 58 a.Foo(); 59 60 -- Test 'as' operator for conversion to derived type. 61 c = a as C; 62 63 -- Test grandfather class. 64 var d : Object; 65 d = a; 66 d = new B(); 67 d = c; 56 68 end 57 69 end -
uspace/dist/sysel/list.sy
rb535aeb r37f527b 27 27 -- 28 28 29 -- Doubly-linked list implementation. 29 30 class List is 30 31 var head : ListNode; 31 32 33 -- Initialize list. 32 34 fun Init() is 33 35 head = new ListNode(); … … 36 38 end 37 39 40 -- Append new entry at the end of the list. 38 41 fun Append(data : int) is 39 42 var n : ListNode; … … 53 56 end 54 57 55 fun GetFirst() : ListNode is 56 return head.next; 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 57 72 end 58 73 end … … 65 80 var head : ListNode; 66 81 67 fun GetNext() : ListNode is 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 68 105 if next != head then 69 106 return next; … … 73 110 end 74 111 75 fun GetPrev() : ListNode is 112 -- Get previous node. 113 fun get_prev() : ListNode is 76 114 if prev != head then 77 115 return next; … … 81 119 end 82 120 83 fun GetValue() : int is84 return value;85 end86 121 end 87 122 … … 100 135 var n : ListNode; 101 136 102 n = list. GetFirst();137 n = list.First; 103 138 while n != nil do 104 139 Builtin.WriteLine(n.value); 105 n = n. GetNext();140 n = n.Next; 106 141 end 107 142 end -
uspace/dist/sysel/property.sy
rb535aeb r37f527b 27 27 -- 28 28 29 class Ais29 class Foo is 30 30 var x : int; 31 31 … … 68 68 end 69 69 end 70 71 -- 72 -- Class-type property. This is used for demonstrating property 73 -- field access. This case is still quite easy. It does not require 74 -- read-modify-write. Since class is a reference type, access 75 -- operator will read the value and dereference it, thereby 76 -- getting somewhere else (so the value will not be modified and 77 -- need not be written back). 78 -- 79 80 var bprop : Bar; 81 82 prop B : Bar is 83 get is 84 Builtin.WriteLine("Getting B"); 85 return bprop; 86 end 87 set value is 88 Builtin.WriteLine("Setting B"); 89 bprop = value; 90 end 91 end 92 93 end 94 95 class Bar is 96 var i : int; 70 97 end 71 98 72 99 class PropertyDemo is 73 100 fun Main() is 74 var a : A;101 var a : Foo; 75 102 var i : int; 76 103 77 a = new A();104 a = new Foo(); 78 105 79 106 -- Get value of named property. … … 96 123 Builtin.WriteLine("Main(): Got "); 97 124 Builtin.WriteLine(i); 125 126 -- Property field access 127 var b : Bar; 128 129 b = new Bar(); 130 131 b.i = 42; 132 a.bprop = b; 133 134 Builtin.WriteLine(a.bprop.i); 135 a.bprop.i = 2; 136 Builtin.WriteLine(a.bprop.i); 98 137 end 99 138 end -
uspace/dist/sysel/string.sy
rb535aeb r37f527b 31 31 -- Concatenate some strings. 32 32 Builtin.WriteLine("One-" + "two-" + "three!"); 33 34 -- Extract characters from a string. 35 var i : int; 36 i = 0; 37 while i < 5 do 38 Builtin.WriteLine("ABCDE"[i]); 39 i = i + 1; 40 end 33 41 end 34 42 end
Note:
See TracChangeset
for help on using the changeset viewer.