rename files

This commit is contained in:
sfja 2024-12-10 21:42:15 +01:00
parent fa5389b828
commit cbb5a02bd9
10 changed files with 75 additions and 77 deletions

View File

@ -1,5 +1,5 @@
import { Pos } from "./Token.ts"; import { Pos } from "./token.ts";
import { VType } from "./vtypes.ts"; import { VType } from "./vtype.ts";
export type UnaryType = "not"; export type UnaryType = "not";
export type BinaryType = export type BinaryType =

View File

@ -1,7 +1,7 @@
import { StmtKind } from "./ast.ts"; import { StmtKind } from "./ast.ts";
import { EType, Expr, Stmt } from "./ast.ts"; import { EType, Expr, Stmt } from "./ast.ts";
import { Pos } from "./Token.ts"; import { Pos } from "./token.ts";
import { VType, VTypeParam, vtypesEqual, vtypeToString } from "./vtypes.ts"; import { VType, VTypeParam, vtypesEqual, vtypeToString } from "./vtype.ts";
export class Checker { export class Checker {
private fnReturnStack: VType[] = []; private fnReturnStack: VType[] = [];
@ -53,8 +53,8 @@ export class Checker {
if (!vtypesEqual(prevBreakType, exprType)) { if (!vtypesEqual(prevBreakType, exprType)) {
this.report( this.report(
`incompatible types for break` + `incompatible types for break` +
`, got ${exprType}` + `, got ${exprType}` +
` incompatible with ${prevBreakType}`, ` incompatible with ${prevBreakType}`,
pos, pos,
); );
return; return;
@ -78,8 +78,8 @@ export class Checker {
if (!vtypesEqual(exprType, returnType)) { if (!vtypesEqual(exprType, returnType)) {
this.report( this.report(
`incompatible return type` + `incompatible return type` +
`, got ${exprType}` + `, got ${exprType}` +
`, expected ${returnType}`, `, expected ${returnType}`,
pos, pos,
); );
} }
@ -110,8 +110,8 @@ export class Checker {
if (!vtypesEqual(returnType, body)) { if (!vtypesEqual(returnType, body)) {
this.report( this.report(
`incompatible return type` + `incompatible return type` +
`, got ${body}` + `, got ${body}` +
`, expected ${returnType}`, `, expected ${returnType}`,
pos, pos,
); );
} }
@ -128,8 +128,8 @@ export class Checker {
if (!vtypesEqual(value, paramVtype)) { if (!vtypesEqual(value, paramVtype)) {
this.report( this.report(
`incompatible value type` + `incompatible value type` +
`, got '${vtypeToString(value)}'` + `, got '${vtypeToString(value)}'` +
`, expected '${vtypeToString(paramVtype)}'`, `, expected '${vtypeToString(paramVtype)}'`,
pos, pos,
); );
return; return;
@ -165,8 +165,8 @@ export class Checker {
if (!vtypesEqual(found.vtype, value)) { if (!vtypesEqual(found.vtype, value)) {
this.report( this.report(
`cannot assign incompatible type to field '${found.ident}'` + `cannot assign incompatible type to field '${found.ident}'` +
`, got '${vtypeToString(value)}'` + `, got '${vtypeToString(value)}'` +
`, expected '${vtypeToString(found.vtype)}'`, `, expected '${vtypeToString(found.vtype)}'`,
pos, pos,
); );
return; return;
@ -187,8 +187,8 @@ export class Checker {
if (!vtypesEqual(subject.inner, value)) { if (!vtypesEqual(subject.inner, value)) {
this.report( this.report(
`cannot assign incompatible type to array ` + `cannot assign incompatible type to array ` +
`'${vtypeToString(subject)}'` + `'${vtypeToString(subject)}'` +
`, got '${vtypeToString(value)}'`, `, got '${vtypeToString(value)}'`,
pos, pos,
); );
return; return;
@ -205,12 +205,11 @@ export class Checker {
) { ) {
this.report( this.report(
`cannot assign to incompatible type` + `cannot assign to incompatible type` +
`, got '${vtypeToString(value)}'` + `, got '${vtypeToString(value)}'` +
`, expected '${ `, expected '${vtypeToString(
vtypeToString( stmt.kind.subject.kind.sym.param.vtype!,
stmt.kind.subject.kind.sym.param.vtype!, )
) }'`,
}'`,
pos, pos,
); );
return; return;
@ -348,7 +347,7 @@ export class Checker {
if (args.length !== subject.params.length) { if (args.length !== subject.params.length) {
this.report( this.report(
`incorrect number of arguments` + `incorrect number of arguments` +
`, expected ${subject.params.length}`, `, expected ${subject.params.length}`,
pos, pos,
); );
} }
@ -356,8 +355,8 @@ export class Checker {
if (!vtypesEqual(args[i], subject.params[i].vtype)) { if (!vtypesEqual(args[i], subject.params[i].vtype)) {
this.report( this.report(
`incorrect argument ${i} '${subject.params[i].ident}'` + `incorrect argument ${i} '${subject.params[i].ident}'` +
`, expected ${vtypeToString(subject.params[i].vtype)}` + `, expected ${vtypeToString(subject.params[i].vtype)}` +
`, got ${vtypeToString(args[i])}`, `, got ${vtypeToString(args[i])}`,
pos, pos,
); );
break; break;
@ -383,7 +382,7 @@ export class Checker {
} }
this.report( this.report(
`cannot apply unary operation '${expr.kind.unaryType}' ` + `cannot apply unary operation '${expr.kind.unaryType}' ` +
`on type '${vtypeToString(subject)}'`, `on type '${vtypeToString(subject)}'`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -410,9 +409,8 @@ export class Checker {
} }
this.report( this.report(
`cannot apply binary operation '${expr.kind.binaryType}' ` + `cannot apply binary operation '${expr.kind.binaryType}' ` +
`on types '${vtypeToString(left)}' and '${ `on types '${vtypeToString(left)}' and '${vtypeToString(right)
vtypeToString(right) }'`,
}'`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -438,7 +436,7 @@ export class Checker {
if (falsy === undefined && truthy.type !== "null") { if (falsy === undefined && truthy.type !== "null") {
this.report( this.report(
`if expressions without false-case must result in type 'null'` + `if expressions without false-case must result in type 'null'` +
`, got '${vtypeToString(truthy)}'`, `, got '${vtypeToString(truthy)}'`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -446,8 +444,8 @@ export class Checker {
if (falsy !== undefined && !vtypesEqual(truthy, falsy)) { if (falsy !== undefined && !vtypesEqual(truthy, falsy)) {
this.report( this.report(
`if cases must be compatible, got incompatible types` + `if cases must be compatible, got incompatible types` +
` '${vtypeToString(truthy)}'` + ` '${vtypeToString(truthy)}'` +
` and '${vtypeToString(falsy)}'`, ` and '${vtypeToString(falsy)}'`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -465,7 +463,7 @@ export class Checker {
if (body.type !== "null") { if (body.type !== "null") {
this.report( this.report(
`loop body must result in type 'null'` + `loop body must result in type 'null'` +
`, got '${vtypeToString(body)}'`, `, got '${vtypeToString(body)}'`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -490,8 +488,8 @@ export class Checker {
if (breakType[1]) { if (breakType[1]) {
this.report( this.report(
`incompatible types in break statements` + `incompatible types in break statements` +
`, got '${vtypeToString(breakType[2])}'` + `, got '${vtypeToString(breakType[2])}'` +
` incompatible with ${vtypeToString(breakType[0])}`, ` incompatible with ${vtypeToString(breakType[0])}`,
pos, pos,
); );
return { type: "error" }; return { type: "error" };
@ -598,35 +596,35 @@ const simpleUnaryOperations: {
operand: VType; operand: VType;
result?: VType; result?: VType;
}[] = [ }[] = [
{ unaryType: "not", operand: { type: "bool" } }, { unaryType: "not", operand: { type: "bool" } },
]; ];
const simpleBinaryOperations: { const simpleBinaryOperations: {
binaryType: string; binaryType: string;
operand: VType; operand: VType;
result?: VType; result?: VType;
}[] = [ }[] = [
// arithmetic // arithmetic
{ binaryType: "+", operand: { type: "int" } }, { binaryType: "+", operand: { type: "int" } },
{ binaryType: "+", operand: { type: "string" } }, { binaryType: "+", operand: { type: "string" } },
{ binaryType: "-", operand: { type: "int" } }, { binaryType: "-", operand: { type: "int" } },
{ binaryType: "*", operand: { type: "int" } }, { binaryType: "*", operand: { type: "int" } },
{ binaryType: "/", operand: { type: "int" } }, { binaryType: "/", operand: { type: "int" } },
// logical // logical
{ binaryType: "and", operand: { type: "bool" } }, { binaryType: "and", operand: { type: "bool" } },
{ binaryType: "or", operand: { type: "bool" } }, { binaryType: "or", operand: { type: "bool" } },
// equality // equality
{ binaryType: "==", operand: { type: "null" }, result: { type: "bool" } }, { binaryType: "==", operand: { type: "null" }, result: { type: "bool" } },
{ binaryType: "==", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: "==", operand: { type: "int" }, result: { type: "bool" } },
{ binaryType: "==", operand: { type: "string" }, result: { type: "bool" } }, { binaryType: "==", operand: { type: "string" }, result: { type: "bool" } },
{ binaryType: "==", operand: { type: "bool" }, result: { type: "bool" } }, { binaryType: "==", operand: { type: "bool" }, result: { type: "bool" } },
{ binaryType: "!=", operand: { type: "null" }, result: { type: "bool" } }, { binaryType: "!=", operand: { type: "null" }, result: { type: "bool" } },
{ binaryType: "!=", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: "!=", operand: { type: "int" }, result: { type: "bool" } },
{ binaryType: "!=", operand: { type: "string" }, result: { type: "bool" } }, { binaryType: "!=", operand: { type: "string" }, result: { type: "bool" } },
{ binaryType: "!=", operand: { type: "bool" }, result: { type: "bool" } }, { binaryType: "!=", operand: { type: "bool" }, result: { type: "bool" } },
// comparison // comparison
{ binaryType: "<", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: "<", operand: { type: "int" }, result: { type: "bool" } },
{ binaryType: ">", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: ">", operand: { type: "int" }, result: { type: "bool" } },
{ binaryType: "<=", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: "<=", operand: { type: "int" }, result: { type: "bool" } },
{ binaryType: ">=", operand: { type: "int" }, result: { type: "bool" } }, { binaryType: ">=", operand: { type: "int" }, result: { type: "bool" } },
]; ];

View File

@ -1,4 +1,4 @@
import { Pos, Token } from "./Token.ts"; import { Pos, Token } from "./token.ts";
export class Lexer { export class Lexer {
private index = 0; private index = 0;

View File

@ -3,7 +3,7 @@ import { BinaryType, Expr, Stmt } from "./ast.ts";
import { LocalLeaf, Locals, LocalsFnRoot } from "./lowerer_locals.ts"; import { LocalLeaf, Locals, LocalsFnRoot } from "./lowerer_locals.ts";
import { Ops } from "./mod.ts"; import { Ops } from "./mod.ts";
import { Assembler } from "./program_builder.ts"; import { Assembler } from "./program_builder.ts";
import { VType, vtypeToString } from "./vtypes.ts"; import { VType, vtypeToString } from "./vtype.ts";
export class Lowerer { export class Lowerer {
private program = new Assembler(); private program = new Assembler();

View File

@ -1,8 +1,8 @@
import { Checker } from "./Checker.ts"; import { Checker } from "./checker.ts";
import { Lexer } from "./Lexer.ts"; import { Lexer } from "./lexer.ts";
import { Lowerer } from "./Lowerer.ts"; import { Lowerer } from "./lowerer.ts";
import { Parser } from "./Parser.ts"; import { Parser } from "./parser.ts";
import { Resolver } from "./Resolver.ts"; import { Resolver } from "./resolver.ts";
const text = await Deno.readTextFile("example.slg"); const text = await Deno.readTextFile("example.slg");

View File

@ -1,12 +1,12 @@
import { Stmt } from "./ast.ts"; import { Stmt } from "./ast.ts";
import { Lexer } from "./Lexer.ts"; import { Lexer } from "./lexer.ts";
import { Parser } from "./Parser.ts"; import { Parser } from "./parser.ts";
export * from "./Parser.ts"; export * from "./parser.ts";
export * from "./ast.ts"; export * from "./ast.ts";
export * from "./arch.ts"; export * from "./arch.ts";
export * from "./Lexer.ts"; export * from "./lexer.ts";
export * from "./Token.ts"; export * from "./token.ts";
export async function compileWithDebug(filepath: string): Promise<Stmt[]> { export async function compileWithDebug(filepath: string): Promise<Stmt[]> {
const text = await Deno.readTextFile(filepath); const text = await Deno.readTextFile(filepath);

View File

@ -8,8 +8,8 @@ import {
Stmt, Stmt,
StmtKind, StmtKind,
} from "./ast.ts"; } from "./ast.ts";
import { Lexer } from "./Lexer.ts"; import { Lexer } from "./lexer.ts";
import { Pos, Token } from "./Token.ts"; import { Pos, Token } from "./token.ts";
export class Parser { export class Parser {
private currentToken: Token | null; private currentToken: Token | null;
@ -501,7 +501,7 @@ export class Parser {
const pos = this.pos(); const pos = this.pos();
if (this.test("ident")) { if (this.test("ident")) {
const ident = this.current().identValue!; const ident = this.current().identValue!;
this.step() this.step();
return this.etype({ type: "ident", value: ident }, pos); return this.etype({ type: "ident", value: ident }, pos);
} }
if (this.test("[")) { if (this.test("[")) {

View File

@ -6,7 +6,7 @@ import {
StaticSyms, StaticSyms,
Syms, Syms,
} from "./resolver_syms.ts"; } from "./resolver_syms.ts";
import { Pos } from "./Token.ts"; import { Pos } from "./token.ts";
export class Resolver { export class Resolver {
private root = new GlobalSyms(); private root = new GlobalSyms();