Changeset 37f527b in mainline


Ignore:
Timestamp:
2010-03-26T21:55:23Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4204ad9
Parents:
b535aeb
Message:

Update SBI to rev. 144.

Location:
uspace
Files:
4 added
40 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/Makefile

    rb535aeb r37f527b  
    3434
    3535SOURCES = \
     36        src/builtin/bi_fun.c \
     37        src/builtin/bi_textfile.c \
    3638        src/os/helenos.c \
    3739        src/ancr.c \
    3840        src/builtin.c \
     41        src/imode.c \
    3942        src/input.c \
    4043        src/intmap.c \
  • uspace/app/sbi/src/ancr.c

    rb535aeb r37f527b  
    5050#include <stdlib.h>
    5151#include <assert.h>
     52#include "builtin.h"
    5253#include "list.h"
    5354#include "mytypes.h"
     
    120121        stree_symbol_t *base_sym;
    121122        stree_csi_t *base_csi, *outer_csi;
     123        stree_csi_t *gf_class;
    122124
    123125        if (node->ancr_state == ws_visited) {
     
    137139
    138140        outer_csi = csi_to_symbol(node)->outer_csi;
     141        gf_class = builtin_get_gf_class(prog->builtin);
    139142
    140143        /* Process outer CSI */
     
    151154                /* Process base CSI. */
    152155                ancr_csi_process(prog, base_csi);
     156        } else if (node != gf_class) {
     157                /* Implicit inheritance from grandfather class. */
     158                base_csi = gf_class;
    153159        } else {
     160                /* Grandfather class has no base class. */
    154161                base_csi = NULL;
    155162        }
  • uspace/app/sbi/src/builtin.c

    rb535aeb r37f527b  
    2727 */
    2828
    29 /** @file Builtin procedures. */
     29/** @file Builtin symbol binding. */
    3030
    3131#include <stdio.h>
    3232#include <stdlib.h>
    3333#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"
    3440#include "list.h"
    3541#include "mytypes.h"
    3642#include "os/os.h"
     43#include "parse.h"
    3744#include "run.h"
    3845#include "stree.h"
     
    4249#include "builtin.h"
    4350
    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;
     51static builtin_t *builtin_new(void);
    5652
    5753/** Declare builtin symbols in the program.
     
    6157void builtin_declare(stree_program_t *program)
    6258{
    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. */
     89stree_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
     97static 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 */
     116void 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. */
     129stree_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. */
     143stree_symbol_t *builtin_find_lvl1(builtin_t *bi, const char *csi_name,
     144    const char *sym_name)
     145{
     146        stree_symbol_t *csi_sym;
    64147        stree_csi_t *csi;
     148
     149        stree_symbol_t *mbr_sym;
    65150        stree_ident_t *ident;
    66         stree_symbol_t *symbol;
    67151
    68152        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         modm->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
     165void 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;
    93177}
    94178
     
    96180{
    97181        stree_symbol_t *fun_sym;
     182        builtin_t *bi;
     183        builtin_proc_t bproc;
    98184
    99185#ifdef DEBUG_RUN_TRACE
     
    101187#endif
    102188        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);
    110197        }
     198
     199        /* Run the builtin procedure handler. */
     200        (*bproc)(run);
     201}
     202
     203/** Get pointer to member var of current object. */
     204rdata_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;
    111220}
    112221
    113222/** Declare a builtin function in @a csi. */
    114 static stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name)
     223stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name)
    115224{
    116225        stree_ident_t *ident;
     
    143252
    144253/** Add one formal parameter to function. */
    145 static void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name)
     254void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name)
    146255{
    147256        stree_proc_arg_t *proc_arg;
     
    158267        list_append(&fun->args, proc_arg);
    159268}
    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_TRACE
    209         printf("Called Builtin.WriteLine()\n");
    210 #endif
    211         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_TRACE
    238         printf("Called Builtin.Exec()\n");
    239 #endif
    240         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  
    3333
    3434void builtin_declare(stree_program_t *program);
     35void builtin_code_snippet(builtin_t *bi, const char *snippet);
     36
     37stree_csi_t *builtin_get_gf_class(builtin_t *builtin);
    3538void builtin_run_proc(run_t *run, stree_proc_t *proc);
    3639
     40rdata_var_t *builtin_get_self_mbr_var(run_t *run, const char *mbr_name);
     41
     42stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name);
     43void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name);
     44
     45stree_symbol_t *builtin_find_lvl0(builtin_t *bi, const char *sym_name);
     46stree_symbol_t *builtin_find_lvl1(builtin_t *bi, const char *csi_name,
     47    const char *sym_name);
     48
     49void builtin_fun_bind(builtin_t *bi, const char *csi_name,
     50    const char *sym_name, builtin_proc_t bproc);
     51
    3752#endif
  • uspace/app/sbi/src/input.c

    rb535aeb r37f527b  
    4242#define INPUT_BUFFER_SIZE 256
    4343
    44 static int input_init(input_t *input, char *fname);
     44static int input_init_file(input_t *input, char *fname);
     45static void input_init_interactive(input_t *input);
     46static void input_init_string(input_t *input, const char *str);
    4547
    46 /** Create new input object. */
    47 int input_new(input_t **input, char *fname)
     48/** Create new input object for reading from file. */
     49int input_new_file(input_t **input, char *fname)
    4850{
    4951        *input = malloc(sizeof(input_t));
     
    5153                return ENOMEM;
    5254
    53         return input_init(*input, fname);
     55        return input_init_file(*input, fname);
    5456}
    5557
    56 /** Initialize input object. */
    57 static int input_init(input_t *input, char *fname)
     58/** Create new input object for reading from interactive input. */
     59int 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. */
     70int 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. */
     81static int input_init_file(input_t *input, char *fname)
    5882{
    5983        FILE *f;
     
    7498}
    7599
     100/** Initialize input object for reading from interactive input. */
     101static 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. */
     114static 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
    76127/** Get next line of input. */
    77128int input_get_line(input_t *input, char **line)
    78129{
    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;
    81133
    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';
    84138
    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
    86178        ++input->line_no;
    87179        return EOK;
  • uspace/app/sbi/src/input.h

    rb535aeb r37f527b  
    3232#include "mytypes.h"
    3333
    34 int input_new(input_t **input, char *fname);
     34int input_new_file(input_t **input, char *fname);
     35int input_new_interactive(input_t **input);
     36int input_new_string(input_t **input, const char *str);
     37
    3538int input_get_line(input_t *input, char **line);
    3639int input_get_line_no(input_t *input);
  • uspace/app/sbi/src/input_t.h

    rb535aeb r37f527b  
    3434/** Input state object */
    3535typedef struct input {
    36         /** Input file */
     36        /** Input file if reading from file. */
    3737        FILE *fin;
    3838
    39         /** Buffer holding current line */
     39        /** Input string if reading from string. */
     40        const char *str;
     41
     42        /** Buffer holding current line. */
    4043        char *buffer;
    4144
  • uspace/app/sbi/src/lex.c

    rb535aeb r37f527b  
    4444#define TAB_WIDTH 8
    4545
    46 static bool_t lex_next_try(lex_t *lex);
     46static void lex_touch(lex_t *lex);
     47static bool_t lex_read_try(lex_t *lex);
    4748
    4849static void lex_skip_comment(lex_t *lex);
     
    7273/** Keyword names. Used both for printing and recognition. */
    7374static struct lc_name keywords[] = {
     75        { lc_as,        "as" },
     76        { lc_builtin,   "builtin" },
    7477        { lc_class,     "class" },
    7578        { lc_constructor,       "constructor" },
     
    9699        { lc_public,    "public" },
    97100        { lc_raise,     "raise" },
     101        { lc_resource,  "resource" },
    98102        { lc_return,    "return" },
    99103        { lc_self,      "self" },
     
    221225        lex->ibp = lex->inbuf;
    222226        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 */
    226234void lex_next(lex_t *lex)
    227235{
     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 */
     247lem_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). */
     254static void lex_touch(lex_t *lex)
     255{
    228256        bool_t got_lem;
    229257
     258        if (lex->current_valid == b_true)
     259                return;
     260
    230261        do {
    231                 got_lem = lex_next_try(lex);
     262                got_lem = lex_read_try(lex);
    232263        } while (got_lem == b_false);
     264
     265        lex->current_valid = b_true;
    233266}
    234267
     
    237270 * @return @c b_true on success or @c b_false if it needs restarting.
    238271 */
    239 static bool_t lex_next_try(lex_t *lex)
     272static bool_t lex_read_try(lex_t *lex)
    240273{
    241274        char *bp;
  • uspace/app/sbi/src/lex.h

    rb535aeb r37f527b  
    3838void lex_init(lex_t *lex, struct input *input);
    3939void lex_next(lex_t *lex);
     40lem_t *lex_get_current(lex_t *lex);
    4041
    4142#endif
  • uspace/app/sbi/src/lex_t.h

    rb535aeb r37f527b  
    4040
    4141        /* Keywords */
     42        lc_as,
     43        lc_builtin,
    4244        lc_class,
    4345        lc_constructor,
     
    6466        lc_public,
    6567        lc_raise,
     68        lc_resource,
    6669        lc_return,
    6770        lc_self,
     
    149152        int col_adj;
    150153
    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) */
    152158        lem_t current;
    153159} lex_t;
  • uspace/app/sbi/src/main.c

    rb535aeb r37f527b  
    3333#include "ancr.h"
    3434#include "builtin.h"
     35#include "imode.h"
    3536#include "mytypes.h"
    3637#include "strtab.h"
     
    5455        int rc;
    5556
     57        if (argc == 1) {
     58                /* Enter interactive mode */
     59                strtab_init();
     60                imode_run();
     61                return 0;
     62        }
     63
    5664        if (argc != 2) {
    5765                syntax_print();
     
    5967        }
    6068
    61         rc = input_new(&input, argv[1]);
     69        rc = input_new_file(&input, argv[1]);
    6270        if (rc != EOK) {
    6371                printf("Failed opening source file '%s'.\n", argv[1]);
  • uspace/app/sbi/src/mytypes.h

    rb535aeb r37f527b  
    4747#define EOK 0
    4848
     49#include "builtin_t.h"
    4950#include "input_t.h"
    5051#include "intmap_t.h"
  • uspace/app/sbi/src/p_expr.c

    rb535aeb r37f527b  
    4949static stree_expr_t *parse_pf_call(parse_t *parse, stree_expr_t *a);
    5050static stree_expr_t *parse_pf_index(parse_t *parse, stree_expr_t *a);
     51static stree_expr_t *parse_pf_as(parse_t *parse, stree_expr_t *a);
    5152static stree_expr_t *parse_primitive(parse_t *parse);
    5253static stree_expr_t *parse_nameref(parse_t *parse);
     
    206207
    207208        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) {
    209210
    210211                switch (lcur_lc(parse)) {
     
    218219                        tmp = parse_pf_index(parse, a);
    219220                        break;
     221                case lc_as:
     222                        tmp = parse_pf_as(parse, a);
     223                        break;
    220224                default:
    221225                        assert(b_false);
     
    316320}
    317321
     322/** Parse @c as operator. */
     323static 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
    318341/** Parse primitive expression. */
    319342static stree_expr_t *parse_primitive(parse_t *parse)
  • uspace/app/sbi/src/p_type.c

    rb535aeb r37f527b  
    179179        case lc_int:
    180180        case lc_string:
     181        case lc_resource:
    181182                texpr = stree_texpr_new(tc_tliteral);
    182183                texpr->u.tliteral = parse_tliteral(parse);
     
    203204                tlc = tlc_string;
    204205                break;
     206        case lc_resource:
     207                tlc = tlc_resource;
     208                break;
    205209        default:
    206210                assert(b_false);
  • uspace/app/sbi/src/parse.c

    rb535aeb r37f527b  
    4343#include "stree.h"
    4444#include "strtab.h"
     45#include "symbol.h"
    4546
    4647#include "parse.h"
     
    4950 * Module members
    5051 */
    51 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass);
     52static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass,
     53    stree_csi_t *outer_csi);
    5254static stree_csimbr_t *parse_csimbr(parse_t *parse, stree_csi_t *outer_csi);
    5355
    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);
     56static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi);
     57static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi);
     58static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi);
     59
     60static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse);
    5761
    5862static stree_proc_arg_t *parse_proc_arg(parse_t *parse);
     
    6367 */
    6468static stree_block_t *parse_block(parse_t *parse);
    65 static stree_stat_t *parse_stat(parse_t *parse);
    6669
    6770static stree_vdecl_t *parse_vdecl(parse_t *parse);
     
    8992        stree_csi_t *csi;
    9093        stree_modm_t *modm;
    91         stree_symbol_t *symbol;
    9294
    9395        while (lcur_lc(parse) != lc_eof) {
     
    9698                case lc_struct:
    9799                case lc_interface:
    98                         csi = parse_csi(parse, lcur_lc(parse));
     100                        csi = parse_csi(parse, lcur_lc(parse), NULL);
    99101                        modm = stree_modm_new(mc_csi);
    100102                        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;
    106103
    107104                        list_append(&parse->cur_mod->members, modm);
     
    117114
    118115/** Parse class, struct or interface declaration. */
    119 static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass)
     116static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass,
     117    stree_csi_t *outer_csi)
    120118{
    121119        stree_csi_t *csi;
    122120        csi_class_t cc;
    123121        stree_csimbr_t *csimbr;
     122        stree_symbol_t *symbol;
    124123
    125124        switch (dclass) {
     
    134133        csi = stree_csi_new(cc);
    135134        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;
    136140
    137141#ifdef DEBUG_PARSE_TRACE
     
    171175        stree_prop_t *prop;
    172176
    173         stree_symbol_t *symbol;
    174 
    175177        switch (lcur_lc(parse)) {
    176178        case lc_class:
    177179        case lc_struct:
    178180        case lc_interface:
    179                 csi = parse_csi(parse, lcur_lc(parse));
     181                csi = parse_csi(parse, lcur_lc(parse), outer_csi);
    180182                csimbr = stree_csimbr_new(csimbr_csi);
    181183                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;
    187184                break;
    188185        case lc_fun:
    189                 fun = parse_fun(parse);
     186                fun = parse_fun(parse, outer_csi);
    190187                csimbr = stree_csimbr_new(csimbr_fun);
    191188                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;
    198189                break;
    199190        case lc_var:
    200                 var = parse_var(parse);
     191                var = parse_var(parse, outer_csi);
    201192                csimbr = stree_csimbr_new(csimbr_var);
    202193                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;
    208194                break;
    209195        case lc_prop:
    210                 prop = parse_prop(parse);
     196                prop = parse_prop(parse, outer_csi);
    211197                csimbr = stree_csimbr_new(csimbr_prop);
    212198                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;
    222199                break;
    223200        default:
     
    231208
    232209/** Parse member function. */
    233 static stree_fun_t *parse_fun(parse_t *parse)
     210static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi)
    234211{
    235212        stree_fun_t *fun;
    236213        stree_proc_arg_t *arg;
     214        stree_symbol_t *symbol;
     215        stree_symbol_attr_t *attr;
    237216
    238217        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;
    239223
    240224        lmatch(parse, lc_fun);
     
    277261        }
    278262
    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
    280272        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        }
    283291
    284292        return fun;
     
    286294
    287295/** Parse member variable. */
    288 static stree_var_t *parse_var(parse_t *parse)
     296static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi)
    289297{
    290298        stree_var_t *var;
     299        stree_symbol_t *symbol;
    291300
    292301        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;
    293306
    294307        lmatch(parse, lc_var);
     
    302315
    303316/** Parse member property. */
    304 static stree_prop_t *parse_prop(parse_t *parse)
     317static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi)
    305318{
    306319        stree_prop_t *prop;
     320        stree_symbol_t *symbol;
     321
    307322        stree_ident_t *ident;
    308323        stree_proc_arg_t *arg;
     
    310325        prop = stree_prop_new();
    311326        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;
    312332
    313333        lmatch(parse, lc_prop);
     
    363383                        prop->getter = stree_proc_new();
    364384                        prop->getter->body = parse_block(parse);
     385                        prop->getter->outer_symbol = symbol;
    365386
    366387                        lmatch(parse, lc_end);
     
    380401                        prop->setter = stree_proc_new();
    381402                        prop->setter->body = parse_block(parse);
     403                        prop->setter->outer_symbol = symbol;
    382404
    383405                        lmatch(parse, lc_end);
     
    393415}
    394416
     417/** Parse symbol attribute. */
     418static 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
    395435/** Parse formal function argument. */
    396436static stree_proc_arg_t *parse_proc_arg(parse_t *parse)
     
    455495
    456496/** Parse statement. */
    457 static stree_stat_t *parse_stat(parse_t *parse)
     497stree_stat_t *parse_stat(parse_t *parse)
    458498{
    459499        stree_stat_t *stat;
     
    719759lem_t *lcur(parse_t *parse)
    720760{
    721         return &parse->lex->current;
     761        return lex_get_current(parse->lex);
    722762}
    723763
     
    725765lclass_t lcur_lc(parse_t *parse)
    726766{
    727         return parse->lex->current.lclass;
     767        lem_t *lem;
     768
     769        lem = lcur(parse);
     770        return lem->lclass;
    728771}
    729772
  • uspace/app/sbi/src/parse.h

    rb535aeb r37f527b  
    3535void parse_module(parse_t *parse);
    3636
     37stree_stat_t *parse_stat(parse_t *parse);
    3738stree_ident_t *parse_ident(parse_t *parse);
    3839
  • uspace/app/sbi/src/rdata.c

    rb535aeb r37f527b  
    4242static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest);
    4343static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest);
     44static void rdata_resource_copy(rdata_resource_t *src,
     45    rdata_resource_t **dest);
    4446
    4547static int rdata_array_get_dim(rdata_array_t *array);
     
    241243
    242244        return string_v;
     245}
     246
     247rdata_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;
    243258}
    244259
     
    306321                rdata_object_copy(src->u.object_v, &nvar->u.object_v);
    307322                break;
     323        case vc_resource:
     324                rdata_resource_copy(src->u.resource_v, &nvar->u.resource_v);
     325                break;
    308326        }
    309327
     
    348366        printf("Unimplemented: Copy object.\n");
    349367        exit(1);
     368}
     369
     370static void rdata_resource_copy(rdata_resource_t *src, rdata_resource_t **dest)
     371{
     372        *dest = rdata_resource_new();
     373        (*dest)->data = src->data;
    350374}
    351375
     
    389413        case vc_array: var->u.array_v = nvar->u.array_v; break;
    390414        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;
    391416        }
    392417
  • uspace/app/sbi/src/rdata.h

    rb535aeb r37f527b  
    4747rdata_int_t *rdata_int_new(void);
    4848rdata_string_t *rdata_string_new(void);
     49rdata_resource_t *rdata_resource_new(void);
    4950
    5051void rdata_array_alloc_element(rdata_array_t *array);
  • uspace/app/sbi/src/rdata_t.h

    rb535aeb r37f527b  
    4444} rdata_int_t;
    4545
     46
    4647/** String variable */
    4748typedef struct {
     
    9293} rdata_object_t;
    9394
     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 */
     100typedef struct {
     101        /** Only understood by the right builtin function. */
     102        void *data;
     103} rdata_resource_t;
     104
    94105typedef enum var_class {
    95106        /** Integer */
     
    109120
    110121        /** Object */
    111         vc_object
     122        vc_object,
     123
     124        /** Interpreter builtin resource */
     125        vc_resource
    112126} var_class_t;
    113127
     
    128142                rdata_array_t *array_v;
    129143                rdata_object_t *object_v;
     144                rdata_resource_t *resource_v;
    130145        } u;
    131146} rdata_var_t;
  • uspace/app/sbi/src/run.c

    rb535aeb r37f527b  
    4848
    4949static void run_block(run_t *run, stree_block_t *block);
    50 static void run_stat(run_t *run, stree_stat_t *stat);
    5150static void run_exps(run_t *run, stree_exps_t *exps);
    5251static void run_vdecl(run_t *run, stree_vdecl_t *vdecl);
     
    132131#ifdef DEBUG_RUN_TRACE
    133132        printf("Start executing function '");
    134         symbol_print_fqn(proc_sym);
     133        symbol_print_fqn(proc->outer_symbol);
    135134        printf("'.\n");
    136135#endif
     
    159158#ifdef DEBUG_RUN_TRACE
    160159        printf("Done executing procedure '");
    161         symbol_print_fqn(proc);
     160        symbol_print_fqn(proc->outer_symbol);
    162161        printf("'.\n");
    163162
     
    216215
    217216/** Run statement. */
    218 static void run_stat(run_t *run, stree_stat_t *stat)
     217void run_stat(run_t *run, stree_stat_t *stat)
    219218{
    220219#ifdef DEBUG_RUN_TRACE
  • uspace/app/sbi/src/run.h

    rb535aeb r37f527b  
    3535void run_program(run_t *run, stree_program_t *prog);
    3636void run_proc(run_t *run, run_proc_ar_t *proc_ar, rdata_item_t **res);
     37void run_stat(run_t *run, stree_stat_t *stat);
    3738
    3839void run_print_fun_bt(run_t *run);
  • uspace/app/sbi/src/run_expr.c

    rb535aeb r37f527b  
    9696    rdata_item_t *base, list_t *args, rdata_item_t **res);
    9797static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res);
     98static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res);
    9899
    99100/** Evaluate expression. */
     
    134135        case ec_assign:
    135136                run_assign(run, expr->u.assign, res);
     137                break;
     138        case ec_as:
     139                run_as(run, expr->u.as_op, res);
    136140                break;
    137141        }
     
    13601364}
    13611365
    1362 
    13631366/** Execute assignment. */
    13641367static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res)
     
    13871390
    13881391        *res = NULL;
     1392}
     1393
     1394/** Execute @c as conversion. */
     1395static 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;
    13891455}
    13901456
  • uspace/app/sbi/src/run_t.h

    rb535aeb r37f527b  
    3030#define RUN_T_H_
    3131
     32#include "intmap_t.h"
    3233#include "list_t.h"
    3334
  • uspace/app/sbi/src/run_texpr.c

    rb535aeb r37f527b  
    165165        case tlc_int: tpc = tpc_int; break;
    166166        case tlc_string: tpc = tpc_string; break;
     167        case tlc_resource: tpc = tpc_resource; break;
    167168        }
    168169
  • uspace/app/sbi/src/stree.c

    rb535aeb r37f527b  
    136136}
    137137
     138stree_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
    138152stree_proc_t *stree_proc_new(void)
    139153{
     
    414428}
    415429
     430stree_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
    416443stree_nameref_t *stree_nameref_new(void)
    417444{
     
    572599
    573600        return program;
     601}
     602
     603/** Determine if @a symbol has attribute of class @a sac. */
     604bool_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;
    574619}
    575620
  • uspace/app/sbi/src/stree.h

    rb535aeb r37f527b  
    4040stree_prop_t *stree_prop_new(void);
    4141
     42stree_symbol_attr_t *stree_symbol_attr_new(symbol_attr_class_t sac);
     43
    4244stree_proc_t *stree_proc_new(void);
    4345stree_proc_arg_t *stree_proc_arg_new(void);
     
    6466stree_call_t *stree_call_new(void);
    6567stree_index_t *stree_index_new(void);
     68stree_as_t *stree_as_new(void);
    6669stree_nameref_t *stree_nameref_new(void);
    6770
     
    8083stree_program_t *stree_program_new(void);
    8184
     85bool_t stree_symbol_has_attr(stree_symbol_t *symbol, symbol_attr_class_t sac);
    8286bool_t stree_arg_has_attr(stree_proc_arg_t *arg, arg_attr_class_t aac);
    8387bool_t stree_is_csi_derived_from_csi(stree_csi_t *a, stree_csi_t *b);
  • uspace/app/sbi/src/stree_t.h

    rb535aeb r37f527b  
    3131
    3232#include "list_t.h"
     33#include "builtin_t.h"
    3334
    3435/*
     
    156157        list_t args; /* of stree_expr_t */
    157158} stree_index_t;
     159
     160/** @c as conversion operation */
     161typedef struct {
     162        /** Expression to convert */
     163        struct stree_expr *arg;
     164        /** Destination type of conversion. */
     165        struct stree_texpr *dtype;
     166} stree_as_t;
    158167
    159168/** Arithmetic expression class */
     
    168177        ec_call,
    169178        ec_assign,
    170         ec_index
     179        ec_index,
     180        ec_as
    171181} expr_class_t;
    172182
     
    188198                stree_index_t *index;
    189199                stree_assign_t *assign;
     200                stree_as_t *as_op;
    190201        } u;
    191202} stree_expr_t;
     
    200211typedef enum {
    201212        tlc_int,
     213        tlc_resource,
    202214        tlc_string
    203215} tliteral_class_t;
     
    388400        struct stree_symbol *outer_symbol;
    389401
    390         /** Main block */
     402        /** Main block for regular procedures */
    391403        stree_block_t *body;
     404
     405        /** Builtin handler for builtin procedures */
     406        builtin_proc_t bi_handler;
    392407} stree_proc_t;
    393408
     
    512527} stree_module_t;
    513528
     529/** Symbol attribute class */
     530typedef enum {
     531        /** Builtin symbol (interpreter hook) */
     532        sac_builtin
     533} symbol_attr_class_t;
     534
     535/** Symbol atribute */
     536typedef struct {
     537        symbol_attr_class_t sac;
     538} stree_symbol_attr_t;
     539
     540
    514541typedef enum {
    515542        sc_csi,
     
    539566        /** Containing block (for block-level symbols) */
    540567        stree_block_t *outer_block;
     568
     569        /** Symbol attributes. */
     570        list_t attr; /* of stree_symbol_attr_t */
    541571} stree_symbol_t;
    542572
     
    545575        /** The one and only module in the program */
    546576        stree_module_t *module;
     577
     578        /** Builtin symbols binding. */
     579        struct builtin *builtin;
    547580} stree_program_t;
    548581
  • uspace/app/sbi/src/stype.c

    rb535aeb r37f527b  
    5454
    5555static void stype_block(stype_t *stype, stree_block_t *block);
    56 static void stype_stat(stype_t *stype, stree_stat_t *stat);
    5756
    5857static void stype_vdecl(stype_t *stype, stree_vdecl_t *vdecl_s);
     
    253252
    254253/** Type statement */
    255 static void stype_stat(stype_t *stype, stree_stat_t *stat)
     254void stype_stat(stype_t *stype, stree_stat_t *stat)
    256255{
    257256#ifdef DEBUG_TYPE_TRACE
  • uspace/app/sbi/src/stype.h

    rb535aeb r37f527b  
    3333
    3434void stype_module(stype_t *stype, stree_module_t *module);
     35void stype_stat(stype_t *stype, stree_stat_t *stat);
    3536
    3637stree_expr_t *stype_convert(stype_t *stype, stree_expr_t *expr,
  • uspace/app/sbi/src/stype_expr.c

    rb535aeb r37f527b  
    9090static void stype_assign(stype_t *stype, stree_assign_t *assign,
    9191    tdata_item_t **rtitem);
     92static void stype_as(stype_t *stype, stree_as_t *as_op, tdata_item_t **rtitem);
    9293
    9394
     
    9697{
    9798        tdata_item_t *et;
     99
    98100#ifdef DEBUG_TYPE_TRACE
    99101        printf("Type expression.\n");
    100102#endif
     103        /* Silence warning. */
     104        et = NULL;
     105
    101106        switch (expr->ec) {
    102107        case ec_nameref: stype_nameref(stype, expr->u.nameref, &et); break;
     
    110115        case ec_index: stype_index(stype, expr->u.index, &et); break;
    111116        case ec_assign: stype_assign(stype, expr->u.assign, &et); break;
     117        case ec_as: stype_as(stype, expr->u.as_op, &et); break;
    112118        }
    113119
     
    337343                rtpc = tpc_string;
    338344                break;
     345        case tpc_resource:
     346                printf("Error: Cannot apply operator to resource type.\n");
     347                exit(1);
    339348        }
    340349
     
    829838        *rtitem = NULL;
    830839}
     840
     841/** Type @c as conversion. */
     842static 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  
    329329        if (symbol->outer_csi != NULL) {
    330330                outer_sym = csi_to_symbol(symbol->outer_csi);
    331                 symbol_print_fqn( outer_sym);
     331                symbol_print_fqn(outer_sym);
    332332                printf(".");
    333333        }
  • uspace/app/sbi/src/tdata.c

    rb535aeb r37f527b  
    6060}
    6161
     62/**
     63 * Determine if CSI described by type item @a a is derived from CSI described
     64 * by type item @a tb.
     65 */
     66bool_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
    6282/** Determine if two type items are equal (i.e. describe the same type). */
    6383bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b)
     
    137157        case tpc_nil: printf("nil"); break;
    138158        case tpc_string: printf("string"); break;
     159        case tpc_resource: printf("resource"); break;
    139160        }
    140161}
  • uspace/app/sbi/src/tdata.h

    rb535aeb r37f527b  
    3939
    4040bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb);
     41bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb);
    4142bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b);
    4243void tdata_item_print(tdata_item_t *titem);
  • uspace/app/sbi/src/tdata_t.h

    rb535aeb r37f527b  
    3939        tpc_nil,
    4040        /** String type */
    41         tpc_string
     41        tpc_string,
     42        /** Resource type */
     43        tpc_resource
    4244} tprimitive_class_t;
    4345
  • uspace/dist/sysel/except.sy

    rb535aeb r37f527b  
    2828
    2929class ExceptionDemo is
    30         fun foo() : int is
     30        fun foo() is
    3131                Builtin.WriteLine("Entered foo().");
    3232                raise new BaseException();
  • uspace/dist/sysel/hexec.sy

    rb535aeb r37f527b  
    2929class HelenOSExecDemo is
    3030        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");
    3333        end
    3434end
  • uspace/dist/sysel/inherit.sy

    rb535aeb r37f527b  
    4545        fun Main() is
    4646                var a : A;
     47                var c : C;
    4748
     49                -- Construct and assign object.
    4850                a = new A();
    4951                a.Foo();
    5052
     53                -- Implicit conversion to base type.
    5154                a = new B();
    5255                a.Foo();
     
    5457                a = new C();
    5558                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;
    5668        end
    5769end
  • uspace/dist/sysel/list.sy

    rb535aeb r37f527b  
    2727--
    2828
     29-- Doubly-linked list implementation.
    2930class List is
    3031        var head : ListNode;
    3132
     33        -- Initialize list.
    3234        fun Init() is
    3335                head = new ListNode();
     
    3638        end
    3739
     40        -- Append new entry at the end of the list.
    3841        fun Append(data : int) is
    3942                var n : ListNode;
     
    5356        end
    5457
    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
    5772        end
    5873end
     
    6580        var head : ListNode;
    6681
    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
    68105                if next != head then
    69106                        return next;
     
    73110        end
    74111
    75         fun GetPrev() : ListNode is
     112        -- Get previous node.
     113        fun get_prev() : ListNode is
    76114                if prev != head then
    77115                        return next;
     
    81119        end
    82120
    83         fun GetValue() : int is
    84                 return value;
    85         end
    86121end
    87122
     
    100135                var n : ListNode;
    101136
    102                 n = list.GetFirst();
     137                n = list.First;
    103138                while n != nil do
    104139                        Builtin.WriteLine(n.value);
    105                         n = n.GetNext();
     140                        n = n.Next;
    106141                end
    107142        end
  • uspace/dist/sysel/property.sy

    rb535aeb r37f527b  
    2727--
    2828
    29 class A is
     29class Foo is
    3030        var x : int;
    3131
     
    6868                end
    6969        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
     93end
     94
     95class Bar is
     96        var i : int;
    7097end
    7198
    7299class PropertyDemo is
    73100        fun Main() is
    74                 var a : A;
     101                var a : Foo;
    75102                var i : int;
    76103
    77                 a = new A();
     104                a = new Foo();
    78105
    79106                -- Get value of named property.
     
    96123                Builtin.WriteLine("Main(): Got ");
    97124                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);
    98137        end
    99138end
  • uspace/dist/sysel/string.sy

    rb535aeb r37f527b  
    3131                -- Concatenate some strings.
    3232                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
    3341        end
    3442end
Note: See TracChangeset for help on using the changeset viewer.