wip
This commit is contained in:
Vendored
+1
-1
@@ -16,7 +16,7 @@
|
||||
"${workspaceFolder}/dutchies-dcs-scripting-tools/dist/**/*.js",
|
||||
"${workspaceFolder}/compiler/dist/**/*.js"
|
||||
],
|
||||
"preLaunchTask": "compile"
|
||||
"preLaunchTask": "watch"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -24,6 +24,7 @@
|
||||
"reveal": "never"
|
||||
},
|
||||
"group": "build",
|
||||
"problemMatcher": "$tsc-watch",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/dutchies-dcs-scripting-tools"
|
||||
}
|
||||
|
||||
+122
-16
@@ -8,7 +8,7 @@ import { CompilationError } from './CompilationError';
|
||||
Require is a special case as it's technically a function call, but it's a special use case.
|
||||
*/
|
||||
export enum BlockType {
|
||||
Line,
|
||||
CodeTextBlock,
|
||||
Function,
|
||||
If,
|
||||
While,
|
||||
@@ -16,9 +16,15 @@ export enum BlockType {
|
||||
Do,
|
||||
Return,
|
||||
Require,
|
||||
Table,
|
||||
File
|
||||
}
|
||||
|
||||
const keywords = ['if', 'while', 'for', 'do', 'return', 'function', 'require', 'end'];
|
||||
function isKeyWord(word: string): boolean {
|
||||
return keywords.includes(word);
|
||||
}
|
||||
|
||||
export abstract class CodeBlock {
|
||||
protected childBlocks: CodeBlock[] = [];
|
||||
private parentBlock?: CodeBlock;
|
||||
@@ -93,8 +99,7 @@ export abstract class CodeBlock {
|
||||
currentWord = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
if(currentChar === '"' || currentChar === "'"){
|
||||
else if (currentChar === '"' || currentChar === "'") {
|
||||
// String literal, skip to closing quote
|
||||
const quoteType = currentChar;
|
||||
leftCursor++;
|
||||
@@ -109,25 +114,101 @@ export abstract class CodeBlock {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(currentChar === "\n") {
|
||||
// In return block, read till the end of the return block
|
||||
// else if (currentBlock.blockType === BlockType.Return) {
|
||||
|
||||
// //Continue reading until there's at least a new word
|
||||
// }
|
||||
else if (currentChar === "\n") {
|
||||
// Process line
|
||||
currentBlockString = currentBlockString.trimEnd(); // Remove trailing whitespace
|
||||
const lineBlock = new LineBlock(lineCounter, currentBlockString, currentBlock);
|
||||
currentBlockString += '\n'; // Add the newline back for the line block
|
||||
if (currentBlockString !== '') {
|
||||
const lineBlock = new CodeTextBlock(lineCounter, currentBlockString, currentBlock);
|
||||
currentBlock.childBlocks.push(lineBlock);
|
||||
currentBlockString = '';
|
||||
currentWord = '';
|
||||
}
|
||||
}
|
||||
else if (currentChar === ")" && currentBlock.blockType === BlockType.Require) {
|
||||
currentBlockString = currentBlockString.trimEnd(); // Remove trailing whitespace
|
||||
currentBlockString += '\n'; // Add the newline back for the line block as it will be skipped otherwise
|
||||
const lineBlock = new CodeTextBlock(lineCounter, currentBlockString, currentBlock);
|
||||
currentBlock.childBlocks.push(lineBlock);
|
||||
currentBlockString = '';
|
||||
|
||||
if(currentBlock.blockType === BlockType.Return){
|
||||
// TODO: multi line return blocks
|
||||
//Return blocks don't end on end, but on a new line
|
||||
currentBlock = currentBlock.getParentBlock()!;
|
||||
}
|
||||
//Table blocks
|
||||
else if (currentChar === "{") {
|
||||
// Only create LineBlock if there's content before the brace
|
||||
if (currentBlockString.trimEnd() !== '' && currentBlockString.trimEnd() !== '{') {
|
||||
const currentLineBlock = new CodeTextBlock(lineCounter, currentBlockString.slice(0, -1).trimEnd(), currentBlock);
|
||||
currentBlock.childBlocks.push(currentLineBlock);
|
||||
}
|
||||
|
||||
let leadingWhiteSpace = '';
|
||||
const braceIndex = currentBlockString.lastIndexOf('{');
|
||||
if (braceIndex > 0) {
|
||||
let wsStart = braceIndex - 1;
|
||||
while (wsStart >= 0 && /[ \t]/.test(currentBlockString[wsStart])) {
|
||||
wsStart--;
|
||||
}
|
||||
leadingWhiteSpace = currentBlockString.substring(wsStart + 1, braceIndex);
|
||||
}
|
||||
currentBlockString = leadingWhiteSpace + '{'; // Start the new block string with the opening brace
|
||||
|
||||
const tableBlock = new TableBlock(lineCounter, currentBlock);
|
||||
currentBlock.childBlocks.push(tableBlock);
|
||||
currentBlock = tableBlock;
|
||||
|
||||
let braceCounter = 1;
|
||||
leftCursor++;
|
||||
while (leftCursor < fileContent.length && braceCounter > 0) {
|
||||
const char = fileContent[leftCursor];
|
||||
currentBlockString += char;
|
||||
if (char === '{') {
|
||||
braceCounter++;
|
||||
} else if (char === '}') {
|
||||
braceCounter--;
|
||||
}
|
||||
|
||||
if (char === '\n') {
|
||||
let block = currentBlockString.trimEnd();
|
||||
block += '\n';
|
||||
const lineBlock = new CodeTextBlock(lineCounter, block, currentBlock);
|
||||
currentBlock.childBlocks.push(lineBlock);
|
||||
currentBlockString = '';
|
||||
lineCounter++;
|
||||
}
|
||||
leftCursor++;
|
||||
}
|
||||
|
||||
// Find newline or other character
|
||||
let tempCursor = leftCursor;
|
||||
while (tempCursor < fileContent.length && fileContent[tempCursor] !== '\n' && fileContent[tempCursor].trim() === '') {
|
||||
currentBlockString += fileContent[tempCursor];
|
||||
tempCursor++;
|
||||
}
|
||||
|
||||
if (tempCursor < fileContent.length && fileContent[tempCursor] === '\n') {
|
||||
currentBlockString += '\n';
|
||||
lineCounter++;
|
||||
} else {
|
||||
leftCursor = tempCursor - 1; // Set leftCursor to the last character before the newline or non-whitespace character
|
||||
}
|
||||
|
||||
if (currentBlockString !== '') {
|
||||
const lineBlock = new CodeTextBlock(lineCounter, currentBlockString, currentBlock);
|
||||
currentBlock.childBlocks.push(lineBlock);
|
||||
currentBlockString = '';
|
||||
}
|
||||
|
||||
if(currentChar === ")" && currentBlock.blockType === BlockType.Require){
|
||||
currentBlock = currentBlock.getParentBlock()!;
|
||||
currentWord = ''; // Reset word accumulator after table block
|
||||
continue; // Skip the leftCursor++ at the end of the loop since we already incremented it
|
||||
}
|
||||
|
||||
if(nextChar.trim() === '' || nextChar === '('){
|
||||
else if (nextChar.trim() === '' || nextChar === '(') {
|
||||
// End of a word, check for keywords
|
||||
const trimmedWord = currentWord.trim();
|
||||
let blockToAdd: CodeBlock | null = null;
|
||||
@@ -144,6 +225,9 @@ export abstract class CodeBlock {
|
||||
blockToAdd = new DoBlock(lineCounter, currentBlock);
|
||||
}
|
||||
else if (trimmedWord === 'return') {
|
||||
const line = new CodeTextBlock(lineCounter, currentBlockString.trimEnd().slice(0, -trimmedWord.length), currentBlock);
|
||||
currentBlock.childBlocks.push(line);
|
||||
currentBlockString = 'return';
|
||||
blockToAdd = new ReturnBlock(lineCounter, currentBlock);
|
||||
}
|
||||
else if (trimmedWord === 'function') {
|
||||
@@ -159,8 +243,9 @@ export abstract class CodeBlock {
|
||||
line: fileContent.substring(0, leftCursor).split('\n').length - 1,
|
||||
message: "Unexpected 'end' without matching block start"
|
||||
});
|
||||
} else {
|
||||
currentBlock = parent;
|
||||
}
|
||||
currentBlock = currentBlock.getParentBlock()!
|
||||
}
|
||||
|
||||
if (blockToAdd) {
|
||||
@@ -176,10 +261,10 @@ export abstract class CodeBlock {
|
||||
}
|
||||
}
|
||||
|
||||
export class LineBlock extends CodeBlock {
|
||||
export class CodeTextBlock extends CodeBlock {
|
||||
|
||||
constructor(sourceLineNumber: number, private line: string, parent?: CodeBlock) {
|
||||
super(sourceLineNumber, BlockType.Line, parent);
|
||||
super(sourceLineNumber, BlockType.CodeTextBlock, parent);
|
||||
}
|
||||
|
||||
toLines(): string[] {
|
||||
@@ -278,6 +363,21 @@ export class ReturnBlock extends CodeBlock {
|
||||
}
|
||||
}
|
||||
|
||||
export class TableBlock extends CodeBlock {
|
||||
|
||||
constructor(sourceLineNumber: number, parent: CodeBlock) {
|
||||
super(sourceLineNumber, BlockType.Table, parent);
|
||||
}
|
||||
|
||||
toLines(): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const child of this.childBlocks) {
|
||||
lines.push(...child.toLines());
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
export class FunctionBlock extends CodeBlock {
|
||||
|
||||
constructor(sourceLineNumber: number, parent: CodeBlock) {
|
||||
@@ -312,8 +412,14 @@ export class RequireBlock extends CodeBlock {
|
||||
return '';
|
||||
}
|
||||
const firstChild = this.childBlocks[0];
|
||||
if(firstChild instanceof LineBlock){
|
||||
return firstChild.toLines()[0].trim().replace(/['"]/g, '');
|
||||
if (firstChild instanceof CodeTextBlock) {
|
||||
// local module = require("module") -> module
|
||||
// local module = require('module') -> module
|
||||
const line = firstChild.toLines()[0];
|
||||
const requireMatch = line.match(/require\s*\(\s*["']([^"']+)["']\s*\)/);
|
||||
if (requireMatch && requireMatch[1]) {
|
||||
return requireMatch[1];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -63,7 +63,9 @@ export class ScriptCompiler {
|
||||
const fullPath = path.join(entry.parentPath, entry.name);
|
||||
const relativePath = path.relative(this.options.sourcePath, fullPath);
|
||||
const luaFile = LuaFile.createFromFile(fullPath, this.options.onError);
|
||||
const parsedFile = new ParsedFile(fileReferenceToLuaVariable(relativePath), luaFile, fullPath);
|
||||
const luaReference = fileReferenceToLuaVariable(relativePath);
|
||||
const key = luaReference.replace(LUA_SCRIPT_GLOBAL_KEYWORD + '.', '').toLowerCase();
|
||||
const parsedFile = new ParsedFile(key, luaFile, fullPath);
|
||||
|
||||
parsedFiles.set(parsedFile.fileKey, parsedFile);
|
||||
metricsMeter.filesRead++;
|
||||
@@ -97,6 +99,7 @@ export class ScriptCompiler {
|
||||
|
||||
class Dependency {
|
||||
public readonly fileKey: string;
|
||||
public readonly luaReference: string;
|
||||
|
||||
constructor(
|
||||
public readonly requiredModule: string,
|
||||
@@ -105,7 +108,8 @@ class Dependency {
|
||||
public readonly charEnd: number
|
||||
)
|
||||
{
|
||||
this.fileKey = fileReferenceToLuaVariable(requiredModule);
|
||||
this.luaReference = fileReferenceToLuaVariable(requiredModule);
|
||||
this.fileKey = this.luaReference.replace(LUA_SCRIPT_GLOBAL_KEYWORD + '.', '').toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,8 +186,8 @@ class Writer {
|
||||
|
||||
private getStartLines(): string[] {
|
||||
return [
|
||||
`-- Transpiled at (UTC): ${new Date().toISOString()}`,
|
||||
`local ${LUA_SCRIPT_GLOBAL_KEYWORD} = {}`
|
||||
`-- Transpiled at (UTC): ${new Date().toISOString()}\n`,
|
||||
`local ${LUA_SCRIPT_GLOBAL_KEYWORD} = {}\n`
|
||||
];
|
||||
}
|
||||
|
||||
@@ -287,11 +291,11 @@ class Writer {
|
||||
}
|
||||
}
|
||||
|
||||
outputLines.push("do -- " + parsedFile.fileKey);
|
||||
outputLines.push("do -- " + parsedFile.fileKey + "\n");
|
||||
const lines = parsedFile.luaFile.toLines();
|
||||
metrics.totalLinesWritten += lines.length;
|
||||
outputLines.push(...lines.filter(line => line.trim() !== '')); // Filter out empty lines
|
||||
outputLines.push("end -- " + parsedFile.fileKey);
|
||||
outputLines.push("end -- " + parsedFile.fileKey + "\n");
|
||||
writtenFiles.add(parsedFile.fileKey);
|
||||
metrics.filesWritten++;
|
||||
};
|
||||
@@ -302,7 +306,7 @@ class Writer {
|
||||
}
|
||||
|
||||
fs.mkdirSync(path.dirname(this.location), { recursive: true });
|
||||
fs.writeFileSync(this.location, outputLines.join('\n'), 'utf-8');
|
||||
fs.writeFileSync(this.location, outputLines.join(''), 'utf-8');
|
||||
|
||||
if(includeDevScript) {
|
||||
const devFileLocation = this.location.replace('.lua', '.dev.lua');
|
||||
|
||||
Reference in New Issue
Block a user