dummyVar

MultimorphBuilder.hx

Nov 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.42 KB | None | 0 0
  1. /*
  2.  * GNU General Public License, Version 3.0
  3.  *
  4.  * Copyright (c) 2018 0x.dummyVar
  5.  *
  6.  * This program is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19.  
  20. package;
  21.  
  22.  
  23. import sys.io.File;
  24.  
  25.  
  26. /*
  27.  * Creates a Multimorph package based on the amount and order of
  28.  * type identifiers in `MultimorphBuilder.LETTERS`.
  29.  *
  30.  *      `haxe -main MultimorphBuilder -neko MultimorphBuilder.n && neko MultimorphBuilder.n && rm MultimorphBuilder.n`
  31.  */
  32. @:final class MultimorphBuilder {
  33.  
  34.  
  35.     static var NEWLINE = "\n";
  36.     static var TAB     = "    ";
  37.     static var LETTERS = [
  38.         "A", "B", "C", "D", "E", "F", "G",
  39.         "H", "I", "J", "K", "L", "M", "N",
  40.         "O", "P", "Q", "R", "S", "T", "U",
  41.         "V", "W", "X", "Y", "Z"
  42.     ];
  43.  
  44.  
  45.     static var output  = new StringBuf ();
  46.  
  47.  
  48.     static function main () {
  49.  
  50.         if ( LETTERS.length < 3 ) {
  51.  
  52.             Sys.println ( "ERROR: `LETTERS` must have at least three elements." );
  53.  
  54.             Sys.exit ( 1 );
  55.  
  56.         }
  57.  
  58.  
  59.         output.add ( "package;" + NEWLINE + NEWLINE + NEWLINE );
  60.         output.add ( "using Type;" + NEWLINE );
  61.  
  62.         output.add ( NEWLINE + NEWLINE );
  63.         buildClass ();
  64.         output.add ( NEWLINE + NEWLINE );
  65.         buildTypedefs ();
  66.         output.add ( NEWLINE + NEWLINE );
  67.         buildEnums ();
  68.         output.add ( NEWLINE + NEWLINE );
  69.         buildAbstracts ();
  70.  
  71.         File.saveContent ( "Multimorph.hx", output.toString () );
  72.  
  73.     }
  74.  
  75.     static function buildClass () {
  76.  
  77.         output.add ( "@:final class Multimorph {" + NEWLINE + NEWLINE + NEWLINE );
  78.         output.add ( TAB + "public static inline function value ( value : AnyAny ) {" + NEWLINE + NEWLINE );
  79.         output.add ( TAB + TAB + "return value.enumParameters () [ 0 ];" + NEWLINE + NEWLINE );
  80.         output.add ( TAB + "}" + NEWLINE + NEWLINE );
  81.         output.add ( TAB + "public static inline function position ( value : AnyAny ) {" + NEWLINE + NEWLINE );
  82.         output.add ( TAB + TAB + "return value.enumIndex ();" + NEWLINE + NEWLINE );
  83.         output.add ( TAB + "}" + NEWLINE + NEWLINE + NEWLINE );
  84.         output.add ( "}" + NEWLINE );
  85.  
  86.     }
  87.  
  88.     static function buildTypedefs () {
  89.  
  90.         output.add ( "typedef D = Dynamic;" + NEWLINE );
  91.  
  92.         for ( index in 2...( LETTERS.length + 1 ) ) {
  93.  
  94.             output.add ( "typedef Any" + index + " = Of" + index + "<" );
  95.  
  96.             for ( loop in 0...index ) {
  97.  
  98.                 output.add ( "D" );
  99.  
  100.                 if ( loop != index - 1 ) {
  101.  
  102.                     output.add ( ", " );
  103.  
  104.                 }
  105.             }
  106.  
  107.             output.add ( ">;" + NEWLINE );
  108.  
  109.         }
  110.  
  111.         output.add ( "typedef AnyAny = Of" + ( LETTERS.length - 1 ) + "<" );
  112.  
  113.         for ( index in 2...( LETTERS.length + 1 ) ) {
  114.  
  115.             output.add ( "Any" + index );
  116.  
  117.             if ( index != LETTERS.length ) {
  118.  
  119.                 output.add ( ", " );
  120.  
  121.             }
  122.  
  123.         }
  124.  
  125.         output.add ( ">;" + NEWLINE );
  126.  
  127.     }
  128.  
  129.     static function buildEnums () {
  130.  
  131.         for ( index in 2...( LETTERS.length + 1 ) ) {
  132.  
  133.             var TRIANGLES = getTriangles ( index );
  134.  
  135.             output.add ( "enum About" + index + " " + TRIANGLES + " {" + NEWLINE );
  136.  
  137.             for ( loop in 0...index ) {
  138.  
  139.                 output.add ( TAB + "Pos" + LETTERS [ loop ] + " ( value : " + LETTERS [ loop ] + " );" + NEWLINE );
  140.             }
  141.  
  142.             output.add ( "}" + NEWLINE );
  143.  
  144.         }
  145.  
  146.     }
  147.  
  148.     static function buildAbstracts () {
  149.  
  150.         for ( index in 2...( LETTERS.length + 1 ) ) {
  151.  
  152.             var TRIANGLES = getTriangles ( index );
  153.  
  154.             output.add ( "abstract Of" + index + " " + TRIANGLES + " ( About" + index + TRIANGLES + " ) from About" + index + TRIANGLES + " to About" + index + TRIANGLES + " {" + NEWLINE + NEWLINE );
  155.  
  156.             for ( loop in 0...index ) {
  157.  
  158.                 output.add ( TAB + "@:from static inline function from_" + LETTERS [ loop ] + " " + TRIANGLES + " ( val : " + LETTERS [ loop ] + " ) : Of" + index + TRIANGLES + " return About" + index + ".Pos" + LETTERS [ loop ] + " ( val );" + NEWLINE );
  159.  
  160.             }
  161.  
  162.             output.add ( NEWLINE );
  163.  
  164.             for ( loop in 0...index ) {
  165.  
  166.                 output.add ( TAB + "@:to inline function to_" + LETTERS [ loop ] + " () : Null<" + LETTERS [ loop ] + "> return switch ( this ) { case About" + index + ".Pos" + LETTERS [ loop ] + " ( val ): val; default: null; }" + NEWLINE );
  167.  
  168.             }
  169.  
  170.             output.add ( NEWLINE + "}" + NEWLINE );
  171.  
  172.         }
  173.  
  174.     }
  175.  
  176.     static function getTriangles ( index : Int ) {
  177.  
  178.         var output = "<";
  179.  
  180.         for ( loop in 0...index ) {
  181.  
  182.             output += LETTERS [ loop ];
  183.  
  184.             if ( loop != index - 1 ) {
  185.  
  186.                 output += ", ";
  187.  
  188.             }
  189.  
  190.         }
  191.  
  192.         output += ">";
  193.  
  194.         return output;
  195.  
  196.     }
  197.  
  198.  
  199. }
Add Comment
Please, Sign In to add comment