www.digitalmars.com         C & C++   DMDScript  

D - Tokenized compile time initialization

reply Fernando Tolentino <josefernandotolentino gmail.com> writes:
Hi, I wonder if D support in future (maybe in 2.0 :) ) this feature. I imagine
like some it.

  char[] mary="Mary Simpson";
  Query q = {select * from person where name = {mary} order by birth};

But the compiler will do

  char[] mary="Mary Simpson";
  Query q = new Query();
  with(q) {
    q.addfield("*");
    q.addfrom("person");
    q.where.eq("name",mary);
  }

This will work if the programmer creates Query class in that way:

module query;

class Query
{
    this() // normal initializer
    {   ...
    }
    tokenized this(token[] tokens) // here is the magic, compiler will execute
this script (interpret or use dynamic library(plugin for compiler))
      init(){
        whitespaces=[" ","\t","\r","\n"]; // must support multiline in tokens
and D style comments
        clauses=["select","from","as","where","order by"]; // must replace any
whitespace for space(ascii 32)
        operators=["=","<>","like","(", ")"]; // i think everythink must be
casesensitive
      }
      main(){
        parseSelect(false);
      }
      parseSelect(bool isSubSelect)
      {
        token.checkClause("select"); // if not is compiling fails
        token.next(); // autoskip whitespaces
        parseField();
        while (!(token.eof() || token.isClause("from"))
        {
          if (token.isOperator(")") {
            if (isSubSelect)
            {
              token.next();
              return;
            }
          }
          checkOperator(',');
          token.next();
          parseField();
        }
        parseField(){
          if (token.isOperator("(") {
            token.next();
            mixin("Query subselect = new Query()");
            mixin("with(subselect){");
            parseSelect(true);
            mixin("}");
            mixin("addfield("""+field+""","""+token.str+""")");
          }
          else {
            auto field=token.text;
            token.next();
            if (token.isClause('as')){
              token.next();
              mixin("addfield("""+field+""","""+token.str+""")");
              token.next();
            }
            else mixin("addfield("""+field+""")");
          }
        }
        parseFrom()
....
     }
    }
}


well, is like some this.

thanks for attention.
Apr 12 2008
parent reply Benjamin Shropshire <ao pathlink.com> writes:
this newsgroup is depricated, use the digitalmars.d group
Apr 12 2008
parent Ary Borenszweig <ary esperanto.org.ar> writes:
Benjamin Shropshire escribió:
 this newsgroup is depricated, use the digitalmars.d group
Why is one allowed to write to this newsgroup if it is deprecated?
Apr 12 2008