Skip to content

Commit

Permalink
improve API
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 31, 2023
1 parent 358ca98 commit 557593c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 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;
}
}
2 changes: 1 addition & 1 deletion src/lib/semantics/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class AnalyzeContext {

dispatchError(message: string, errorNode?: SyntaxNode) {
if (errorNode != null) {
this.error.push(`${message} (${errorNode.pos[0]}:${errorNode.pos[1]})`);
this.error.push(`${message} (${errorNode.pos[0]}:${errorNode.pos[1]}) ErrorNode=${errorNode.kind}`);
} else {
this.error.push(message);
}
Expand Down

0 comments on commit 557593c

Please sign in to comment.