Changeset ad5c8a48 in mainline for uspace/app/bithenge/script.c
- Timestamp:
- 2012-08-07T21:35:50Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a66ea217
- Parents:
- c12b2ae
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/script.c
rc12b2ae rad5c8a48 52 52 * other token uses one of these values: */ 53 53 typedef enum { 54 TOKEN_ERROR = -128, 54 TOKEN_EQUALS = -128, 55 TOKEN_ERROR, 55 56 TOKEN_EOF, 56 57 TOKEN_IDENTIFIER, … … 261 262 state->token = TOKEN_LEFT_ARROW; 262 263 } 264 } else if (ch == '=') { 265 state->token = ch; 266 state->buffer_pos++; 267 if (state->buffer[state->buffer_pos] == '=') { 268 state->token = TOKEN_EQUALS; 269 state->buffer_pos++; 270 } 263 271 } else { 264 272 state->token = ch; … … 360 368 static bithenge_transform_t *parse_transform(state_t *state); 361 369 static bithenge_transform_t *parse_struct(state_t *state); 362 363 static bithenge_expression_t *parse_expression(state_t *state) 370 static bithenge_expression_t *parse_expression(state_t *state); 371 372 373 374 /***************** Expressions *****************/ 375 376 typedef enum { 377 PRECEDENCE_NONE, 378 PRECEDENCE_EQUALS, 379 PRECEDENCE_ADD, 380 PRECEDENCE_MULTIPLY, 381 } precedence_t; 382 383 static bithenge_binary_op_t token_as_binary_operator(token_type_t token) 384 { 385 switch ((int)token) { 386 case '+': 387 return BITHENGE_EXPRESSION_ADD; 388 case '-': 389 return BITHENGE_EXPRESSION_SUBTRACT; 390 case '*': 391 return BITHENGE_EXPRESSION_MULTIPLY; 392 case TOKEN_EQUALS: 393 return BITHENGE_EXPRESSION_EQUALS; 394 default: 395 return BITHENGE_EXPRESSION_INVALID_BINARY_OP; 396 } 397 } 398 399 static precedence_t binary_operator_precedence(bithenge_binary_op_t op) 400 { 401 switch (op) { 402 case BITHENGE_EXPRESSION_ADD: /* fallthrough */ 403 case BITHENGE_EXPRESSION_SUBTRACT: 404 return PRECEDENCE_ADD; 405 case BITHENGE_EXPRESSION_MULTIPLY: 406 return PRECEDENCE_MULTIPLY; 407 case BITHENGE_EXPRESSION_EQUALS: 408 return PRECEDENCE_EQUALS; 409 default: 410 assert(false); 411 return PRECEDENCE_NONE; 412 } 413 } 414 415 static bithenge_expression_t *parse_term(state_t *state) 364 416 { 365 417 int rc; … … 451 503 452 504 return expr; 505 } else if (state->token == '(') { 506 next_token(state); 507 bithenge_expression_t *expr = parse_expression(state); 508 expect(state, ')'); 509 return expr; 453 510 } else { 454 511 syntax_error(state, "expression expected"); … … 457 514 } 458 515 459 // state->token must be TOKEN_IDENTIFIER when this is called 516 static bithenge_expression_t *parse_expression_precedence(state_t *state, 517 precedence_t prev_precedence) 518 { 519 bithenge_expression_t *expr = parse_term(state); 520 while (state->error == EOK) { 521 bithenge_binary_op_t op = 522 token_as_binary_operator(state->token); 523 if (op == BITHENGE_EXPRESSION_INVALID_BINARY_OP) 524 break; 525 precedence_t precedence = binary_operator_precedence(op); 526 if (precedence <= prev_precedence) 527 break; 528 next_token(state); 529 530 bithenge_expression_t *expr2 = parse_term(state); 531 if (state->error != EOK) { 532 bithenge_expression_dec_ref(expr2); 533 break; 534 } 535 int rc = bithenge_binary_expression(&expr, op, expr, expr2); 536 if (rc != EOK) 537 error_errno(state, rc); 538 } 539 if (state->error != EOK) { 540 bithenge_expression_dec_ref(expr); 541 expr = NULL; 542 } 543 return expr; 544 } 545 546 static bithenge_expression_t *parse_expression(state_t *state) 547 { 548 return parse_expression_precedence(state, PRECEDENCE_NONE); 549 } 550 551 552 553 /* state->token must be TOKEN_IDENTIFIER when this is called. */ 460 554 static bithenge_transform_t *parse_invocation(state_t *state) 461 555 {
Note:
See TracChangeset
for help on using the changeset viewer.