From 7cdfe72c794e596669c15a9d44b783326b7b4b15 Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Mon, 26 Dec 2016 17:56:02 +0100 Subject: [PATCH] =?UTF-8?q?=C3=BCbertragung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + project.json | 53 ++++++++++++++ source/base.ts | 79 ++++++++++++++++++++ source/type2.ts | 190 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 326 insertions(+) create mode 100644 .gitignore create mode 100644 project.json create mode 100644 source/base.ts create mode 100644 source/type2.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..942d017 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +temp/ +build/ +tools/ + diff --git a/project.json b/project.json new file mode 100644 index 0000000..25ffe92 --- /dev/null +++ b/project.json @@ -0,0 +1,53 @@ +{ + "name": "type2", + "version": "0.0.1", + "dependencies": [ + "../plankton/lang/project.json" + ], + "roottask": { + "name": "link", + "type": "schwamm-apply", + "parameters": { + "path": "build/schwamm.json", + "outputs": { + "logic-impl": "build/type2.js" + } + }, + "sub": [ + { + "name": "build", + "type": "schwamm-create", + "parameters": { + "includes": [ + "../plankton/lang/build/schwamm.json" + ], + "adhoc": { + "logic-decl": [ + "temp/unlinked-logic-decl.d.ts" + ], + "logic-impl": [ + "temp/unlinked-logic-impl.js" + ] + }, + "output": "build/schwamm.json" + }, + "sub": [ + { + "name": "logic", + "type": "typescript", + "parameters": { + "allowUnreachableCode": true, + "inputs": [ + "source/base.ts", + "source/type2.ts" + ], + "declaration": "temp/unlinked-logic-decl.d.ts", + "output": "temp/unlinked-logic-impl.js" + } + } + ] + } + ] + } +} + diff --git a/source/base.ts b/source/base.ts new file mode 100644 index 0000000..c4d2ea8 --- /dev/null +++ b/source/base.ts @@ -0,0 +1,79 @@ + +/** + * @author fenris + */ +type char = string; + + +/** + * @author fenris + */ +declare var require : (path : string)=>any; + + +/** + * @author fenris + */ +declare var process : any; + + +/** + * @author fenris + */ +declare class Buffer + { + public toString() : string; + } + + +/** + * @author fenris + */ +declare class Promise + { + + /** + * @author fenris + */ + public constructor + ( + executor : (resolve : (result ?: type_result)=>void, reject ?: (reason ?: type_reason)=>void)=>void + ) + ; + + + /** + * @author fenris + */ + public then + ( + onFulfilled : (result : type_result)=>any, + onRejected ?: (reason : type_reason)=>any + ) + : Promise + ; + + + /** + * @author fenris + */ + public catch + ( + onRejected : (reason : type_reason)=>any + ) + : Promise + ; + + + /** + * @author fenris + */ + public static resolve + ( + result : type_result + ) + : Promise + ; + + } + diff --git a/source/type2.ts b/source/type2.ts new file mode 100644 index 0000000..e2ff6ef --- /dev/null +++ b/source/type2.ts @@ -0,0 +1,190 @@ + +/// + + +/** + * @author fenris + */ +type type_myreader = lib_lang.class_reader>; + + +/** + * @author fenris + */ +var generate_reader : (path : string)=>Promise = + path => Promise.resolve(path) + .then + ( + path => new Promise + ( + (resolve, reject) => + { + let _fs = require("fs"); + _fs.readFile + ( + path, + { + "encoding": "utf8", + "flag": "r", + }, + (error, buffer) => + { + if (error == null) + { + resolve(buffer); + } + else + { + reject(error); + } + } + ) + ; + } + ) + ) + .then + ( + buffer => new Promise + ( + (resolve, reject) => + { + resolve(JSON.parse(buffer.toString())); + } + ) + ) + .then + ( + data => new Promise + ( + (resolve, reject) => + { + resolve(lib_lang.class_reader.default_raw(data)); + } + ) + ) +; + + +/** + * @author fenris + */ +var transform_arguments : (args : Array)=>Promise<{path ?: string; input ?: Array;}, Error> = + args => Promise.resolve<{path ?: string; input ?: Array;}, Error>({}) + .then<{path ?: string; input ?: Array;}, Error> + ( + parameters => new Promise<{path ?: string; input ?: Array;}, Error> + ( + (resolve, reject) => + { + let path : string; + path = ((args.length >= 1) ? args.shift() : null); + if (path == null) + { + reject(new Error("no path specified")); + } + else + { + resolve({"path": path, "input": parameters.input}); + } + } + ) + ) + .then<{path ?: string; input ?: Array;}, Error> + ( + parameters => new Promise<{path ?: string; input ?: Array;}, Error> + ( + (resolve, reject) => + { + let input : Array; + let input_raw : string = args.join(" "); + if (input_raw == "") + { + reject(new Error("no input given")); + } + else + { + input = input_raw.split(""); + resolve({"path": parameters.path, "input": input}); + } + } + ) + ) +; + + +/** + * @author fenris + */ +var main : (args : Array)=>Promise = + args => Promise.resolve(undefined) + .then<{path ?: string; input ?: Array;}, Error> + ( + _ => transform_arguments(args) + ) + .then<{parameters : {path ?: string; input ?: Array;}; reader : type_myreader;}, Error> + ( + parameters => generate_reader(parameters.path) + .then<{parameters : {path ?: string; input ?: Array;}; reader : type_myreader;}, Error> + ( + reader => new Promise<{parameters : {path ?: string; input ?: Array;}; reader : type_myreader;}, Error> + ( + (resolve_, reject_) => + { + resolve_({"parameters": parameters, "reader": reader}); + } + ) + ) + ) + .then, Error> + ( + vars => new Promise, Error> + ( + (resolve, reject) => + { + lib_log.level_push(1); + let result : lib_lang.type_tree = vars.reader.run(vars.parameters.input); + lib_log.level_pop(); + if (result == null) + { + reject(new Error("didn't work")); + } + else + { + resolve(result); + } + } + ) + ) + .then + ( + result => new Promise + ( + (resolve, reject) => + { + console.info(JSON.stringify(result, undefined, "\t")); + resolve(undefined); + } + ) + ) +; + + +/** + * @author fenris + */ +main(process.argv.slice(2)) + .then + ( + output => + { + // console.info("--", "done:", output); + } + , + error => + { + console.error("--", "failed:", error); + } + ) +; +