Ignore:
File:
1 edited

Legend:

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

    r6c39a907 rcaad59a  
    2727 */
    2828
    29 /** @file Run expressions. */
     29/** @file Runner (executes the code). */
    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 static 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  */
     111
     112/** Evaluate expression. */
    123113void run_expr(run_t *run, stree_expr_t *expr, rdata_item_t **res)
    124114{
     
    161151                run_as(run, expr->u.as_op, res);
    162152                break;
    163         case ec_box:
    164                 run_box(run, expr->u.box, res);
    165                 break;
    166153        }
    167154
     
    173160}
    174161
    175 /** Evaluate name reference expression.
    176  *
    177  * @param run           Runner object
    178  * @param nameref       Name reference
    179  * @param res           Place to store result
    180  */
     162/** Evaluate name reference expression. */
    181163static void run_nameref(run_t *run, stree_nameref_t *nameref,
    182164    rdata_item_t **res)
     
    265247                assert(csi != NULL);
    266248
    267                 if (symbol_search_csi(run->program, csi, nameref->name)
    268                     == NULL) {
     249                if (sym->outer_csi != csi) {
    269250                        /* Function is not in the current object. */
    270251                        printf("Error: Cannot access non-static member "
     
    302283                assert(obj != NULL);
    303284
    304                 if (symbol_search_csi(run->program, csi, nameref->name)
    305                     == NULL) {
     285                if (sym->outer_csi != csi) {
    306286                        /* Variable is not in the current object. */
    307287                        printf("Error: Cannot access non-static member "
     
    336316}
    337317
    338 /** Evaluate literal.
    339  *
    340  * @param run           Runner object
    341  * @param literal       Literal
    342  * @param res           Place to store result
    343  */
     318/** Evaluate literal. */
    344319static void run_literal(run_t *run, stree_literal_t *literal,
    345320    rdata_item_t **res)
     
    367342}
    368343
    369 /** Evaluate Boolean literal.
    370  *
    371  * @param run           Runner object
    372  * @param lit_bool      Boolean literal
    373  * @param res           Place to store result
    374  */
     344/** Evaluate Boolean literal. */
    375345static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
    376346    rdata_item_t **res)
     
    426396}
    427397
    428 /** Evaluate integer literal.
    429  *
    430  * @param run           Runner object
    431  * @param lit_int       Integer literal
    432  * @param res           Place to store result
    433  */
     398/** Evaluate integer literal. */
    434399static void run_lit_int(run_t *run, stree_lit_int_t *lit_int,
    435400    rdata_item_t **res)
     
    458423}
    459424
    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  */
     425/** Evaluate reference literal (@c nil). */
    466426static void run_lit_ref(run_t *run, stree_lit_ref_t *lit_ref,
    467427    rdata_item_t **res)
     
    491451}
    492452
    493 /** Evaluate string literal.
    494  *
    495  * @param run           Runner object
    496  * @param lit_string    String literal
    497  * @param res           Place to store result
    498  */
     453/** Evaluate string literal. */
    499454static void run_lit_string(run_t *run, stree_lit_string_t *lit_string,
    500455    rdata_item_t **res)
     
    523478}
    524479
    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  */
     480/** Evaluate @c self reference. */
    531481static void run_self_ref(run_t *run, stree_self_ref_t *self_ref,
    532482    rdata_item_t **res)
     
    544494}
    545495
    546 /** Evaluate binary operation.
    547  *
    548  * @param run           Runner object
    549  * @param binop         Binary operation
    550  * @param res           Place to store result
    551  */
     496/** Evaluate binary operation. */
    552497static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res)
    553498{
     
    629574}
    630575
    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  */
     576/** Evaluate binary operation on bool arguments. */
    639577static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    640578    rdata_value_t *v2, rdata_item_t **res)
     
    690628}
    691629
    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 */
     630/** Evaluate binary operation on char arguments. */
    700631static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    701632    rdata_value_t *v2, rdata_item_t **res)
     
    760691}
    761692
    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 */
     693/** Evaluate binary operation on int arguments. */
    770694static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    771695    rdata_value_t *v2, rdata_item_t **res)
     
    857781}
    858782
    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  */
     783/** Evaluate binary operation on string arguments. */
    867784static void run_binop_string(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    868785    rdata_value_t *v2, rdata_item_t **res)
     
    873790        rdata_string_t *string_v;
    874791
    875         const char *s1, *s2;
     792        char *s1, *s2;
    876793
    877794        (void) run;
     
    903820}
    904821
    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  */
     822/** Evaluate binary operation on ref arguments. */
    913823static void run_binop_ref(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    914824    rdata_value_t *v2, rdata_item_t **res)
     
    952862
    953863
    954 /** Evaluate unary operation.
    955  *
    956  * @param run           Runner object
    957  * @param unop          Unary operation
    958  * @param res           Place to store result
    959  */
     864/** Evaluate unary operation. */
    960865static void run_unop(run_t *run, stree_unop_t *unop, rdata_item_t **res)
    961866{
     
    993898}
    994899
    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  */
     900/** Evaluate unary operation on int argument. */
    1002901static void run_unop_int(run_t *run, stree_unop_t *unop, rdata_value_t *val,
    1003902    rdata_item_t **res)
     
    1033932
    1034933
    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  */
     934/** Evaluate @c new operation. */
    1044935static void run_new(run_t *run, stree_new_t *new_op, rdata_item_t **res)
    1045936{
     
    1067958}
    1068959
    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  */
     960/** Create new array. */
    1076961static void run_new_array(run_t *run, stree_new_t *new_op,
    1077962    tdata_item_t *titem, rdata_item_t **res)
     
    11611046        /* Create member variables */
    11621047        for (i = 0; i < length; ++i) {
    1163                 /* Create and initialize element. */
    1164                 run_var_new(run, tarray->base_ti, &elem_var);
     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);
    11651052
    11661053                array->element[i] = elem_var;
     
    11751062}
    11761063
    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  */
     1064/** Create new object. */
    11841065static void run_new_object(run_t *run, stree_new_t *new_op,
    11851066    tdata_item_t *titem, rdata_item_t **res)
     
    12001081}
    12011082
    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  */
     1083/** Evaluate member acccess. */
    12101084static void run_access(run_t *run, stree_access_t *access, rdata_item_t **res)
    12111085{
     
    12291103}
    12301104
    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  */
     1105/** Evaluate member acccess (with base already evaluated). */
    12381106static void run_access_item(run_t *run, stree_access_t *access,
    12391107    rdata_item_t *arg, rdata_item_t **res)
     
    12631131}
    12641132
    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  */
     1133/** Evaluate reference acccess. */
    12721134static void run_access_ref(run_t *run, stree_access_t *access,
    12731135    rdata_item_t *arg, rdata_item_t **res)
     
    12871149}
    12881150
    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  */
     1151/** Evaluate delegate-member acccess. */
    12961152static void run_access_deleg(run_t *run, stree_access_t *access,
    12971153    rdata_item_t *arg, rdata_item_t **res)
     
    13361192}
    13371193
    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  */
     1194/** Evaluate object member acccess. */
    13451195static void run_access_object(run_t *run, stree_access_t *access,
    13461196    rdata_item_t *arg, rdata_item_t **res)
     
    13861236#endif
    13871237
    1388         /* Make compiler happy. */
    1389         ritem = NULL;
    1390 
    13911238        switch (member->sc) {
    13921239        case sc_csi:
    13931240                printf("Error: Accessing object member which is nested CSI.\n");
    13941241                exit(1);
    1395         case sc_deleg:
    1396                 printf("Error: Accessing object member which is a delegate.\n");
    1397                 exit(1);
    13981242        case sc_fun:
    1399                 /* Construct anonymous delegate. */
     1243                /* Construct delegate. */
    14001244                ritem = rdata_item_new(ic_value);
    14011245                value = rdata_value_new();
     
    14371281                addr_prop->u.named->prop_d = deleg_p;
    14381282                break;
     1283        default:
     1284                ritem = NULL;
    14391285        }
    14401286
     
    14421288}
    14431289
    1444 /** Call a function.
    1445  *
    1446  * Call a function and return the result in @a res.
    1447  *
    1448  * @param run           Runner object
    1449  * @param call          Call operation
    1450  * @param res           Place to store result
    1451  */
     1290/** Call a function. */
    14521291static void run_call(run_t *run, stree_call_t *call, rdata_item_t **res)
    14531292{
    1454         rdata_item_t *rdeleg, *rdeleg_vi;
     1293        rdata_item_t *rfun;
    14551294        rdata_deleg_t *deleg_v;
    14561295        list_t arg_vals;
     
    14651304        printf("Run call operation.\n");
    14661305#endif
    1467         run_expr(run, call->fun, &rdeleg);
     1306        run_expr(run, call->fun, &rfun);
    14681307        if (run_is_bo(run)) {
    14691308                *res = NULL;
     
    14761315        }
    14771316
    1478         run_cvt_value_item(run, rdeleg, &rdeleg_vi);
    1479         assert(rdeleg_vi->ic == ic_value);
    1480 
    1481         if (rdeleg_vi->u.value->var->vc != vc_deleg) {
    1482                 printf("Unimplemented: Call expression of this type (");
    1483                 rdata_item_print(rdeleg_vi);
    1484                 printf(").\n");
     1317        if (rfun->ic != ic_value || rfun->u.value->var->vc != vc_deleg) {
     1318                printf("Unimplemented: Call expression of this type.\n");
    14851319                exit(1);
    14861320        }
    14871321
    1488         deleg_v = rdeleg_vi->u.value->var->u.deleg_v;
     1322        deleg_v = rfun->u.value->var->u.deleg_v;
    14891323
    14901324        if (deleg_v->sym->sc != sc_fun) {
     
    15331367}
    15341368
    1535 /** Run index operation.
    1536  *
    1537  * Evaluate operation per the indexing ('[', ']') operator.
    1538  *
    1539  * @param run           Runner object
    1540  * @param index         Index operation
    1541  * @param res           Place to store result
    1542  */
     1369/** Run index operation. */
    15431370static void run_index(run_t *run, stree_index_t *index, rdata_item_t **res)
    15441371{
     
    16061433}
    16071434
    1608 /** Run index operation on array.
    1609  *
    1610  * @param run           Runner object
    1611  * @param index         Index operation
    1612  * @param base          Evaluated base expression
    1613  * @param args          Evaluated indices (list of rdata_item_t)
    1614  * @param res           Place to store result
    1615  */
     1435/** Run index operation on array. */
    16161436static void run_index_array(run_t *run, stree_index_t *index,
    16171437    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    17051525}
    17061526
    1707 /** Index an object (via its indexer).
    1708  *
    1709  * @param run           Runner object
    1710  * @param index         Index operation
    1711  * @param base          Evaluated base expression
    1712  * @param args          Evaluated indices (list of rdata_item_t)
    1713  * @param res           Place to store result
    1714  */
     1527/** Index an object (via its indexer). */
    17151528static void run_index_object(run_t *run, stree_index_t *index,
    17161529    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    17841597}
    17851598
    1786 /** Run index operation on string.
    1787  *
    1788  * @param run           Runner object
    1789  * @param index         Index operation
    1790  * @param base          Evaluated base expression
    1791  * @param args          Evaluated indices (list of rdata_item_t)
    1792  * @param res           Place to store result
    1793  */
     1599/** Run index operation on string. */
    17941600static void run_index_string(run_t *run, stree_index_t *index,
    17951601    rdata_item_t *base, list_t *args, rdata_item_t **res)
     
    18841690}
    18851691
    1886 /** Run assignment.
    1887  *
    1888  * Executes an assignment. @c NULL is always stored to @a res because
    1889  * an assignment does not have a value.
    1890  *
    1891  * @param run           Runner object
    1892  * @param assign        Assignment expression
    1893  * @param res           Place to store result
    1894 */
     1692/** Execute assignment. */
    18951693static void run_assign(run_t *run, stree_assign_t *assign, rdata_item_t **res)
    18961694{
     
    19291727}
    19301728
    1931 /** Execute @c as conversion.
    1932  *
    1933  * @param run           Runner object
    1934  * @param as_op         @c as conversion expression
    1935  * @param res           Place to store result
    1936  */
     1729/** Execute @c as conversion. */
    19371730static void run_as(run_t *run, stree_as_t *as_op, rdata_item_t **res)
    19381731{
     
    20011794}
    20021795
    2003 /** Execute boxing operation.
    2004  *
    2005  * XXX We can scrap this special operation once we have constructors.
    2006  *
    2007  * @param run           Runner object
    2008  * @param box           Boxing operation
    2009  * @param res           Place to store result
    2010  */
    2011 static void run_box(run_t *run, stree_box_t *box, rdata_item_t **res)
    2012 {
    2013         rdata_item_t *rarg_i;
    2014         rdata_item_t *rarg_vi;
    2015 
    2016         stree_symbol_t *csi_sym;
    2017         stree_csi_t *csi;
    2018         builtin_t *bi;
    2019         rdata_var_t *var;
    2020         rdata_object_t *object;
    2021 
    2022         sid_t mbr_name_sid;
    2023         rdata_var_t *mbr_var;
    2024 
    2025 #ifdef DEBUG_RUN_TRACE
    2026         printf("Run boxing operation.\n");
    2027 #endif
    2028         run_expr(run, box->arg, &rarg_i);
    2029         if (run_is_bo(run)) {
    2030                 *res = NULL;
    2031                 return;
    2032         }
    2033 
    2034         run_cvt_value_item(run, rarg_i, &rarg_vi);
    2035         assert(rarg_vi->ic == ic_value);
    2036 
    2037         bi = run->program->builtin;
    2038 
    2039         /* Just to keep the compiler happy. */
    2040         csi_sym = NULL;
    2041 
    2042         switch (rarg_vi->u.value->var->vc) {
    2043         case vc_bool: csi_sym = bi->boxed_bool; break;
    2044         case vc_char: csi_sym = bi->boxed_char; break;
    2045         case vc_int: csi_sym = bi->boxed_int; break;
    2046         case vc_string: csi_sym = bi->boxed_string; break;
    2047 
    2048         case vc_ref:
    2049         case vc_deleg:
    2050         case vc_array:
    2051         case vc_object:
    2052         case vc_resource:
    2053                 assert(b_false);
    2054         }
    2055 
    2056         csi = symbol_to_csi(csi_sym);
    2057         assert(csi != NULL);
    2058 
    2059         /* Construct object of the relevant boxed type. */
    2060         run_new_csi_inst(run, csi, res);
    2061 
    2062         /* Set the 'Value' field */
    2063 
    2064         assert((*res)->ic == ic_value);
    2065         assert((*res)->u.value->var->vc == vc_ref);
    2066         var = (*res)->u.value->var->u.ref_v->vref;
    2067         assert(var->vc == vc_object);
    2068         object = var->u.object_v;
    2069 
    2070         mbr_name_sid = strtab_get_sid("Value");
    2071         mbr_var = intmap_get(&object->fields, mbr_name_sid);
    2072         assert(mbr_var != NULL);
    2073 
    2074         rdata_var_write(mbr_var, rarg_vi->u.value);
    2075 }
    2076 
    2077 /** Create new CSI instance.
    2078  *
    2079  * Create a new object, instance of @a csi.
    2080  * XXX This does not work with generics as @a csi cannot specify a generic
    2081  * type.
    2082  *
    2083  * Initialize the fields with default values of their types, but do not
    2084  * run any constructor.
    2085  *
    2086  * @param run           Runner object
    2087  * @param as_op         @c as conversion expression
    2088  * @param res           Place to store result
    2089  */
     1796/** Create new CSI instance. */
    20901797void run_new_csi_inst(run_t *run, stree_csi_t *csi, rdata_item_t **res)
    20911798{
     
    20971804
    20981805        rdata_var_t *mbr_var;
     1806
    20991807        list_node_t *node;
    2100         tdata_item_t *field_ti;
    21011808
    21021809        csi_sym = csi_to_symbol(csi);
     
    21171824
    21181825        /* Create object fields. */
    2119         while (csi != NULL) {
    2120                 node = list_first(&csi->members);
    2121                 while (node != NULL) {
    2122                         csimbr = list_node_data(node, stree_csimbr_t *);
    2123                         if (csimbr->cc == csimbr_var) {
    2124                                 /* Compute field type. XXX Memoize. */
    2125                                 run_texpr(run->program, csi,
    2126                                     csimbr->u.var->type,
    2127                                     &field_ti);
    2128 
    2129                                 /* Create and initialize field. */
    2130                                 run_var_new(run, field_ti, &mbr_var);
    2131 
    2132                                 /* Add to field map. */
    2133                                 intmap_set(&obj->fields,
    2134                                     csimbr->u.var->name->sid,
    2135                                     mbr_var);
    2136                         }
    2137 
    2138                         node = list_next(&csi->members, node);
     1826        node = list_first(&csi->members);
     1827        while (node != NULL) {
     1828                csimbr = list_node_data(node, stree_csimbr_t *);
     1829                if (csimbr->cc == csimbr_var) {
     1830                        /* XXX Depends on member variable type. */
     1831                        mbr_var = rdata_var_new(vc_int);
     1832                        mbr_var->u.int_v = rdata_int_new();
     1833                        bigint_init(&mbr_var->u.int_v->value, 0);
     1834
     1835                        intmap_set(&obj->fields, csimbr->u.var->name->sid,
     1836                            mbr_var);
    21391837                }
    21401838
    2141                 /* Continue with base CSI */
    2142                 csi = csi->base_csi;
     1839                node = list_next(&csi->members, node);
    21431840        }
    21441841
     
    21491846/** Return boolean value of an item.
    21501847 *
    2151  * Try to interpret @a item as a boolean value. If it is not a boolean
    2152  * value, generate an error.
    2153  *
    2154  * @param run           Runner object
    2155  * @param item          Input item
    2156  * @return              Resulting boolean value
     1848 * Tries to interpret @a item as a boolean value. If it is not a boolean
     1849 * value, this generates an error.
    21571850 */
    21581851bool_t run_item_boolean_value(run_t *run, rdata_item_t *item)
Note: See TracChangeset for help on using the changeset viewer.