view src/TypeRulesGenerator.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/TypeRulesGenerator.d@f51305056196
children 451ede0105e0
line wrap: on
line source

#! /usr/bin/rdmd
/++
  Author: Aziz Köksal
  License: GPL3
+/
module TypeRulesGenerator;

import tango.io.File;
import tango.io.FilePath;

alias char[] string;

void main(char[][] args)
{
  char[] text = "// Generated by TypeRulesGenerator.d\n"
                "module TypeRules.d;\n\n";
  text ~= "char[][][] unaryExpsResults = [\n";
  foreach (results; unaryExpsResults)
  {
    text ~= "  [";
    foreach (result; results)
      text ~= '"' ~ result ~ '"' ~ ", ";
    text[$-2] = ']';
    text[$-1] = ',';
    text ~= \n;
  }
  text[$-2] = '\n';
  text[$-1] = ']';
  text ~= ";\n\n";

  text ~= "char[][][][] binaryExpsResults = [\n";
  foreach (expResults; binaryExpsResults)
  {
    text ~= "  [\n";
    foreach (results; expResults)
    {
      text ~= "    [";
      foreach (result; results)
        text ~= '"' ~ result ~ '"' ~ ", ";
      text[$-2] = ']';
      text[$-1] = ',';
      text ~= \n;
    }
    text[$-2] = '\n';
    text[$-1] = ' ';
    text ~= " ],\n";
  }
  text[$-2] = '\n';
  text[$-1] = ']';
  text ~= ";\n";

  // Write the text to a D module.
  auto file = new File("TypeRulesData.d");
  file.write(text);
}

template ExpressionType(char[] T1, char[] T2, char[] expression)
{
  mixin("alias "~T1~" X;");
  mixin("alias "~T2~" Y;");
  X x;
  Y y;
  static if(is(typeof(mixin(expression)) ResultType))
    const char[] result = ResultType.stringof;
  else
    const char[] result = "Error";
}
alias ExpressionType EType;

// pragma(msg, EType!("char", "int", "&x").result);

static const string[] basicTypes = [
  "char"[],   "wchar",   "dchar", "bool",
  "byte",   "ubyte",   "short", "ushort",
  "int",    "uint",    "long",  "ulong",
  /+"cent",   "ucent",+/
  "float",  "double",  "real",
  "ifloat", "idouble", "ireal",
  "cfloat", "cdouble", "creal"/+, "void"+/
];

static const string[] unaryExpressions = [
  "!x",
  "&x",
  "~x",
  "+x",
  "-x",
  "++x",
  "--x",
  "x++",
  "x--",
];

static const string[] binaryExpressions = [
  "x!<>=y",
  "x!<>y",
  "x!<=y",
  "x!<y",
  "x!>=y",
  "x!>y",
  "x<>=y",
  "x<>y",

  "x=y", "x==y", "x!=y",
  "x<=y", "x<y",
  "x>=y", "x>y",
  "x<<=y", "x<<y",
  "x>>=y","x>>y",
  "x>>>=y", "x>>>y",
  "x|=y", "x||y", "x|y",
  "x&=y", "x&&y", "x&y",
  "x+=y", "x+y",
  "x-=y", "x-y",
  "x/=y", "x/y",
  "x*=y", "x*y",
  "x%=y", "x%y",
  "x^=y", "x^y",
  "x~=y",
  "x~y",
  "x,y"
];

char[] genBinaryExpArray(char[] expression)
{
  char[] result = "[\n";
  foreach (t1; basicTypes)
  {
    result ~= "[\n";
    foreach (t2; basicTypes)
      result ~= `EType!("`~t1~`", "`~t2~`", "`~expression~`").result,`\n;
    result[result.length-2] = ']'; // Overwrite last comma.
    result[result.length-1] = ','; // Overwrite last \n.
  }
  result[result.length-1] = ']'; // Overwrite last comma.
  return result;
}
// pragma(msg, mixin(genBinaryExpArray("x%y")).stringof);

char[] genBinaryExpsArray()
{
  char[] result = "[\n";
  foreach (expression; binaryExpressions)
  {
    result ~= genBinaryExpArray(expression);
    result ~= ",\n";
  }
  result[result.length-2] = ']';
  return result;
}

// pragma(msg, mixin(genBinaryExpsArray()).stringof);

char[] genUnaryExpArray(char[] expression)
{
  char[] result = "[\n";
  foreach (t1; basicTypes)
    result ~= `EType!("`~t1~`", "int", "`~expression~`").result,`\n;
  result[result.length-2] = ']'; // Overwrite last comma.
  return result;
}

char[] genUnaryExpsArray()
{
  char[] result = "[\n";
  foreach (expression; unaryExpressions)
    result ~= genUnaryExpArray(expression) ~ ",\n";
  result[result.length-2] = ']';
  return result;
}

// pragma(msg, mixin(genUnaryExpsArray()).stringof);

auto unaryExpsResults = mixin(genUnaryExpsArray());
auto binaryExpsResults = mixin(genBinaryExpsArray());