Best JavaScript code snippet using storybook-root
BinaryExpression.js
Source:BinaryExpression.js
...7879 /**80 * Code generation for a binary operation81 */82 public void generateCode(83 BlockScope currentScope,84 CodeStream codeStream,85 boolean valueRequired) {8687 int pc = codeStream.position;88 Label falseLabel, endLabel;89 if (constant != Constant.NotAConstant) {90 if (valueRequired)91 codeStream.generateConstant(constant, implicitConversion);92 codeStream.recordPositionsFrom(pc, this.sourceStart);93 return;94 }95 bits |= OnlyValueRequiredMASK;96 switch ((bits & OperatorMASK) >> OperatorSHIFT) {97 case PLUS :98 switch (bits & ReturnTypeIDMASK) {99 case T_String :100 codeStream.generateStringConcatenationAppend(currentScope, left, right);101 if (!valueRequired)102 codeStream.pop();103 break;104 case T_int :105 left.generateCode(currentScope, codeStream, valueRequired);106 right.generateCode(currentScope, codeStream, valueRequired);107 if (valueRequired)108 codeStream.iadd();109 break;110 case T_long :111 left.generateCode(currentScope, codeStream, valueRequired);112 right.generateCode(currentScope, codeStream, valueRequired);113 if (valueRequired)114 codeStream.ladd();115 break;116 case T_double :117 left.generateCode(currentScope, codeStream, valueRequired);118 right.generateCode(currentScope, codeStream, valueRequired);119 if (valueRequired)120 codeStream.dadd();121 break;122 case T_float :123 left.generateCode(currentScope, codeStream, valueRequired);124 right.generateCode(currentScope, codeStream, valueRequired);125 if (valueRequired)126 codeStream.fadd();127 break;128 }129 break;130 case MINUS :131 switch (bits & ReturnTypeIDMASK) {132 case T_int :133 left.generateCode(currentScope, codeStream, valueRequired);134 right.generateCode(currentScope, codeStream, valueRequired);135 if (valueRequired)136 codeStream.isub();137 break;138 case T_long :139 left.generateCode(currentScope, codeStream, valueRequired);140 right.generateCode(currentScope, codeStream, valueRequired);141 if (valueRequired)142 codeStream.lsub();143 break;144 case T_double :145 left.generateCode(currentScope, codeStream, valueRequired);146 right.generateCode(currentScope, codeStream, valueRequired);147 if (valueRequired)148 codeStream.dsub();149 break;150 case T_float :151 left.generateCode(currentScope, codeStream, valueRequired);152 right.generateCode(currentScope, codeStream, valueRequired);153 if (valueRequired)154 codeStream.fsub();155 break;156 }157 break;158 case MULTIPLY :159 switch (bits & ReturnTypeIDMASK) {160 case T_int :161 left.generateCode(currentScope, codeStream, valueRequired);162 right.generateCode(currentScope, codeStream, valueRequired);163 if (valueRequired)164 codeStream.imul();165 break;166 case T_long :167 left.generateCode(currentScope, codeStream, valueRequired);168 right.generateCode(currentScope, codeStream, valueRequired);169 if (valueRequired)170 codeStream.lmul();171 break;172 case T_double :173 left.generateCode(currentScope, codeStream, valueRequired);174 right.generateCode(currentScope, codeStream, valueRequired);175 if (valueRequired)176 codeStream.dmul();177 break;178 case T_float :179 left.generateCode(currentScope, codeStream, valueRequired);180 right.generateCode(currentScope, codeStream, valueRequired);181 if (valueRequired)182 codeStream.fmul();183 break;184 }185 break;186 case DIVIDE :187 switch (bits & ReturnTypeIDMASK) {188 case T_int :189 left.generateCode(currentScope, codeStream, true);190 right.generateCode(currentScope, codeStream, true);191 codeStream.idiv();192 if (!valueRequired)193 codeStream.pop();194 break;195 case T_long :196 left.generateCode(currentScope, codeStream, true);197 right.generateCode(currentScope, codeStream, true);198 codeStream.ldiv();199 if (!valueRequired)200 codeStream.pop2();201 break;202 case T_double :203 left.generateCode(currentScope, codeStream, valueRequired);204 right.generateCode(currentScope, codeStream, valueRequired);205 if (valueRequired)206 codeStream.ddiv();207 break;208 case T_float :209 left.generateCode(currentScope, codeStream, valueRequired);210 right.generateCode(currentScope, codeStream, valueRequired);211 if (valueRequired)212 codeStream.fdiv();213 break;214 }215 break;216 case REMAINDER :217 switch (bits & ReturnTypeIDMASK) {218 case T_int :219 left.generateCode(currentScope, codeStream, true);220 right.generateCode(currentScope, codeStream, true);221 codeStream.irem();222 if (!valueRequired)223 codeStream.pop();224 break;225 case T_long :226 left.generateCode(currentScope, codeStream, true);227 right.generateCode(currentScope, codeStream, true);228 codeStream.lrem();229 if (!valueRequired)230 codeStream.pop2();231 break;232 case T_double :233 left.generateCode(currentScope, codeStream, valueRequired);234 right.generateCode(currentScope, codeStream, valueRequired);235 if (valueRequired)236 codeStream.drem();237 break;238 case T_float :239 left.generateCode(currentScope, codeStream, valueRequired);240 right.generateCode(currentScope, codeStream, valueRequired);241 if (valueRequired)242 codeStream.frem();243 break;244 }245 break;246 case AND :247 switch (bits & ReturnTypeIDMASK) {248 case T_int :249 // 0 & x250 if ((left.constant != Constant.NotAConstant)251 && (left.constant.typeID() == T_int)252 && (left.constant.intValue() == 0)) {253 right.generateCode(currentScope, codeStream, false);254 if (valueRequired)255 codeStream.iconst_0();256 } else {257 // x & 0258 if ((right.constant != Constant.NotAConstant)259 && (right.constant.typeID() == T_int)260 && (right.constant.intValue() == 0)) {261 left.generateCode(currentScope, codeStream, false);262 if (valueRequired)263 codeStream.iconst_0();264 } else {265 left.generateCode(currentScope, codeStream, valueRequired);266 right.generateCode(currentScope, codeStream, valueRequired);267 if (valueRequired)268 codeStream.iand();269 }270 }271 break;272 case T_long :273 // 0 & x274 if ((left.constant != Constant.NotAConstant)275 && (left.constant.typeID() == T_long)276 && (left.constant.longValue() == 0L)) {277 right.generateCode(currentScope, codeStream, false);278 if (valueRequired)279 codeStream.lconst_0();280 } else {281 // x & 0282 if ((right.constant != Constant.NotAConstant)283 && (right.constant.typeID() == T_long)284 && (right.constant.longValue() == 0L)) {285 left.generateCode(currentScope, codeStream, false);286 if (valueRequired)287 codeStream.lconst_0();288 } else {289 left.generateCode(currentScope, codeStream, valueRequired);290 right.generateCode(currentScope, codeStream, valueRequired);291 if (valueRequired)292 codeStream.land();293 }294 }295 break;296 case T_boolean : // logical and297 generateOptimizedLogicalAnd(298 currentScope,299 codeStream,300 null,301 (falseLabel = new Label(codeStream)),302 valueRequired);303 /* improving code gen for such a case: boolean b = i < 0 && false;304 * since the label has never been used, we have the inlined value on the stack. */305 if (falseLabel.hasForwardReferences()) {306 if (valueRequired) {307 codeStream.iconst_1();308 if ((bits & ValueForReturnMASK) != 0) {309 codeStream.ireturn();310 falseLabel.place();311 codeStream.iconst_0();312 } else {313 codeStream.goto_(endLabel = new Label(codeStream));314 codeStream.decrStackSize(1);315 falseLabel.place();316 codeStream.iconst_0();317 endLabel.place();318 }319 } else {320 falseLabel.place();321 }322 }323 }324 break;325 case OR :326 switch (bits & ReturnTypeIDMASK) {327 case T_int :328 // 0 | x329 if ((left.constant != Constant.NotAConstant)330 && (left.constant.typeID() == T_int)331 && (left.constant.intValue() == 0)) {332 right.generateCode(currentScope, codeStream, valueRequired);333 } else {334 // x | 0335 if ((right.constant != Constant.NotAConstant)336 && (right.constant.typeID() == T_int)337 && (right.constant.intValue() == 0)) {338 left.generateCode(currentScope, codeStream, valueRequired);339 } else {340 left.generateCode(currentScope, codeStream, valueRequired);341 right.generateCode(currentScope, codeStream, valueRequired);342 if (valueRequired)343 codeStream.ior();344 }345 }346 break;347 case T_long :348 // 0 | x349 if ((left.constant != Constant.NotAConstant)350 && (left.constant.typeID() == T_long)351 && (left.constant.longValue() == 0L)) {352 right.generateCode(currentScope, codeStream, valueRequired);353 } else {354 // x | 0355 if ((right.constant != Constant.NotAConstant)356 && (right.constant.typeID() == T_long)357 && (right.constant.longValue() == 0L)) {358 left.generateCode(currentScope, codeStream, valueRequired);359 } else {360 left.generateCode(currentScope, codeStream, valueRequired);361 right.generateCode(currentScope, codeStream, valueRequired);362 if (valueRequired)363 codeStream.lor();364 }365 }366 break;367 case T_boolean : // logical or368 generateOptimizedLogicalOr(369 currentScope,370 codeStream,371 null,372 (falseLabel = new Label(codeStream)),373 valueRequired);374 /* improving code gen for such a case: boolean b = i < 0 || true;375 * since the label has never been used, we have the inlined value on the stack. */376 if (falseLabel.hasForwardReferences()) {377 if (valueRequired) {378 codeStream.iconst_1();379 if ((bits & ValueForReturnMASK) != 0) {380 codeStream.ireturn();381 falseLabel.place();382 codeStream.iconst_0();383 } else {384 codeStream.goto_(endLabel = new Label(codeStream));385 codeStream.decrStackSize(1);386 falseLabel.place();387 codeStream.iconst_0();388 endLabel.place();389 }390 } else {391 falseLabel.place();392 }393 }394 }395 break;396 case XOR :397 switch (bits & ReturnTypeIDMASK) {398 case T_int :399 // 0 ^ x400 if ((left.constant != Constant.NotAConstant)401 && (left.constant.typeID() == T_int)402 && (left.constant.intValue() == 0)) {403 right.generateCode(currentScope, codeStream, valueRequired);404 } else {405 // x ^ 0406 if ((right.constant != Constant.NotAConstant)407 && (right.constant.typeID() == T_int)408 && (right.constant.intValue() == 0)) {409 left.generateCode(currentScope, codeStream, valueRequired);410 } else {411 left.generateCode(currentScope, codeStream, valueRequired);412 right.generateCode(currentScope, codeStream, valueRequired);413 if (valueRequired)414 codeStream.ixor();415 }416 }417 break;418 case T_long :419 // 0 ^ x420 if ((left.constant != Constant.NotAConstant)421 && (left.constant.typeID() == T_long)422 && (left.constant.longValue() == 0L)) {423 right.generateCode(currentScope, codeStream, valueRequired);424 } else {425 // x ^ 0426 if ((right.constant != Constant.NotAConstant)427 && (right.constant.typeID() == T_long)428 && (right.constant.longValue() == 0L)) {429 left.generateCode(currentScope, codeStream, valueRequired);430 } else {431 left.generateCode(currentScope, codeStream, valueRequired);432 right.generateCode(currentScope, codeStream, valueRequired);433 if (valueRequired)434 codeStream.lxor();435 }436 }437 break;438 case T_boolean :439 generateOptimizedLogicalXor(440 currentScope,441 codeStream,442 null,443 (falseLabel = new Label(codeStream)),444 valueRequired);445 /* improving code gen for such a case: boolean b = i < 0 ^ bool;446 * since the label has never been used, we have the inlined value on the stack. */447 if (falseLabel.hasForwardReferences()) {448 if (valueRequired) {449 codeStream.iconst_1();450 if ((bits & ValueForReturnMASK) != 0) {451 codeStream.ireturn();452 falseLabel.place();453 codeStream.iconst_0();454 } else {455 codeStream.goto_(endLabel = new Label(codeStream));456 codeStream.decrStackSize(1);457 falseLabel.place();458 codeStream.iconst_0();459 endLabel.place();460 }461 } else {462 falseLabel.place();463 }464 }465 }466 break;467 case LEFT_SHIFT :468 switch (bits & ReturnTypeIDMASK) {469 case T_int :470 left.generateCode(currentScope, codeStream, valueRequired);471 right.generateCode(currentScope, codeStream, valueRequired);472 if (valueRequired)473 codeStream.ishl();474 break;475 case T_long :476 left.generateCode(currentScope, codeStream, valueRequired);477 right.generateCode(currentScope, codeStream, valueRequired);478 if (valueRequired)479 codeStream.lshl();480 }481 break;482 case RIGHT_SHIFT :483 switch (bits & ReturnTypeIDMASK) {484 case T_int :485 left.generateCode(currentScope, codeStream, valueRequired);486 right.generateCode(currentScope, codeStream, valueRequired);487 if (valueRequired)488 codeStream.ishr();489 break;490 case T_long :491 left.generateCode(currentScope, codeStream, valueRequired);492 right.generateCode(currentScope, codeStream, valueRequired);493 if (valueRequired)494 codeStream.lshr();495 }496 break;497 case UNSIGNED_RIGHT_SHIFT :498 switch (bits & ReturnTypeIDMASK) {499 case T_int :500 left.generateCode(currentScope, codeStream, valueRequired);501 right.generateCode(currentScope, codeStream, valueRequired);502 if (valueRequired)503 codeStream.iushr();504 break;505 case T_long :506 left.generateCode(currentScope, codeStream, valueRequired);507 right.generateCode(currentScope, codeStream, valueRequired);508 if (valueRequired)509 codeStream.lushr();510 }511 break;512 case GREATER :513 generateOptimizedGreaterThan(514 currentScope,515 codeStream,516 null,517 (falseLabel = new Label(codeStream)),518 valueRequired);519 if (valueRequired) {520 codeStream.iconst_1();521 if ((bits & ValueForReturnMASK) != 0) {522 codeStream.ireturn();523 falseLabel.place();524 codeStream.iconst_0();525 } else {526 codeStream.goto_(endLabel = new Label(codeStream));527 codeStream.decrStackSize(1);528 falseLabel.place();529 codeStream.iconst_0();530 endLabel.place();531 }532 }533 break;534 case GREATER_EQUAL :535 generateOptimizedGreaterThanOrEqual(536 currentScope,537 codeStream,538 null,539 (falseLabel = new Label(codeStream)),540 valueRequired);541 if (valueRequired) {542 codeStream.iconst_1();543 if ((bits & ValueForReturnMASK) != 0) {544 codeStream.ireturn();545 falseLabel.place();546 codeStream.iconst_0();547 } else {548 codeStream.goto_(endLabel = new Label(codeStream));549 codeStream.decrStackSize(1);550 falseLabel.place();551 codeStream.iconst_0();552 endLabel.place();553 }554 }555 break;556 case LESS :557 generateOptimizedLessThan(558 currentScope,559 codeStream,560 null,561 (falseLabel = new Label(codeStream)),562 valueRequired);563 if (valueRequired) {564 codeStream.iconst_1();565 if ((bits & ValueForReturnMASK) != 0) {566 codeStream.ireturn();567 falseLabel.place();568 codeStream.iconst_0();569 } else {570 codeStream.goto_(endLabel = new Label(codeStream));571 codeStream.decrStackSize(1);572 falseLabel.place();573 codeStream.iconst_0();574 endLabel.place();575 }576 }577 break;578 case LESS_EQUAL :579 generateOptimizedLessThanOrEqual(580 currentScope,581 codeStream,582 null,583 (falseLabel = new Label(codeStream)),584 valueRequired);585 if (valueRequired) {586 codeStream.iconst_1();587 if ((bits & ValueForReturnMASK) != 0) {588 codeStream.ireturn();589 falseLabel.place();590 codeStream.iconst_0();591 } else {592 codeStream.goto_(endLabel = new Label(codeStream));593 codeStream.decrStackSize(1);594 falseLabel.place();595 codeStream.iconst_0();596 endLabel.place();597 }598 }599 }600 if (valueRequired) {601 codeStream.generateImplicitConversion(implicitConversion);602 }603 codeStream.recordPositionsFrom(pc, this.sourceStart);604 }605606 /**607 * Boolean operator code generation608 * Optimized operations are: <, <=, >, >=, &, |, ^609 */610 public void generateOptimizedBoolean(611 BlockScope currentScope,612 CodeStream codeStream,613 Label trueLabel,614 Label falseLabel,615 boolean valueRequired) {616617 if ((constant != Constant.NotAConstant) && (constant.typeID() == T_boolean)) {618 super.generateOptimizedBoolean(619 currentScope,620 codeStream,621 trueLabel,622 falseLabel,623 valueRequired);624 return;625 }626 switch ((bits & OperatorMASK) >> OperatorSHIFT) {627 case LESS :628 generateOptimizedLessThan(629 currentScope,630 codeStream,631 trueLabel,632 falseLabel,633 valueRequired);634 return;635 case LESS_EQUAL :636 generateOptimizedLessThanOrEqual(637 currentScope,638 codeStream,639 trueLabel,640 falseLabel,641 valueRequired);642 return;643 case GREATER :644 generateOptimizedGreaterThan(645 currentScope,646 codeStream,647 trueLabel,648 falseLabel,649 valueRequired);650 return;651 case GREATER_EQUAL :652 generateOptimizedGreaterThanOrEqual(653 currentScope,654 codeStream,655 trueLabel,656 falseLabel,657 valueRequired);658 return;659 case AND :660 generateOptimizedLogicalAnd(661 currentScope,662 codeStream,663 trueLabel,664 falseLabel,665 valueRequired);666 return;667 case OR :668 generateOptimizedLogicalOr(669 currentScope,670 codeStream,671 trueLabel,672 falseLabel,673 valueRequired);674 return;675 case XOR :676 generateOptimizedLogicalXor(677 currentScope,678 codeStream,679 trueLabel,680 falseLabel,681 valueRequired);682 return;683 }684 super.generateOptimizedBoolean(685 currentScope,686 codeStream,687 trueLabel,688 falseLabel,689 valueRequired);690 }691692 /**693 * Boolean generation for >694 */695 public void generateOptimizedGreaterThan(696 BlockScope currentScope,697 CodeStream codeStream,698 Label trueLabel,699 Label falseLabel,700 boolean valueRequired) {701702 int promotedTypeID = left.implicitConversion >> 4;703 // both sides got promoted in the same way704 if (promotedTypeID == T_int) {705 // 0 > x706 if ((left.constant != NotAConstant) && (left.constant.intValue() == 0)) {707 right.generateCode(currentScope, codeStream, valueRequired);708 if (valueRequired) {709 if (falseLabel == null) {710 if (trueLabel != null) {711 // implicitly falling through the FALSE case712 codeStream.iflt(trueLabel);713 }714 } else {715 if (trueLabel == null) {716 // implicitly falling through the TRUE case717 codeStream.ifge(falseLabel);718 } else {719 // no implicit fall through TRUE/FALSE --> should never occur720 }721 }722 }723 // reposition the endPC724 codeStream.updateLastRecordedEndPC(codeStream.position); 725 return;726 }727 // x > 0728 if ((right.constant != NotAConstant) && (right.constant.intValue() == 0)) {729 left.generateCode(currentScope, codeStream, valueRequired);730 if (valueRequired) {731 if (falseLabel == null) {732 if (trueLabel != null) {733 // implicitly falling through the FALSE case734 codeStream.ifgt(trueLabel);735 }736 } else {737 if (trueLabel == null) {738 // implicitly falling through the TRUE case739 codeStream.ifle(falseLabel);740 } else {741 // no implicit fall through TRUE/FALSE --> should never occur742 }743 }744 }745 // reposition the endPC746 codeStream.updateLastRecordedEndPC(codeStream.position); 747 return;748 }749 }750 // default comparison751 left.generateCode(currentScope, codeStream, valueRequired);752 right.generateCode(currentScope, codeStream, valueRequired);753 if (valueRequired) {754 if (falseLabel == null) {755 if (trueLabel != null) {756 // implicit falling through the FALSE case757 switch (promotedTypeID) {758 case T_int :759 codeStream.if_icmpgt(trueLabel);760 break;761 case T_float :762 codeStream.fcmpl();763 codeStream.ifgt(trueLabel);764 break;765 case T_long :766 codeStream.lcmp();767 codeStream.ifgt(trueLabel);768 break;769 case T_double :770 codeStream.dcmpl();771 codeStream.ifgt(trueLabel);772 }773 // reposition the endPC774 codeStream.updateLastRecordedEndPC(codeStream.position); 775 return;776 }777 } else {778 if (trueLabel == null) {779 // implicit falling through the TRUE case780 switch (promotedTypeID) {781 case T_int :782 codeStream.if_icmple(falseLabel);783 break;784 case T_float :785 codeStream.fcmpl();786 codeStream.ifle(falseLabel);787 break;788 case T_long :789 codeStream.lcmp();790 codeStream.ifle(falseLabel);791 break;792 case T_double :793 codeStream.dcmpl();794 codeStream.ifle(falseLabel);795 }796 // reposition the endPC797 codeStream.updateLastRecordedEndPC(codeStream.position); 798 return;799 } else {800 // no implicit fall through TRUE/FALSE --> should never occur801 }802 }803 }804 }805806 /**807 * Boolean generation for >=808 */809 public void generateOptimizedGreaterThanOrEqual(810 BlockScope currentScope,811 CodeStream codeStream,812 Label trueLabel,813 Label falseLabel,814 boolean valueRequired) {815816 int promotedTypeID = left.implicitConversion >> 4;817 // both sides got promoted in the same way818 if (promotedTypeID == T_int) {819 // 0 >= x820 if ((left.constant != NotAConstant) && (left.constant.intValue() == 0)) {821 right.generateCode(currentScope, codeStream, valueRequired);822 if (valueRequired) {823 if (falseLabel == null) {824 if (trueLabel != null) {825 // implicitly falling through the FALSE case826 codeStream.ifle(trueLabel);827 }828 } else {829 if (trueLabel == null) {830 // implicitly falling through the TRUE case831 codeStream.ifgt(falseLabel);832 } else {833 // no implicit fall through TRUE/FALSE --> should never occur834 }835 }836 }837 // reposition the endPC838 codeStream.updateLastRecordedEndPC(codeStream.position); 839 return;840 }841 // x >= 0842 if ((right.constant != NotAConstant) && (right.constant.intValue() == 0)) {843 left.generateCode(currentScope, codeStream, valueRequired);844 if (valueRequired) {845 if (falseLabel == null) {846 if (trueLabel != null) {847 // implicitly falling through the FALSE case848 codeStream.ifge(trueLabel);849 }850 } else {851 if (trueLabel == null) {852 // implicitly falling through the TRUE case853 codeStream.iflt(falseLabel);854 } else {855 // no implicit fall through TRUE/FALSE --> should never occur856 }857 }858 }859 // reposition the endPC860 codeStream.updateLastRecordedEndPC(codeStream.position); 861 return;862 }863 }864 // default comparison865 left.generateCode(currentScope, codeStream, valueRequired);866 right.generateCode(currentScope, codeStream, valueRequired);867 if (valueRequired) {868 if (falseLabel == null) {869 if (trueLabel != null) {870 // implicit falling through the FALSE case871 switch (promotedTypeID) {872 case T_int :873 codeStream.if_icmpge(trueLabel);874 break;875 case T_float :876 codeStream.fcmpl();877 codeStream.ifge(trueLabel);878 break;879 case T_long :880 codeStream.lcmp();881 codeStream.ifge(trueLabel);882 break;883 case T_double :884 codeStream.dcmpl();885 codeStream.ifge(trueLabel);886 }887 // reposition the endPC888 codeStream.updateLastRecordedEndPC(codeStream.position); 889 return;890 }891 } else {892 if (trueLabel == null) {893 // implicit falling through the TRUE case894 switch (promotedTypeID) {895 case T_int :896 codeStream.if_icmplt(falseLabel);897 break;898 case T_float :899 codeStream.fcmpl();900 codeStream.iflt(falseLabel);901 break;902 case T_long :903 codeStream.lcmp();904 codeStream.iflt(falseLabel);905 break;906 case T_double :907 codeStream.dcmpl();908 codeStream.iflt(falseLabel);909 }910 // reposition the endPC911 codeStream.updateLastRecordedEndPC(codeStream.position); 912 return;913 } else {914 // no implicit fall through TRUE/FALSE --> should never occur915 }916 }917 }918 }919920 /**921 * Boolean generation for <922 */923 public void generateOptimizedLessThan(924 BlockScope currentScope,925 CodeStream codeStream,926 Label trueLabel,927 Label falseLabel,928 boolean valueRequired) {929930 int promotedTypeID = left.implicitConversion >> 4;931 // both sides got promoted in the same way932 if (promotedTypeID == T_int) {933 // 0 < x934 if ((left.constant != NotAConstant) && (left.constant.intValue() == 0)) {935 right.generateCode(currentScope, codeStream, valueRequired);936 if (valueRequired) {937 if (falseLabel == null) {938 if (trueLabel != null) {939 // implicitly falling through the FALSE case940 codeStream.ifgt(trueLabel);941 }942 } else {943 if (trueLabel == null) {944 // implicitly falling through the TRUE case945 codeStream.ifle(falseLabel);946 } else {947 // no implicit fall through TRUE/FALSE --> should never occur948 }949 }950 }951 codeStream.updateLastRecordedEndPC(codeStream.position);952 return;953 }954 // x < 0955 if ((right.constant != NotAConstant) && (right.constant.intValue() == 0)) {956 left.generateCode(currentScope, codeStream, valueRequired);957 if (valueRequired) {958 if (falseLabel == null) {959 if (trueLabel != null) {960 // implicitly falling through the FALSE case961 codeStream.iflt(trueLabel);962 }963 } else {964 if (trueLabel == null) {965 // implicitly falling through the TRUE case966 codeStream.ifge(falseLabel);967 } else {968 // no implicit fall through TRUE/FALSE --> should never occur969 }970 }971 }972 codeStream.updateLastRecordedEndPC(codeStream.position);973 return;974 }975 }976 // default comparison977 left.generateCode(currentScope, codeStream, valueRequired);978 right.generateCode(currentScope, codeStream, valueRequired);979 if (valueRequired) {980 if (falseLabel == null) {981 if (trueLabel != null) {982 // implicit falling through the FALSE case983 switch (promotedTypeID) {984 case T_int :985 codeStream.if_icmplt(trueLabel);986 break;987 case T_float :988 codeStream.fcmpg();989 codeStream.iflt(trueLabel);990 break;991 case T_long :992 codeStream.lcmp();993 codeStream.iflt(trueLabel);994 break;995 case T_double :996 codeStream.dcmpg();997 codeStream.iflt(trueLabel);998 }999 codeStream.updateLastRecordedEndPC(codeStream.position);1000 return;1001 }1002 } else {1003 if (trueLabel == null) {1004 // implicit falling through the TRUE case1005 switch (promotedTypeID) {1006 case T_int :1007 codeStream.if_icmpge(falseLabel);1008 break;1009 case T_float :1010 codeStream.fcmpg();1011 codeStream.ifge(falseLabel);1012 break;1013 case T_long :1014 codeStream.lcmp();1015 codeStream.ifge(falseLabel);1016 break;1017 case T_double :1018 codeStream.dcmpg();1019 codeStream.ifge(falseLabel);1020 }1021 codeStream.updateLastRecordedEndPC(codeStream.position);1022 return;1023 } else {1024 // no implicit fall through TRUE/FALSE --> should never occur1025 }1026 }1027 }1028 }1029 1030 /**1031 * Boolean generation for <=1032 */1033 public void generateOptimizedLessThanOrEqual(1034 BlockScope currentScope,1035 CodeStream codeStream,1036 Label trueLabel,1037 Label falseLabel,1038 boolean valueRequired) {10391040 int promotedTypeID = left.implicitConversion >> 4;1041 // both sides got promoted in the same way1042 if (promotedTypeID == T_int) {1043 // 0 <= x1044 if ((left.constant != NotAConstant) && (left.constant.intValue() == 0)) {1045 right.generateCode(currentScope, codeStream, valueRequired);1046 if (valueRequired) {1047 if (falseLabel == null) {1048 if (trueLabel != null) {1049 // implicitly falling through the FALSE case1050 codeStream.ifge(trueLabel);1051 }1052 } else {1053 if (trueLabel == null) {1054 // implicitly falling through the TRUE case1055 codeStream.iflt(falseLabel);1056 } else {1057 // no implicit fall through TRUE/FALSE --> should never occur1058 }1059 }1060 }1061 // reposition the endPC1062 codeStream.updateLastRecordedEndPC(codeStream.position); 1063 return;1064 }1065 // x <= 01066 if ((right.constant != NotAConstant) && (right.constant.intValue() == 0)) {1067 left.generateCode(currentScope, codeStream, valueRequired);1068 if (valueRequired) {1069 if (falseLabel == null) {1070 if (trueLabel != null) {1071 // implicitly falling through the FALSE case1072 codeStream.ifle(trueLabel);1073 }1074 } else {1075 if (trueLabel == null) {1076 // implicitly falling through the TRUE case1077 codeStream.ifgt(falseLabel);1078 } else {1079 // no implicit fall through TRUE/FALSE --> should never occur1080 }1081 }1082 }1083 // reposition the endPC1084 codeStream.updateLastRecordedEndPC(codeStream.position); 1085 return;1086 }1087 }1088 // default comparison1089 left.generateCode(currentScope, codeStream, valueRequired);1090 right.generateCode(currentScope, codeStream, valueRequired);1091 if (valueRequired) {1092 if (falseLabel == null) {1093 if (trueLabel != null) {1094 // implicit falling through the FALSE case1095 switch (promotedTypeID) {1096 case T_int :1097 codeStream.if_icmple(trueLabel);1098 break;1099 case T_float :1100 codeStream.fcmpg();1101 codeStream.ifle(trueLabel);1102 break;1103 case T_long :1104 codeStream.lcmp();1105 codeStream.ifle(trueLabel);1106 break;1107 case T_double :1108 codeStream.dcmpg();1109 codeStream.ifle(trueLabel);1110 }1111 // reposition the endPC1112 codeStream.updateLastRecordedEndPC(codeStream.position); 1113 return;1114 }1115 } else {1116 if (trueLabel == null) {1117 // implicit falling through the TRUE case1118 switch (promotedTypeID) {1119 case T_int :1120 codeStream.if_icmpgt(falseLabel);1121 break;1122 case T_float :1123 codeStream.fcmpg();1124 codeStream.ifgt(falseLabel);1125 break;1126 case T_long :1127 codeStream.lcmp();1128 codeStream.ifgt(falseLabel);1129 break;1130 case T_double :1131 codeStream.dcmpg();1132 codeStream.ifgt(falseLabel);1133 }1134 // reposition the endPC1135 codeStream.updateLastRecordedEndPC(codeStream.position); 1136 return;1137 } else {1138 // no implicit fall through TRUE/FALSE --> should never occur1139 }1140 }1141 }1142 }1143 1144 /**1145 * Boolean generation for &1146 */1147 public void generateOptimizedLogicalAnd(1148 BlockScope currentScope,1149 CodeStream codeStream,1150 Label trueLabel,1151 Label falseLabel,1152 boolean valueRequired) {1153 1154 Constant condConst;1155 if ((left.implicitConversion & 0xF) == T_boolean) {1156 if ((condConst = left.optimizedBooleanConstant()) != NotAConstant) {1157 if (condConst.booleanValue() == true) {1158 // <something equivalent to true> & x1159 left.generateOptimizedBoolean(1160 currentScope,1161 codeStream,1162 trueLabel,1163 falseLabel,1164 false);1165 if ((bits & OnlyValueRequiredMASK) != 0) {1166 right.generateCode(currentScope, codeStream, valueRequired);1167 } else {1168 right.generateOptimizedBoolean(1169 currentScope,1170 codeStream,1171 trueLabel,1172 falseLabel,1173 valueRequired);1174 }1175 } else {1176 // <something equivalent to false> & x1177 left.generateOptimizedBoolean(1178 currentScope,1179 codeStream,1180 trueLabel,1181 falseLabel,1182 false);1183 Label internalTrueLabel = new Label(codeStream);1184 right.generateOptimizedBoolean(1185 currentScope,1186 codeStream,1187 trueLabel,1188 falseLabel,1189 false);1190 internalTrueLabel.place();1191 if (valueRequired) {1192 if ((bits & OnlyValueRequiredMASK) != 0) {1193 codeStream.iconst_0();1194 } else {1195 if (falseLabel != null) {1196 // implicit falling through the TRUE case1197 codeStream.goto_(falseLabel);1198 }1199 }1200 }1201 // reposition the endPC1202 codeStream.updateLastRecordedEndPC(codeStream.position); 1203 }1204 return;1205 }1206 if ((condConst = right.optimizedBooleanConstant()) != NotAConstant) {1207 if (condConst.booleanValue() == true) {1208 // x & <something equivalent to true>1209 if ((bits & OnlyValueRequiredMASK) != 0) {1210 left.generateCode(currentScope, codeStream, valueRequired);1211 } else {1212 left.generateOptimizedBoolean(1213 currentScope,1214 codeStream,1215 trueLabel,1216 falseLabel,1217 valueRequired);1218 }1219 right.generateOptimizedBoolean(1220 currentScope,1221 codeStream,1222 trueLabel,1223 falseLabel,1224 false);1225 } else {1226 // x & <something equivalent to false>1227 Label internalTrueLabel = new Label(codeStream);1228 left.generateOptimizedBoolean(1229 currentScope,1230 codeStream,1231 internalTrueLabel,1232 falseLabel,1233 false);1234 internalTrueLabel.place();1235 right.generateOptimizedBoolean(1236 currentScope,1237 codeStream,1238 trueLabel,1239 falseLabel,1240 false);1241 if (valueRequired) {1242 if ((bits & OnlyValueRequiredMASK) != 0) {1243 codeStream.iconst_0();1244 } else {1245 if (falseLabel != null) {1246 // implicit falling through the TRUE case1247 codeStream.goto_(falseLabel);1248 }1249 }1250 }1251 // reposition the endPC1252 codeStream.updateLastRecordedEndPC(codeStream.position); 1253 }1254 return;1255 }1256 }1257 // default case1258 left.generateCode(currentScope, codeStream, valueRequired);1259 right.generateCode(currentScope, codeStream, valueRequired);1260 if (valueRequired) {1261 codeStream.iand();1262 if ((bits & OnlyValueRequiredMASK) == 0) {1263 if (falseLabel == null) {1264 if (trueLabel != null) {1265 // implicit falling through the FALSE case1266 codeStream.ifne(trueLabel);1267 }1268 } else {1269 // implicit falling through the TRUE case1270 if (trueLabel == null) {1271 codeStream.ifeq(falseLabel);1272 } else {1273 // no implicit fall through TRUE/FALSE --> should never occur1274 }1275 }1276 }1277 }1278 // reposition the endPC1279 codeStream.updateLastRecordedEndPC(codeStream.position); 1280 }1281 1282 /**1283 * Boolean generation for |1284 */1285 public void generateOptimizedLogicalOr(1286 BlockScope currentScope,1287 CodeStream codeStream,1288 Label trueLabel,1289 Label falseLabel,1290 boolean valueRequired) {1291 1292 Constant condConst;1293 if ((left.implicitConversion & 0xF) == T_boolean) {1294 if ((condConst = left.optimizedBooleanConstant()) != NotAConstant) {1295 if (condConst.booleanValue() == true) {1296 // <something equivalent to true> | x1297 left.generateOptimizedBoolean(1298 currentScope,1299 codeStream,1300 trueLabel,1301 falseLabel,1302 false);1303 Label internalFalseLabel = new Label(codeStream);1304 right.generateOptimizedBoolean(1305 currentScope,1306 codeStream,1307 trueLabel,1308 internalFalseLabel,1309 false);1310 internalFalseLabel.place();1311 if (valueRequired) {1312 if ((bits & OnlyValueRequiredMASK) != 0) {1313 codeStream.iconst_1();1314 } else {1315 if (trueLabel != null) {1316 codeStream.goto_(trueLabel);1317 }1318 }1319 }1320 // reposition the endPC1321 codeStream.updateLastRecordedEndPC(codeStream.position); 1322 } else {1323 // <something equivalent to false> | x1324 left.generateOptimizedBoolean(1325 currentScope,1326 codeStream,1327 trueLabel,1328 falseLabel,1329 false);1330 if ((bits & OnlyValueRequiredMASK) != 0) {1331 right.generateCode(currentScope, codeStream, valueRequired);1332 } else {1333 right.generateOptimizedBoolean(1334 currentScope,1335 codeStream,1336 trueLabel,1337 falseLabel,1338 valueRequired);1339 }1340 }1341 return;1342 }1343 if ((condConst = right.optimizedBooleanConstant()) != NotAConstant) {1344 if (condConst.booleanValue() == true) {1345 // x | <something equivalent to true>1346 Label internalFalseLabel = new Label(codeStream);1347 left.generateOptimizedBoolean(1348 currentScope,1349 codeStream,1350 trueLabel,1351 internalFalseLabel,1352 false);1353 internalFalseLabel.place();1354 right.generateOptimizedBoolean(1355 currentScope,1356 codeStream,1357 trueLabel,1358 falseLabel,1359 false);1360 if (valueRequired) {1361 if ((bits & OnlyValueRequiredMASK) != 0) {1362 codeStream.iconst_1();1363 } else {1364 if (trueLabel != null) {1365 codeStream.goto_(trueLabel);1366 }1367 }1368 }1369 // reposition the endPC1370 codeStream.updateLastRecordedEndPC(codeStream.position); 1371 } else {1372 // x | <something equivalent to false>1373 if ((bits & OnlyValueRequiredMASK) != 0) {1374 left.generateCode(currentScope, codeStream, valueRequired);1375 } else {1376 left.generateOptimizedBoolean(1377 currentScope,1378 codeStream,1379 trueLabel,1380 falseLabel,1381 valueRequired);1382 }1383 right.generateOptimizedBoolean(1384 currentScope,1385 codeStream,1386 trueLabel,1387 falseLabel,1388 false);1389 }1390 return;1391 }1392 }1393 // default case1394 left.generateCode(currentScope, codeStream, valueRequired);1395 right.generateCode(currentScope, codeStream, valueRequired);1396 if (valueRequired) {1397 codeStream.ior();1398 if ((bits & OnlyValueRequiredMASK) == 0) {1399 if (falseLabel == null) {1400 if (trueLabel != null) {1401 // implicit falling through the FALSE case1402 codeStream.ifne(trueLabel);1403 }1404 } else {1405 // implicit falling through the TRUE case1406 if (trueLabel == null) {1407 codeStream.ifeq(falseLabel);1408 } else {1409 // no implicit fall through TRUE/FALSE --> should never occur1410 }1411 }1412 }1413 }1414 // reposition the endPC1415 codeStream.updateLastRecordedEndPC(codeStream.position); 1416 }1417 1418 /**1419 * Boolean generation for ^1420 */1421 public void generateOptimizedLogicalXor(1422 BlockScope currentScope,1423 CodeStream codeStream,1424 Label trueLabel,1425 Label falseLabel,1426 boolean valueRequired) {1427 1428 Constant condConst;1429 if ((left.implicitConversion & 0xF) == T_boolean) {1430 if ((condConst = left.optimizedBooleanConstant()) != NotAConstant) {1431 if (condConst.booleanValue() == true) {1432 // <something equivalent to true> ^ x1433 left.generateOptimizedBoolean(1434 currentScope,1435 codeStream,1436 trueLabel,1437 falseLabel,1438 false);1439 right.generateOptimizedBoolean(1440 currentScope,1441 codeStream,1442 falseLabel,1443 trueLabel,1444 valueRequired);1445 } else {1446 // <something equivalent to false> ^ x1447 left.generateOptimizedBoolean(1448 currentScope,1449 codeStream,1450 trueLabel,1451 falseLabel,1452 false);1453 if ((bits & OnlyValueRequiredMASK) != 0) {1454 right.generateCode(currentScope, codeStream, valueRequired);1455 } else {1456 right.generateOptimizedBoolean(1457 currentScope,1458 codeStream,1459 trueLabel,1460 falseLabel,1461 valueRequired);1462 }1463 }1464 return;1465 }1466 if ((condConst = right.optimizedBooleanConstant()) != NotAConstant) {1467 if (condConst.booleanValue() == true) {1468 // x ^ <something equivalent to true>1469 left.generateOptimizedBoolean(1470 currentScope,1471 codeStream,1472 falseLabel,1473 trueLabel,1474 valueRequired);1475 right.generateOptimizedBoolean(1476 currentScope,1477 codeStream,1478 trueLabel,1479 falseLabel,1480 false);1481 } else {1482 // x ^ <something equivalent to false>1483 if ((bits & OnlyValueRequiredMASK) != 0) {1484 left.generateCode(currentScope, codeStream, valueRequired);1485 } else {1486 left.generateOptimizedBoolean(1487 currentScope,1488 codeStream,1489 trueLabel,1490 falseLabel,1491 valueRequired);1492 }1493 right.generateOptimizedBoolean(1494 currentScope,1495 codeStream,1496 trueLabel,1497 falseLabel,1498 false);1499 }1500 return;1501 }1502 }1503 // default case1504 left.generateCode(currentScope, codeStream, valueRequired);1505 right.generateCode(currentScope, codeStream, valueRequired);1506 if (valueRequired) {1507 codeStream.ixor();1508 if ((bits & OnlyValueRequiredMASK) == 0) {1509 if (falseLabel == null) {1510 if (trueLabel != null) {1511 // implicit falling through the FALSE case1512 codeStream.ifne(trueLabel);1513 }1514 } else {1515 // implicit falling through the TRUE case1516 if (trueLabel == null) {1517 codeStream.ifeq(falseLabel);1518 } else {1519 // no implicit fall through TRUE/FALSE --> should never occur
...
ParserNode.ts
Source:ParserNode.ts
...1920export class ParserNode {21 type: string = '-';2223 generateCode(context: ParserNodeGenerateCodeContext):string {24 return '<invalid>';25 }2627 iterate(handler: (node: ParserNode) => void ) {28 handler(this);29 }30}3132export class ParserNodeExpression extends ParserNode {33}3435export class ParserNodeWriteExpression extends ParserNodeExpression {36 constructor(public expression: ParserNodeExpression) {37 super();38 }3940 generateCode(context: ParserNodeGenerateCodeContext):string {41 if (!context.doWrite) {42 throw (new Error('A template that extends another one cannot have a body'));43 // return '';44 }45 return 'runtimeContext.writeExpression(' + this.expression.generateCode(context) + ')';46 }4748 iterate(handler: (node: ParserNode) => void ) {49 handler(this);50 this.expression.iterate(handler);51 }52}5354///////////////////////////////////////////////////////////////////////////////55///////////////////////////////////////////////////////////////////////////////5657export class ParserNodeContainer extends ParserNode {58 type: string = 'ParserNodeContainer';5960 constructor(public nodes: ParserNode[]) {61 super();62 if (this.nodes == null) this.nodes = [];63 }6465 add(node: ParserNode) {66 this.nodes.push(node);67 }6869 generateCode(context: ParserNodeGenerateCodeContext):string {70 var output:string = '';71 for (var n in this.nodes) {72 output += this.nodes[n].generateCode(context);73 }74 return output;75 }7677 iterate(handler: (node: ParserNode) => void ) {78 handler(this);79 for (var n in this.nodes) {80 this.nodes[n].iterate(handler);81 }82 }83}8485export class ParserNodeContainerExpression extends ParserNodeExpression {86 type: string = 'ParserNodeContainerExpression';8788 constructor(public nodes: ParserNode[]) {89 super();90 if (this.nodes == null) this.nodes = [];91 }9293 add(node: ParserNode) {94 this.nodes.push(node);95 }9697 generateCode(context: ParserNodeGenerateCodeContext):string {98 var output:string = '';99 for (var n in this.nodes) {100 output += this.nodes[n].generateCode(context);101 }102 return output;103 }104105 iterate(handler: (node: ParserNode) => void ) {106 handler(this);107 for (var n in this.nodes) {108 this.nodes[n].iterate(handler);109 }110 }111}112113///////////////////////////////////////////////////////////////////////////////114///////////////////////////////////////////////////////////////////////////////115116export class ParserNodeObjectItem extends ParserNode {117 type: string = 'ParserNodeObjectItem';118119 constructor(private key: ParserNodeExpression, private value: ParserNodeExpression) {120 super();121 }122123 generateCode(context: ParserNodeGenerateCodeContext) {124 return this.key.generateCode(context) + ' : ' + this.value.generateCode(context);125 }126127 iterate(handler: (node: ParserNode) => void ) {128 handler(this);129 this.key.iterate(handler);130 this.value.iterate(handler);131 }132133}134135export class ParserNodeObjectContainer extends ParserNodeExpression {136 type: string = 'ParserNodeObjectContainer';137138 constructor(private items: ParserNodeObjectItem[]) {139 super();140 }141142 generateCode(context: ParserNodeGenerateCodeContext) {143 return '{' + this.items.map(node => node.generateCode(context)).join(', ') + '}';144 }145146 iterate(handler: (node: ParserNode) => void ) {147 handler(this);148 for (var n in this.items) this.items[n].iterate(handler);149 }150}151152export class ParserNodeArrayContainer extends ParserNodeExpression {153 type: string = 'ParserNodeArrayContainer';154155 constructor(private items: ParserNodeExpression[]) {156 super();157 }158159 generateCode(context: ParserNodeGenerateCodeContext) {160 return '[' + this.items.map(node => node.generateCode(context)).join(', ') + ']';161 }162163 iterate(handler: (node: ParserNode) => void ) {164 handler(this);165 for (var n in this.items) this.items[n].iterate(handler);166 }167}168169///////////////////////////////////////////////////////////////////////////////170///////////////////////////////////////////////////////////////////////////////171172export interface ParseNodeLiteralIdentifier {173 type: string;174 value: any;175 generateCode(context: ParserNodeGenerateCodeContext): any;176}177178export class ParserNodeLiteral extends ParserNodeExpression implements ParseNodeLiteralIdentifier {179 type: string = 'ParserNodeLiteral';180181 constructor(public value: any) {182 super();183 }184185 generateCode(context: ParserNodeGenerateCodeContext) {186 return JSON.stringify(this.value);187 }188189 iterate(handler: (node: ParserNode) => void ) {190 handler(this);191 }192}193194///////////////////////////////////////////////////////////////////////////////195///////////////////////////////////////////////////////////////////////////////196197export class ParserNodeLeftValue extends ParserNodeExpression {198 type: string = 'ParserNodeLeftValue';199200 generateAssign(context: ParserNodeGenerateCodeContext, expr: ParserNodeExpression): string {201 throw (new Error("Must implement"));202 }203}204205export class ParserNodeIdentifier extends ParserNodeLeftValue implements ParseNodeLiteralIdentifier {206 type: string = 'ParserNodeIdentifier';207208 constructor(public value: string) {209 super();210 }211212 generateAssign(context: ParserNodeGenerateCodeContext, expr: ParserNodeExpression) {213 return 'runtimeContext.scopeSet(' + JSON.stringify(this.value) + ', ' + expr.generateCode(context) + ')';214 }215216 generateCode(context: ParserNodeGenerateCodeContext) {217 return 'runtimeContext.scopeGet(' + JSON.stringify(this.value) + ')';218 }219}220221export class ParserNodeStatement extends ParserNode {222 type: string = 'ParserNodeStatement';223}224225export class ParserNodeRaw extends ParserNodeExpression {226 type: string = 'ParserNodeRaw';227228 constructor(public value: string, public putAlways: boolean = true) {229 super();230 }231232 generateCode(context: ParserNodeGenerateCodeContext) {233 if (!context.doWrite && !this.putAlways) return '';234 return this.value;235 }236}237238export class ParserNodeStatementExpression extends ParserNodeStatement {239 type: string = 'ParserNodeStatementExpression';240241 constructor(public expression: ParserNodeExpression) {242 super();243 }244245 generateCode(context: ParserNodeGenerateCodeContext) {246 return this.expression.generateCode(context) + ';';247 }248249 iterate(handler: (node: ParserNode) => void ) {250 handler(this);251 this.expression.iterate(handler);252 }253}254255export class ParserNodeAssignment extends ParserNodeExpression {256 type: string = 'ParserNodeAssignment';257258 constructor(public leftValue: ParserNodeLeftValue, public rightValue: ParserNodeExpression) {259 super();260 }261262 generateCode(context: ParserNodeGenerateCodeContext) {263 return this.leftValue.generateAssign(context, this.rightValue);264 }265266 iterate(handler: (node: ParserNode) => void ) {267 handler(this);268 this.leftValue.iterate(handler);269 this.rightValue.iterate(handler);270 }271}272273///////////////////////////////////////////////////////////////////////////////274///////////////////////////////////////////////////////////////////////////////275276export class ParserNodeCommaExpression extends ParserNode {277 type: string = 'ParserNodeCommaExpression';278279 constructor(public expressions: ParserNodeExpression[], public names: string[] = null) {280 super();281 }282283 generateCode(context: ParserNodeGenerateCodeContext) {284 return this.expressions.map((item) => item.generateCode(context)).join(', ');285 }286287 iterate(handler: (node: ParserNode) => void ) {288 handler(this);289 for (var n in this.expressions) this.expressions[n].iterate(handler);290 }291}292293///////////////////////////////////////////////////////////////////////////////294///////////////////////////////////////////////////////////////////////////////295296export class ParserNodeArrayAccess extends ParserNodeExpression {297 type: string = 'ParserNodeArrayAccess';298299 constructor(public object: ParserNodeExpression, public key: ParserNodeExpression) {300 super();301 }302303 generateCode(context: ParserNodeGenerateCodeContext) {304 return 'runtimeContext.access(' + this.object.generateCode(context) + ', ' + this.key.generateCode(context) + ')';305 }306307 iterate(handler: (node: ParserNode) => void ) {308 handler(this);309 this.object.iterate(handler);310 this.key.iterate(handler);311 }312}313314export class ParserNodeArraySlice extends ParserNodeExpression {315 type: string = 'ParserNodeArraySlice';316317 constructor(public object: ParserNodeExpression, public left: ParserNodeExpression, public right: ParserNodeExpression) {318 super();319 }320321 generateCode(context: ParserNodeGenerateCodeContext) {322 return 'runtimeContext.slice(' + this.object.generateCode(context) + ', ' + this.left.generateCode(context) + ', ' + this.right.generateCode(context) + ')';323 }324325 iterate(handler: (node: ParserNode) => void ) {326 handler(this);327 this.object.iterate(handler);328 this.left.iterate(handler);329 this.right.iterate(handler);330 }331}332333export class ParserNodeFunctionCall extends ParserNodeExpression {334 type: string = 'ParserNodeFunctionCall';335336 constructor(public functionExpr: ParserNodeExpression, public args: ParserNodeCommaExpression) {337 super();338 }339340 generateCode(context: ParserNodeGenerateCodeContext) {341 if (this.functionExpr instanceof ParserNodeArrayAccess) {342 var arrayAccess = <ParserNodeArrayAccess>this.functionExpr;343 return 'runtimeContext.callContext(' + arrayAccess.object.generateCode(context) + ', ' + arrayAccess.key.generateCode(context) + ', [' + this.args.generateCode(context) + '], ' + JSON.stringify(this.args.names) + ')';344 } else {345 return 'runtimeContext.call(' + this.functionExpr.generateCode(context) + ', [' + this.args.generateCode(context) + '], ' + JSON.stringify(this.args.names) + ')';346 }347 }348349 iterate(handler: (node: ParserNode) => void ) {350 handler(this);351 this.functionExpr.iterate(handler);352 this.args.iterate(handler);353 }354}355356export class ParserNodeFilterCall extends ParserNodeExpression {357 type: string = 'ParserNodeFilterCall';358359 constructor(public filterName: string, public args: ParserNodeCommaExpression) {360 super();361 }362363 generateCode(context: ParserNodeGenerateCodeContext) {364 return 'runtimeContext.filter(' + JSON.stringify(this.filterName) + ', [' + this.args.generateCode(context) + '])';365 }366367 iterate(handler: (node: ParserNode) => void ) {368 handler(this);369 this.args.iterate(handler);370 }371}372373///////////////////////////////////////////////////////////////////////////////374///////////////////////////////////////////////////////////////////////////////375376export class ParserNodeUnaryOperation extends ParserNodeExpression {377 type: string = 'ParserNodeUnaryOperation';378379 constructor(public operator: string, public right: ParserNodeExpression) {380 super();381 }382383 generateCode(context: ParserNodeGenerateCodeContext) {384 switch (this.operator) {385 case 'not':386 return '!(' + this.right.generateCode(context) + ')';387 default:388 return this.operator + '(' + this.right.generateCode(context) + ')';389 }390 }391392 iterate(handler: (node: ParserNode) => void ) {393 handler(this);394 this.right.iterate(handler);395 }396}397398///////////////////////////////////////////////////////////////////////////////399///////////////////////////////////////////////////////////////////////////////400401export class ParserNodeBinaryOperation extends ParserNodeExpression {402 type: string = 'ParserNodeBinaryOperation';403404 constructor(public operator: string, public left: ParserNodeExpression, public right: ParserNodeExpression) {405 super();406 }407408 iterate(handler: (node: ParserNode) => void ) {409 handler(this);410 this.left.iterate(handler);411 this.right.iterate(handler);412 }413414 generateCode(context: ParserNodeGenerateCodeContext) {415 switch (this.operator) {416 case 'b-or': return '("" + ' + this.left.generateCode(context) + ' | ' + this.right.generateCode(context) + ')';417 case 'b-and': return '("" + ' + this.left.generateCode(context) + ' & ' + this.right.generateCode(context) + ')';418 case 'b-xor': return '("" + ' + this.left.generateCode(context) + ' ^ ' + this.right.generateCode(context) + ')';419 case '~': return '("" + ' + this.left.generateCode(context) + ' + ' + this.right.generateCode(context) + ')';420 case '..': return 'runtimeContext.range(' + this.left.generateCode(context) + ', ' + this.right.generateCode(context) + ')';421 case '?:': return 'runtimeContext.ternaryShortcut(' + this.left.generateCode(context) + ', ' + this.right.generateCode(context) + ')';422 case '//': return 'Math.floor(' + this.left.generateCode(context) + ' / ' + this.right.generateCode(context) + ')';423 case '**': return 'Math.pow(' + this.left.generateCode(context) + ',' + this.right.generateCode(context) + ')';424 case 'not in':425 case 'in':426 var ret = 'runtimeContext.inArray(' + this.left.generateCode(context) + ',' + this.right.generateCode(context) + ')';427 if ((this.operator == 'not in')) ret = '!(' + ret + ')';428429 return ret;430 case 'is':431 case 'is not':432 var ret = '';433 var left:ParserNodeExpression = this.left;434 var right:ParserNodeExpression = this.right;435436 if (this.right instanceof ParserNodeUnaryOperation) {437 right = (<ParserNodeUnaryOperation>this.right).right;438 }439440 if (right instanceof ParserNodeFunctionCall) {441 //throw (new Error("Not implemented ParserNodeFunctionCall"));442 ret = 'runtimeContext.test(' + (<ParserNodeFunctionCall>right).functionExpr.generateCode(context) + ', [' + left.generateCode(context) + ',' + (<ParserNodeFunctionCall>right).args.generateCode(context) + '])';443 } else if (right instanceof ParserNodeIdentifier) {444 ret = 'runtimeContext.test(' + JSON.stringify((<ParserNodeIdentifier>right).value) + ', [' + left.generateCode(context) + '])';445 } else if (right instanceof ParserNodeLiteral && (<ParserNodeLiteral>right).value === null) {446 ret = 'runtimeContext.test("null", [' + left.generateCode(context) + '])';447 } else {448 throw (new Error("ParserNodeBinaryOperation: Not implemented 'is' operator for tests with " + JSON.stringify(right)));449 }450451 if (this.operator == 'is not') ret = '!(' + ret + ')';452453 return ret;454 default:455 return (456 '(' +457 this.left.generateCode(context) +458 ' ' + this.operator + ' ' +459 this.right.generateCode(context) +460 ')'461 );462 }463 }464}465466///////////////////////////////////////////////////////////////////////////////467///////////////////////////////////////////////////////////////////////////////468469export class ParserNodeTernaryOperation extends ParserNodeExpression {470 type: string = 'ParserNodeTernaryOperation';471472 constructor(public cond: ParserNode, public exprTrue: ParserNode, public exprFalse: ParserNode) {473 super();474 }475476 iterate(handler: (node: ParserNode) => void ) {477 handler(this);478 this.cond.iterate(handler);479 this.exprTrue.iterate(handler);480 this.exprFalse.iterate(handler);481 }482483 generateCode(context: ParserNodeGenerateCodeContext) {484 return (485 '(' +486 this.cond.generateCode(context) +487 " ? " + this.exprTrue.generateCode(context) +488 " : " + this.exprFalse.generateCode(context) +489 ')'490 );491 }492}493494///////////////////////////////////////////////////////////////////////////////495///////////////////////////////////////////////////////////////////////////////496497export class ParserNodeOutputText extends ParserNode {498 text: string = '';499 type: string = 'ParserNodeOutputText';500501 constructor(text: string) {502 super();503 this.text = String(text);504 }505506 generateCode(context: ParserNodeGenerateCodeContext) {507 if (!context.doWrite) {508 if (this.text.match(/\S/)) throw (new Error('A template that extends another one cannot have a body'));509 return '';510 }511 return 'runtimeContext.write(' + JSON.stringify(this.text) + ');';512 }513}514515export class ParserNodeOutputNodeExpression extends ParserNodeExpression {516 type: string = 'ParserNodeOutputNodeExpression';517518 constructor(public expression: ParserNode) {519 super();520 }521522 iterate(handler: (node: ParserNode) => void ) {523 handler(this);524 this.expression.iterate(handler);525 }526527 generateCode(context: ParserNodeGenerateCodeContext) {528 if (!context.doWrite) return '';529 return 'runtimeContext.write(' + this.expression.generateCode(context) + ')';530 }531}532533export class ParserNodeReturnStatement extends ParserNodeStatement {534 type: string = 'ParserNodeReturnStatement';535536 constructor(public expression: ParserNodeExpression) {537 super();538 }539540 iterate(handler: (node: ParserNode) => void ) {541 handler(this);542 this.expression.iterate(handler);543 }544545 generateCode(context: ParserNodeGenerateCodeContext) {546 return 'return ' + this.expression.generateCode(context) + ';';547 }548}549550///////////////////////////////////////////////////////////////////////////////
...
generateCode.test.ts
Source:generateCode.test.ts
...7 kind: "num",8 val: 13,9 },10 ];11 const code = generateCode(nodes);12 expect(code).toEqual("+++++++++++++");13 });14 it("input", () => {15 const nodes: AstNode[] = [16 {17 kind: "assign",18 lhs: {19 kind: "var",20 index: 0,21 },22 rhs: {23 kind: "add",24 lhs: {25 kind: "input",26 },27 rhs: {28 kind: "num",29 val: 2,30 },31 },32 },33 ];34 const code = generateCode(nodes);35 expect(code).toEqual(">,>++[<+>-]<<[-]>[<+>>+<-]");36 });37 it("putchar", () => {38 const nodes: AstNode[] = [39 {40 kind: "putchar",41 arg: {42 kind: "num",43 val: 5,44 },45 },46 ];47 const code = generateCode(nodes);48 expect(code).toEqual("+++++.[-]");49 });50 it("print", () => {51 const nodes: AstNode[] = [52 {53 kind: "print",54 arg: {55 kind: "add",56 lhs: {57 kind: "num",58 val: 2,59 },60 rhs: {61 kind: "num",62 val: 3,63 },64 },65 },66 ];67 const code = generateCode(nodes);68 expect(code).toEqual(69 "++>+++[<+>-]++++++++++<[->->+<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[<<[<+>-]>>>+<-]<<<<]>>[>>>>>+<<<<<-]>>>[<<<<<+>>>>>-]<<<<[-]++++++++++<[->->+<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[<<[<+>-]>>>+<-]<<<<]>>[>>>>+<<<<-]>>>[++++++++++++++++++++++++++++++++++++++++++++++++.<+>>+<[-]]>[<<[>>-<<-]>>++++++++++++++++++++++++++++++++++++++++++++++++.[-]]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<[-]",70 );71 });72 it("add", () => {73 const nodes: AstNode[] = [74 {75 kind: "add",76 lhs: {77 kind: "num",78 val: 3,79 },80 rhs: {81 kind: "num",82 val: 4,83 },84 },85 ];86 const code = generateCode(nodes);87 expect(code).toEqual("+++>++++[<+>-]");88 });89 it("sub", () => {90 const nodes: AstNode[] = [91 {92 kind: "sub",93 lhs: {94 kind: "num",95 val: 5,96 },97 rhs: {98 kind: "num",99 val: 2,100 },101 },102 ];103 const code = generateCode(nodes);104 expect(code).toEqual("+++++>++[<->-]");105 });106 it("mul", () => {107 const nodes: AstNode[] = [108 {109 kind: "mul",110 lhs: {111 kind: "num",112 val: 4,113 },114 rhs: {115 kind: "num",116 val: 3,117 },118 },119 ];120 const code = generateCode(nodes);121 expect(code).toEqual("++++>+++<[>[>+>+<<-]>>[<<+>>-]<<<-]>[-]>[<<+>>-]");122 });123 it("div", () => {124 const nodes: AstNode[] = [125 {126 kind: "div",127 lhs: {128 kind: "num",129 val: 6,130 },131 rhs: {132 kind: "num",133 val: 2,134 },135 },136 ];137 const code = generateCode(nodes);138 expect(code).toEqual(139 "++++++>++<[>>+<<-]>>[<[>>+>+<<<-]>>>[<<<+>>>-]<[>+<<-[>>[-]>+<<<-]>>>[<<<+>>>-]<[<-[<<<->>>[-]]+>-]<-]<<<+>>]<[-]",140 );141 });142 it("mod", () => {143 const nodes: AstNode[] = [144 {145 kind: "mod",146 lhs: {147 kind: "num",148 val: 5,149 },150 rhs: {151 kind: "num",152 val: 2,153 },154 },155 ];156 const code = generateCode(nodes);157 expect(code).toEqual(158 "+++++>++<[>->+<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[<<[<+>-]>>-]<<<<-]>[-]",159 );160 });161 it("exp", () => {162 const nodes: AstNode[] = [163 {164 kind: "exp",165 lhs: {166 kind: "num",167 val: 2,168 },169 rhs: {170 kind: "exp",171 lhs: {172 kind: "num",173 val: 1,174 },175 rhs: {176 kind: "num",177 val: 2,178 },179 },180 },181 ];182 const code = generateCode(nodes);183 expect(code).toEqual(184 "++>+>++<[>>+<<-]+>[>>[-]>[-]<<<<[>>>>+<<<<-]>>>>[<<[<<+>>>+<-]>[<+>-]>-]<<<-]>[-]<<<[>>+<<-]+>[>>[-]>[-]<<<<[>>>>+<<<<-]>>>>[<<[<<+>>>+<-]>[<+>-]>-]<<<-]>[-]",185 );186 });187 it("not", () => {188 const nodes: AstNode[] = [189 {190 kind: "not",191 operand: {192 kind: "num",193 val: 2,194 },195 },196 ];197 const code = generateCode(nodes);198 expect(code).toEqual("++>+<[>-<[-]]");199 });200 it("pre-inc", () => {201 const nodes: AstNode[] = [202 {203 kind: "assign",204 lhs: {205 kind: "var",206 index: 0,207 },208 rhs: {209 kind: "num",210 val: 1,211 },212 },213 {214 kind: "pre-inc",215 operand: {216 kind: "var",217 index: 0,218 },219 },220 ];221 const code = generateCode(nodes);222 expect(code).toEqual(">+<[-]>[<+>>+<-]>[-]<<+[>>+<+<-]>[<+>-]");223 });224 it("pre-dec", () => {225 const nodes: AstNode[] = [226 {227 kind: "assign",228 lhs: {229 kind: "var",230 index: 0,231 },232 rhs: {233 kind: "num",234 val: 1,235 },236 },237 {238 kind: "pre-dec",239 operand: {240 kind: "var",241 index: 0,242 },243 },244 ];245 const code = generateCode(nodes);246 expect(code).toEqual(">+<[-]>[<+>>+<-]>[-]<<-[>>+<+<-]>[<+>-]");247 });248 it("post-inc", () => {249 const nodes: AstNode[] = [250 {251 kind: "assign",252 lhs: {253 kind: "var",254 index: 0,255 },256 rhs: {257 kind: "num",258 val: 1,259 },260 },261 {262 kind: "post-inc",263 operand: {264 kind: "var",265 index: 0,266 },267 },268 ];269 const code = generateCode(nodes);270 expect(code).toEqual(">+<[-]>[<+>>+<-]>[-]<<[>>+<+<-]>[<+>-]<+");271 });272 it("post-dec", () => {273 const nodes: AstNode[] = [274 {275 kind: "assign",276 lhs: {277 kind: "var",278 index: 0,279 },280 rhs: {281 kind: "num",282 val: 1,283 },284 },285 {286 kind: "post-dec",287 operand: {288 kind: "var",289 index: 0,290 },291 },292 ];293 const code = generateCode(nodes);294 expect(code).toEqual(">+<[-]>[<+>>+<-]>[-]<<[>>+<+<-]>[<+>-]<-");295 });296 it("equ", () => {297 const nodes: AstNode[] = [298 {299 kind: "equ",300 lhs: {301 kind: "num",302 val: 1,303 },304 rhs: {305 kind: "num",306 val: 3,307 },308 },309 ];310 const code = generateCode(nodes);311 expect(code).toEqual("+>+++<[>-<-]+>[<->[-]]");312 });313 it("neq", () => {314 const nodes: AstNode[] = [315 {316 kind: "neq",317 lhs: {318 kind: "num",319 val: 1,320 },321 rhs: {322 kind: "num",323 val: 3,324 },325 },326 ];327 const code = generateCode(nodes);328 expect(code).toEqual("+>+++<[>-<-]>[<+>[-]]");329 });330 it("lss", () => {331 const nodes: AstNode[] = [332 {333 kind: "lss",334 lhs: {335 kind: "num",336 val: 1,337 },338 rhs: {339 kind: "num",340 val: 3,341 },342 },343 ];344 const code = generateCode(nodes);345 expect(code).toEqual(346 "+>+++<[->[>+>+<<-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>[<+>[-]]",347 );348 });349 it("leq", () => {350 const nodes: AstNode[] = [351 {352 kind: "leq",353 lhs: {354 kind: "num",355 val: 1,356 },357 rhs: {358 kind: "num",359 val: 3,360 },361 },362 ];363 const code = generateCode(nodes);364 expect(code).toEqual(365 "+>+++[-<[>>+>+<<<-]>>>[<<<+>>>-]+<[<<->>>-<[-]]>[<<[-]>>-]<<]+<[>-<[-]]",366 );367 });368 it("and", () => {369 const nodes: AstNode[] = [370 {371 kind: "and",372 lhs: {373 kind: "num",374 val: 1,375 },376 rhs: {377 kind: "num",378 val: 2,379 },380 },381 ];382 const code = generateCode(nodes);383 expect(code).toEqual("+>++<[>>+<<[-]]>[>+<[-]]++>[<->-]+<[>-<[-]]");384 });385 it("or", () => {386 const nodes: AstNode[] = [387 {388 kind: "or",389 lhs: {390 kind: "num",391 val: 1,392 },393 rhs: {394 kind: "num",395 val: 2,396 },397 },398 ];399 const code = generateCode(nodes);400 expect(code).toEqual("+>++<[>>+<<[-]]>[>+<[-]]>[<+>[-]]");401 });402 it("assign", () => {403 const nodes: AstNode[] = [404 {405 kind: "assign",406 lhs: {407 kind: "var",408 index: 0,409 },410 rhs: {411 kind: "num",412 val: 3,413 },414 },415 ];416 const code = generateCode(nodes);417 expect(code).toEqual(">+++<[-]>[<+>>+<-]");418 });419 it("var", () => {420 const nodes: AstNode[] = [421 {422 kind: "assign",423 lhs: {424 kind: "var",425 index: 0,426 },427 rhs: {428 kind: "num",429 val: 4,430 },431 },432 {433 kind: "var",434 index: 0,435 },436 ];437 const code = generateCode(nodes);438 expect(code).toEqual(">++++<[-]>[<+>>+<-]>[-]<<[>>+<+<-]>[<+>-]");439 });440 it("if", () => {441 const nodes: AstNode[] = [442 {443 kind: "if",444 cond: {445 kind: "add",446 lhs: {447 kind: "num",448 val: 1,449 },450 rhs: {451 kind: "num",452 val: 2,453 },454 },455 consequence: {456 kind: "block",457 stmts: [458 {459 kind: "putchar",460 arg: {461 kind: "num",462 val: 1,463 },464 },465 ],466 },467 },468 ];469 const code = generateCode(nodes);470 expect(code).toEqual("+>++[<+>-]<[>+.[-]<[-]]");471 });472 it("if-else", () => {473 const nodes: AstNode[] = [474 {475 kind: "if",476 cond: {477 kind: "sub",478 lhs: {479 kind: "num",480 val: 1,481 },482 rhs: {483 kind: "num",484 val: 1,485 },486 },487 consequence: {488 kind: "block",489 stmts: [490 {491 kind: "putchar",492 arg: {493 kind: "num",494 val: 1,495 },496 },497 ],498 },499 alternative: {500 kind: "block",501 stmts: [502 {503 kind: "putchar",504 arg: {505 kind: "num",506 val: 2,507 },508 },509 ],510 },511 },512 ];513 const code = generateCode(nodes);514 expect(code).toEqual("+>+[<->-]+<[>>>+.[-]<<-<[-]]>[<++.[-]>-]");515 });516 it("for", () => {517 const nodes: AstNode[] = [518 {519 kind: "for",520 init: {521 kind: "assign",522 lhs: {523 kind: "var",524 index: 0,525 },526 rhs: {527 kind: "num",528 val: 3,529 },530 },531 cond: {532 kind: "var",533 index: 0,534 },535 after: {536 kind: "assign",537 lhs: {538 kind: "var",539 index: 0,540 },541 rhs: {542 kind: "sub",543 lhs: {544 kind: "var",545 index: 0,546 },547 rhs: {548 kind: "num",549 val: 1,550 },551 },552 },553 body: {554 kind: "block",555 stmts: [556 {557 kind: "putchar",558 arg: {559 kind: "var",560 index: 0,561 },562 },563 ],564 },565 },566 ];567 /**568 * >+++ [0, 3] // make 3569 * <[-] [0, 3] // reset 'x'570 * >[<+>>+<-] [3, 0, 3] // assign 3 to 'x'571 * >[-] [3, 0, 0] // free572 * <<[>>+<+<-] [0, 3, 3]573 * >[<+>-] [3, 0, 3] // compute cond574 * >[575 * <<[>+>>+<<<-]576 * >>>[<<<+>>>-] [3, 3, 3, 0] // copy 'x'577 * <<.[-] [3, 0, 3] // free578 * <[>+>>+<<<-] [0, 3, 3, 3]579 * >>>[<<<+>>>-] [3, 3, 3, 0] // copy 'x'580 * + [3, 3, 3, 1] // make 1581 * [<<->>-] [3, 2, 3, 0] // x = x - 1582 * <<<[-] [0, 2, 3, 0] // reset x583 * >[<+>>>+<<-] [2, 0, 3, 2]584 * >>[-] [2, 0, 3, 0] // assign 2 to 'x'585 * <[-] [2, 0, 0, 0] // reset cond586 * <<[>>>+<<+<-] [0, 2, 0, 2]587 * >[<+>-] [2, 0, 0, 2]588 * >>[<+>-] [2, 0, 2, 0] // compute cond589 * <590 * ][-]591 */592 const code = generateCode(nodes);593 expect(code).toEqual(594 ">+++<[-]>[<+>>+<-]>[-]<<[>>+<+<-]>[<+>-]>[<<[>+>>+<<<-]>>>[<<<+>>>-]<<.[-]<[>+>>+<<<-]>>>[<<<+>>>-]+[<<->>-]<<<[-]>[<+>>>+<<-]>>[-]<[-]<<[>>>+<<+<-]>[<+>-]>>[<+>-]<]",595 );596 });597 it("while", () => {598 const nodes: AstNode[] = [599 {600 kind: "while",601 cond: {602 kind: "lss",603 lhs: {604 kind: "pre-inc",605 operand: {606 kind: "var",607 index: 0,608 },609 },610 rhs: {611 kind: "num",612 val: 3,613 },614 },615 body: {616 kind: "putchar",617 arg: {618 kind: "add",619 lhs: {620 kind: "num",621 val: 48,622 },623 rhs: {624 kind: "var",625 index: 0,626 },627 },628 },629 },630 ];631 const code = generateCode(nodes);632 expect(code).toEqual(633 "+[>+>+<<-]>>[<<+>>-]+++<[->[>+>+<<-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>[<+>[-]]<[>>++++++[<++++++++>-]<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[<+>-]<.[-]<[-]<+[>>+>+<<<-]>>>[<<<+>>>-]+++<[->[>+>+<<-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>[<+>[-]]<[<+>-]<]",634 );635 });636 it("block", () => {637 const nodes: AstNode[] = [638 {639 kind: "assign",640 lhs: {641 kind: "var",642 index: 0,643 },644 rhs: {645 kind: "block",646 stmts: [647 {648 kind: "assign",649 lhs: {650 kind: "var",651 index: 1,652 },653 rhs: {654 kind: "num",655 val: 1,656 },657 },658 {659 kind: "assign",660 lhs: {661 kind: "var",662 index: 2,663 },664 rhs: {665 kind: "num",666 val: 2,667 },668 },669 ],670 },671 },672 ];673 const code = generateCode(nodes);674 expect(code).toEqual(675 ">>>+<<[-]>>[<<+>>>+<-]>[-]++<<[-]>>[<<+>+>-]<<<<[-]>>>[<<<+>>>>+<-]",676 );677 });...
Using AI Code Generation
1import { generateCode } from 'storybook-root';2const code = generateCode();3console.log(code);4const { generateCode } = require('storybook-root');5const code = generateCode();6console.log(code);
Using AI Code Generation
1var storybook_root = require('storybook-root');2var code = storybook_root.generateCode('test');3console.log(code);4var storybook_root = require('storybook-root');5var code = storybook_root.generateCode('test');6console.log(code);7The generateCode() method takes
Using AI Code Generation
1import { generateCode } from 'storybook-root-provider';2const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });3console.log(code);4import { generateCode } from 'storybook-root-provider';5const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });6console.log(code);7import { generateCode } from 'storybook-root-provider';8const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });9console.log(code);10import { generateCode } from 'storybook-root-provider';11const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });12console.log(code);13import { generateCode } from 'storybook-root-provider';14const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });15console.log(code);16import { generateCode } from 'storybook-root-provider';17const code = generateCode('myComponent', { prop1: 'value1', prop2: 'value2' });18console.log(code);19import { generateCode } from 'storybook-root-provider';20const code = generateCode('myComponent', { prop1:
Using AI Code Generation
1var storybookRoot = require("storybook-root");2var code = storybookRoot.generateCode();3var fs = require("fs");4fs.writeFile("storybook.js", code, function(err) {5 if(err) {6 return console.log(err);7 }8 console.log("The file was saved!");9});10var storybookRoot = require("storybook-root");11storybookRoot.storybook({12 {13 story: function() {14 console.log("Story 1");15 }16 },17 {18 story: function() {19 console.log("Story 2");20 }21 }22});23{24 "scripts": {25 },26 "dependencies": {27 }28}29var storybookRoot = require("storybook-root");30var code = storybookRoot.generateCode();31var fs = require("fs");32fs.writeFile("storybook.js", code, function(err) {33 if(err) {34 return console.log(err);35 }36 console.log("The file was saved!");37});38var storybookRoot = require("storybook-root");39storybookRoot.storybook({40 {41 story: function() {42 console.log("Story 1");43 }44 },45 {46 story: function() {47 console.log("Story 2");48 }49 }50});51{
Using AI Code Generation
1const generateCode = require('storybook-root').generateCode;2const fs = require('fs');3const path = require('path');4const component = require('./component.js');5const code = generateCode(component);6fs.writeFileSync(path.resolve(__dirname, 'generated-code.js'), code);7import React from 'react';8import { storiesOf } from '@storybook/react';9import { action } from '@storybook/addon-actions';10import Component from './component.js';11storiesOf('Component', module)12 .add('default', () => (13 ));14import React from 'react';15export default class Component extends React.Component {16 render() {17 return (18 );19 }20}21import { configure } from '@storybook/react';22const req = require.context('../', true, /\.stories\.js$/);23function loadStories() {24 req.keys().forEach(filename => req(filename));25}26require('../generated-code.js');27configure(loadStories, module);28import React from 'react';29import { storiesOf } from '@storybook/react';30import { action } from '@storybook/addon-actions';31import Component from './component.js';32storiesOf('Component', module)33 .add('default', () => (34 ));35const generateCode = require('storybook-root').generateCode;36const fs = require('fs');37const path = require('path');38const component = require('./component.js
Using AI Code Generation
1import { generateCode } from "storybook-root-provider";2const code = generateCode({ name: "my-story", props: { width: 300, height: 300 }, children: "Hello world" });3import { storiesOf } from "@storybook/react";4import { withKnobs } from "@storybook/addon-knobs";5import { generateCode } from "storybook-root-provider";6storiesOf("my-story", module)7 .addDecorator(withKnobs)8 .add("default", () => {9 const code = generateCode({ name: "my-story", props: { width: 300, height: 300 }, children: "Hello world" });10 return (11 <my-story width={300} height={300}>Hello world</my-story>12 <pre>{code}</pre>13 );14 });15import { addDecorator } from "@storybook/react";16import { withKnobs } from "@storybook/addon-knobs";17import { withRootProvider } from "storybook-root-provider";18addDecorator(withKnobs);19addDecorator(withRootProvider);20import { addDecorator } from "@storybook/react";21import { withKnobs } from "@storybook/addon-knobs";22import { withRootProvider } from "storybook-root-provider";23addDecorator(withKnobs);24addDecorator(withRootProvider);25import { addDecorator } from "@storybook/react";26import { withKnobs } from "@storybook/addon-knobs";27import { withRootProvider } from "storybook-root-provider";28addDecorator(withKnobs);29addDecorator(withRootProvider);30import { addDecorator } from "@storybook/react";31import { withKnobs } from "@storybook/addon-knobs";32import { withRootProvider } from "storybook-root-provider";33addDecorator(withKnobs
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!