Skip to content

Commit

Permalink
Multi-file improvements + tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy9 committed Jan 13, 2024
1 parent 7c2e0ab commit a10fdcf
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 148 deletions.
14 changes: 0 additions & 14 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
"outFiles": ["${workspaceRoot}/client/out/**/*.js", "${workspaceRoot}/server/out/**/*.js"],
"sourceMaps": true,
"autoAttachChildProcesses": true,
"preLaunchTask": {
"type": "npm",
"script": "watch"
}
},
{
"name": "ESBuild Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
// "outFiles": ["${workspaceRoot}/out/main.js", "${workspaceRoot}/out/server.js"],
"sourceMaps": true,
"autoAttachChildProcesses": true,
Expand Down
2 changes: 1 addition & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function createTargetCommand(): void
// set as the default build and test target if this is the first task
// TODO: check if other targets already exist, but are not set as the default build? Can do this with target select.
if (tasks.length === 1) {
setCurrentTarget(tasksObject, label, selection);
setCurrentTarget(tasksObject, label, target);
}

// write the new tasks.json file
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@
"vscode:prepublish": "npm-run-all --parallel \"esbuild-base -- --minify\" \"esbuild-server -- --minify\"",
"esbuild-base": "esbuild ./client/src/extension.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"esbuild-server": "esbuild ./server/src/server.ts --bundle --outfile=out/server.js --external:vscode --format=cjs --platform=node",
"compile-for-tests": "tsc -b -w",
"esbuild": "npm-run-all --parallel \"esbuild-base -- --sourcemap\" \"esbuild-server -- --sourcemap\"",
"esbuild-watch": "npm-run-all --parallel \"esbuild-base -- --sourcemap --watch\" \"esbuild-server -- --sourcemap --watch\"",
"esbuild-watch": "npm-run-all --parallel \"esbuild-base -- --sourcemap --watch\" \"esbuild-server -- --sourcemap --watch\" \"compile-for-tests\"",
"lint": "eslint ./client/src ./server/src --ext .ts,.tsx",
"postinstall": "cd client && npm install && cd ../server && npm install && cd .."
},
Expand Down
1 change: 1 addition & 0 deletions server/src/beebasm-ts/lineparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,7 @@ export class LineParser {
}
}
}
this._currentAST.children.push(this._ASTValueStack[0]);
}
else {
this.EatWhitespace();
Expand Down
134 changes: 44 additions & 90 deletions server/src/beebasm-ts/sourcecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,91 +83,6 @@ export class SourceCode {
this._uri = uri;
}

// void SourceCode::Process()
// {
// // Remember the FOR and IF stack initial pointer values

// m_initialForStackPtr = m_forStackPtr;
// m_initialIfStackPtr = m_ifStackPtr;

// // Iterate through the file line-by-line

// string lineFromFile;

// while ( GetLine( lineFromFile ) )
// {
// // Convert tabs to spaces

// StringUtils::ExpandTabsToSpaces( lineFromFile, 8 );

// try
// {
// LineParser thisLine( this, lineFromFile );
// thisLine.Process();
// }
// catch ( AsmException_SyntaxError& e )
// {
// // Augment exception with more details
// e.SetFilename( m_filename );
// e.SetLineNumber( m_lineNumber );
// throw;
// }

// m_lineNumber++;
// m_lineStartPointer = GetFilePointer();
// }

// // Check whether we aborted prematurely

// if ( !IsAtEnd() )
// {
// throw AsmException_FileError_ReadSourceFile( m_filename );
// }

// // Check that we have no FOR / braces mismatch

// if ( m_forStackPtr != m_initialForStackPtr )
// {
// For& mismatchedFor = m_forStack[ m_forStackPtr - 1 ];

// if ( mismatchedFor.m_step == 0.0 )
// {
// AsmException_SyntaxError_MismatchedBraces e( mismatchedFor.m_line, mismatchedFor.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedFor.m_lineNumber );
// throw e;
// }
// else
// {
// AsmException_SyntaxError_ForWithoutNext e( mismatchedFor.m_line, mismatchedFor.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedFor.m_lineNumber );
// throw e;
// }
// }

// // Check that we have no IF / MACRO mismatch

// if ( m_ifStackPtr != m_initialIfStackPtr )
// {
// If& mismatchedIf = m_ifStack[ m_ifStackPtr - 1 ];

// if ( mismatchedIf.m_isMacroDefinition )
// {
// AsmException_SyntaxError_NoEndMacro e( mismatchedIf.m_line, mismatchedIf.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedIf.m_lineNumber );
// throw e;
// }
// else
// {
// AsmException_SyntaxError_IfWithoutEndif e( mismatchedIf.m_line, mismatchedIf.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedIf.m_lineNumber );
// throw e;
// }
// }
// }
Process() {
// Remember the FOR and IF stack initial pointer values
this._initialForStackPtr = this._forStackPtr;
Expand Down Expand Up @@ -232,10 +147,49 @@ export class SourceCode {

this._trees.set(this._uri, trees);
//Validation routines
//TODO - check whether we aborted prematurely
// // Check that we have no FOR / braces mismatch
// if ( m_forStackPtr != m_initialForStackPtr )
// {
// For& mismatchedFor = m_forStack[ m_forStackPtr - 1 ];
// if ( mismatchedFor.m_step == 0.0 )
// {
// AsmException_SyntaxError_MismatchedBraces e( mismatchedFor.m_line, mismatchedFor.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedFor.m_lineNumber );
// throw e;
// }
// else
// {
// AsmException_SyntaxError_ForWithoutNext e( mismatchedFor.m_line, mismatchedFor.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedFor.m_lineNumber );
// throw e;
// }
// }
// // Check that we have no IF / MACRO mismatch
// if ( m_ifStackPtr != m_initialIfStackPtr )
// {
// If& mismatchedIf = m_ifStack[ m_ifStackPtr - 1 ];
// if ( mismatchedIf.m_isMacroDefinition )
// {
// AsmException_SyntaxError_NoEndMacro e( mismatchedIf.m_line, mismatchedIf.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedIf.m_lineNumber );
// throw e;
// }
// else
// {
// AsmException_SyntaxError_IfWithoutEndif e( mismatchedIf.m_line, mismatchedIf.m_column );
// e.SetFilename( m_filename );
// e.SetLineNumber( mismatchedIf.m_lineNumber );
// throw e;
// }
// }
// }

//TODO - Check that we have no FOR / braces mismatch
//TODO - Check that we have no IF / MACRO mismatch

// NB - don't throw, just create a diagnostic
}

GetDiagnostics(): Map<string, Diagnostic[]> {
Expand Down Expand Up @@ -446,7 +400,7 @@ export class SourceCode {
lineNumber: this._lineNumber,
firstPass: true
};
SymbolTable.Instance.PushBrace(this._lineNumber, this._forStack[this._forStackPtr].id);
SymbolTable.Instance.PushBrace(this._uri, this._lineNumber, this._forStack[this._forStackPtr].id);
this._forStackPtr++;
}

Expand Down Expand Up @@ -489,7 +443,7 @@ export class SourceCode {
const symbolname = varName + this.GetSymbolNameSuffix();
this._forStack[this._forStackPtr-1].varName = symbolname; // Update for stack to include suffix
SymbolTable.Instance.AddSymbol(symbolname, start, loc);
SymbolTable.Instance.PushFor(symbolname, start, this._lineNumber, this._forStack[this._forStackPtr-1].id);
SymbolTable.Instance.PushFor(symbolname, start, this._uri, this._lineNumber, this._forStack[this._forStackPtr-1].id);
}

UpdateFor(line: string, column: number):void {
Expand Down Expand Up @@ -517,7 +471,7 @@ export class SourceCode {
SymbolTable.Instance.ChangeSymbol(thisFor.varName, thisFor.current);
this.SetLinePointer( thisFor.filePtr );
SymbolTable.Instance.PopScope();
SymbolTable.Instance.PushFor(thisFor.varName, thisFor.current);
SymbolTable.Instance.PushFor(thisFor.varName, thisFor.current, this._uri);
thisFor.count++;
// If parsing NEXT statement for a second time then no longer in first loop
if (thisFor.count > 1 || (thisFor.count === 1 && thisFor.lineNumber !== this._lineNumber)) {
Expand Down
19 changes: 8 additions & 11 deletions server/src/beebasm-ts/symboltable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ type Label = {
}

type ScopeDetails = {
uri: string;
startLine: integer;
endLine: integer;
}

const noLocation = { uri: "", range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } } };

// TODO: Position data should be scope i.e. global, block, macro or for loop (any others?)

export class SymbolTable {
private static _instance: SymbolTable;
private _labelScopes: number; // Just like For stack index?
Expand Down Expand Up @@ -70,13 +69,14 @@ export class SymbolTable {
return filtered;
}

GetSymbolByLine(symbolname: string, line: number): [SymbolData | undefined, string] {
GetSymbolByLine(symbolname: string, uri: string, line: number): [SymbolData | undefined, string] {
// Relies on scopeLevel matching ForScopePtr when each scopeDetail was created
let search = "";
for ( let scopeLevel = this._labelScopes - 1; scopeLevel >= 0; scopeLevel-- ) {
if ( scopeLevel < this._scopeDetails.length
&& line >= this._scopeDetails[scopeLevel].startLine
&& line <= this._scopeDetails[scopeLevel].endLine ) {
&& line <= this._scopeDetails[scopeLevel].endLine
&& this._scopeDetails[scopeLevel].uri === uri ) {
search = `@${scopeLevel}_0` + search;
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export class SymbolTable {
// TODO - check if can add symbol on second pass, currently getting deleted after first pass and not re-added
}

PushBrace(startLine: number, forID: number): void {
PushBrace(uri: string, startLine: number, forID: number): void {
if (GlobalData.Instance.IsSecondPass()) {
const addr = ObjectCode.Instance.GetPC();
if (this._lastLabel!._addr !== addr) {
Expand All @@ -173,9 +173,8 @@ export class SymbolTable {
this._lastLabel!._addr = addr;
}
this._lastLabel!._scope = this._labelScopes;
this._scopeDetails.push({ startLine: startLine, endLine: -1 });
this._scopeDetails.push({uri: uri, startLine: startLine, endLine: -1 });
this._labelScopes++;
// console.log("PushBrace: " + forID + " " + startLine);
this._labelStack.push(this._lastLabel!);
}
}
Expand All @@ -187,11 +186,10 @@ export class SymbolTable {
if ( forID !== -1 ) {
this._scopeDetails[forID].endLine = endLine;
}
// console.log("PopScope: " + forID + " " + endLine);
}
}

PushFor(symbol: string, value: number, startLine = -1, forID = -1): void {
PushFor(symbol: string, value: number, uri: string, startLine = -1, forID = -1): void {
if (GlobalData.Instance.IsSecondPass()) {
const addr = ObjectCode.Instance.GetPC();
symbol = symbol.substr(0, symbol.indexOf('@'));
Expand All @@ -200,9 +198,8 @@ export class SymbolTable {
this._lastLabel!._addr = addr;
this._lastLabel!._scope = this._labelScopes;
if ( startLine !== -1) {
this._scopeDetails.push({ startLine: startLine, endLine: -1 });
this._scopeDetails.push({ uri: uri, startLine: startLine, endLine: -1 });
}
// console.log("PushFor: " + forID + " " + startLine);
this._labelScopes++;
this._labelStack.push(this._lastLabel!);
}
Expand Down
8 changes: 6 additions & 2 deletions server/src/hoverprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class HoverProvider {
}

onHover(params: HoverParams): Hover | null {
const docTrees = this.trees.get(URI.parse(params.textDocument.uri).fsPath);
const fspath = URI.parse(params.textDocument.uri).fsPath;
const docTrees = this.trees.get(fspath);
const location = params.position;
if (docTrees !== undefined) {
const lineTree = docTrees[location.line];
Expand Down Expand Up @@ -72,11 +73,14 @@ Return: ${cmd.return}`
const symbol = SymbolOrLabel(lineTree, location.character);
if (symbol !== '') {

const [defn, _] = SymbolTable.Instance.GetSymbolByLine(symbol, location.line);
const [defn, _] = SymbolTable.Instance.GetSymbolByLine(symbol, fspath, location.line);
if (defn === undefined) {
return null;
}
const loc = defn.GetLocation();
if (loc.uri === '') {
return null; // Built in constant - could have special info here documenting P%, CPU etc.
}
const document = FileHandler.Instance.GetDocumentText(URI.parse(loc.uri).fsPath);
if (document === undefined) {
return null;
Expand Down
9 changes: 3 additions & 6 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ connection.onInitialized(() => {
});


// TODO - base settings on beebasm command line options
// TODO - base settings on beebasm command line options if any are suitable for this extension
interface ExampleSettings {
maxNumberOfProblems: number;
}
Expand Down Expand Up @@ -221,7 +221,8 @@ const renameProvider = new RenameProvider();
connection.onPrepareRename(renameProvider.onPrepareRename.bind(renameProvider));
connection.onRenameRequest(renameProvider.onRename.bind(renameProvider));

// TODO - add document link provider for INCBIN, PUTBASIC, PUTTEXT, PUTFILE statements?
// TODO - add document link provider for INCBIN, PUTBASIC, PUTTEXT, PUTFILE statements?
// This extension doesn't support the file types but could still link to them and leave it to the user
connection.onDocumentLinks((params) => {
const doc = URI.parse(params.textDocument.uri).fsPath;
const docLinks = links.get(doc);
Expand All @@ -231,13 +232,9 @@ connection.onDocumentLinks((params) => {
return [];
});

// TODO - update hover handler to include
// - symbol information (copy whole line to include any comments? maybe location (file & line))
const hoverHandler = new HoverProvider(trees);
connection.onHover(hoverHandler.onHover.bind(hoverHandler));

// TODO - extend code completion to include labels and symbols

// Make the text document manager listen on the connection
// for open, change and close text document events
FileHandler.Instance.documents.listen(connection);
Expand Down
Loading

0 comments on commit a10fdcf

Please sign in to comment.