Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103324

Re: need {..} block, and switch case, need, and needs keywords

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: need {..} block, and switch case, need, and needs keywords
Date 2017-02-06 16:23 +0000
Organization A noiseless patient Spider
Message-ID <o7a7r6$f4a$1@dont-email.me> (permalink)
References <cccf37f2-ec4b-4780-a406-ac52f46e8c79@googlegroups.com>

Show all headers | View raw


On 06/02/2017 15:57, Rick C. Hodgin wrote:
> I had an idea for injecting code into a system which is tied to a needs
> keyword used later in flow control.  Consider the needs of this a switch
> {..} block, having some code which would only be executed if flow reached
> a certain point.
>
>     switch (x)
>     {
>         case 0:
>             break;
>         case 1:
>             break;
>         case 2:
>             break;
>
>         default:
>             value = get_something(); /* Possibly expensive call, therefore
>                                         only issued if flow reaches here */
>             // Continue testing
>             switch (x)
>             {
>                 case 3:
>                     // "value" is used here
>                     break;
>                 case 4:
>                     value2 = get_something_else();
>                     // "value" and "value2" are valid and in scope here
>                     break;
>                 default:
>                     // Not found
>                     break;
>             }
>
>     }
>
> I had the idea of creating the need keyword for definition, and the
> needs keyword for use:
>
>     switch (x)
>     {
>         need val:
>             // Documentation about why this would be needed
>             value = get_something();
>             break;
>         need val2:
>             // Documentation about why this would be needed
>             value2 = get_something_else();
>             break;
>
>         case 0:
>             break;
>         case 1:
>             break;
>         case 2:
>             break;
>
>         case 3 needs val:
>             // "value" is used here
>             break;
>         case 4 needs val, val2:
>             // "value" and "value2" are used here
>             break;
>
>         default:
>             // Not found
>             break;
>     }
>
> -----
> In addition, the more general form could be used in code with
> independently named blocks which can be executed only when needed
> in the runtime flow of the code:
>
>     need val {
>         // Documentation about why this would be needed
>         int value = get_something();
>     }
>
>     need val2 {
>         // Documentation about why this would be needed
>         int value2 = get_something_else();
>     }

This is similar to the flow blocks you mentioned a couple of years back. 
I actually implemented something along those lines which can do the 
above. Example (not in C):

proc start=
     int x
     int value=0, value2=0

     x:=4

     switch x
     when 0 then
     when 1 then
     when 2 then
     when 3 then
         @getval
         println value
     when 4 then
         @getval
         @getval2
         println value,value2
     else
     end switch

     println "ABC"

     blockdef getval=
         value:=100
     end

     blockdef getval2=
         value:=200
     end

     println "DEF"
end

When x is 4, the output is:

100 200
ABC
DEF

(The ABC/DEF shows it 'steps around' those block definitions.)

The C output is (requiring gcc's label pointers, otherwise it's trickier 
and less efficient):

  void start  (void) {
  int32   x;
  int32   value=0;
  int32   value2=0;
  int32 * av_1;
  int32 * av_2;
      x = 4;
      switch (x) {
      case 0:
          break;
      case 1:
          break;
      case 2:
          break;
      case 3:
          av_1=&&L2;
          goto L1;
  L2:;
          printf("%d\n",value);
          break;
      case 4:
          av_1=&&L3;
          goto L1;
  L3:;
          av_2=&&L5;
          goto L4;
  L5:;
          printf("%d %d\n",value,value2);
          break;
      default:;
      }
      printf("%s\n","ABC");
      goto L6;
  L1:;
      value = 100;
      goto *av_1;
  L6:;
      goto L7;
  L4:;
      value2 = 200;
      goto *av_2;
  L7:;
      printf("%s\n","DEF");
  }

The feature is still there but I never use and in the next version it 
would have been dropped. There just seems something 'off' about it.

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-06 07:57 -0800
  Re: need {..} block, and switch case, need, and needs keywords BartC <bc@freeuk.com> - 2017-02-06 16:23 +0000
    Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-06 08:36 -0800
  Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-07 09:39 +0100
    Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-07 07:32 -0800
      Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-08 09:43 +0100
        Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 03:26 -0800
          Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-08 14:30 +0100
            Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 05:38 -0800
              Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 05:43 -0800

csiph-web