Changeset 0153c87 in mainline


Ignore:
Timestamp:
2012-08-10T20:09:36Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6be4142
Parents:
c9797067
Message:

Bithenge: more FAT, operators, and fixes

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/expression.c

    rc9797067 r0153c87  
    104104        case BITHENGE_EXPRESSION_ADD: /* fallthrough */
    105105        case BITHENGE_EXPRESSION_SUBTRACT: /* fallthrough */
    106         case BITHENGE_EXPRESSION_MULTIPLY:
     106        case BITHENGE_EXPRESSION_MULTIPLY: /* fallthrough */
     107        case BITHENGE_EXPRESSION_INTEGER_DIVIDE: /* fallthrough */
     108        case BITHENGE_EXPRESSION_MODULO: /* fallthrough */
     109        case BITHENGE_EXPRESSION_LESS_THAN: /* fallthrough */
     110        case BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL: /* fallthrough */
     111        case BITHENGE_EXPRESSION_GREATER_THAN: /* fallthrough */
     112        case BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL:
    107113                rc = EINVAL;
    108114                if (bithenge_node_type(a) != BITHENGE_NODE_INTEGER)
     
    127133                rc = bithenge_new_integer_node(out, a_int * b_int);
    128134                break;
     135        case BITHENGE_EXPRESSION_INTEGER_DIVIDE:
     136                /* Integer division can behave in three major ways when the
     137                 * operands are signed: truncated, floored, or Euclidean. When
     138                 * b > 0, we give the same result as floored and Euclidean;
     139                 * otherwise, we currently raise an error. See
     140                 * https://en.wikipedia.org/wiki/Modulo_operation and its
     141                 * references. */
     142                if (b_int <= 0) {
     143                        rc = EINVAL;
     144                        break;
     145                }
     146                rc = bithenge_new_integer_node(out,
     147                    (a_int / b_int) + (a_int % b_int < 0 ? -1 : 0));
     148                break;
     149        case BITHENGE_EXPRESSION_MODULO:
     150                /* This is consistent with division; see above. */
     151                if (b_int <= 0) {
     152                        rc = EINVAL;
     153                        break;
     154                }
     155                rc = bithenge_new_integer_node(out,
     156                    (a_int % b_int) + (a_int % b_int < 0 ? b_int : 0));
     157                break;
     158        case BITHENGE_EXPRESSION_LESS_THAN:
     159                rc = bithenge_new_boolean_node(out, a_int < b_int);
     160                break;
     161        case BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL:
     162                rc = bithenge_new_boolean_node(out, a_int <= b_int);
     163                break;
     164        case BITHENGE_EXPRESSION_GREATER_THAN:
     165                rc = bithenge_new_boolean_node(out, a_int > b_int);
     166                break;
     167        case BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL:
     168                rc = bithenge_new_boolean_node(out, a_int >= b_int);
     169                break;
    129170        case BITHENGE_EXPRESSION_EQUALS:
    130171                rc = bithenge_new_boolean_node(out, bithenge_node_equal(a, b));
     172                break;
     173        case BITHENGE_EXPRESSION_NOT_EQUALS:
     174                rc = bithenge_new_boolean_node(out,
     175                    ~bithenge_node_equal(a, b));
    131176                break;
    132177        case BITHENGE_EXPRESSION_INVALID_BINARY_OP:
     
    495540        for (; scope && !bithenge_scope_is_barrier(scope);
    496541            scope = bithenge_scope_outer(scope)) {
     542                bithenge_node_t *cur = bithenge_scope_get_current_node(scope);
     543                if (!cur)
     544                        continue;
    497545                bithenge_node_inc_ref(self->key);
    498                 bithenge_node_t *cur = bithenge_scope_get_current_node(scope);
    499546                int rc = bithenge_node_get(cur, self->key, out);
    500547                bithenge_node_dec_ref(cur);
  • uspace/app/bithenge/expression.h

    rc9797067 r0153c87  
    9696        BITHENGE_EXPRESSION_SUBTRACT,
    9797        BITHENGE_EXPRESSION_MULTIPLY,
     98        BITHENGE_EXPRESSION_INTEGER_DIVIDE,
     99        BITHENGE_EXPRESSION_MODULO,
     100        BITHENGE_EXPRESSION_LESS_THAN,
     101        BITHENGE_EXPRESSION_GREATER_THAN,
     102        BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL,
     103        BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL,
    98104        BITHENGE_EXPRESSION_EQUALS,
     105        BITHENGE_EXPRESSION_NOT_EQUALS,
    99106} bithenge_binary_op_t;
    100107
  • uspace/app/bithenge/script.c

    rc9797067 r0153c87  
    5656        TOKEN_ERROR,
    5757        TOKEN_EOF,
     58        TOKEN_GREATER_THAN_OR_EQUAL,
    5859        TOKEN_IDENTIFIER,
    5960        TOKEN_INTEGER,
     61        TOKEN_INTEGER_DIVIDE,
    6062        TOKEN_LEFT_ARROW,
     63        TOKEN_LESS_THAN_OR_EQUAL,
     64        TOKEN_NOT_EQUAL,
    6165
    6266        /* Keywords */
     
    272276                        state->buffer_pos++;
    273277                        state->token = TOKEN_LEFT_ARROW;
     278                } else if (state->buffer[state->buffer_pos] == '=') {
     279                        state->buffer_pos++;
     280                        state->token = TOKEN_LESS_THAN_OR_EQUAL;
     281                }
     282        } else if (ch == '>') {
     283                state->token = ch;
     284                state->buffer_pos++;
     285                if (state->buffer[state->buffer_pos] == '=') {
     286                        state->buffer_pos++;
     287                        state->token = TOKEN_GREATER_THAN_OR_EQUAL;
    274288                }
    275289        } else if (ch == '=') {
     
    280294                        state->buffer_pos++;
    281295                }
     296        } else if (ch == '/') {
     297                state->token = ch;
     298                state->buffer_pos++;
     299                if (state->buffer[state->buffer_pos] == '/') {
     300                        state->token = TOKEN_INTEGER_DIVIDE;
     301                        state->buffer_pos++;
     302                }
     303        } else if (ch == '!') {
     304                state->token = ch;
     305                state->buffer_pos++;
     306                if (state->buffer[state->buffer_pos] == '=') {
     307                        state->token = TOKEN_NOT_EQUAL;
     308                        state->buffer_pos++;
     309                }
    282310        } else {
    283311                state->token = ch;
     
    388416        PRECEDENCE_NONE,
    389417        PRECEDENCE_EQUALS,
     418        PRECEDENCE_COMPARE,
    390419        PRECEDENCE_ADD,
    391420        PRECEDENCE_MULTIPLY,
     
    401430        case '*':
    402431                return BITHENGE_EXPRESSION_MULTIPLY;
     432        case TOKEN_INTEGER_DIVIDE:
     433                return BITHENGE_EXPRESSION_INTEGER_DIVIDE;
     434        case '%':
     435                return BITHENGE_EXPRESSION_MODULO;
     436        case '<':
     437                return BITHENGE_EXPRESSION_LESS_THAN;
     438        case TOKEN_LESS_THAN_OR_EQUAL:
     439                return BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL;
     440        case '>':
     441                return BITHENGE_EXPRESSION_GREATER_THAN;
     442        case TOKEN_GREATER_THAN_OR_EQUAL:
     443                return BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL;
    403444        case TOKEN_EQUALS:
    404445                return BITHENGE_EXPRESSION_EQUALS;
     446        case TOKEN_NOT_EQUAL:
     447                return BITHENGE_EXPRESSION_NOT_EQUALS;
    405448        default:
    406449                return BITHENGE_EXPRESSION_INVALID_BINARY_OP;
     
    414457        case BITHENGE_EXPRESSION_SUBTRACT:
    415458                return PRECEDENCE_ADD;
    416         case BITHENGE_EXPRESSION_MULTIPLY:
     459        case BITHENGE_EXPRESSION_MULTIPLY: /* fallthrough */
     460        case BITHENGE_EXPRESSION_INTEGER_DIVIDE: /* fallthrough */
     461        case BITHENGE_EXPRESSION_MODULO:
    417462                return PRECEDENCE_MULTIPLY;
    418         case BITHENGE_EXPRESSION_EQUALS:
     463        case BITHENGE_EXPRESSION_LESS_THAN: /* fallthrough */
     464        case BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL: /* fallthrough */
     465        case BITHENGE_EXPRESSION_GREATER_THAN: /* fallthrough */
     466        case BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL:
     467                return PRECEDENCE_COMPARE;
     468        case BITHENGE_EXPRESSION_EQUALS: /* fallthrough */
     469        case BITHENGE_EXPRESSION_NOT_EQUALS:
    419470                return PRECEDENCE_EQUALS;
    420471        default:
     
    614665                next_token(state);
    615666
    616                 bithenge_expression_t *expr2 = parse_postfix_expression(state);
     667                bithenge_expression_t *expr2 =
     668                    parse_expression_precedence(state, precedence);
    617669                if (state->error != EOK) {
    618670                        bithenge_expression_dec_ref(expr2);
  • uspace/dist/src/bithenge/fat.bh

    rc9797067 r0153c87  
    3232transform u32 = uint32le;
    3333
     34transform fat_dir_entry(disk) = struct {
     35        .filename <- known_length(8);
     36        .extension <- known_length(3);
     37        .attrs <- u8;
     38        .flags <- u8;
     39        .ctime_fine <- u8;
     40        .ctime <- u16;
     41        .cdate <- u16;
     42        .adate <- u16;
     43        .permissions <- u16;
     44        .mtime <- u16;
     45        .mdate <- u16;
     46        .start <- u16;
     47        .size <- u32;
     48};
     49
     50transform fat_table(bits, num_clusters) = switch (bits) {
     51        12: partial {repeat(num_clusters) { uint_le(12) }} <- bits_le;
     52        16: partial {repeat(num_clusters) { u16 }};
     53        32: partial {repeat(num_clusters) { u32 }};
     54};
     55
    3456transform fat_super(disk) = struct {
    3557        .jump_instruction <- known_length(3);
     
    6385        };
    6486
     87        .first_root_sector <- (.num_reserved_sectors + .num_fats * .sectors_per_fat);
     88        .first_data_sector <- (.first_root_sector +
     89            (.num_root_entries * 32 + .bytes_per_sector - 1) //
     90            .bytes_per_sector);
     91        .num_clusters <- (2 + (.num_sectors - .first_data_sector) // .sectors_per_cluster);
     92        .bits <- if (.num_clusters < 4085) { (12) }
     93            else { if (.num_clusters < 65525) { (16) } else { (32) } };
     94
     95        .fats <- partial(.num_reserved_sectors * .bytes_per_sector) {
     96                repeat(.num_fats) {
     97                        fat_table(.bits, .num_clusters) <-
     98                                known_length(.sectors_per_fat * .bytes_per_sector)
     99                }
     100        } <- (disk);
     101
     102        .root <- partial(.first_root_sector * .bytes_per_sector) {
     103                repeat(.num_root_entries) { fat_dir_entry(disk) } } <- (disk);
     104
    65105        .boot_signature <- (disk[510,2]); # b"\x55\xaa"; TODO: what if .bytes_per_sector < 512?
    66106};
Note: See TracChangeset for help on using the changeset viewer.