Skip to content

Commit

Permalink
better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Mar 22, 2024
1 parent 868f00e commit 7993239
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
29 changes: 28 additions & 1 deletion src/error/error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import chalk from "chalk";
import { Range } from "../ast/Range";
import { TokenData } from "../tokenizer/tokendata";
import { Program } from "../ast/Program";

export enum ErrorTypes {
TokenMismatch,
TypeError,
SyntaxError
}

export class CompileTimeError {
Expand Down Expand Up @@ -34,3 +35,29 @@ export class TokenMismatchError extends CompileTimeError {
);
}
}

export class SyntaxError extends CompileTimeError {
public range: Range;
constructor(program: Program, prefix: string,message: string, code: number, range: Range, action: "FAIL" | "WARN" | "INFO") {
super(message, ErrorTypes.SyntaxError, code);
this.range = range;
let color;
if (action === "FAIL") {
color = chalk.bold.red;
} else if (action === "WARN") {
color = chalk.bold.yellowBright;
} else {
color = chalk.bold.blue;
}
console.log(color(prefix) + chalk.grey(":") + " " + message);
console.log(
" " +
chalk.cyan("test.zp") +
chalk.gray(":") +
chalk.cyan(this.range.line) +
chalk.gray(":") +
chalk.cyan(this.range.start),
);
if (action === "FAIL") process.exit(0);
}
}
29 changes: 25 additions & 4 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { NumberLiteral } from "../ast/nodes/NumberLiteral.js";
import { FunctionImport } from "../ast/nodes/FunctionImport.js";
import { ModifierExpression } from "../ast/nodes/ModifierExpression.js";
import { ReferenceExpression } from "../ast/nodes/ReferenceExpression.js";
import { TokenMismatchError } from "../error/error.js";
import { CompileTimeError, ErrorTypes, SyntaxError, TokenMismatchError } from "../error/error.js";
import { Range } from "../ast/Range.js";
import { Token } from "../tokenizer/token.js";
import { TokenData } from "../tokenizer/tokendata.js";
Expand Down Expand Up @@ -119,6 +119,10 @@ export class Parser {
state.resume();
if ((node = this.parseModifierExpression(scope))) return node;
state.resume();
if ((node = this.parseVariableDeclaration(scope))) return node;
state.resume();
if ((node = this.parseFunctionDeclaration(scope))) return node;
state.resume();
//if ((node = this.parseIdentifierExpression(scope))) return node;
//state.resume();
return null;
Expand All @@ -135,7 +139,7 @@ export class Parser {
if (!isIdentifier(name)) return null;
this.tokenizer.getToken(); // =

const value = this.parseExpression(); // Expression
const value = this.parseExpression(scope, "IdentifierExpression"); // Expression
if (!value) {
const value = this.tokenizer.getToken();
new TokenMismatchError(
Expand All @@ -154,7 +158,6 @@ export class Parser {
mutable,
);

this.program.statements.push(node);
scope.add(name.text, node);
return node;
}
Expand Down Expand Up @@ -274,7 +277,25 @@ export class Parser {
exported
);

if (exported && scope.parentScope) throw new Error("Exported functions must occur at the global scope!");
if (scope.parentScope) {
if (exported) {
new SyntaxError(
this.program,
"Warn", "Exported functions must occur at the global scope! Not exporting function and moving on.",
0x1,
fn.range,
"WARN"
);
} else {
new SyntaxError(
this.program,
"Error", "Closures not yet supported!",
0x2,
fn.range,
"FAIL"
);
}
}
this.program.globalScope.add(name.text, node);
this.program.topLevelStatements.push(node);
return node;
Expand Down
9 changes: 6 additions & 3 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ const start = Date.now();
const tokenizer = new Tokenizer(`
#[export]: add
fn add(a: i32, b: i32) -> i32 {
rt a + b
#[export]: main
fn main() -> none {}
i32? c = a + b
rt c
}
`);

console.log(tokenizer.getAll());
const parser = new Parser(tokenizer, "test.zp");
const program = parser.parseProgram();
console.dir(program, { depth: null });

//console.dir(program, { depth: null });
console.log(program);
console.log("Transpiled:\n" + Transpile.from(program));
2 changes: 2 additions & 0 deletions test.zp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
i32 d = asdfsdc
rt c

0 comments on commit 7993239

Please sign in to comment.