Changeset 38aaacc2 in mainline for uspace/app/sbi/src/run_expr.c


Ignore:
Timestamp:
2010-04-23T21:41:10Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f4f866c
Parents:
074444f
Message:

Update SBI to rev. 207.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/run_expr.c

    r074444f r38aaacc2  
    2727 */
    2828
    29 /** @file Runner (executes the code). */
     29/** @file Run expressions. */
    3030
    3131#include <stdio.h>
     
    109109static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res);
    110110static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res);
    111 
    112 /** Evaluate expression. */
     111static void run_box(run_t *run, stree_box_t *box, rdata_item_t **res);
     112
     113/** Evaluate expression.
     114 *
     115 * Run the expression @a expr and store pointer to the result in *(@a res).
     116 * If the expression has on value (assignment) then @c NULL is returned.
     117 * @c NULL is also returned if an error or exception occurs.
     118 *
     119 * @param run           Runner object
     120 * @param expr          Expression to run
     121 * @param res           Place to store result
     122 */
    113123void run_expr(run_t *run, stree_expr_t *expr, rdata_item_t **res)
    114124{
     
    151161                run_as(run, expr->u.as_op, res);
    152162                break;
     163        case ec_box:
     164                run_box(run, expr->u.box, res);
     165                break;
    153166        }
    154167
     
    160173}
    161174
    162 /** Evaluate name reference expression. */
     175/** Evaluate name reference expression.
     176 *
     177 * @param run           Runner object
     178 * @param nameref       Name reference
     179 * @param res           Place to store result
     180 */
    163181static void run_nameref(run_t *run, stree_nameref_t *nameref,
    164182    rdata_item_t **res)
     
    247265                assert(csi != NULL);
    248266
    249                 if (sym->outer_csi != csi) {
     267                if (symbol_search_csi(run->program, csi, nameref->name)
     268                    == NULL) {
    250269                        /* Function is not in the current object. */
    251270                        printf("Error: Cannot access non-static member "
     
    283302                assert(obj != NULL);
    284303
    285                 if (sym->outer_csi != csi) {
     304                if (symbol_search_csi(run->program, csi, nameref->name)
     305                    == NULL) {
    286306                        /* Variable is not in the current object. */
    287307                        printf("Error: Cannot access non-static member "
     
    316336}
    317337
    318 /** Evaluate literal. */
     338/** Evaluate literal.
     339 *
     340 * @param run           Runner object
     341 * @param literal       Literal
     342 * @param res           Place to store result
     343 */
    319344static void run_literal(run_t *run, stree_literal_t *literal,
    320345    rdata_item_t **res)
     
    342367}
    343368
    344 /** Evaluate Boolean literal. */
     369/** Evaluate Boolean literal.
     370 *
     371 * @param run           Runner object
     372 * @param lit_bool      Boolean literal
     373 * @param res           Place to store result
     374 */
    345375static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
    346376    rdata_item_t **res)
     
    396426}
    397427
    398 /** Evaluate integer literal. */
     428/** Evaluate integer literal.
     429 *
     430 * @param run           Runner object
     431 * @param lit_int       Integer literal
     432 * @param res           Place to store result
     433 */
    399434static void run_lit_int(run_t *run, stree_lit_int_t *lit_int,
    400435    rdata_item_t **res)
     
    423458}
    424459
    425 /** Evaluate reference literal (@c nil). */
     460/** Evaluate reference literal (@c nil).
     461 *
     462 * @param run           Runner object
     463 * @param lit_ref       Reference literal
     464 * @param res           Place to store result
     465 */
    426466static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref,
    427467    rdata_item_t **res)
     
    451491}
    452492
    453 /** Evaluate string literal. */
     493/** Evaluate string literal.
     494 *
     495 * @param run           Runner object
     496 * @param lit_string    String literal
     497 * @param res           Place to store result
     498 */
    454499static void run_lit_string(run_t *run, stree_lit_string_t *lit_string,
    455500    rdata_item_t **res)
     
    478523}
    479524
    480 /** Evaluate @c self reference. */
     525/** Evaluate @c self reference.
     526 *
     527 * @param run           Runner object
     528 * @param self_ref      Self reference
     529 * @param res           Place to store result
     530 */
    481531static void run_self_ref(run_t *run, stree_self_ref_t *self_ref,
    482532    rdata_item_t **res)
     
    494544}
    495545
    496 /** Evaluate binary operation. */
     546/** Evaluate binary operation.
     547 *
     548 * @param run           Runner object
     549 * @param binop         Binary operation
     550 * @param res           Place to store result
     551 */
    497552static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res)
    498553{
     
    574629}
    575630
    576 /** Evaluate binary operation on bool arguments. */
     631/** Evaluate binary operation on bool arguments.
     632 *
     633 * @param run           Runner object
     634 * @param binop         Binary operation
     635 * @param v1            Value of first argument
     636 * @param v2            Value of second argument
     637 * @param res           Place to store result
     638 */
    577639static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    578640    rdata_value_t *v2, rdata_item_t **res)
     
    628690}
    629691
    630 /** Evaluate binary operation on char arguments. */
     692/** Evaluate binary operation on char arguments.
     693 *
     694 * @param run           Runner object
     695 * @param binop         Binary operation
     696 * @param v1            Value of first argument
     697 * @param v2            Value of second argument
     698 * @param res           Place to store result
     699*/
    631700static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    632701    rdata_value_t *v2, rdata_item_t **res)
     
    691760}
    692761
    693 /** Evaluate binary operation on int arguments. */
     762/** Evaluate binary operation on int arguments.
     763 *
     764 * @param run           Runner object
     765 * @param binop         Binary operation
     766 * @param v1            Value of first argument
     767 * @param v2            Value of second argument
     768 * @param res           Place to store result
     769*/
    694770static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    695771    rdata_value_t *v2, rdata_item_t **res)
     
    781857}
    782858
    783 /** Evaluate binary operation on string arguments. */
     859/** Evaluate binary operation on string arguments.
     860 *
     861 * @param run           Runner object
     862 * @param binop         Binary operation
     863 * @param v1            Value of first argument
     864 * @param v2            Value of second argument
     865 * @param res           Place to store result
     866 */
    784867static void run_binop_string(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    785868    rdata_value_t *v2, rdata_item_t **res)
     
    790873        rdata_string_t *string_v;
    791874
    792         char *s1, *s2;
     875        const char *s1, *s2;
    793876
    794877        (void) run;
     
    820903}
    821904
    822 /** Evaluate binary operation on ref arguments. */
     905/** Evaluate binary operation on ref arguments.
     906 *
     907 * @param run           Runner object
     908 * @param binop         Binary operation
     909 * @param v1            Value of first argument
     910 * @param v2            Value of second argument
     911 * @param res           Place to store result
     912 */
    823913static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    824914    rdata_value_t *v2, rdata_item_t **res)
     
    862952
    863953
    864 /** Evaluate unary operation. */
     954/** Evaluate unary operation.
     955 *
     956 * @param run           Runner object
     957 * @param unop          Unary operation
     958 * @param res           Place to store result
     959 */
    865960static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res)
    866961{
     
    898993}
    899994
    900 /** Evaluate unary operation on int argument. */
     995/** Evaluate unary operation on int argument.
     996 *
     997 * @param run           Runner object
     998 * @param unop          Unary operation
     999 * @param val           Value of argument
     1000 * @param res           Place to store result
     1001 */
    9011002static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val,
    9021003    rdata_item_t **res)
     
    9321033
    9331034
    934 /** Evaluate @c new operation. */
     1035/** Evaluate @c new operation.
     1036 *
     1037 * Evaluates operation per the @c new operator that creates a new
     1038 * instance of some type.
     1039 *
     1040 * @param run           Runner object
     1041 * @param unop          Unary operation
     1042 * @param res           Place to store result
     1043 */
    9351044static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res)
    9361045{
     
    9581067}
    9591068
    960 /** Create new array. */
     1069/** Create new array.
     1070 *
     1071 * @param run           Runner object
     1072 * @param new_op        New operation
     1073 * @param titem         Type of new var node (tic_tarray)
     1074 * @param res           Place to store result
     1075 */
    9611076static void run_new_array(run_t *run, stree_new_t *new_op,
    9621077    tdata_item_t *titem, rdata_item_t **res)
     
    10461161        /* Create member variables */
    10471162        for (i = 0; i < length; ++i) {
    1048                 /* XXX Depends on member variable type. */
    1049                 elem_var = rdata_var_new(vc_int);
    1050                 elem_var->u.int_v = rdata_int_new();
    1051                 bigint_init(&elem_var->u.int_v->value, 0);
     1163                /* Create and initialize element. */
     1164                run_var_new(run, tarray->base_ti, &elem_var);
    10521165
    10531166                array->element[i] = elem_var;
     
    10621175}
    10631176
    1064 /** Create new object. */
     1177/** Create new object.
     1178 *
     1179 * @param run           Runner object
     1180 * @param new_op        New operation
     1181 * @param titem         Type of new var node (tic_tobject)
     1182 * @param res           Place to store result
     1183 */
    10651184static void run_new_object(run_t *run, stree_new_t *new_op,
    10661185    tdata_item_t *titem, rdata_item_t **res)
     
    10811200}
    10821201
    1083 /** Evaluate member acccess. */
     1202/** Evaluate member acccess.
     1203 *
     1204 * Evaluate operation per the member access ('.') operator.
     1205 *
     1206 * @param run           Runner object
     1207 * @param access        Access operation
     1208 * @param res           Place to store result
     1209 */
    10841210static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res)
    10851211{
     
    11031229}
    11041230
    1105 /** Evaluate member acccess (with base already evaluated). */
     1231/** Evaluate member acccess (with base already evaluated).
     1232 *
     1233 * @param run           Runner object
     1234 * @param access        Access operation
     1235 * @param arg           Evaluated base expression
     1236 * @param res           Place to store result
     1237 */
    11061238static void run_access_item(run_t *run, stree_access_t *access,
    11071239    rdata_item_t *arg, rdata_item_t **res)
     
    11311263}
    11321264
    1133 /** Evaluate reference acccess. */
     1265/** Evaluate reference acccess.
     1266 *
     1267 * @param run           Runner object
     1268 * @param access        Access operation
     1269 * @param arg           Evaluated base expression
     1270 * @param res           Place to store result
     1271 */
    11341272static void run_access_ref(run_t *run, stree_access_t *access,
    11351273    rdata_item_t *arg, rdata_item_t **res)
     
    11491287}
    11501288
    1151 /** Evaluate delegate-member acccess. */
     1289/** Evaluate delegate-member acccess.
     1290 *
     1291 * @param run           Runner object
     1292 * @param access        Access operation
     1293 * @param arg           Evaluated base expression
     1294 * @param res           Place to store result
     1295 */
    11521296static void run_access_deleg(run_t *run, stree_access_t *access,
    11531297    rdata_item_t *arg, rdata_item_t **res)
     
    11921336}
    11931337
    1194 /** Evaluate object member acccess. */
     1338/** Evaluate object member acccess.
     1339 *
     1340 * @param run           Runner object
     1341 * @param access        Access operation
     1342 * @param arg           Evaluated base expression
     1343 * @param res           Place to store result
     1344 */
    11951345static void run_access_object(run_t *run, stree_access_t *access,
    11961346    rdata_item_t *arg, rdata_item_t **res)
     
    12401390                printf("Error: Accessing object member which is nested CSI.\n");
    12411391                exit(1);
     1392        case sc_deleg:
     1393                printf("Error: Accessing object member which is a delegate.\n");
     1394                exit(1);
    12421395        case sc_fun:
    1243                 /* Construct delegate. */
     1396                /* Construct anonymous delegate. */
    12441397                ritem = rdata_item_new(ic_value);
    12451398                value = rdata_value_new();
     
    12861439}
    12871440
    1288 /** Call a function. */
     1441/** Call a function.
     1442 *
     1443 * Call a function and return the result in @a res.
     1444 *
     1445 * @param run           Runner object
     1446 * @param call          Call operation
     1447 * @param res           Place to store result
     1448 */
    12891449static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res)
    12901450{
    1291         rdata_item_t *rfun;
     1451        rdata_item_t *rdeleg, *rdeleg_vi;
    12921452        rdata_deleg_t *deleg_v;
    12931453        list_t arg_vals;
     
    13021462        printf("Run call operation.\n");
    13031463#endif
    1304         run_expr(run, call->fun, &rfun);
     1464        run_expr(run, call->fun, &rdeleg);
    13051465        if (run_is_bo(run)) {
    13061466                *res = NULL;
     
    13131473        }
    13141474
    1315         if (rfun->ic != ic_value || rfun->u.value->var->vc != vc_deleg) {
    1316                 printf("Unimplemented: Call expression of this type.\n");
     1475        run_cvt_value_item(run, rdeleg, &rdeleg_vi);
     1476        assert(rdeleg_vi->ic == ic_value);
     1477
     1478        if (rdeleg_vi->u.value->var->vc != vc_deleg) {
     1479                printf("Unimplemented: Call expression of this type (");
     1480                rdata_item_print(rdeleg_vi);
     1481                printf(").\n");
    13171482                exit(1);
    13181483        }
    13191484
    1320         deleg_v = rfun->u.value->var->u.deleg_v;
     1485        deleg_v = rdeleg_vi->u.value->var->u.deleg_v;
    13211486
    13221487        if (deleg_v->sym->sc != sc_fun) {
     
    13651530}
    13661531
    1367 /** Run index operation. */
     1532/** Run index operation.
     1533 *
     1534 * Evaluate operation per the indexing ('[', ']') operator.
     1535 *
     1536 * @param run           Runner object
     1537 * @param index         Index operation
     1538 * @param res           Place to store result
     1539 */
    13681540static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res)
    13691541{
     
    14311603}
    14321604
    1433 /** Run index operation on array. */
     1605/** Run index operation on array.
     1606 *
     1607 * @param run           Runner object
     1608 * @param index         Index operation
     1609 * @param base          Evaluated base expression
     1610 * @param args          Evaluated indices (list of rdata_item_t)
     1611 * @param res           Place to store result
     1612 */
    14341613static void run_index_array(run_t *run, stree_index_t *index,
    14351614    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    15231702}
    15241703
    1525 /** Index an object (via its indexer). */
     1704/** Index an object (via its indexer).
     1705 *
     1706 * @param run           Runner object
     1707 * @param index         Index operation
     1708 * @param base          Evaluated base expression
     1709 * @param args          Evaluated indices (list of rdata_item_t)
     1710 * @param res           Place to store result
     1711 */
    15261712static void run_index_object(run_t *run, stree_index_t *index,
    15271713    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    15951781}
    15961782
    1597 /** Run index operation on string. */
     1783/** Run index operation on string.
     1784 *
     1785 * @param run           Runner object
     1786 * @param index         Index operation
     1787 * @param base          Evaluated base expression
     1788 * @param args          Evaluated indices (list of rdata_item_t)
     1789 * @param res           Place to store result
     1790 */
    15981791static void run_index_string(run_t *run, stree_index_t *index,
    15991792    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    16881881}
    16891882
    1690 /** Execute assignment. */
     1883/** Run assignment.
     1884 *
     1885 * Executes an assignment. @c NULL is always stored to @a res because
     1886 * an assignment does not have a value.
     1887 *
     1888 * @param run           Runner object
     1889 * @param assign        Assignment expression
     1890 * @param res           Place to store result
     1891*/
    16911892static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res)
    16921893{
     
    17251926}
    17261927
    1727 /** Execute @c as conversion. */
     1928/** Execute @c as conversion.
     1929 *
     1930 * @param run           Runner object
     1931 * @param as_op         @c as conversion expression
     1932 * @param res           Place to store result
     1933 */
    17281934static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res)
    17291935{
     
    17921998}
    17931999
    1794 /** Create new CSI instance. */
     2000/** Execute boxing operation.
     2001 *
     2002 * XXX We can scrap this special operation once we have constructors.
     2003 *
     2004 * @param run           Runner object
     2005 * @param box           Boxing operation
     2006 * @param res           Place to store result
     2007 */
     2008static void run_box(run_t *run, stree_box_t *box, rdata_item_t **res)
     2009{
     2010        rdata_item_t *rarg_i;
     2011        rdata_item_t *rarg_vi;
     2012
     2013        stree_symbol_t *csi_sym;
     2014        stree_csi_t *csi;
     2015        builtin_t *bi;
     2016        rdata_var_t *var;
     2017        rdata_object_t *object;
     2018
     2019        sid_t mbr_name_sid;
     2020        rdata_var_t *mbr_var;
     2021
     2022#ifdef DEBUG_RUN_TRACE
     2023        printf("Run boxing operation.\n");
     2024#endif
     2025        run_expr(run, box->arg, &rarg_i);
     2026        if (run_is_bo(run)) {
     2027                *res = NULL;
     2028                return;
     2029        }
     2030
     2031        run_cvt_value_item(run, rarg_i, &rarg_vi);
     2032        assert(rarg_vi->ic == ic_value);
     2033
     2034        bi = run->program->builtin;
     2035
     2036        /* Just to keep the compiler happy. */
     2037        csi_sym = NULL;
     2038
     2039        switch (rarg_vi->u.value->var->vc) {
     2040        case vc_bool: csi_sym = bi->boxed_bool; break;
     2041        case vc_char: csi_sym = bi->boxed_char; break;
     2042        case vc_int: csi_sym = bi->boxed_int; break;
     2043        case vc_string: csi_sym = bi->boxed_string; break;
     2044
     2045        case vc_ref:
     2046        case vc_deleg:
     2047        case vc_array:
     2048        case vc_object:
     2049        case vc_resource:
     2050                assert(b_false);
     2051        }
     2052
     2053        csi = symbol_to_csi(csi_sym);
     2054        assert(csi != NULL);
     2055
     2056        /* Construct object of the relevant boxed type. */
     2057        run_new_csi_inst(run, csi, res);
     2058
     2059        /* Set the 'Value' field */
     2060
     2061        assert((*res)->ic == ic_value);
     2062        assert((*res)->u.value->var->vc == vc_ref);
     2063        var = (*res)->u.value->var->u.ref_v->vref;
     2064        assert(var->vc == vc_object);
     2065        object = var->u.object_v;
     2066
     2067        mbr_name_sid = strtab_get_sid("Value");
     2068        mbr_var = intmap_get(&object->fields, mbr_name_sid);
     2069        assert(mbr_var != NULL);
     2070
     2071        rdata_var_write(mbr_var, rarg_vi->u.value);
     2072}
     2073
     2074/** Create new CSI instance.
     2075 *
     2076 * Create a new object, instance of @a csi.
     2077 * XXX This does not work with generics as @a csi cannot specify a generic
     2078 * type.
     2079 *
     2080 * Initialize the fields with default values of their types, but do not
     2081 * run any constructor.
     2082 *
     2083 * @param run           Runner object
     2084 * @param as_op         @c as conversion expression
     2085 * @param res           Place to store result
     2086 */
    17952087void run_new_csi_inst(run_t *run, stree_csi_t *csi, rdata_item_t **res)
    17962088{
     
    18022094
    18032095        rdata_var_t *mbr_var;
    1804 
    18052096        list_node_t *node;
     2097        tdata_item_t *field_ti;
    18062098
    18072099        csi_sym = csi_to_symbol(csi);
     
    18222114
    18232115        /* Create object fields. */
    1824         node = list_first(&csi->members);
    1825         while (node != NULL) {
    1826                 csimbr = list_node_data(node, stree_csimbr_t *);
    1827                 if (csimbr->cc == csimbr_var) {
    1828                         /* XXX Depends on member variable type. */
    1829                         mbr_var = rdata_var_new(vc_int);
    1830                         mbr_var->u.int_v = rdata_int_new();
    1831                         bigint_init(&mbr_var->u.int_v->value, 0);
    1832 
    1833                         intmap_set(&obj->fields, csimbr->u.var->name->sid,
    1834                             mbr_var);
     2116        while (csi != NULL) {
     2117                node = list_first(&csi->members);
     2118                while (node != NULL) {
     2119                        csimbr = list_node_data(node, stree_csimbr_t *);
     2120                        if (csimbr->cc == csimbr_var) {
     2121                                /* Compute field type. XXX Memoize. */
     2122                                run_texpr(run->program, csi,
     2123                                    csimbr->u.var->type,
     2124                                    &field_ti);
     2125
     2126                                /* Create and initialize field. */
     2127                                run_var_new(run, field_ti, &mbr_var);
     2128
     2129                                /* Add to field map. */
     2130                                intmap_set(&obj->fields,
     2131                                    csimbr->u.var->name->sid,
     2132                                    mbr_var);
     2133                        }
     2134
     2135                        node = list_next(&csi->members, node);
    18352136                }
    18362137
    1837                 node = list_next(&csi->members, node);
     2138                /* Continue with base CSI */
     2139                csi = csi->base_csi;
    18382140        }
    18392141
     
    18442146/** Return boolean value of an item.
    18452147 *
    1846  * Tries to interpret @a item as a boolean value. If it is not a boolean
    1847  * value, this generates an error.
     2148 * Try to interpret @a item as a boolean value. If it is not a boolean
     2149 * value, generate an error.
     2150 *
     2151 * @param run           Runner object
     2152 * @param item          Input item
     2153 * @return              Resulting boolean value
    18482154 */
    18492155bool_t run_item_boolean_value(run_t *run, rdata_item_t *item)
Note: See TracChangeset for help on using the changeset viewer.