nv-macro-lexvar

1.0.1 • Public • Published

nv-macro-lexvar

  • nv-macro-lexvar is a simple macro-tool, only one syntax
  • cli tool, use a simple syntax ##lexvar(...) in your script
  • to let you get/set lexical-variables(let,var,params) from outside
  • to avoid using for/while/if-[elif]-else/switch

install

  • npm install nv-macro-lexvar -g

usage

Usage: nv_lexvar [options]
Options:
    -i, --input             input string ,default stdin
    -o, --output            output string,default stdout
    -c, --compile           compile the macro
    -m, --macro_name        `##${macro_name}` used in script,default is lexvar
    -I, --IR                list four basic funcions : sync_while,async_while,sync_if,async_if
    -h, --help              usage




       nv-macro-lexvar# nv_lexvar -M
           the default macro_name:  lexvar
           use it begin with two #:  ##lexvar(...)


       nv-macro-lexvar# nv_lexvar -S
          .....
          let i=1;let a =100; let b=999;.....
          ////-for example, you need to change
          ////     the lexical <a> AND <i> from outside handler <handler>
          let ctx = ##lexvar(i,a);
          ////
          handler(ctx)
          .....

example

    % nv_lexvar -c 

    ////the source code

    function swhile(
        ctx,
        condf    =(ctx)=>ctx.i<5,
        handler  =(ctx)=>{ctx.rslt=ctx.rslt+ctx.i},
        post     =(ctx)=>{ctx.i=ctx.i+1}
    ) {
        while(condf(ctx)) {
            handler(ctx);
            post(ctx);
        }
        return(ctx)
    }


    function tst(param) {
        let i =0;
        let rslt=i;
        let ctx = ##lexvar(i,rslt);     // capture lexical-variables for using from outside
        swhile(ctx);
        return(rslt)
    }

    //--------> press ctrl+D


    function swhile(
        ctx,
        condf    =(ctx)=>ctx.i<5,
        handler  =(ctx)=>{ctx.rslt=ctx.rslt+ctx.i},
        post     =(ctx)=>{ctx.i=ctx.i+1}
    ) {
        while(condf(ctx)) {
            handler(ctx);
            post(ctx);
        }
        return(ctx)
    }

    function tst(param) {
      let i = 0;
      let rslt = i;

      let ctx = (() => {
        let ctx = {};
        Object.defineProperty(ctx, 'i', {
          get: () => i,
          set: v => {
            i = v;
          }
        });
        Object.defineProperty(ctx, 'rslt', {
          get: () => rslt,
          set: v => {
            rslt = v;
          }
        });
        return ctx;
      })();

      swhile(ctx);
      return rslt;
    }

    /*
    > tst()
    10
    >
    */

    function swhile(
        ctx,
        condf    =(ctx)=>ctx.i<5,
        handler  =(ctx)=>{ctx.rslt=ctx.rslt+ctx.i},
        post     =(ctx)=>{ctx.i=ctx.i+1}
    ) {
        while(condf(ctx)) {
            handler(ctx);
            post(ctx);
        }
        return(ctx)
    }


    function sif(
        ctx,
        entries=[
            (ctx)=>ctx.param,(ctx)=>{console.log([ctx.rslt,':',ctx.param])}
        ],
        else_handler=(ctx)=>console.log([ctx.param,':',ctx.rslt]),
    ) {
        for(let i=0;i<entries.length;i=i+2) {
            let condf = entries[i]
            let handler = entries[i+1]
            let cond = condf(ctx);
            if(cond) {
                handler(ctx)
                return(ctx)
            } else {}
        }
        ctx = else_handler(ctx);
        return(ctx)
    }

    function tst(param) {
        let i =0;
        let rslt=i;
        ////
        let ctx = ##lexvar(i,rslt);           //capture lexical-variables <i AND rslt> for using from outside
        swhile(ctx);
        ////
        console.log("lex rslt changed:",rslt)
        ////
        ctx = ##lexvar(param,rslt);      //capture lexical-variables <param AND rslt> for using from outside
        sif(ctx);
        ////
        return(rslt)
    }

    ////-------press ctrl+D

    ...... //generated code below

    function tst(param) {
      let i = 0;
      let rslt = i; ////
      
      /*##lexvar(i,rslt) generated code*/

      let ctx = (() => {
        let ctx = {};
        Object.defineProperty(ctx, 'i', {
          get: () => i,
          set: v => {
            i = v;
          }
        });
        Object.defineProperty(ctx, 'rslt', {
          get: () => rslt,
          set: v => {
            rslt = v;
          }
        });
        return ctx;
      })(); //capture lexical-variables <i AND rslt> for using from outside


      swhile(ctx); ////

      console.log("lex rslt changed:", rslt); ////

      /*##lexvar(param,rslt) generated code*/

      ctx = (() => {
        let ctx = {};
        Object.defineProperty(ctx, 'param', {
          get: () => param,
          set: v => {
            param = v;
          }
        });
        Object.defineProperty(ctx, 'rslt', {
          get: () => rslt,
          set: v => {
            rslt = v;
          }
        });
        return ctx;
      })(); //capture lexical-variables <param AND rslt> for using from outside


      sif(ctx); ////

      return rslt;
    }



    /*
    > tst(true)
    lex rslt changed: 10
    [ 10, ':', true ]
    10
    >
    > tst(false)
    lex rslt changed: 10
    [ false, ':', 10 ]
    10
    >

    */

four basic functions

  • sync while
  • async while
  • sync if
  • async if
  • MOST function could BE done with these four operations
  • the ONLY exception is IF you want to implement goto-like,these four is NOT enough

    swhile
    -------

        function swhile(
            ctx,
            condf,
            handler,
            post
        ) {
            while(condf(ctx)) {
                handler(ctx);
                post(ctx);
            }
            return(ctx)
        }


    awhile
    -------

        async function awhile(
            ctx,
            condf,
            handler,
            post
        ) {
            let cond = await condf(ctx)
            while(cond) {
                await handler(ctx);
                await post(ctx);
                cond = await condf(ctx);
            }
            return(ctx)
        }


    sif
    -------

        function sif(
            ctx,
            entries,
            else_handler,
        ) {
            for(let i=0;i<entries.length;i=i+2) {
                let condf = entries[i];
                let handler = entries[i+1];
                let cond = condf(ctx);
                if(cond) {
                    handler(ctx);
                    return(ctx)
                } else {}
            }
            ctx = else_handler(ctx);
            return(ctx)
        }


    aif
    -------

        async function aif(
            ctx,
            entries,
            else_handler,
        ) {
            for(let i=0;i<entries.length;i=i+2) {
                let condf = entries[i];
                let handler = entries[i+1];
                let cond = await condf(ctx);
                if(cond) {
                    await handler(ctx);
                    return(ctx)
                } else {}
            }
            ctx = await else_handler(ctx);
            return(ctx)
        }

LICENSE

  • ISC

Package Sidebar

Install

npm i nv-macro-lexvar

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

13.8 kB

Total Files

7

Last publish

Collaborators

  • ihgazni2