Changeset a35b458 in mainline for uspace/app/vcalc/vcalc.c
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/vcalc/vcalc.c
r3061bc1 ra35b458 85 85 typedef struct { 86 86 link_t link; 87 87 88 88 stack_item_type_t type; 89 89 union { … … 104 104 { 105 105 assert(is_digit(c)); 106 106 107 107 return (c - '0'); 108 108 } … … 149 149 if (!item) 150 150 return false; 151 151 152 152 link_initialize(&item->link); 153 153 item->type = ITEM_VALUE; 154 154 155 155 if (value_neg) 156 156 item->data.value = -value; 157 157 else 158 158 item->data.value = value; 159 159 160 160 list_prepend(&item->link, stack); 161 161 162 162 return true; 163 163 } … … 168 168 if (!item) 169 169 return false; 170 170 171 171 link_initialize(&item->link); 172 172 item->type = ITEM_OPERATOR; 173 173 item->data.operator = operator; 174 174 list_prepend(&item->link, stack); 175 175 176 176 return true; 177 177 } … … 182 182 if (!link) 183 183 return false; 184 184 185 185 stack_item_t *item = list_get_instance(link, stack_item_t, link); 186 186 if (item->type != ITEM_VALUE) 187 187 return false; 188 188 189 189 *value = item->data.value; 190 190 191 191 list_remove(link); 192 192 free(item); 193 193 194 194 return true; 195 195 } … … 200 200 if (!link) 201 201 return false; 202 202 203 203 stack_item_t *item = list_get_instance(link, stack_item_t, link); 204 204 if (item->type != ITEM_OPERATOR) 205 205 return false; 206 206 207 207 *operator = item->data.operator; 208 208 209 209 list_remove(link); 210 210 free(item); 211 211 212 212 return true; 213 213 } … … 220 220 stack_item_t *item = list_get_instance(link, stack_item_t, 221 221 link); 222 222 223 223 list_remove(link); 224 224 free(item); … … 242 242 if (b == 0) 243 243 return false; 244 244 245 245 *value = a / b; 246 246 break; … … 248 248 return false; 249 249 } 250 250 251 251 return true; 252 252 } … … 277 277 break; 278 278 } 279 279 280 280 if (!list_empty(stack)) { 281 281 operator_t operator; … … 285 285 break; 286 286 } 287 287 288 288 int64_t value_a; 289 289 if (!stack_pop_value(stack, &value_a)) { … … 292 292 break; 293 293 } 294 294 295 295 if (!compute(value_a, operator, *value, value)) { 296 296 *state = STATE_ERROR; … … 298 298 break; 299 299 } 300 300 301 301 if (!stack_push_value(stack, *value, false)) { 302 302 *state = STATE_ERROR; … … 322 322 expr = NULL; 323 323 } 324 324 325 325 switch (error_type) { 326 326 case ERROR_SYNTAX: … … 338 338 { 339 339 const char *subexpr = (const char *) widget_get_data(widget); 340 340 341 341 if (expr != NULL) { 342 342 char *new_expr; 343 343 344 344 asprintf(&new_expr, "%s%s", expr, subexpr); 345 345 free(expr); … … 347 347 } else 348 348 expr = str_dup(subexpr); 349 349 350 350 display_update(); 351 351 } … … 357 357 expr = NULL; 358 358 } 359 359 360 360 display_update(); 361 361 } … … 365 365 if (expr == NULL) 366 366 return; 367 367 368 368 list_t stack; 369 369 list_initialize(&stack); 370 370 371 371 error_type_t error_type = ERROR_SYNTAX; 372 372 size_t i = 0; … … 375 375 bool value_neg = false; 376 376 operator_t last_operator = OPERATOR_NONE; 377 377 378 378 while ((state != STATE_FINISH) && (state != STATE_ERROR)) { 379 379 switch (state) { … … 394 394 state = STATE_ERROR; 395 395 break; 396 396 397 397 case STATE_DIGIT: 398 398 if (is_digit(expr[i])) { … … 403 403 state = STATE_ERROR; 404 404 break; 405 405 406 406 case STATE_VALUE: 407 407 if (is_digit(expr[i])) { … … 414 414 break; 415 415 } 416 416 417 417 value = 0; 418 418 value_neg = false; 419 419 420 420 operator_t operator = get_operator(expr[i]); 421 421 422 422 if (get_priority(operator) <= get_priority(last_operator)) { 423 423 evaluate(&stack, &value, &state, &error_type); 424 424 if (state == STATE_ERROR) 425 425 break; 426 426 427 427 if (!stack_push_value(&stack, value, value_neg)) { 428 428 state = STATE_ERROR; … … 430 430 } 431 431 } 432 432 433 433 if (!stack_push_operator(&stack, operator)) { 434 434 state = STATE_ERROR; 435 435 break; 436 436 } 437 437 438 438 last_operator = operator; 439 439 i++; … … 444 444 break; 445 445 } 446 446 447 447 state = STATE_FINISH; 448 448 } else 449 449 state = STATE_ERROR; 450 450 break; 451 451 452 452 default: 453 453 state = STATE_ERROR; 454 454 } 455 455 } 456 456 457 457 evaluate(&stack, &value, &state, &error_type); 458 458 stack_cleanup(&stack); 459 459 460 460 if (state == STATE_ERROR) { 461 461 display_error(error_type); 462 462 return; 463 463 } 464 464 465 465 free(expr); 466 466 asprintf(&expr, "%" PRId64, value); … … 474 474 return 1; 475 475 } 476 476 477 477 window_t *main_window = window_open(argv[1], NULL, 478 478 WINDOW_MAIN | WINDOW_DECORATED | WINDOW_RESIZEABLE, NAME); … … 481 481 return 2; 482 482 } 483 483 484 484 pixel_t grd_bg = PIXEL(255, 240, 240, 240); 485 485 486 486 pixel_t btn_bg = PIXEL(255, 0, 0, 0); 487 487 pixel_t btn_fg = PIXEL(200, 200, 200, 200); 488 488 489 489 pixel_t lbl_bg = PIXEL(255, 240, 240, 240); 490 490 pixel_t lbl_fg = PIXEL(255, 0, 0, 0); 491 491 492 492 grid_t *grid = create_grid(window_root(main_window), NULL, 4, 5, grd_bg); 493 493 494 494 display = create_label(NULL, NULL, NULL_DISPLAY, 16, lbl_bg, lbl_fg); 495 495 496 496 button_t *btn_1 = create_button(NULL, "1", "1", 16, btn_bg, btn_fg, 497 497 lbl_fg); … … 514 514 button_t *btn_0 = create_button(NULL, "0", "0", 16, btn_bg, btn_fg, 515 515 lbl_fg); 516 516 517 517 button_t *btn_add = create_button(NULL, "+", "+", 16, btn_bg, btn_fg, 518 518 lbl_fg); … … 523 523 button_t *btn_div = create_button(NULL, "/", "/", 16, btn_bg, btn_fg, 524 524 lbl_fg); 525 525 526 526 button_t *btn_eval = create_button(NULL, NULL, "=", 16, btn_bg, btn_fg, 527 527 lbl_fg); 528 528 button_t *btn_c = create_button(NULL, NULL, "C", 16, btn_bg, btn_fg, 529 529 lbl_fg); 530 530 531 531 if ((!grid) || (!display) || (!btn_1) || (!btn_2) || (!btn_3) || 532 532 (!btn_4) || (!btn_5) || (!btn_6) || (!btn_7) || (!btn_8) || … … 537 537 return 3; 538 538 } 539 539 540 540 sig_connect(&btn_1->clicked, &btn_1->widget, on_btn_click); 541 541 sig_connect(&btn_2->clicked, &btn_2->widget, on_btn_click); … … 548 548 sig_connect(&btn_9->clicked, &btn_9->widget, on_btn_click); 549 549 sig_connect(&btn_0->clicked, &btn_0->widget, on_btn_click); 550 550 551 551 sig_connect(&btn_add->clicked, &btn_add->widget, on_btn_click); 552 552 sig_connect(&btn_sub->clicked, &btn_sub->widget, on_btn_click); 553 553 sig_connect(&btn_div->clicked, &btn_div->widget, on_btn_click); 554 554 sig_connect(&btn_mul->clicked, &btn_mul->widget, on_btn_click); 555 555 556 556 sig_connect(&btn_eval->clicked, &btn_eval->widget, on_eval_click); 557 557 sig_connect(&btn_c->clicked, &btn_c->widget, on_c_click); 558 558 559 559 grid->add(grid, &display->widget, 0, 0, 4, 1); 560 560 561 561 grid->add(grid, &btn_1->widget, 0, 1, 1, 1); 562 562 grid->add(grid, &btn_2->widget, 1, 1, 1, 1); 563 563 grid->add(grid, &btn_3->widget, 2, 1, 1, 1); 564 564 grid->add(grid, &btn_add->widget, 3, 1, 1, 1); 565 565 566 566 grid->add(grid, &btn_4->widget, 0, 2, 1, 1); 567 567 grid->add(grid, &btn_5->widget, 1, 2, 1, 1); 568 568 grid->add(grid, &btn_6->widget, 2, 2, 1, 1); 569 569 grid->add(grid, &btn_sub->widget, 3, 2, 1, 1); 570 570 571 571 grid->add(grid, &btn_7->widget, 0, 3, 1, 1); 572 572 grid->add(grid, &btn_8->widget, 1, 3, 1, 1); 573 573 grid->add(grid, &btn_9->widget, 2, 3, 1, 1); 574 574 grid->add(grid, &btn_mul->widget, 3, 3, 1, 1); 575 575 576 576 grid->add(grid, &btn_c->widget, 0, 4, 1, 1); 577 577 grid->add(grid, &btn_0->widget, 1, 4, 1, 1); 578 578 grid->add(grid, &btn_eval->widget, 2, 4, 1, 1); 579 579 grid->add(grid, &btn_div->widget, 3, 4, 1, 1); 580 580 581 581 window_resize(main_window, 0, 0, 400, 400, WINDOW_PLACEMENT_ANY); 582 582 window_exec(main_window); 583 583 584 584 task_retval(0); 585 585 async_manager(); 586 586 587 587 return 0; 588 588 }
Note:
See TracChangeset
for help on using the changeset viewer.