Skip to content

Commit

Permalink
add formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Nov 12, 2024
1 parent ea58ebf commit e3de8dd
Show file tree
Hide file tree
Showing 31 changed files with 663 additions and 754 deletions.
Binary file modified bun.lockb
Binary file not shown.
16 changes: 16 additions & 0 deletions src/ast/Program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Parser } from "../parser";
import { Source, SourceKind } from "./Source";

export class Program {
public sources: Source[];
public entry!: Source;
constructor(sources: Source[]) {
this.sources = sources;
const entrySource = this.sources.filter(v => v.sourceKind == SourceKind.UserEntry);
if (entrySource.length > 1) throw new Error("Cannot have more than two entry files!");
for (const source of this.sources) {
if (source.sourceKind === SourceKind.UserEntry) this.entry = source;
source.parser = new Parser(source, this);
}
}
}
28 changes: 24 additions & 4 deletions src/ast/Source.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import { Scope } from "../checker/scope/Scope.js";
import { Parser } from "../parser/index.js";
import { Tokenizer } from "../tokenizer/index.js";
import { Declaration } from "./nodes/DoStatement.js";
import { NodeKind } from "./nodes/Node.js";
import { Statement } from "./nodes/Statement.js";

export class Source {
public kind = NodeKind.Source;
public sourceKind: SourceKind;
public fileName: string;
public tokenizer: Tokenizer;
public parser!: Parser;

public topLevelStatements: Statement[] = [];
public statements: Statement[] = [];
public globalScope: Scope = new Scope();
constructor(
public name: string,
statements: Statement[] | null = null,
topLevelStatements: Statement[] | null = null,
fileName: string,
text: string,
sourceKind: SourceKind
) {
if (statements) this.statements = statements;
this.fileName = fileName;
this.tokenizer = new Tokenizer(text);
this.sourceKind = sourceKind;
}
parse(): Source {
this.parser.parseSource();
return this;
}
}

export enum SourceKind {
User = 1,
UserEntry = 2,
Library = 3,
LibraryEntry = 4
}
1 change: 1 addition & 0 deletions src/ast/nodes/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class BinaryExpression extends Expression {
export enum Operator {
Add = "+",
Sub = "-",
Mul = "*",
Mod = "%",
Equals = "=",
EqualsEquals = "==",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Scope } from "../../checker/scope/Scope.js";
import { Range } from "../Range.js";
import { Expression } from "./Expression.js";
import { Statement } from "./Statement.js";

export class BlockExpression extends Expression {
public nameOf: string = "BlockExpression";
export class BlockExpression extends Statement {
public nameOf: string = "BlockStatement";
public statements: Statement[];
public scope: Scope;
constructor(statements: Statement[], scope: Scope, range: Range) {
Expand Down
Empty file added src/ast/nodes/Declaration.ts
Empty file.
22 changes: 7 additions & 15 deletions src/ast/nodes/DoStatement.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { Range } from "../Range";
import { BlockExpression } from "./BlockExpression";
import { Expression } from "./Expression.js";
import { Statement } from "./Statement.js";
import { EnumDeclaration } from "./EnumDeclaration";
import { FunctionDeclaration } from "./FunctionDeclaration";
import { FunctionImportDeclaration } from "./FunctionImportDeclaration";
import { ImportDeclaration } from "./ImportDeclaration";
import { ImportFromDeclaration } from "./ImportFromDeclaration";
import { VariableDeclaration } from "./VariableDeclaration";

export class DoStatement extends Statement {
public nameOf: string = "DoStatement";
public condition: Expression;
public block: BlockExpression;
constructor(condition: Expression, block: BlockExpression, range: Range) {
super();
this.condition = condition;
this.block = block;
this.range = range;
}
}
export type Declaration = FunctionDeclaration | FunctionImportDeclaration | VariableDeclaration | ImportDeclaration | ImportFromDeclaration | EnumDeclaration;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Scope } from "../../checker/scope/Scope.js";
import { Range } from "../Range.js";
import { BlockExpression } from "./BlockExpression.js";
import { BlockExpression } from "./BlockStatement.js";
import { Identifier } from "./Identifier.js";
import { ParameterExpression } from "./ParameterExpression.js";
import { Statement } from "./Statement.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ParameterExpression } from "./ParameterExpression.js";
import { Statement } from "./Statement.js";
import { TypeExpression } from "./TypeExpression.js";

export class FunctionImport extends Statement {
public nameOf: string = "FunctionImport";
export class FunctionImportDeclaration extends Statement {
public nameOf: string = "FunctionImportDeclaration";
public path: Identifier;

public name: Identifier;
Expand Down
19 changes: 14 additions & 5 deletions src/ast/nodes/IfStatement.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { Range } from "../Range";
import { Node } from "./Node";
import { Expression } from "./Expression.js";
import { Statement } from "./Statement.js";

export class IfStatement extends Statement {
public nameOf: string = "IfStatement";
public condition: Expression;
public body: Node;
constructor(condition: Expression, block: Node, range: Range) {
public condition: Expression | null;
public ifTrue: Statement;
public ifFalse: Statement | null;
public kind: IfStatementKind;
constructor(condition: Expression | null, ifTrue: Statement, ifFalse: Statement | null, kind: IfStatementKind, range: Range) {
super();
this.condition = condition;
this.body = block;
this.ifTrue = ifTrue;
this.ifFalse = ifFalse;
this.kind = kind;
this.range = range;
}
}

export enum IfStatementKind {
If = 0,
ElseIf = 1,
Else = 2
}
16 changes: 8 additions & 8 deletions src/ast/nodes/ImportDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Range } from "../Range.js";
import { Identifier } from "./Identifier.js";
import { Statement } from "./Statement.js";
import { StringLiteral } from "./StringLiteral.js";

export class ImportDeclaration extends Statement {
public nameOf: string = "ImportDeclaration";
public path: Identifier;
constructor(path: Identifier, range: Range) {
super();
this.path = path;
this.range = range;
}
public nameOf: string = "ImportDeclaration";
public path: StringLiteral;
constructor(path: StringLiteral, range: Range) {
super();
this.path = path;
this.range = range;
}
}
16 changes: 16 additions & 0 deletions src/ast/nodes/ImportFromDeclaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Range } from "../Range.js";
import { Identifier } from "./Identifier.js";
import { Statement } from "./Statement.js";
import { StringLiteral } from "./StringLiteral.js";

export class ImportFromDeclaration extends Statement {
public nameOf: string = "ImportFromDeclaration";
public value: Identifier// | ObjectLiteral | ArrayLiteral;
public path: StringLiteral;
constructor(value: Identifier, path: StringLiteral, range: Range) {
super();
this.value = value;
this.path = path;
this.range = range;
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/checker/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Source } from "../ast/Source";
import { FunctionDeclaration } from "../ast/nodes/Function";
import { FunctionDeclaration } from "../ast/nodes/FunctionDeclaration";
import { ErrorTypes } from "../error/error";

export class Validator {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import { Generator } from "../generator/index.js";
parser.parseFunctionDeclaration();

const connector = new Generator();
connector.parseProgram(parser.program);
connector.parseProgram(parser.source);

const watText = connector.toWat();
await Bun.write(output, watText);
Expand Down
42 changes: 36 additions & 6 deletions src/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Range } from "../ast/Range";
import { Source } from "../ast/Source";

export enum ErrorTypes {
UserError,
TokenMismatch,
TypeError,
SyntaxError,
Expand Down Expand Up @@ -37,17 +38,15 @@ export class TokenMismatchError extends CompileTimeError {
}

export class SyntaxError extends CompileTimeError {
public range: Range;
constructor(
program: Source,
source: Source,
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;
Expand All @@ -59,11 +58,42 @@ export class SyntaxError extends CompileTimeError {
console.log(color(prefix) + chalk.grey(":") + " " + message);
console.log(
" " +
chalk.cyan("test.zp") +
chalk.cyan(source.fileName) +
chalk.gray(":") +
chalk.cyan(this.range.line) +
chalk.cyan(range.start.line) +
chalk.gray(":") +
chalk.cyan(this.range.start),
chalk.cyan(range.start.column),
);
if (action === "FAIL") process.exit(0);
}
}

export class UserError extends CompileTimeError {
constructor(
source: Source,
prefix: string,
message: string,
code: number,
range: Range,
action: "FAIL" | "WARN" | "INFO",
) {
super(message, ErrorTypes.UserError, code);
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(source.fileName) +
chalk.gray(":") +
chalk.cyan(range.start.line) +
chalk.gray(":") +
chalk.cyan(range.start.column),
);
if (action === "FAIL") process.exit(0);
}
Expand Down
Loading

0 comments on commit e3de8dd

Please sign in to comment.