From c50ee9aabb71d45937d7f673af6a7ea349acb67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Mon, 5 Jun 2023 19:28:23 +0300 Subject: [PATCH] d --- shared/JavaScript/memoryItemPositions.js | 1 - shared/Swift/memoryItemPositions.swift | 22 ++++++++++--- shared/gen | 2 +- shared/memoryItemPositions.ts | 1 - toSwift | 42 ++++++++++++++++++++++-- будущее | 2 ++ прошлое | 6 ++++ 7 files changed, 66 insertions(+), 10 deletions(-) diff --git a/shared/JavaScript/memoryItemPositions.js b/shared/JavaScript/memoryItemPositions.js index bdcae51..eeb7b98 100644 --- a/shared/JavaScript/memoryItemPositions.js +++ b/shared/JavaScript/memoryItemPositions.js @@ -5,7 +5,6 @@ var Position = /** @class */ (function () { } return Position; }()); -// @ts-nocheck function memoryItemPositions(c) { var pos = []; for (var i = 0; i < c.itemsCount; i++) { diff --git a/shared/Swift/memoryItemPositions.swift b/shared/Swift/memoryItemPositions.swift index e4a51a9..8c4ed05 100644 --- a/shared/Swift/memoryItemPositions.swift +++ b/shared/Swift/memoryItemPositions.swift @@ -1,11 +1,25 @@ -// @ts-nocheck -func memoryItemPositions(c: Context) -> [Position] { - var pos = [Position] +protocol Context { + var itemsCount: Float { get } + +} + +class Position { + x: Float + y: Float + + constructor(x, y) { + this.x = x + this.y = y + } +} + +func memoryItemPositions(c: Context) -> Position[] { + var pos: Position[] = [] for (var i = 0; i < c.itemsCount; i++) { let row = Math.floor(i / 4) let x = memoryGap() + (i - row * 4) * memoryGap() let y = memoryGap() + row * memoryGap() - pos.push(Position(x, y)) + pos.push(new Position(x, y)) } return pos } diff --git a/shared/gen b/shared/gen index 66bb530..4c5bc4a 100755 --- a/shared/gen +++ b/shared/gen @@ -10,7 +10,7 @@ FILES=( ) for file in ${FILES[*]}; do - tsc $SCRIPT_DIR/$file.ts --outfile $SCRIPT_DIR/JavaScript/$file.js $SCRIPT_DIR/../toSwift $SCRIPT_DIR/$file.ts $SCRIPT_DIR/Swift/$file.swift + tsc $SCRIPT_DIR/$file.ts --outfile $SCRIPT_DIR/JavaScript/$file.js done diff --git a/shared/memoryItemPositions.ts b/shared/memoryItemPositions.ts index b96e798..f818fca 100644 --- a/shared/memoryItemPositions.ts +++ b/shared/memoryItemPositions.ts @@ -12,7 +12,6 @@ class Position { } } -// @ts-nocheck function memoryItemPositions(c: Context): Position[] { var pos: Position[] = [] for (var i = 0; i < c.itemsCount; i++) { diff --git a/toSwift b/toSwift index a4b082c..30be650 100755 --- a/toSwift +++ b/toSwift @@ -10,10 +10,11 @@ var fs = require("fs"); let fileSrc = process.argv[2]; let fileDst = process.argv[3]; -let replacements = { +let globalReplacements = { "function": "func", "number": "Float", "):": ") ->", + "interface": "protocol", }; console.log(`Converting '${fileSrc}' to '${fileDst}'`); @@ -33,9 +34,44 @@ fs.writeFileSync(fileDst, contentsDst); function convert(line) { var result = line; - for (let src in replacements) { - let dst = replacements[src]; + for (let src in globalReplacements) { + let dst = globalReplacements[src]; result = result.replace(src, dst); } + result = protocolReplace(result); return result; } + +var isProtocol = false; + +function protocolReplace(line) { + if (line.includes("protocol")) { + isProtocol = true; + } + if (line == "}") { + isProtocol = false; + } + console.log("ИГР protocolR isP/line:", isProtocol, line); + if (!isProtocol) { + return line; + } + + var result = line; + if (isProtocol) { + result = protocolReplaceVariable(result); + } + return result; +} + +function protocolReplaceVariable(line) { + let parts = line.split(": "); + if (parts.length == 2) { + let type = parts[1]; + let name = parts[0].trim(); + let spaceLength = parts[0].length - name.length; + let spaces = " ".repeat(spaceLength); + //console.log("Variable. name/spaceL/parts:", name, spaceLength, parts); + return `${spaces}var ${name}: ${type} { get }\n`; + } + return line; +} diff --git a/будущее b/будущее index 641dd32..5bce9a3 100644 --- a/будущее +++ b/будущее @@ -1,5 +1,7 @@ НАДО: +* починить ^M +* конвертировать interface в protocol * генерить interface в Swift * генерить class в Swift * нет struct, к сожалению diff --git a/прошлое b/прошлое index 665b70d..a2f33a3 100644 --- a/прошлое +++ b/прошлое @@ -1,4 +1,10 @@ +05.06: 40 + +* ввести понятие контекста в разбор +* определять контекст протокола +* конвертировать поля в свойства протокола + 02.06: 20 * геренить memoryItemPositions.js