-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: very basic type computer (#596)
Closes partially #541 ### Summary of Changes Add a very basic implementation of a type computer that can only deal with literals and template strings. It does, however, lay the groundwork to implement more complex features later, by adding a system to access builtin classes like `String` and registering the type computer as an added service. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
928b520
commit b3d786c
Showing
48 changed files
with
1,258 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import path from 'path'; | ||
import { URI } from 'langium'; | ||
import { SAFE_DS_FILE_EXTENSIONS } from '../helpers/fileExtensions.js'; | ||
import { globSync } from 'glob'; | ||
|
||
let builtinsPath: string; | ||
if (__filename.endsWith('.ts')) { | ||
// Before running ESBuild | ||
builtinsPath = path.join(__dirname, '..', '..', 'resources', 'builtins'); | ||
} /* c8 ignore start */ else { | ||
// After running ESBuild | ||
builtinsPath = path.join(__dirname, '..', 'resources', 'builtins'); | ||
} /* c8 ignore stop */ | ||
|
||
/** | ||
* Lists all Safe-DS files in `src/resources/builtins`. | ||
* | ||
* @return URIs of all discovered files. | ||
*/ | ||
export const listBuiltinsFiles = (): URI[] => { | ||
const pattern = `**/*.{${SAFE_DS_FILE_EXTENSIONS.join(',')}}`; | ||
const relativePaths = globSync(pattern, { cwd: builtinsPath, nodir: true }); | ||
return relativePaths.map((relativePath) => { | ||
const absolutePath = path.join(builtinsPath, relativePath); | ||
return URI.file(absolutePath); | ||
}); | ||
}; | ||
|
||
/** | ||
* Resolves a relative path to a builtin file. | ||
* | ||
* @param relativePath | ||
*/ | ||
export const resolveRelativePathToBuiltinFile = (relativePath: string): URI => { | ||
const absolutePath = path.join(builtinsPath, relativePath); | ||
return URI.file(absolutePath); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { SafeDsServices } from '../safe-ds-module.js'; | ||
import { resolveRelativePathToBuiltinFile } from './fileFinder.js'; | ||
import { isSdsClass, isSdsModule, SdsClass } from '../generated/ast.js'; | ||
import { LangiumDocuments } from 'langium'; | ||
import { moduleMembersOrEmpty } from '../helpers/shortcuts.js'; | ||
|
||
const CORE_CLASSES_URI = resolveRelativePathToBuiltinFile('safeds/lang/coreClasses.sdsstub'); | ||
|
||
export class SafeDsCoreClasses { | ||
private readonly langiumDocuments: LangiumDocuments; | ||
|
||
constructor(services: SafeDsServices) { | ||
this.langiumDocuments = services.shared.workspace.LangiumDocuments; | ||
} | ||
|
||
private cachedAny: SdsClass | undefined; | ||
|
||
/* c8 ignore start */ | ||
get Any(): SdsClass | undefined { | ||
if (!this.cachedAny) { | ||
this.cachedAny = this.getClass('Any'); | ||
} | ||
return this.cachedAny; | ||
} | ||
|
||
private cachedBoolean: SdsClass | undefined; | ||
/* c8 ignore stop */ | ||
|
||
get Boolean(): SdsClass | undefined { | ||
if (!this.cachedBoolean) { | ||
this.cachedBoolean = this.getClass('Boolean'); | ||
} | ||
return this.cachedBoolean; | ||
} | ||
|
||
private cachedFloat: SdsClass | undefined; | ||
|
||
get Float(): SdsClass | undefined { | ||
if (!this.cachedFloat) { | ||
this.cachedFloat = this.getClass('Float'); | ||
} | ||
return this.cachedFloat; | ||
} | ||
|
||
private cachedInt: SdsClass | undefined; | ||
|
||
get Int(): SdsClass | undefined { | ||
if (!this.cachedInt) { | ||
this.cachedInt = this.getClass('Int'); | ||
} | ||
return this.cachedInt; | ||
} | ||
|
||
private cachedNothing: SdsClass | undefined; | ||
|
||
get Nothing(): SdsClass | undefined { | ||
if (!this.cachedNothing) { | ||
this.cachedNothing = this.getClass('Nothing'); | ||
} | ||
return this.cachedNothing; | ||
} | ||
|
||
private cachedString: SdsClass | undefined; | ||
|
||
get String(): SdsClass | undefined { | ||
if (!this.cachedString) { | ||
this.cachedString = this.getClass('String'); | ||
} | ||
return this.cachedString; | ||
} | ||
|
||
private getClass(name: string): SdsClass | undefined { | ||
if (!this.langiumDocuments.hasDocument(CORE_CLASSES_URI)) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const document = this.langiumDocuments.getOrCreateDocument(CORE_CLASSES_URI); | ||
const root = document.parseResult.value; | ||
if (!isSdsModule(root)) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
const firstMatchingModuleMember = moduleMembersOrEmpty(root).find((m) => m.name === name); | ||
if (!isSdsClass(firstMatchingModuleMember)) { | ||
/* c8 ignore next 2 */ | ||
return undefined; | ||
} | ||
|
||
return firstMatchingModuleMember; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.