Changeset 846


Ignore:
Timestamp:
01/06/19 02:44:47 (5 years ago)
Author:
Maciej Komosinski
Message:
  • Added support for ternary conditional operator a?b:c
Fixed incorrect stack allocation in some cases of &&
, and for(x in null)
&& and
operators always return one of the input expressions (previously the result was in some cases simplified to 0 or 1)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/vm/framscript.y

    r836 r846  
    479479                           }
    480480                   }
     481           $$.stack=trstack.currentPos();
    481482           }
    482483           expr_or_objname ')'
     
    506507           trctx.out->printf(":_loop_end_%d\n",$1.counter);
    507508           trstack.loops.drop();
    508            if ($1.stack != trstack.currentPos())
    509                    trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos());
    510            trstack.adjust($1.stack-trstack.currentPos());
     509           if ($4.constant)
     510                   trstack.adjust($3.stack-trstack.currentPos());
     511           int adj=$1.stack-trstack.currentPos();
     512           trstack.adjust(adj);
     513           if (adj!=0)
     514                   trctx.out->printf("add %d,m0\n",adj);
    511515           }
    512516           
     
    769773 //     pop; goto and_b
    770774 //   else
    771  //     pop; push 0; goto and_end
     775 //     goto and_end
    772776 // and_b:
    773777 //   push b
     
    776780// trctx.out->printf("\n####### logic AND\n");
    777781 int c=trctx.labelcounter++;
    778  $$.setInt(c);
     782 $$.counter=c;
    779783 if ($1.constant)
    780784         {
     
    783787                 {
    784788                 $1.counter=0;
    785                  // no stack adjust - next tokens are processed in a different context
    786                  trctx.out->printf("push 0\njump :_and_end_%d\n",c);
     789                 trctx.out->printf("jump :_and_end_%d\n",c);
    787790                 }
    788791         else
     
    791794 else
    792795         {
    793          trstack.adjust(+1); // stack as if (a==true), b expr is processed
    794          trctx.out->printf("if !~,m[m0++],:_and_b_%d\n"
    795                            "push 0\n"
     796         trctx.out->printf("if !~,m[m0],:_and_b_%d\n" // skip to b if a!~=false, "a" stays on top
    796797                           "jump :_and_end_%d\n"
    797798                           ":_and_b_%d\n"
     
    801802         expr
    802803 {
    803  $$.ident=false;
    804  $$.constant=0;
    805  if ($4.constant)
     804 if ($1.constant)
    806805         {
    807          if (!($1.constant && $1.counter==0))
     806         if ($1.counter==0)
    808807                 {
    809                  ExtValue::CompareResult  cond=$4.compare(ExtValue::zero());
    810                  bool value=!resultIsRelaxedEqual(cond);
    811                  trstack.adjust(-1);
    812                  trctx.out->printf("push %d\n",value);
     808                 $$=$1;
     809                 if (!$4.constant)
     810                         trstack.adjust(+1);
     811                 }
     812         else
     813                 $$=$4;
     814         }
     815 else
     816         {
     817         $$.ident=false;
     818         $$.constant=0;
     819         if ($4.constant)
     820                 {
     821                 trctx.out->printf("inc m0\n"
     822                                   "push %s\n",litstr($4));
     823                 }
     824         else
     825                 {
     826                 trstack.adjust(+1);
     827                 trctx.out->printf("move m[m0],m[m0+1]\n"
     828                                   "inc m0\n"); //drop "a"
    813829                 }
    814830         }
    815  trctx.out->printf(":_and_end_%d\n",$3.getInt());
     831 trctx.out->printf(":_and_end_%d\n",$3.counter);
    816832// trctx.out->printf("#################\n\n");
    817833 }
     
    822838 //   push a
    823839 //   if (!a)
    824  //     pop; goto and_b
     840 //     pop; goto or_b
    825841 //   else
    826  //     pop; push 1; goto and_end
    827  // and_b:
     842 //     goto or_end
     843 // or_b:
    828844 //   push b
    829  // and_end:
     845 // or_end:
    830846 trctx.emitLine();
    831 // trctx.out->printf("\n####### logic AND\n");
     847// trctx.out->printf("\n####### logic OR\n");
    832848 int c=trctx.labelcounter++;
    833  $$.setInt(c);
     849 $$.counter=c;
    834850 if ($1.constant)
    835851         {
     
    838854                 {
    839855                 $1.counter=1;
    840                  // no stack adjust - next tokens are processed in a different context
    841                  trctx.out->printf("push 1\njump :_or_end_%d\n",c);
     856                 trctx.out->printf("jump :_or_end_%d\n",c);
    842857                 }
    843858         else
     
    846861 else
    847862         {
    848          trstack.adjust(+1); // stack for (a==false)
    849          trctx.out->printf("if ~=,m[m0++],:_or_b_%d\n"
    850                            "push 1\n"
     863         trctx.out->printf("if ~=,m[m0],:_or_b_%d\n" // skip to b if a~=false, "a" stays on top
    851864                           "jump :_or_end_%d\n"
    852865                           ":_or_b_%d\n"
     
    856869         expr
    857870 {
    858  $$.ident=false;
    859  $$.constant=0;
    860  if ($4.constant)
     871 if ($1.constant)
    861872         {
    862          if (!($1.constant && $1.counter==1))
     873         if ($1.counter==1)
    863874                 {
    864                  ExtValue::CompareResult cond=$4.compare(ExtValue::zero());
    865                  bool value=!resultIsRelaxedEqual(cond);
    866                  trstack.adjust(-1);
    867                  trctx.out->printf("push %d\n",value);
     875                 $$=$1;
     876                 if (!$4.constant)
     877                         trstack.adjust(+1);
     878                 }
     879         else
     880                 $$=$4;
     881         }
     882 else
     883         {
     884         $$.ident=false;
     885         $$.constant=0;
     886         if ($4.constant)
     887                 {
     888                 trctx.out->printf("inc m0\n"
     889                                   "push %s\n",litstr($4));
     890                 }
     891         else
     892                 {
     893                 trstack.adjust(+1);
     894                 trctx.out->printf("move m[m0],m[m0+1]\n"
     895                                   "inc m0\n"); //drop "a"
    868896                 }
    869897         }
    870  trctx.out->printf(":_or_end_%d\n",$3.getInt());
     898 trctx.out->printf(":_or_end_%d\n",$3.counter);
    871899// trctx.out->printf("#################\n\n");
    872900 }
Note: See TracChangeset for help on using the changeset viewer.