Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GNU General Public License, Version 3.0
- *
- * Copyright (c) 2018 0x.dummyVar
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package;
- import sys.io.File;
- /*
- * Creates a Multimorph package based on the amount and order of
- * type identifiers in `MultimorphBuilder.LETTERS`.
- *
- * `haxe -main MultimorphBuilder -neko MultimorphBuilder.n && neko MultimorphBuilder.n && rm MultimorphBuilder.n`
- */
- @:final class MultimorphBuilder {
- static var NEWLINE = "\n";
- static var TAB = " ";
- static var LETTERS = [
- "A", "B", "C", "D", "E", "F", "G",
- "H", "I", "J", "K", "L", "M", "N",
- "O", "P", "Q", "R", "S", "T", "U",
- "V", "W", "X", "Y", "Z"
- ];
- static var output = new StringBuf ();
- static function main () {
- if ( LETTERS.length < 3 ) {
- Sys.println ( "ERROR: `LETTERS` must have at least three elements." );
- Sys.exit ( 1 );
- }
- output.add ( "package;" + NEWLINE + NEWLINE + NEWLINE );
- output.add ( "using Type;" + NEWLINE );
- output.add ( NEWLINE + NEWLINE );
- buildClass ();
- output.add ( NEWLINE + NEWLINE );
- buildTypedefs ();
- output.add ( NEWLINE + NEWLINE );
- buildEnums ();
- output.add ( NEWLINE + NEWLINE );
- buildAbstracts ();
- File.saveContent ( "Multimorph.hx", output.toString () );
- }
- static function buildClass () {
- output.add ( "@:final class Multimorph {" + NEWLINE + NEWLINE + NEWLINE );
- output.add ( TAB + "public static inline function value ( value : AnyAny ) {" + NEWLINE + NEWLINE );
- output.add ( TAB + TAB + "return value.enumParameters () [ 0 ];" + NEWLINE + NEWLINE );
- output.add ( TAB + "}" + NEWLINE + NEWLINE );
- output.add ( TAB + "public static inline function position ( value : AnyAny ) {" + NEWLINE + NEWLINE );
- output.add ( TAB + TAB + "return value.enumIndex ();" + NEWLINE + NEWLINE );
- output.add ( TAB + "}" + NEWLINE + NEWLINE + NEWLINE );
- output.add ( "}" + NEWLINE );
- }
- static function buildTypedefs () {
- output.add ( "typedef D = Dynamic;" + NEWLINE );
- for ( index in 2...( LETTERS.length + 1 ) ) {
- output.add ( "typedef Any" + index + " = Of" + index + "<" );
- for ( loop in 0...index ) {
- output.add ( "D" );
- if ( loop != index - 1 ) {
- output.add ( ", " );
- }
- }
- output.add ( ">;" + NEWLINE );
- }
- output.add ( "typedef AnyAny = Of" + ( LETTERS.length - 1 ) + "<" );
- for ( index in 2...( LETTERS.length + 1 ) ) {
- output.add ( "Any" + index );
- if ( index != LETTERS.length ) {
- output.add ( ", " );
- }
- }
- output.add ( ">;" + NEWLINE );
- }
- static function buildEnums () {
- for ( index in 2...( LETTERS.length + 1 ) ) {
- var TRIANGLES = getTriangles ( index );
- output.add ( "enum About" + index + " " + TRIANGLES + " {" + NEWLINE );
- for ( loop in 0...index ) {
- output.add ( TAB + "Pos" + LETTERS [ loop ] + " ( value : " + LETTERS [ loop ] + " );" + NEWLINE );
- }
- output.add ( "}" + NEWLINE );
- }
- }
- static function buildAbstracts () {
- for ( index in 2...( LETTERS.length + 1 ) ) {
- var TRIANGLES = getTriangles ( index );
- output.add ( "abstract Of" + index + " " + TRIANGLES + " ( About" + index + TRIANGLES + " ) from About" + index + TRIANGLES + " to About" + index + TRIANGLES + " {" + NEWLINE + NEWLINE );
- for ( loop in 0...index ) {
- 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 );
- }
- output.add ( NEWLINE );
- for ( loop in 0...index ) {
- 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 );
- }
- output.add ( NEWLINE + "}" + NEWLINE );
- }
- }
- static function getTriangles ( index : Int ) {
- var output = "<";
- for ( loop in 0...index ) {
- output += LETTERS [ loop ];
- if ( loop != index - 1 ) {
- output += ", ";
- }
- }
- output += ">";
- return output;
- }
- }
Add Comment
Please, Sign In to add comment