Changes in / [2721a75:1ef0fc3] in mainline


Ignore:
Location:
uspace
Files:
6 added
28 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/Makefile

    r2721a75 r1ef0fc3  
    5050        src/p_type.c \
    5151        src/parse.c \
     52        src/program.c \
    5253        src/rdata.c \
    5354        src/run.c \
  • uspace/app/sbi/src/builtin/bi_fun.c

    r2721a75 r1ef0fc3  
    9999{
    100100        rdata_var_t *var;
     101        int char_val;
     102        int rc;
    101103
    102104#ifdef DEBUG_RUN_TRACE
     
    107109
    108110        switch (var->vc) {
     111        case vc_char:
     112                rc = bigint_get_value_int(&var->u.char_v->value, &char_val);
     113                if (rc == EOK)
     114                        printf("%lc\n", char_val);
     115                else
     116                        printf("???\n");
     117                break;
    109118        case vc_int:
    110119                bigint_print(&var->u.int_v->value);
  • uspace/app/sbi/src/imode.c

    r2721a75 r1ef0fc3  
    4747#include "list.h"
    4848#include "mytypes.h"
     49#include "program.h"
    4950#include "rdata.h"
    5051#include "stree.h"
     
    9091        builtin_declare(program);
    9192
     93        /* Process the library. */
     94        if (program_lib_process(program) != EOK)
     95                exit(1);
     96
    9297        /* Resolve ancestry. */
    9398        ancr_module_process(program, program->module);
     
    130135                parse.error = b_false;
    131136                stype.error = b_false;
     137                run.thread_ar->exc_payload = NULL;
     138                run.thread_ar->bo_mode = bm_none;
    132139
    133140                input_new_interactive(&input);
  • uspace/app/sbi/src/input.c

    r2721a75 r1ef0fc3  
    4545#define INPUT_BUFFER_SIZE 256
    4646
    47 static int input_init_file(input_t *input, char *fname);
     47static int input_init_file(input_t *input, const char *fname);
    4848static void input_init_interactive(input_t *input);
    4949static void input_init_string(input_t *input, const char *str);
     
    5757 *                      ENOENT when opening file fails.
    5858 */
    59 int input_new_file(input_t **input, char *fname)
     59int input_new_file(input_t **input, const char *fname)
    6060{
    6161        *input = malloc(sizeof(input_t));
     
    104104 * @return              EOK on success, ENOENT when opening file fails.
    105105*/
    106 static int input_init_file(input_t *input, char *fname)
     106static int input_init_file(input_t *input, const char *fname)
    107107{
    108108        FILE *f;
  • uspace/app/sbi/src/input.h

    r2721a75 r1ef0fc3  
    3232#include "mytypes.h"
    3333
    34 int input_new_file(input_t **input, char *fname);
     34int input_new_file(input_t **input, const char *fname);
    3535int input_new_interactive(input_t **input);
    3636int input_new_string(input_t **input, const char *str);
  • uspace/app/sbi/src/lex.c

    r2721a75 r1ef0fc3  
    5353static bool_t is_digit(char c);
    5454static void lex_word(lex_t *lex);
     55static void lex_char(lex_t *lex);
    5556static void lex_number(lex_t *lex);
    5657static void lex_string(lex_t *lex);
     
    7475static struct lc_name keywords[] = {
    7576        { lc_as,        "as" },
     77        { lc_bool,      "bool" },
     78        { lc_char,      "char" },
    7679        { lc_builtin,   "builtin" },
    7780        { lc_class,     "class" },
     
    8184        { lc_end,       "end" },
    8285        { lc_except,    "except" },
     86        { lc_false,     "false" },
    8387        { lc_finally,   "finally" },
    8488        { lc_for,       "for" },
     
    108112        { lc_then,      "then" },
    109113        { lc_this,      "this" },
     114        { lc_true,      "true" },
    110115        { lc_var,       "var" },
    111116        { lc_with,      "with" },
     
    338343        }
    339344
     345        if (bp[0] == '\'') {
     346                lex_char(lex);
     347                return b_true;
     348        }
     349
    340350        if (is_digit(bp[0])) {
    341351                lex_number(lex);
     
    460470        lex->current.lclass = lc_ident;
    461471        lex->current.u.ident.sid = strtab_get_sid(ident_buf);
     472}
     473
     474/** Lex a character literal.
     475 *
     476 * Reads in a character literal and stores it in the current lem in @a lex.
     477 *
     478 * @param lex           Lexer object.
     479 */
     480static void lex_char(lex_t *lex)
     481{
     482        char *bp;
     483        int idx;
     484        size_t len;
     485        int char_val;
     486
     487        bp = lex->ibp + 1;
     488        idx = 0;
     489
     490        while (bp[idx] != '\'') {
     491                if (idx >= SLBUF_SIZE) {
     492                        printf("Error: Character literal too long.\n");
     493                        exit(1);
     494                }
     495
     496                if (bp[idx] == '\0') {
     497                        printf("Error: Unterminated character literal.\n");
     498                        exit(1);
     499                }
     500
     501                strlit_buf[idx] = bp[idx];
     502                ++idx;
     503        }
     504
     505        lex->ibp = bp + idx + 1;
     506
     507        strlit_buf[idx] = '\0';
     508        len = os_str_length(strlit_buf);
     509        if (len != 1) {
     510                printf("Character literal should contain one character, "
     511                    "but contains %u characters instead.\n", (unsigned) len);
     512                exit(1);
     513        }
     514
     515        os_str_get_char(strlit_buf, 0, &char_val);
     516        lex->current.lclass = lc_lit_char;
     517        bigint_init(&lex->current.u.lit_char.value, char_val);
    462518}
    463519
  • uspace/app/sbi/src/lex_t.h

    r2721a75 r1ef0fc3  
    3838
    3939        lc_ident,
     40        lc_lit_char,
    4041        lc_lit_int,
    4142        lc_lit_string,
     
    4344        /* Keywords */
    4445        lc_as,
     46        lc_bool,
    4547        lc_builtin,
     48        lc_char,
    4649        lc_class,
    4750        lc_constructor,
     
    5053        lc_end,
    5154        lc_except,
     55        lc_false,
    5256        lc_finally,
    5357        lc_for,
     
    7781        lc_then,
    7882        lc_this,
     83        lc_true,
    7984        lc_var,
    8085        lc_with,
     
    115120
    116121typedef struct {
     122        /* Character value */
     123        bigint_t value;
     124} lem_lit_char_t;
     125
     126typedef struct {
    117127        /* Integer value */
    118128        bigint_t value;
     
    131141        union {
    132142                lem_ident_t ident;
     143                lem_lit_char_t lit_char;
    133144                lem_lit_int_t lit_int;
    134145                lem_lit_string_t lit_string;
  • uspace/app/sbi/src/main.c

    r2721a75 r1ef0fc3  
    3737#include <stdlib.h>
    3838#include "ancr.h"
     39#include "os/os.h"
    3940#include "builtin.h"
    4041#include "imode.h"
    4142#include "mytypes.h"
     43#include "program.h"
    4244#include "strtab.h"
    4345#include "stree.h"
     
    4850#include "run.h"
    4951
    50 void syntax_print(void);
     52static void syntax_print(void);
    5153
    5254/** Main entry point.
     
    5658int main(int argc, char *argv[])
    5759{
    58         input_t *input;
    59         lex_t lex;
    60         parse_t parse;
    6160        stree_program_t *program;
    6261        stype_t stype;
     
    6463        int rc;
    6564
    66         if (argc == 1) {
     65        /* Store executable file path under which we have been invoked. */
     66        os_store_ef_path(*argv);
     67
     68        argv += 1;
     69        argc -= 1;
     70
     71        if (argc == 0) {
    6772                /* Enter interactive mode */
    6873                strtab_init();
     
    7176        }
    7277
    73         if (argc != 2) {
     78        if (os_str_cmp(*argv, "-h") == 0) {
    7479                syntax_print();
    75                 exit(1);
    76         }
    77 
    78         rc = input_new_file(&input, argv[1]);
    79         if (rc != EOK) {
    80                 printf("Failed opening source file '%s'.\n", argv[1]);
    81                 exit(1);
     80                return 0;
    8281        }
    8382
     
    8988        builtin_declare(program);
    9089
    91         /* Parse input file. */
    92         lex_init(&lex, input);
    93         parse_init(&parse, program, &lex);
    94         parse_module(&parse);
    95 
    96         /* Check for parse errors. */
    97         if (parse.error)
     90        /* Process source files in the library. */
     91        if (program_lib_process(program) != EOK)
    9892                return 1;
    9993
     94        /* Process all source files specified in command-line arguments. */
     95        while (argc > 0) {
     96                rc = program_file_process(program, *argv);
     97                if (rc != EOK)
     98                        return 1;
     99
     100                argv += 1;
     101                argc -= 1;
     102        }
     103
    100104        /* Resolve ancestry. */
    101         ancr_module_process(program, parse.cur_mod);
     105        ancr_module_process(program, program->module);
    102106
    103107        /* Type program. */
     
    122126
    123127/** Print command-line syntax help. */
    124 void syntax_print(void)
     128static void syntax_print(void)
    125129{
    126         printf("Missing or invalid arguments.\n");
    127130        printf("Syntax: sbi <source_file.sy>\n");
    128131}
  • uspace/app/sbi/src/os/helenos.c

    r2721a75 r1ef0fc3  
    3838#include "os.h"
    3939
     40/** Path to executable file via which we have been invoked. */
     41static char *ef_path;
     42
    4043/*
    4144 * Using HelenOS-specific string API.
     
    7073{
    7174        return str_cmp(a, b);
     75}
     76
     77/** Return number of characters in string. */
     78size_t os_str_length(const char *str)
     79{
     80        return str_length(str);
    7281}
    7382
     
    156165        return EOK;
    157166}
     167
     168/** Store the executable file path via which we were executed. */
     169void os_store_ef_path(char *path)
     170{
     171        ef_path = path;
     172}
     173
     174/** Return path to the Sysel library
     175 *
     176 * @return New string. Caller should deallocate it using @c free().
     177 */
     178char *os_get_lib_path(void)
     179{
     180        return os_str_dup("/src/sysel/lib");
     181}
  • uspace/app/sbi/src/os/os.h

    r2721a75 r1ef0fc3  
    3333int os_str_cmp(const char *a, const char *b);
    3434char *os_str_dup(const char *str);
     35size_t os_str_length(const char *str);
    3536int os_str_get_char(const char *str, int index, int *out_char);
    3637void os_input_disp_help(void);
     
    3839int os_exec(char *const cmd[]);
    3940
     41void os_store_ef_path(char *path);
     42char *os_get_lib_path(void);
    4043
    4144#endif
  • uspace/app/sbi/src/os/posix.c

    r2721a75 r1ef0fc3  
    2929/** @file POSIX-specific code. */
    3030
     31#include <libgen.h>
    3132#include <stdio.h>
    3233#include <stdlib.h>
     
    3940
    4041#include "os.h"
     42
     43/** Path to executable file via which we have been invoked. */
     44static char *ef_path;
    4145
    4246/*
     
    7175{
    7276        return strcmp(a, b);
     77}
     78
     79/** Return number of characters in string. */
     80size_t os_str_length(const char *str)
     81{
     82        return strlen(str);
    7383}
    7484
     
    146156        return EOK;
    147157}
     158
     159/** Store the executable file path via which we were executed. */
     160void os_store_ef_path(char *path)
     161{
     162        ef_path = path;
     163}
     164
     165/** Return path to the Sysel library
     166 *
     167 * @return New string. Caller should deallocate it using @c free().
     168 */
     169char *os_get_lib_path(void)
     170{
     171        return os_str_acat(dirname(ef_path), "/lib");
     172}
  • uspace/app/sbi/src/p_expr.c

    r2721a75 r1ef0fc3  
    5656static stree_expr_t *parse_primitive(parse_t *parse);
    5757static stree_expr_t *parse_nameref(parse_t *parse);
     58static stree_expr_t *parse_lit_bool(parse_t *parse);
     59static stree_expr_t *parse_lit_char(parse_t *parse);
    5860static stree_expr_t *parse_lit_int(parse_t *parse);
    5961static stree_expr_t *parse_lit_ref(parse_t *parse);
     
    494496                expr = parse_nameref(parse);
    495497                break;
     498        case lc_false:
     499        case lc_true:
     500                expr = parse_lit_bool(parse);
     501                break;
     502        case lc_lit_char:
     503                expr = parse_lit_char(parse);
     504                break;
    496505        case lc_lit_int:
    497506                expr = parse_lit_int(parse);
     
    531540}
    532541
     542/** Parse boolean literal.
     543 *
     544 * @param parse         Parser object.
     545 */
     546static stree_expr_t *parse_lit_bool(parse_t *parse)
     547{
     548        stree_literal_t *literal;
     549        stree_expr_t *expr;
     550        bool_t value;
     551
     552        switch (lcur_lc(parse)) {
     553        case lc_false: value = b_false; break;
     554        case lc_true: value = b_true; break;
     555        default: assert(b_false);
     556        }
     557
     558        lskip(parse);
     559
     560        literal = stree_literal_new(ltc_bool);
     561        literal->u.lit_bool.value = value;
     562
     563        expr = stree_expr_new(ec_literal);
     564        expr->u.literal = literal;
     565
     566        return expr;
     567}
     568
     569/** Parse character literal.
     570 *
     571 * @param parse         Parser object.
     572 */
     573static stree_expr_t *parse_lit_char(parse_t *parse)
     574{
     575        stree_literal_t *literal;
     576        stree_expr_t *expr;
     577
     578        lcheck(parse, lc_lit_char);
     579
     580        literal = stree_literal_new(ltc_char);
     581        bigint_clone(&lcur(parse)->u.lit_char.value,
     582            &literal->u.lit_char.value);
     583
     584        lskip(parse);
     585
     586        expr = stree_expr_new(ec_literal);
     587        expr->u.literal = literal;
     588
     589        return expr;
     590}
     591
    533592/** Parse integer literal.
    534593 *
  • uspace/app/sbi/src/p_type.c

    r2721a75 r1ef0fc3  
    7878static stree_texpr_t *parse_tapply(parse_t *parse)
    7979{
    80         stree_texpr_t *a, *b, *tmp;
     80        stree_texpr_t *gtype;
     81        stree_texpr_t *aexpr;
     82        stree_texpr_t *targ;
    8183        stree_tapply_t *tapply;
    8284
    83         a = parse_tpostfix(parse);
     85        gtype = parse_tpostfix(parse);
     86        if (lcur_lc(parse) != lc_slash)
     87                return gtype;
     88
     89        tapply = stree_tapply_new();
     90        tapply->gtype = gtype;
     91        list_init(&tapply->targs);
     92
    8493        while (lcur_lc(parse) == lc_slash) {
    8594
     
    8897
    8998                lskip(parse);
    90                 b = parse_tpostfix(parse);
    91 
    92                 tapply = stree_tapply_new();
    93                 tapply->gtype = a;
    94                 tapply->targ = b;
    95 
    96                 tmp = stree_texpr_new(tc_tapply);
    97                 tmp->u.tapply = tapply;
    98                 a = tmp;
    99         }
    100 
    101         return a;
     99                targ = parse_tpostfix(parse);
     100
     101                list_append(&tapply->targs, targ);
     102        }
     103
     104        aexpr = stree_texpr_new(tc_tapply);
     105        aexpr->u.tapply = tapply;
     106        return aexpr;
    102107}
    103108
     
    224229                texpr->u.tnameref = parse_tnameref(parse);
    225230                break;
     231        case lc_bool:
     232        case lc_char:
    226233        case lc_int:
    227234        case lc_string:
     
    249256
    250257        switch (lcur_lc(parse)) {
     258        case lc_bool:
     259                tlc = tlc_bool;
     260                break;
     261        case lc_char:
     262                tlc = tlc_char;
     263                break;
    251264        case lc_int:
    252265                tlc = tlc_int;
  • uspace/app/sbi/src/parse.c

    r2721a75 r1ef0fc3  
    156156        stree_csimbr_t *csimbr;
    157157        stree_symbol_t *symbol;
     158        stree_ident_t *targ_name;
    158159
    159160        switch (dclass) {
     
    168169        csi = stree_csi_new(cc);
    169170        csi->name = parse_ident(parse);
     171
     172        list_init(&csi->targ_names);
     173
     174        while (lcur_lc(parse) == lc_slash) {
     175                lskip(parse);
     176                targ_name = parse_ident(parse);
     177                list_append(&csi->targ_names, targ_name);
     178        }
    170179
    171180        symbol = stree_symbol_new(sc_csi);
  • uspace/app/sbi/src/rdata.c

    r2721a75 r1ef0fc3  
    5151#include "mytypes.h"
    5252#include "stree.h"
     53#include "symbol.h"
    5354
    5455#include "rdata.h"
    5556
     57static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest);
     58static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest);
    5659static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest);
    5760static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest);
     
    287290}
    288291
     292/** Allocate new boolean.
     293 *
     294 * @return      New boolean.
     295 */
     296rdata_bool_t *rdata_bool_new(void)
     297{
     298        rdata_bool_t *bool_v;
     299
     300        bool_v = calloc(1, sizeof(rdata_bool_t));
     301        if (bool_v == NULL) {
     302                printf("Memory allocation failed.\n");
     303                exit(1);
     304        }
     305
     306        return bool_v;
     307}
     308
     309/** Allocate new character.
     310 *
     311 * @return      New character.
     312 */
     313rdata_char_t *rdata_char_new(void)
     314{
     315        rdata_char_t *char_v;
     316
     317        char_v = calloc(1, sizeof(rdata_char_t));
     318        if (char_v == NULL) {
     319                printf("Memory allocation failed.\n");
     320                exit(1);
     321        }
     322
     323        return char_v;
     324}
     325
    289326/** Allocate new integer.
    290327 *
     
    398435
    399436        switch (src->vc) {
     437        case vc_bool:
     438                rdata_bool_copy(src->u.bool_v, &nvar->u.bool_v);
     439                break;
     440        case vc_char:
     441                rdata_char_copy(src->u.char_v, &nvar->u.char_v);
     442                break;
    400443        case vc_int:
    401444                rdata_int_copy(src->u.int_v, &nvar->u.int_v);
     
    422465
    423466        *dest = nvar;
     467}
     468
     469/** Copy boolean.
     470 *
     471 * @param src           Source boolean.
     472 * @param dest          Place to store pointer to new boolean.
     473 */
     474static void rdata_bool_copy(rdata_bool_t *src, rdata_bool_t **dest)
     475{
     476        *dest = rdata_bool_new();
     477        (*dest)->value = src->value;
     478}
     479
     480/** Copy character.
     481 *
     482 * @param src           Source character.
     483 * @param dest          Place to store pointer to new character.
     484 */
     485static void rdata_char_copy(rdata_char_t *src, rdata_char_t **dest)
     486{
     487        *dest = rdata_char_new();
     488        bigint_clone(&src->value, &(*dest)->value);
    424489}
    425490
     
    550615        var->vc = nvar->vc;
    551616        switch (nvar->vc) {
     617        case vc_bool: var->u.bool_v = nvar->u.bool_v; break;
     618        case vc_char: var->u.char_v = nvar->u.char_v; break;
    552619        case vc_int: var->u.int_v = nvar->u.int_v; break;
    553620        case vc_string: var->u.string_v = nvar->u.string_v; break;
     
    621688static void rdata_var_print(rdata_var_t *var)
    622689{
     690        int val;
     691
    623692        switch (var->vc) {
     693        case vc_bool:
     694                printf("bool(%s)", var->u.bool_v->value ? "true" : "false");
     695                break;
     696        case vc_char:
     697                printf("char(");
     698                if (bigint_get_value_int(&var->u.char_v->value, &val) == EOK)
     699                        printf("'%c'", val);
     700                else
     701                        printf("???:x%x\n", (unsigned) val);
     702                printf(")");
     703                break;
    624704        case vc_int:
    625705                printf("int(");
     
    631711                break;
    632712        case vc_ref:
    633                 printf("ref");
     713                printf("ref(");
     714                rdata_var_print(var->u.ref_v->vref);
     715                printf(")");
    634716                break;
    635717        case vc_deleg:
    636                 printf("deleg");
     718                printf("deleg(");
     719                if (var->u.deleg_v->obj != NULL) {
     720                        rdata_var_print(var->u.deleg_v->obj);
     721                        printf(",");
     722                }
     723                symbol_print_fqn(var->u.deleg_v->sym);
     724                printf(")");
     725                break;
     726        case vc_array:
     727                printf("array");
    637728                break;
    638729        case vc_object:
    639730                printf("object");
    640731                break;
    641         default:
    642                 printf("print(%d)\n", var->vc);
    643                 assert(b_false);
    644         }
    645 }
     732        case vc_resource:
     733                printf("resource(%p)", var->u.resource_v->data);
     734                break;
     735        }
     736}
  • uspace/app/sbi/src/rdata.h

    r2721a75 r1ef0fc3  
    4545rdata_array_t *rdata_array_new(int rank);
    4646rdata_object_t *rdata_object_new(void);
     47rdata_bool_t *rdata_bool_new(void);
     48rdata_char_t *rdata_char_new(void);
    4749rdata_int_t *rdata_int_new(void);
    4850rdata_string_t *rdata_string_new(void);
  • uspace/app/sbi/src/rdata_t.h

    r2721a75 r1ef0fc3  
    3535#include "intmap_t.h"
    3636
     37/** Boolean variable. */
     38typedef struct {
     39        bool_t value;
     40} rdata_bool_t;
     41
     42/** Character variable.
     43 *
     44 * Sysel character type should be able to store arbitrarily (or at least
     45 * very) large character sets.
     46 */
     47typedef struct {
     48        bigint_t value;
     49} rdata_char_t;
     50
    3751/** Integer variable.
    3852 *
     
    4357        bigint_t value;
    4458} rdata_int_t;
    45 
    4659
    4760/** String variable */
     
    104117
    105118typedef enum var_class {
     119        /** Boolean */
     120        vc_bool,
     121
     122        /** Character **/
     123        vc_char,
     124
    106125        /** Integer */
    107126        vc_int,
     
    136155
    137156        union {
     157                rdata_bool_t *bool_v;
     158                rdata_char_t *char_v;
    138159                rdata_int_t *int_v;
    139160                rdata_string_t *string_v;
  • uspace/app/sbi/src/run.c

    r2721a75 r1ef0fc3  
    657657void run_value_item_to_var(rdata_item_t *item, rdata_var_t **var)
    658658{
     659        rdata_char_t *char_v;
    659660        rdata_int_t *int_v;
    660661        rdata_string_t *string_v;
     
    666667
    667668        switch (in_var->vc) {
     669        case vc_char:
     670                *var = rdata_var_new(vc_char);
     671                char_v = rdata_char_new();
     672
     673                (*var)->u.char_v = char_v;
     674                bigint_clone(&item->u.value->var->u.char_v->value,
     675                    &char_v->value);
     676                break;
    668677        case vc_int:
    669678                *var = rdata_var_new(vc_int);
  • uspace/app/sbi/src/run_expr.c

    r2721a75 r1ef0fc3  
    5353static void run_literal(run_t *run, stree_literal_t *literal,
    5454    rdata_item_t **res);
     55static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
     56    rdata_item_t **res);
     57static void run_lit_char(run_t *run, stree_lit_char_t *lit_char,
     58    rdata_item_t **res);
    5559static void run_lit_int(run_t *run, stree_lit_int_t *lit_int,
    5660    rdata_item_t **res);
     
    6468
    6569static void run_binop(run_t *run, stree_binop_t *binop, rdata_item_t **res);
     70static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
     71    rdata_value_t *v2, rdata_item_t **res);
     72static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
     73    rdata_value_t *v2, rdata_item_t **res);
    6674static void run_binop_int(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
    6775    rdata_value_t *v2, rdata_item_t **res);
     
    315323        printf("Run literal.\n");
    316324#endif
    317 
    318325        switch (literal->ltc) {
     326        case ltc_bool:
     327                run_lit_bool(run, &literal->u.lit_bool, res);
     328                break;
     329        case ltc_char:
     330                run_lit_char(run, &literal->u.lit_char, res);
     331                break;
    319332        case ltc_int:
    320333                run_lit_int(run, &literal->u.lit_int, res);
     
    326339                run_lit_string(run, &literal->u.lit_string, res);
    327340                break;
    328         default:
    329                 assert(b_false);
    330         }
     341        }
     342}
     343
     344/** Evaluate Boolean literal. */
     345static void run_lit_bool(run_t *run, stree_lit_bool_t *lit_bool,
     346    rdata_item_t **res)
     347{
     348        rdata_item_t *item;
     349        rdata_value_t *value;
     350        rdata_var_t *var;
     351        rdata_bool_t *bool_v;
     352
     353#ifdef DEBUG_RUN_TRACE
     354        printf("Run Boolean literal.\n");
     355#endif
     356        (void) run;
     357
     358        item = rdata_item_new(ic_value);
     359        value = rdata_value_new();
     360        var = rdata_var_new(vc_bool);
     361        bool_v = rdata_bool_new();
     362
     363        item->u.value = value;
     364        value->var = var;
     365        var->u.bool_v = bool_v;
     366        bool_v->value = lit_bool->value;
     367
     368        *res = item;
     369}
     370
     371/** Evaluate character literal. */
     372static void run_lit_char(run_t *run, stree_lit_char_t *lit_char,
     373    rdata_item_t **res)
     374{
     375        rdata_item_t *item;
     376        rdata_value_t *value;
     377        rdata_var_t *var;
     378        rdata_char_t *char_v;
     379
     380#ifdef DEBUG_RUN_TRACE
     381        printf("Run character literal.\n");
     382#endif
     383        (void) run;
     384
     385        item = rdata_item_new(ic_value);
     386        value = rdata_value_new();
     387        var = rdata_var_new(vc_char);
     388        char_v = rdata_char_new();
     389
     390        item->u.value = value;
     391        value->var = var;
     392        var->u.char_v = char_v;
     393        bigint_clone(&lit_char->value, &char_v->value);
     394
     395        *res = item;
    331396}
    332397
     
    486551
    487552        switch (v1->var->vc) {
     553        case vc_bool:
     554                run_binop_bool(run, binop, v1, v2, res);
     555                break;
     556        case vc_char:
     557                run_binop_char(run, binop, v1, v2, res);
     558                break;
    488559        case vc_int:
    489560                run_binop_int(run, binop, v1, v2, res);
     
    495566                run_binop_ref(run, binop, v1, v2, res);
    496567                break;
     568        case vc_deleg:
     569        case vc_array:
     570        case vc_object:
     571        case vc_resource:
     572                assert(b_false);
     573        }
     574}
     575
     576/** Evaluate binary operation on bool arguments. */
     577static void run_binop_bool(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
     578    rdata_value_t *v2, rdata_item_t **res)
     579{
     580        rdata_item_t *item;
     581        rdata_value_t *value;
     582        rdata_var_t *var;
     583        rdata_bool_t *bool_v;
     584
     585        bool_t b1, b2;
     586
     587        (void) run;
     588
     589        item = rdata_item_new(ic_value);
     590        value = rdata_value_new();
     591        var = rdata_var_new(vc_bool);
     592        bool_v = rdata_bool_new();
     593
     594        item->u.value = value;
     595        value->var = var;
     596        var->u.bool_v = bool_v;
     597
     598        b1 = v1->var->u.bool_v->value;
     599        b2 = v2->var->u.bool_v->value;
     600
     601        switch (binop->bc) {
     602        case bo_plus:
     603        case bo_minus:
     604        case bo_mult:
     605                assert(b_false);
     606
     607        case bo_equal:
     608                bool_v->value = (b1 == b2);
     609                break;
     610        case bo_notequal:
     611                bool_v->value = (b1 != b2);
     612                break;
     613        case bo_lt:
     614                bool_v->value = (b1 == b_false) && (b2 == b_true);
     615                break;
     616        case bo_gt:
     617                bool_v->value = (b1 == b_true) && (b2 == b_false);
     618                break;
     619        case bo_lt_equal:
     620                bool_v->value = (b1 == b_false) || (b2 == b_true);
     621                break;
     622        case bo_gt_equal:
     623                bool_v->value = (b1 == b_true) || (b2 == b_false);
     624                break;
     625        }
     626
     627        *res = item;
     628}
     629
     630/** Evaluate binary operation on char arguments. */
     631static void run_binop_char(run_t *run, stree_binop_t *binop, rdata_value_t *v1,
     632    rdata_value_t *v2, rdata_item_t **res)
     633{
     634        rdata_item_t *item;
     635        rdata_value_t *value;
     636        rdata_var_t *var;
     637        rdata_bool_t *bool_v;
     638
     639        bigint_t *c1, *c2;
     640        bigint_t diff;
     641        bool_t zf, nf;
     642
     643        (void) run;
     644
     645        item = rdata_item_new(ic_value);
     646        value = rdata_value_new();
     647
     648        item->u.value = value;
     649
     650        c1 = &v1->var->u.char_v->value;
     651        c2 = &v2->var->u.char_v->value;
     652
     653        var = rdata_var_new(vc_bool);
     654        bool_v = rdata_bool_new();
     655        var->u.bool_v = bool_v;
     656        value->var = var;
     657
     658        bigint_sub(c1, c2, &diff);
     659        zf = bigint_is_zero(&diff);
     660        nf = bigint_is_negative(&diff);
     661
     662        switch (binop->bc) {
     663        case bo_plus:
     664        case bo_minus:
     665        case bo_mult:
     666                assert(b_false);
     667
     668        case bo_equal:
     669                bool_v->value = zf;
     670                break;
     671        case bo_notequal:
     672                bool_v->value = !zf;
     673                break;
     674        case bo_lt:
     675                bool_v->value = (!zf && nf);
     676                break;
     677        case bo_gt:
     678                bool_v->value = (!zf && !nf);
     679                break;
     680        case bo_lt_equal:
     681                bool_v->value = (zf || nf);
     682                break;
     683        case bo_gt_equal:
     684                bool_v->value = !nf;
     685                break;
    497686        default:
    498                 printf("Unimplemented: Binary operation arguments of "
    499                     "type %d.\n", v1->var->vc);
    500                 exit(1);
    501         }
     687                assert(b_false);
     688        }
     689
     690        *res = item;
    502691}
    503692
     
    510699        rdata_var_t *var;
    511700        rdata_int_t *int_v;
     701        rdata_bool_t *bool_v;
    512702
    513703        bigint_t *i1, *i2;
     
    520710        item = rdata_item_new(ic_value);
    521711        value = rdata_value_new();
    522         var = rdata_var_new(vc_int);
    523         int_v = rdata_int_new();
    524712
    525713        item->u.value = value;
    526         value->var = var;
    527         var->u.int_v = int_v;
    528714
    529715        i1 = &v1->var->u.int_v->value;
     
    534720        switch (binop->bc) {
    535721        case bo_plus:
     722                int_v = rdata_int_new();
    536723                bigint_add(i1, i2, &int_v->value);
    537724                break;
    538725        case bo_minus:
     726                int_v = rdata_int_new();
    539727                bigint_sub(i1, i2, &int_v->value);
    540728                break;
    541729        case bo_mult:
     730                int_v = rdata_int_new();
    542731                bigint_mul(i1, i2, &int_v->value);
    543732                break;
     
    548737
    549738        if (done) {
     739                var = rdata_var_new(vc_int);
     740                var->u.int_v = int_v;
     741                value->var = var;
    550742                *res = item;
    551743                return;
    552744        }
     745
     746        var = rdata_var_new(vc_bool);
     747        bool_v = rdata_bool_new();
     748        var->u.bool_v = bool_v;
     749        value->var = var;
    553750
    554751        /* Relational operation. */
     
    558755        nf = bigint_is_negative(&diff);
    559756
    560         /* XXX We should have a real boolean type. */
    561757        switch (binop->bc) {
    562758        case bo_equal:
    563                 bigint_init(&int_v->value, zf ? 1 : 0);
     759                bool_v->value = zf;
    564760                break;
    565761        case bo_notequal:
    566                 bigint_init(&int_v->value, !zf ? 1 : 0);
     762                bool_v->value = !zf;
    567763                break;
    568764        case bo_lt:
    569                 bigint_init(&int_v->value, (!zf && nf) ? 1 : 0);
     765                bool_v->value = (!zf && nf);
    570766                break;
    571767        case bo_gt:
    572                 bigint_init(&int_v->value, (!zf && !nf) ? 1 : 0);
     768                bool_v->value = (!zf && !nf);
    573769                break;
    574770        case bo_lt_equal:
    575                 bigint_init(&int_v->value, (zf || nf) ? 1 : 0);
     771                bool_v->value = (zf || nf);
    576772                break;
    577773        case bo_gt_equal:
    578                 bigint_init(&int_v->value, !nf ? 1 : 0);
     774                bool_v->value = !nf;
    579775                break;
    580776        default:
     
    631827        rdata_value_t *value;
    632828        rdata_var_t *var;
    633         rdata_int_t *int_v;
     829        rdata_bool_t *bool_v;
    634830
    635831        rdata_var_t *ref1, *ref2;
     
    639835        item = rdata_item_new(ic_value);
    640836        value = rdata_value_new();
    641         var = rdata_var_new(vc_int);
    642         int_v = rdata_int_new();
     837        var = rdata_var_new(vc_bool);
     838        bool_v = rdata_bool_new();
    643839
    644840        item->u.value = value;
    645841        value->var = var;
    646         var->u.int_v = int_v;
     842        var->u.bool_v = bool_v;
    647843
    648844        ref1 = v1->var->u.ref_v->vref;
     
    650846
    651847        switch (binop->bc) {
    652         /* XXX We should have a real boolean type. */
    653848        case bo_equal:
    654                 bigint_init(&int_v->value, (ref1 == ref2) ? 1 : 0);
     849                bool_v->value = (ref1 == ref2);
    655850                break;
    656851        case bo_notequal:
    657                 bigint_init(&int_v->value, (ref1 != ref2) ? 1 : 0);
     852                bool_v->value = (ref1 != ref2);
    658853                break;
    659854        default:
     
    9801175            access->member_name);
    9811176
    982         if (member == NULL) {
    983                 printf("Error: CSI '");
    984                 symbol_print_fqn(deleg_v->sym);
    985                 printf("' has no member named '%s'.\n",
    986                     strtab_get_str(access->member_name->sid));
    987                 exit(1);
    988         }
     1177        /* Member existence should be ensured by static type checking. */
     1178        assert(member != NULL);
    9891179
    9901180#ifdef DEBUG_RUN_TRACE
     
    14751665
    14761666        if (rc1 != EOK || rc2 != EOK) {
     1667#ifdef DEBUG_RUN_TRACE
    14771668                printf("Error: String index (value: %d) is out of range.\n",
    14781669                    arg_val);
    1479                 exit(1);
     1670#endif
     1671                /* Raise Error.OutOfBounds */
     1672                run_raise_exc(run, run->program->builtin->error_outofbounds);
     1673                *res = run_recovery_item(run);
     1674                return;
    14801675        }
    14811676
     
    14851680        ritem->u.value = value;
    14861681
    1487         cvar = rdata_var_new(vc_int);
    1488         cvar->u.int_v = rdata_int_new();
    1489         bigint_init(&cvar->u.int_v->value, cval);
     1682        cvar = rdata_var_new(vc_char);
     1683        cvar->u.char_v = rdata_char_new();
     1684        bigint_init(&cvar->u.char_v->value, cval);
    14901685        value->var = cvar;
    14911686
     
    16511846 * Tries to interpret @a item as a boolean value. If it is not a boolean
    16521847 * value, this generates an error.
    1653  *
    1654  * XXX Currently int supplants the role of a true boolean type.
    16551848 */
    16561849bool_t run_item_boolean_value(run_t *run, rdata_item_t *item)
     
    16651858        var = vitem->u.value->var;
    16661859
    1667         if (var->vc != vc_int) {
    1668                 printf("Error: Boolean (int) expression expected.\n");
    1669                 exit(1);
    1670         }
    1671 
    1672         return !bigint_is_zero(&var->u.int_v->value);
    1673 }
     1860        assert(var->vc == vc_bool);
     1861        return var->u.bool_v->value;
     1862}
  • uspace/app/sbi/src/run_texpr.c

    r2721a75 r1ef0fc3  
    3333#include "list.h"
    3434#include "mytypes.h"
     35#include "strtab.h"
    3536#include "symbol.h"
    3637#include "tdata.h"
     
    4647static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx,
    4748    stree_tnameref_t *tnameref, tdata_item_t **res);
     49static void run_tapply(stree_program_t *prog, stree_csi_t *ctx,
     50    stree_tapply_t *tapply, tdata_item_t **res);
    4851
    4952void run_texpr(stree_program_t *prog, stree_csi_t *ctx, stree_texpr_t *texpr,
     
    6467                break;
    6568        case tc_tapply:
    66                 printf("Unimplemented: Evaluate type expression type %d.\n",
    67                     texpr->tc);
    68                 exit(1);
     69                run_tapply(prog, ctx, texpr->u.tapply, res);
     70                break;
    6971        }
    7072}
     
    8587        run_texpr(prog, ctx, taccess->arg, &targ_i);
    8688
     89        if (targ_i->tic == tic_ignore) {
     90                *res = tdata_item_new(tic_ignore);
     91                return;
     92        }
     93
    8794        if (targ_i->tic != tic_tobject) {
    8895                printf("Error: Using '.' with type which is not an object.\n");
    89                 exit(1);
     96                *res = tdata_item_new(tic_ignore);
     97                return;
    9098        }
    9199
     
    94102
    95103        sym = symbol_lookup_in_csi(prog, base_csi, taccess->member_name);
    96 
    97         /* Existence should have been verified in type checking phase. */
    98         assert(sym != NULL);
     104        if (sym == NULL) {
     105                printf("Error: CSI '");
     106                symbol_print_fqn(csi_to_symbol(base_csi));
     107                printf("' has no member named '%s'.\n",
     108                    strtab_get_str(taccess->member_name->sid));
     109                *res = tdata_item_new(tic_ignore);
     110                return;
     111        }
    99112
    100113        if (sym->sc != sc_csi) {
     
    102115                symbol_print_fqn(sym);
    103116                printf("' is not a CSI.\n");
    104                 exit(1);
     117                *res = tdata_item_new(tic_ignore);
     118                return;
    105119        }
    106120
     
    130144        /* Evaluate base type. */
    131145        run_texpr(prog, ctx, tindex->base_type, &base_ti);
     146
     147        if (base_ti->tic == tic_ignore) {
     148                *res = tdata_item_new(tic_ignore);
     149                return;
     150        }
    132151
    133152        /* Construct type item. */
     
    167186
    168187        switch (tliteral->tlc) {
     188        case tlc_bool: tpc = tpc_bool; break;
     189        case tlc_char: tpc = tpc_char; break;
    169190        case tlc_int: tpc = tpc_int; break;
    170191        case tlc_string: tpc = tpc_string; break;
     
    191212#endif
    192213        sym = symbol_lookup_in_csi(prog, ctx, tnameref->name);
    193 
    194         /* Existence should have been verified in type-checking phase. */
    195         assert(sym);
     214        if (sym == NULL) {
     215                printf("Error: Symbol '%s' not found.\n",
     216                    strtab_get_str(tnameref->name->sid));
     217                *res = tdata_item_new(tic_ignore);
     218                return;
     219        }
    196220
    197221        if (sym->sc != sc_csi) {
     
    199223                symbol_print_fqn(sym);
    200224                printf("' is not a CSI.\n");
    201                 exit(1);
     225                *res = tdata_item_new(tic_ignore);
     226                return;
    202227        }
    203228
     
    212237        *res = titem;
    213238}
     239
     240static void run_tapply(stree_program_t *prog, stree_csi_t *ctx,
     241    stree_tapply_t *tapply, tdata_item_t **res)
     242{
     243        tdata_item_t *base_ti;
     244        tdata_item_t *arg_ti;
     245        tdata_item_t *titem;
     246        tdata_object_t *tobject;
     247
     248        list_node_t *arg_n;
     249        stree_texpr_t *arg;
     250
     251#ifdef DEBUG_RUN_TRACE
     252        printf("Evaluating type apply operation.\n");
     253#endif
     254        /* Construct type item. */
     255        titem = tdata_item_new(tic_tobject);
     256        tobject = tdata_object_new();
     257        titem->u.tobject = tobject;
     258
     259        /* Evaluate base (generic) type. */
     260        run_texpr(prog, ctx, tapply->gtype, &base_ti);
     261
     262        if (base_ti->tic != tic_tobject) {
     263                printf("Error: Base type of generic application is not "
     264                    "a CSI.\n");
     265                *res = tdata_item_new(tic_ignore);
     266                return;
     267        }
     268
     269        tobject->static_ref = b_false;
     270        tobject->csi = base_ti->u.tobject->csi;
     271        list_init(&tobject->targs);
     272
     273        /* Evaluate type arguments. */
     274        arg_n = list_first(&tapply->targs);
     275        while (arg_n != NULL) {
     276                arg = list_node_data(arg_n, stree_texpr_t *);
     277                run_texpr(prog, ctx, arg, &arg_ti);
     278
     279                if (arg_ti->tic == tic_ignore) {
     280                        *res = tdata_item_new(tic_ignore);
     281                        return;
     282                }
     283
     284                list_append(&tobject->targs, arg_ti);
     285
     286                arg_n = list_next(&tapply->targs, arg_n);
     287        }
     288
     289        *res = titem;
     290}
  • uspace/app/sbi/src/stree_t.h

    r2721a75 r1ef0fc3  
    5454} stree_self_ref_t;
    5555
     56/** Boolean literal */
     57typedef struct {
     58        bool_t value;
     59} stree_lit_bool_t;
     60
     61/** Character literal */
     62typedef struct {
     63        bigint_t value;
     64} stree_lit_char_t;
     65
     66/** Integer literal */
    5667typedef struct {
    5768        bigint_t value;
     
    6273} stree_lit_ref_t;
    6374
     75/** String literal */
    6476typedef struct {
    6577        char *value;
     
    6779
    6880typedef enum {
     81        ltc_bool,
     82        ltc_char,
    6983        ltc_int,
    7084        ltc_ref,
     
    7690        literal_class_t ltc;
    7791        union {
     92                stree_lit_bool_t lit_bool;
     93                stree_lit_char_t lit_char;
    7894                stree_lit_int_t lit_int;
    7995                stree_lit_ref_t lit_ref;
     
    214230/** Type literal class */
    215231typedef enum {
     232        tlc_bool,
     233        tlc_char,
    216234        tlc_int,
    217235        tlc_resource,
     
    239257/** Type application operation */
    240258typedef struct {
    241         /** Arguments */
    242         struct stree_texpr *gtype, *targ;
     259        /* Base type */
     260        struct stree_texpr *gtype;
     261
     262        /** (Formal) type arguments */
     263        list_t targs; /* of stree_texpr_t */
    243264} stree_tapply_t;
    244265
     
    496517        stree_ident_t *name;
    497518
     519        /** List of type argument names */
     520        list_t targ_names; /* of stree_ident_t */
     521
    498522        /** Symbol for this CSI */
    499523        struct stree_symbol *symbol;
  • uspace/app/sbi/src/stype.c

    r2721a75 r1ef0fc3  
    6464static void stype_wef(stype_t *stype, stree_wef_t *wef_s);
    6565
    66 static tdata_item_t *stype_boolean_titem(stype_t *stype);
    67 
    6866/** Type module */
    6967void stype_module(stype_t *stype, stree_module_t *module)
     
    189187static void stype_var(stype_t *stype, stree_var_t *var)
    190188{
     189        tdata_item_t *titem;
     190
    191191        (void) stype;
    192192        (void) var;
     193
     194        run_texpr(stype->program, stype->current_csi, var->type,
     195            &titem);
     196        if (titem->tic == tic_ignore) {
     197                /* An error occured. */
     198                stype_note_error(stype);
     199                return;
     200        }
    193201}
    194202
     
    283291        stype_block_vr_t *block_vr;
    284292        stree_vdecl_t *old_vdecl;
     293        tdata_item_t *titem;
    285294
    286295#ifdef DEBUG_TYPE_TRACE
     
    297306        }
    298307
     308        run_texpr(stype->program, stype->current_csi, vdecl_s->type,
     309            &titem);
     310        if (titem->tic == tic_ignore) {
     311                /* An error occured. */
     312                stype_note_error(stype);
     313                return;
     314        }
     315
    299316        intmap_set(&block_vr->vdecls, vdecl_s->name->sid, vdecl_s);
    300 
    301317}
    302318
     
    524540                        goto failure;
    525541                break;
    526         default:
     542        case tic_tfun:
    527543                printf("Error: Unimplemented: Converting '");
    528544                tdata_item_print(src);
     
    531547                printf("'.\n");
    532548                stype_note_error(stype);
     549                break;
     550        case tic_ignore:
     551                assert(b_false);
    533552        }
    534553
     
    547566
    548567/** Return a boolean type item */
    549 static tdata_item_t *stype_boolean_titem(stype_t *stype)
     568tdata_item_t *stype_boolean_titem(stype_t *stype)
    550569{
    551570        tdata_item_t *titem;
     
    554573        (void) stype;
    555574
    556         /* XXX Use a true boolean type */
    557575        titem = tdata_item_new(tic_tprimitive);
    558         tprimitive = tdata_primitive_new(tpc_int);
     576        tprimitive = tdata_primitive_new(tpc_bool);
    559577        titem->u.tprimitive = tprimitive;
    560578
  • uspace/app/sbi/src/stype.h

    r2721a75 r1ef0fc3  
    4141    tdata_item_t *dest);
    4242
     43tdata_item_t *stype_boolean_titem(stype_t *stype);
     44
    4345stree_vdecl_t *stype_local_vars_lookup(stype_t *stype, sid_t name);
    4446stree_proc_arg_t *stype_proc_args_lookup(stype_t *stype, sid_t name);
  • uspace/app/sbi/src/stype_expr.c

    r2721a75 r1ef0fc3  
    5353static void stype_binop(stype_t *stype, stree_binop_t *binop,
    5454    tdata_item_t **rtitem);
     55
    5556static void stype_binop_tprimitive(stype_t *stype, stree_binop_t *binop,
    5657    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem);
     58static void stype_binop_bool(stype_t *stype, stree_binop_t *binop,
     59    tdata_item_t **rtitem);
     60static void stype_binop_char(stype_t *stype, stree_binop_t *binop,
     61    tdata_item_t **rtitem);
     62static void stype_binop_int(stype_t *stype, stree_binop_t *binop,
     63    tdata_item_t **rtitem);
     64static void stype_binop_nil(stype_t *stype, stree_binop_t *binop,
     65    tdata_item_t **rtitem);
     66static void stype_binop_string(stype_t *stype, stree_binop_t *binop,
     67    tdata_item_t **rtitem);
     68static void stype_binop_resource(stype_t *stype, stree_binop_t *binop,
     69    tdata_item_t **rtitem);
     70
    5771static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop,
    5872    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem);
     
    7387static void stype_access_tarray(stype_t *stype, stree_access_t *access,
    7488    tdata_item_t *arg_ti, tdata_item_t **rtitem);
    75 static void stype_access_tgeneric(stype_t *stype, stree_access_t *access,
    76     tdata_item_t *arg_ti, tdata_item_t **rtitem);
    7789
    7890static void stype_call(stype_t *stype, stree_call_t *call,
     
    8698    tdata_item_t *base_ti, tdata_item_t **rtitem);
    8799static void stype_index_tarray(stype_t *stype, stree_index_t *index,
    88     tdata_item_t *base_ti, tdata_item_t **rtitem);
    89 static void stype_index_tgeneric(stype_t *stype, stree_index_t *index,
    90100    tdata_item_t *base_ti, tdata_item_t **rtitem);
    91101
     
    248258
    249259        switch (literal->ltc) {
     260        case ltc_bool: tpc = tpc_bool; break;
     261        case ltc_char: tpc = tpc_char; break;
    250262        case ltc_int: tpc = tpc_int; break;
    251263        case ltc_ref: tpc = tpc_nil; break;
     
    333345}
    334346
    335 /** Type a binary operation arguments of primitive type. */
     347/** Type a binary operation with arguments of primitive type. */
    336348static void stype_binop_tprimitive(stype_t *stype, stree_binop_t *binop,
    337349    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem)
    338350{
     351        assert(ta->tic == tic_tprimitive);
     352        assert(tb->tic == tic_tprimitive);
     353
     354        switch (ta->u.tprimitive->tpc) {
     355        case tpc_bool:
     356                stype_binop_bool(stype, binop, rtitem);
     357                break;
     358        case tpc_char:
     359                stype_binop_char(stype, binop, rtitem);
     360                break;
     361        case tpc_int:
     362                stype_binop_int(stype, binop, rtitem);
     363                break;
     364        case tpc_nil:
     365                stype_binop_nil(stype, binop, rtitem);
     366                break;
     367        case tpc_string:
     368                stype_binop_string(stype, binop, rtitem);
     369                break;
     370        case tpc_resource:
     371                stype_binop_resource(stype, binop, rtitem);
     372                break;
     373        }
     374}
     375
     376/** Type a binary operation with bool arguments. */
     377static void stype_binop_bool(stype_t *stype, stree_binop_t *binop,
     378    tdata_item_t **rtitem)
     379{
    339380        tprimitive_class_t rtpc;
    340381        tdata_item_t *res_ti;
    341382
    342         (void) stype;
    343 
    344         assert(ta->tic == tic_tprimitive);
    345         assert(tb->tic == tic_tprimitive);
    346 
    347         switch (ta->u.tprimitive->tpc) {
    348         case tpc_int:
    349                 rtpc = tpc_int;
    350                 break;
    351         case tpc_nil:
    352                 printf("Unimplemented; Binary operation on nil.\n");
    353                 stype_note_error(stype);
    354                 rtpc = tpc_nil;
    355                 break;
    356         case tpc_string:
    357                 if (binop->bc != bo_plus) {
    358                         printf("Unimplemented: Binary operation(%d) "
    359                             "on strings.\n", binop->bc);
    360                         stype_note_error(stype);
    361                 }
    362                 rtpc = tpc_string;
    363                 break;
    364         case tpc_resource:
    365                 printf("Error: Cannot apply operator to resource type.\n");
    366                 stype_note_error(stype);
    367                 rtpc = tpc_resource;
     383        switch (binop->bc) {
     384        case bo_equal:
     385        case bo_notequal:
     386        case bo_lt:
     387        case bo_gt:
     388        case bo_lt_equal:
     389        case bo_gt_equal:
     390                /* Comparison -> boolean type */
     391                rtpc = tpc_bool;
     392                break;
     393        case bo_plus:
     394        case bo_minus:
     395        case bo_mult:
     396                /* Arithmetic -> error */
     397                printf("Error: Binary operation (%d) on booleans.\n",
     398                    binop->bc);
     399                stype_note_error(stype);
     400                *rtitem = stype_recovery_titem(stype);
     401                return;
    368402        }
    369403
     
    374408}
    375409
    376 /** Type a binary operation arguments of an object type. */
     410/** Type a binary operation with char arguments. */
     411static void stype_binop_char(stype_t *stype, stree_binop_t *binop,
     412    tdata_item_t **rtitem)
     413{
     414        tprimitive_class_t rtpc;
     415        tdata_item_t *res_ti;
     416
     417        (void) stype;
     418
     419        switch (binop->bc) {
     420        case bo_equal:
     421        case bo_notequal:
     422        case bo_lt:
     423        case bo_gt:
     424        case bo_lt_equal:
     425        case bo_gt_equal:
     426                /* Comparison -> boolean type */
     427                rtpc = tpc_bool;
     428                break;
     429        case bo_plus:
     430        case bo_minus:
     431        case bo_mult:
     432                /* Arithmetic -> error */
     433                printf("Error: Binary operation (%d) on characters.\n",
     434                    binop->bc);
     435                stype_note_error(stype);
     436                rtpc = tpc_char;
     437                break;
     438        }
     439
     440        res_ti = tdata_item_new(tic_tprimitive);
     441        res_ti->u.tprimitive = tdata_primitive_new(rtpc);
     442
     443        *rtitem = res_ti;
     444}
     445
     446/** Type a binary operation with int arguments. */
     447static void stype_binop_int(stype_t *stype, stree_binop_t *binop,
     448    tdata_item_t **rtitem)
     449{
     450        tprimitive_class_t rtpc;
     451        tdata_item_t *res_ti;
     452
     453        (void) stype;
     454
     455        switch (binop->bc) {
     456        case bo_equal:
     457        case bo_notequal:
     458        case bo_lt:
     459        case bo_gt:
     460        case bo_lt_equal:
     461        case bo_gt_equal:
     462                /* Comparison -> boolean type */
     463                rtpc = tpc_bool;
     464                break;
     465        case bo_plus:
     466        case bo_minus:
     467        case bo_mult:
     468                /* Arithmetic -> int type */
     469                rtpc = tpc_int;
     470                break;
     471        }
     472
     473        res_ti = tdata_item_new(tic_tprimitive);
     474        res_ti->u.tprimitive = tdata_primitive_new(rtpc);
     475
     476        *rtitem = res_ti;
     477}
     478
     479/** Type a binary operation with nil arguments. */
     480static void stype_binop_nil(stype_t *stype, stree_binop_t *binop,
     481    tdata_item_t **rtitem)
     482{
     483        (void) binop;
     484
     485        printf("Unimplemented; Binary operation on nil.\n");
     486        stype_note_error(stype);
     487        *rtitem = stype_recovery_titem(stype);
     488}
     489
     490/** Type a binary operation with string arguments. */
     491static void stype_binop_string(stype_t *stype, stree_binop_t *binop,
     492    tdata_item_t **rtitem)
     493{
     494        tprimitive_class_t rtpc;
     495        tdata_item_t *res_ti;
     496
     497        if (binop->bc != bo_plus) {
     498                printf("Unimplemented: Binary operation(%d) "
     499                    "on strings.\n", binop->bc);
     500                stype_note_error(stype);
     501                *rtitem = stype_recovery_titem(stype);
     502                return;
     503        }
     504
     505        rtpc = tpc_string;
     506
     507        res_ti = tdata_item_new(tic_tprimitive);
     508        res_ti->u.tprimitive = tdata_primitive_new(rtpc);
     509
     510        *rtitem = res_ti;
     511}
     512
     513/** Type a binary operation with resource arguments. */
     514static void stype_binop_resource(stype_t *stype, stree_binop_t *binop,
     515    tdata_item_t **rtitem)
     516{
     517        tprimitive_class_t rtpc;
     518        tdata_item_t *res_ti;
     519
     520        (void) binop;
     521
     522        printf("Error: Cannot apply operator to resource type.\n");
     523        stype_note_error(stype);
     524        rtpc = tpc_resource;
     525
     526        res_ti = tdata_item_new(tic_tprimitive);
     527        res_ti->u.tprimitive = tdata_primitive_new(rtpc);
     528
     529        *rtitem = res_ti;
     530}
     531
     532/** Type a binary operation with arguments of an object type. */
    377533static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop,
    378534    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem)
     
    390546        case bo_equal:
    391547        case bo_notequal:
    392                 /* Comparing objects -> boolean (XXX int for now) type */
    393                 res_ti = tdata_item_new(tic_tprimitive);
    394                 res_ti->u.tprimitive = tdata_primitive_new(tpc_int);
     548                /* Comparing objects -> boolean type */
     549                res_ti = stype_boolean_titem(stype);
    395550                break;
    396551        default:
    397552                printf("Error: Binary operation (%d) on objects.\n",
    398553                    binop->bc);
    399                 res_ti = NULL;
     554                stype_note_error(stype);
     555                *rtitem = stype_recovery_titem(stype);
    400556                return;
    401557        }
     
    451607
    452608        switch (ta->u.tprimitive->tpc) {
     609        case tpc_bool:
     610                rtpc = tpc_bool;
     611                break;
    453612        case tpc_int:
    454613                rtpc = tpc_int;
     
    480639         */
    481640        run_texpr(stype->program, stype->current_csi, new_op->texpr, rtitem);
     641
     642        if ((*rtitem)->tic == tic_ignore) {
     643                /* An error occured when evaluating the type expression. */
     644                stype_note_error(stype);
     645        }
    482646}
    483647
     
    510674        case tic_tarray:
    511675                stype_access_tarray(stype, access, arg_ti, rtitem);
    512                 break;
    513         case tic_tgeneric:
    514                 stype_access_tgeneric(stype, access, arg_ti, rtitem);
    515676                break;
    516677        case tic_tfun:
     
    566727                printf("' has no member named '%s'.\n",
    567728                    strtab_get_str(access->member_name->sid));
     729                stype_note_error(stype);
    568730                *rtitem = stype_recovery_titem(stype);
    569731                return;
     
    615777
    616778        printf("Error: Unimplemented: Accessing array type '");
    617         tdata_item_print(arg_ti);
    618         printf("'.\n");
    619         stype_note_error(stype);
    620         *rtitem = stype_recovery_titem(stype);
    621 }
    622 
    623 /** Type a generic access operation. */
    624 static void stype_access_tgeneric(stype_t *stype, stree_access_t *access,
    625     tdata_item_t *arg_ti, tdata_item_t **rtitem)
    626 {
    627         (void) stype;
    628         (void) access;
    629         (void) rtitem;
    630 
    631         printf("Error: Unimplemented: Accessing generic type '");
    632779        tdata_item_print(arg_ti);
    633780        printf("'.\n");
     
    792939                stype_index_tarray(stype, index, base_ti, rtitem);
    793940                break;
    794         case tic_tgeneric:
    795                 stype_index_tgeneric(stype, index, base_ti, rtitem);
    796                 break;
    797941        case tic_tfun:
    798942                printf("Error: Indexing a function.\n");
     
    821965        if (tprimitive->tpc == tpc_string) {
    822966                titem = tdata_item_new(tic_tprimitive);
    823                 titem->u.tprimitive = tdata_primitive_new(tpc_int);
     967                titem->u.tprimitive = tdata_primitive_new(tpc_char);
    824968                *rtitem = titem;
    825969                return;
     
    9141058}
    9151059
    916 /** Type a generic indexing operation. */
    917 static void stype_index_tgeneric(stype_t *stype, stree_index_t *index,
    918     tdata_item_t *base_ti, tdata_item_t **rtitem)
    919 {
    920         (void) stype;
    921         (void) index;
    922         (void) rtitem;
    923 
    924         printf("Error: Unimplemented: Indexing generic type '");
    925         tdata_item_print(base_ti);
    926         printf("'.\n");
    927         stype_note_error(stype);
    928         *rtitem = stype_recovery_titem(stype);
    929 }
    930 
    9311060/** Type an assignment. */
    9321061static void stype_assign(stype_t *stype, stree_assign_t *assign,
  • uspace/app/sbi/src/symbol.c

    r2721a75 r1ef0fc3  
    6868                return b;
    6969        case tc_tapply:
    70                 printf("Internal error: Generic types not implemented.\n");
    71                 exit(1);
     70                return symbol_xlookup_in_csi(prog, scope,
     71                    texpr->u.tapply->gtype);
    7272        default:
    7373                assert(b_false);
  • uspace/app/sbi/src/tdata.c

    r2721a75 r1ef0fc3  
    3131#include <stdlib.h>
    3232#include <assert.h>
     33#include "list.h"
    3334#include "mytypes.h"
    3435#include "stree.h"
     
    4041static void tdata_tobject_print(tdata_object_t *tobject);
    4142static void tdata_tarray_print(tdata_array_t *tarray);
    42 static void tdata_tgeneric_print(tdata_generic_t *tgeneric);
    4343static void tdata_tfun_print(tdata_fun_t *tfun);
    4444
     
    142142                tdata_tarray_print(titem->u.tarray);
    143143                break;
    144         case tic_tgeneric:
    145                 tdata_tgeneric_print(titem->u.tgeneric);
    146                 break;
    147144        case tic_tfun:
    148145                tdata_tfun_print(titem->u.tfun);
     
    157154{
    158155        switch (tprimitive->tpc) {
     156        case tpc_bool: printf("bool"); break;
     157        case tpc_char: printf("char"); break;
    159158        case tpc_int: printf("int"); break;
    160159        case tpc_nil: printf("nil"); break;
     
    167166{
    168167        stree_symbol_t *csi_sym;
     168        list_node_t *arg_n;
     169        tdata_item_t *arg;
    169170
    170171        csi_sym = csi_to_symbol(tobject->csi);
    171172        assert(csi_sym != NULL);
    172173        symbol_print_fqn(csi_sym);
     174
     175        arg_n = list_first(&tobject->targs);
     176        while (arg_n != NULL) {
     177                arg = list_node_data(arg_n, tdata_item_t *);
     178                putchar('/');
     179                tdata_item_print(arg);
     180                arg_n = list_next(&tobject->targs, arg_n);
     181        }
    173182}
    174183
     
    185194}
    186195
    187 static void tdata_tgeneric_print(tdata_generic_t *tgeneric)
    188 {
    189         (void) tgeneric;
    190         printf("unimplemented(generic)");
    191 }
    192 
    193196static void tdata_tfun_print(tdata_fun_t *tfun)
    194197{
  • uspace/app/sbi/src/tdata_t.h

    r2721a75 r1ef0fc3  
    3434/** Class of primitive type. */
    3535typedef enum {
     36        /** Boolean type */
     37        tpc_bool,
     38        /** Character type */
     39        tpc_char,
    3640        /** Integer type */
    3741        tpc_int,
     
    5761        /** CSI definition */
    5862        struct stree_csi *csi;
     63
     64        /** (Real) type arguments */
     65        list_t targs; /* of tdata_item_t */
    5966} tdata_object_t;
    6067
     
    7077        list_t extents; /* of stree_expr_t */
    7178} tdata_array_t;
    72 
    73 /** Generic type. */
    74 typedef struct {
    75 } tdata_generic_t;
    7679
    7780/** Functional type. */
     
    9093        /** Array type item */
    9194        tic_tarray,
    92         /** Generic type item */
    93         tic_tgeneric,
    9495        /** Function type item */
    9596        tic_tfun,
     
    106107                tdata_object_t *tobject;
    107108                tdata_array_t *tarray;
    108                 tdata_generic_t *tgeneric;
    109109                tdata_fun_t *tfun;
    110110        } u;
  • uspace/dist/src/sysel/demos/list.sy

    r2721a75 r1ef0fc3  
    2727--
    2828
    29 -- Doubly-linked list implementation.
    30 class List is
    31         var head : ListNode;
    32 
    33         -- Initialize list.
    34         fun Init() is
    35                 head = new ListNode();
    36                 head.prev = head;
    37                 head.next = head;
    38         end
    39 
    40         -- Append new entry at the end of the list.
    41         fun Append(data : int) is
    42                 var n : ListNode;
    43                 var ntl : ListNode;
    44 
    45                 ntl = head.prev;
    46 
    47                 n = new ListNode();
    48                 n.value = data;
    49 
    50                 n.prev = ntl;
    51                 n.next = head;
    52                 n.head = head;
    53 
    54                 ntl.next = n;
    55                 head.prev = n;
    56         end
    57 
    58         -- Return first node in the list or @c nil if there is none.
    59         prop First : ListNode is
    60                 get is
    61                     return get_first();
    62                 end
    63         end
    64 
    65         -- Return first node in the list or @c nil if there is none.
    66         fun get_first() : ListNode is
    67                 if head.next == head then
    68                         return nil;
    69                 else
    70                         return head.next;
    71                 end
    72         end
    73 end
    74 
    75 class ListNode is
    76         var value : int;
    77 
    78         var prev : ListNode;
    79         var next : ListNode;
    80         var head : ListNode;
    81 
    82         -- Value stored in this node.
    83         prop Value : int is
    84                 get is
    85                         return value;
    86                 end
    87         end
    88 
    89         -- Previous node in list.
    90         prop Prev : ListNode is
    91                 get is
    92                         return get_prev();
    93                 end
    94         end
    95 
    96         -- Next node in list.
    97         prop Next : ListNode is
    98                 get is
    99                         return get_next();
    100                 end
    101         end
    102 
    103         -- Get next node.
    104         fun get_next() : ListNode is
    105                 if next != head then
    106                         return next;
    107                 else
    108                         return nil;
    109                 end
    110         end
    111 
    112         -- Get previous node.
    113         fun get_prev() : ListNode is
    114                 if prev != head then
    115                         return next;
    116                 else
    117                         return nil;
    118                 end
    119         end
    120 
    121 end
    122 
     29-- Using the List class from the library.
    12330class ListDemo is
    12431        fun Main() is
    12532                var list : List;
     33                var i : Int;
    12634
    12735                list = new List();
    12836                list.Init();
    12937
    130                 list.Append(5);
    131                 list.Append(6);
    132                 list.Append(7);
    133                 list.Append(8);
     38                -- We need autoboxing or generics to get rid of this mess.
     39                i = new Int(); i.Value = 5; list.Append(i);
     40                i = new Int(); i.Value = 6; list.Append(i);
     41                i = new Int(); i.Value = 7; list.Append(i);
     42                i = new Int(); i.Value = 8; list.Append(i);
    13443
    13544                var n : ListNode;
     
    13746                n = list.First;
    13847                while n != nil do
    139                         Builtin.WriteLine(n.value);
     48                        Builtin.WriteLine((n.value as Int).Value);
    14049                        n = n.Next;
    14150                end
Note: See TracChangeset for help on using the changeset viewer.