Skip to content

Commit

Permalink
symbols (#86)
Browse files Browse the repository at this point in the history
* symbols

* fix struct

* fix check for assignment

* clean up

* clean up

* fix analyze

* improve API

* refactor

* improve analyze
  • Loading branch information
marihachi committed Aug 31, 2023
1 parent 5f5dcb1 commit 20c067a
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 318 deletions.
13 changes: 12 additions & 1 deletion src/bin/uguisu/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ export function command(args: string[]) {
// lint
try {
const uguisu = new Uguisu();
uguisu.check(dirPath);
const result = uguisu.check(dirPath);

if (!result.success) {
for (const message of result.errors) {
console.log(`Syntax Error: ${message}`);
}
for (const warn of result.warnings) {
console.log(`Warning: ${warn}`);
}
process.exitCode = -1;
process.exit();
}
}
catch (e) {
console.log(e);
Expand Down
13 changes: 12 additions & 1 deletion src/bin/uguisu/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,18 @@ export function command(args: string[]) {
console.log(str);
}
});
uguisu.run(dirPath, { skipCheck: match.skipCheck });
const result = uguisu.run(dirPath, { skipCheck: match.skipCheck });

if (!result.success) {
for (const message of result.errors) {
console.log(`Syntax Error: ${message}`);
}
for (const warn of result.warnings) {
console.log(`Warning: ${warn}`);
}
process.exitCode = -1;
process.exit();
}
}
catch (e) {
console.log(e);
Expand Down
33 changes: 21 additions & 12 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export {
UguisuError
};

export class UguisuResult {
constructor(
public success: boolean,
public warnings: string[],
public errors: string[],
) { }
}

export class Uguisu {
private _options: UguisuOptions;

Expand All @@ -33,8 +41,8 @@ export class Uguisu {
* @throws TypeError (Invalid arguments)
* @throws UguisuError
*/
check(dirPath: string) {
this._perform(dirPath, {
check(dirPath: string): UguisuResult {
return this._perform(dirPath, {
check: true,
run: false,
});
Expand All @@ -44,16 +52,16 @@ export class Uguisu {
* @throws TypeError (Invalid arguments)
* @throws UguisuError
*/
run(dirPath: string, opts?: { skipCheck?: boolean }) {
run(dirPath: string, opts?: { skipCheck?: boolean }): UguisuResult {
opts = opts ?? {};
const skipCheck = opts.skipCheck ?? false;
this._perform(dirPath, {
return this._perform(dirPath, {
check: !skipCheck,
run: true,
});
}

private _perform(dirPath: string, tasks: { check: boolean, run: boolean }) {
private _perform(dirPath: string, tasks: { check: boolean, run: boolean }): UguisuResult {
if (typeof dirPath != 'string') {
throw new TypeError('Invalid arguments.');
}
Expand Down Expand Up @@ -93,19 +101,18 @@ export class Uguisu {
// parse
const sourceFile = parse(sourceCode, scriptFilePath, projectInfo);

let checkResult = new UguisuResult(true, [], []);

// static analysis
if (tasks.check) {
const analysisEnv = new AnalysisEnv();
const symbolTable = new Map();
const result = analyze(sourceFile, analysisEnv, symbolTable, projectInfo);
for (const message of result.errors) {
console.log(`Syntax Error: ${message}`);
}
for (const warn of result.warnings) {
console.log(`Warning: ${warn}`);
}
checkResult.success = result.success;
checkResult.errors = result.errors;
checkResult.warnings = result.warnings;
if (!result.success) {
throw new UguisuError('Syntax error.');
return checkResult;
}
}

Expand All @@ -114,5 +121,7 @@ export class Uguisu {
const runningEnv = new RunningEnv();
run(sourceFile, runningEnv, this._options, projectInfo);
}

return checkResult;
}
}
Loading

0 comments on commit 20c067a

Please sign in to comment.