Skip to content

Commit

Permalink
fix: base error handling for Kin
Browse files Browse the repository at this point in the history
  • Loading branch information
pacifiquem committed May 16, 2024
1 parent 24af508 commit a9f5f67
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
4 changes: 3 additions & 1 deletion bin/kin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ program
if (error.code === 'ENOENT') {
LogError(`Kin Error: Can't resolve file at '${file_location}'`);
} else {
LogError(`Kin Error: Unhandled : ${(error as Error).message}`);
(error as Error).message
? LogError(`Kin Error: Unhandled : ${(error as Error).message}`)
: LogError(`Kin Error: Unhandled : ${error as Error}`);
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const LogMessage = console.log;
export const LogError = (...args: unknown[]) => {
console.error(...args);
process.exit(1);
// throw new Error(...args.map((arg: unknown) => (arg as any).toString()).join(' '));
};
7 changes: 5 additions & 2 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export default class Parser {
if (this.at().type == TokenType.SEMI_COLON) {
this.eat();

if (isConstant) throw 'Constant variables must be assigned a value';
if (isConstant)
throw new Error('Constant variables must be assigned a value');

return {
kind: 'VariableDeclaration',
Expand Down Expand Up @@ -323,7 +324,9 @@ export default class Parser {
property = this.parse_primary_expr();

if (property.kind !== 'Identifier') {
throw 'Dot operator (".") is illegal without right-hand-side (<-) being an Identifier.';
throw new Error(
'Dot operator (".") is illegal without right-hand-side (<-) being an Identifier.',
);
}
} // computed values (obj[computedVal])
else {
Expand Down
10 changes: 7 additions & 3 deletions src/runtime/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class Environment {
constant: boolean,
): RuntimeVal {
if (this.variables.has(varname)) {
throw `Cannot declare variable ${varname}. As it already is defined.`;
throw new Error(
`Cannot declare variable ${varname}. As it already is defined.`,
);
}

this.variables.set(varname, value);
Expand All @@ -40,7 +42,9 @@ export default class Environment {

// Cannot assign to constant
if (env.constants.has(varname)) {
throw `Cannot reassign to variable "${varname}" as it's constant.`;
throw new Error(
`Cannot reassign to variable "${varname}" as it's constant.`,
);
}

env.variables.set(varname, value);
Expand Down Expand Up @@ -117,7 +121,7 @@ export default class Environment {
if (this.variables.has(varname)) return this;

if (this.parent == undefined)
throw `Cannot resolve '${varname}' as it does not exist.`;
throw new Error(`Cannot resolve '${varname}' as it does not exist.`);

return this.parent.resolve(varname);
}
Expand Down
20 changes: 15 additions & 5 deletions src/runtime/eval/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export default class EvalExpr {
if (node.assigne.kind === 'MemberExpression')
return this.eval_member_expr(env, node);
if (node.assigne.kind !== 'Identifier')
throw `Invalid left-hand-side expression: ${JSON.stringify(node.assigne)}.`;
throw new Error(
`Invalid left-hand-side expression: ${JSON.stringify(node.assigne)}.`,
);

const varname = (node.assigne as Identifier).symbol;

Expand Down Expand Up @@ -147,7 +149,9 @@ export default class EvalExpr {
return result;
}

throw 'Cannot call value that is not a function: ' + JSON.stringify(fn);
throw new Error(
'Cannot call value that is not a function: ' + JSON.stringify(fn),
);
}

public static eval_return_expr(
Expand Down Expand Up @@ -179,7 +183,9 @@ export default class EvalExpr {

return variable;
} else {
throw `Evaluating a member expression is not possible without a member or assignment expression.`;
throw new Error(
`Evaluating a member expression is not possible without a member or assignment expression.`,
);
}
}

Expand Down Expand Up @@ -228,7 +234,9 @@ export default class EvalExpr {
case '>=':
return MK_BOOL(llhs.value >= rrhs.value);
default:
throw `Unknown operator provided in operation: ${lhs}, ${rhs}.`;
throw new Error(
`Unknown operator provided in operation: ${lhs}, ${rhs}.`,
);
}
} else {
return MK_NULL();
Expand Down Expand Up @@ -272,7 +280,9 @@ export default class EvalExpr {
compare((lhs as ObjectVal).properties, (rhs as ObjectVal).properties),
);
default:
throw `RunTime: Unhandled type in equals function: ${lhs}, ${rhs}`;
throw new Error(
`RunTime: Unhandled type in equals function: ${lhs}, ${rhs}`,
);
}
}
}
8 changes: 4 additions & 4 deletions src/runtime/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export function createGlobalEnv(filename: string): Environment {
try {
const result = execSync(cmd, { encoding: 'utf-8' });
return MK_STRING(result.trim());
} catch (error) {
throw error;
} catch (error: unknown) {
throw new Error((error as Error).toString());
}
}),
true,
Expand All @@ -86,8 +86,8 @@ export function createGlobalEnv(filename: string): Environment {
} else {
return MK_NULL();
}
} catch (error) {
throw error;
} catch (error: unknown) {
throw new Error((error as Error).toString());
}
}),
true,
Expand Down

0 comments on commit a9f5f67

Please sign in to comment.