diff --git a/compiler/ast.ts b/compiler/ast.ts index 89b1ef8..7b4e3bd 100644 --- a/compiler/ast.ts +++ b/compiler/ast.ts @@ -1,5 +1,5 @@ -import { Pos } from "./Token.ts"; -import { VType } from "./vtypes.ts"; +import { Pos } from "./token.ts"; +import { VType } from "./vtype.ts"; export type UnaryType = "not"; export type BinaryType = diff --git a/compiler/Checker.ts b/compiler/checker.ts similarity index 86% rename from compiler/Checker.ts rename to compiler/checker.ts index 92525cc..efd45e4 100644 --- a/compiler/Checker.ts +++ b/compiler/checker.ts @@ -1,7 +1,7 @@ import { StmtKind } from "./ast.ts"; import { EType, Expr, Stmt } from "./ast.ts"; -import { Pos } from "./Token.ts"; -import { VType, VTypeParam, vtypesEqual, vtypeToString } from "./vtypes.ts"; +import { Pos } from "./token.ts"; +import { VType, VTypeParam, vtypesEqual, vtypeToString } from "./vtype.ts"; export class Checker { private fnReturnStack: VType[] = []; @@ -53,8 +53,8 @@ export class Checker { if (!vtypesEqual(prevBreakType, exprType)) { this.report( `incompatible types for break` + - `, got ${exprType}` + - ` incompatible with ${prevBreakType}`, + `, got ${exprType}` + + ` incompatible with ${prevBreakType}`, pos, ); return; @@ -78,8 +78,8 @@ export class Checker { if (!vtypesEqual(exprType, returnType)) { this.report( `incompatible return type` + - `, got ${exprType}` + - `, expected ${returnType}`, + `, got ${exprType}` + + `, expected ${returnType}`, pos, ); } @@ -110,8 +110,8 @@ export class Checker { if (!vtypesEqual(returnType, body)) { this.report( `incompatible return type` + - `, got ${body}` + - `, expected ${returnType}`, + `, got ${body}` + + `, expected ${returnType}`, pos, ); } @@ -128,8 +128,8 @@ export class Checker { if (!vtypesEqual(value, paramVtype)) { this.report( `incompatible value type` + - `, got '${vtypeToString(value)}'` + - `, expected '${vtypeToString(paramVtype)}'`, + `, got '${vtypeToString(value)}'` + + `, expected '${vtypeToString(paramVtype)}'`, pos, ); return; @@ -165,8 +165,8 @@ export class Checker { if (!vtypesEqual(found.vtype, value)) { this.report( `cannot assign incompatible type to field '${found.ident}'` + - `, got '${vtypeToString(value)}'` + - `, expected '${vtypeToString(found.vtype)}'`, + `, got '${vtypeToString(value)}'` + + `, expected '${vtypeToString(found.vtype)}'`, pos, ); return; @@ -187,8 +187,8 @@ export class Checker { if (!vtypesEqual(subject.inner, value)) { this.report( `cannot assign incompatible type to array ` + - `'${vtypeToString(subject)}'` + - `, got '${vtypeToString(value)}'`, + `'${vtypeToString(subject)}'` + + `, got '${vtypeToString(value)}'`, pos, ); return; @@ -205,12 +205,11 @@ export class Checker { ) { this.report( `cannot assign to incompatible type` + - `, got '${vtypeToString(value)}'` + - `, expected '${ - vtypeToString( - stmt.kind.subject.kind.sym.param.vtype!, - ) - }'`, + `, got '${vtypeToString(value)}'` + + `, expected '${vtypeToString( + stmt.kind.subject.kind.sym.param.vtype!, + ) + }'`, pos, ); return; @@ -348,7 +347,7 @@ export class Checker { if (args.length !== subject.params.length) { this.report( `incorrect number of arguments` + - `, expected ${subject.params.length}`, + `, expected ${subject.params.length}`, pos, ); } @@ -356,8 +355,8 @@ export class Checker { if (!vtypesEqual(args[i], subject.params[i].vtype)) { this.report( `incorrect argument ${i} '${subject.params[i].ident}'` + - `, expected ${vtypeToString(subject.params[i].vtype)}` + - `, got ${vtypeToString(args[i])}`, + `, expected ${vtypeToString(subject.params[i].vtype)}` + + `, got ${vtypeToString(args[i])}`, pos, ); break; @@ -383,7 +382,7 @@ export class Checker { } this.report( `cannot apply unary operation '${expr.kind.unaryType}' ` + - `on type '${vtypeToString(subject)}'`, + `on type '${vtypeToString(subject)}'`, pos, ); return { type: "error" }; @@ -410,9 +409,8 @@ export class Checker { } this.report( `cannot apply binary operation '${expr.kind.binaryType}' ` + - `on types '${vtypeToString(left)}' and '${ - vtypeToString(right) - }'`, + `on types '${vtypeToString(left)}' and '${vtypeToString(right) + }'`, pos, ); return { type: "error" }; @@ -438,7 +436,7 @@ export class Checker { if (falsy === undefined && truthy.type !== "null") { this.report( `if expressions without false-case must result in type 'null'` + - `, got '${vtypeToString(truthy)}'`, + `, got '${vtypeToString(truthy)}'`, pos, ); return { type: "error" }; @@ -446,8 +444,8 @@ export class Checker { if (falsy !== undefined && !vtypesEqual(truthy, falsy)) { this.report( `if cases must be compatible, got incompatible types` + - ` '${vtypeToString(truthy)}'` + - ` and '${vtypeToString(falsy)}'`, + ` '${vtypeToString(truthy)}'` + + ` and '${vtypeToString(falsy)}'`, pos, ); return { type: "error" }; @@ -465,7 +463,7 @@ export class Checker { if (body.type !== "null") { this.report( `loop body must result in type 'null'` + - `, got '${vtypeToString(body)}'`, + `, got '${vtypeToString(body)}'`, pos, ); return { type: "error" }; @@ -490,8 +488,8 @@ export class Checker { if (breakType[1]) { this.report( `incompatible types in break statements` + - `, got '${vtypeToString(breakType[2])}'` + - ` incompatible with ${vtypeToString(breakType[0])}`, + `, got '${vtypeToString(breakType[2])}'` + + ` incompatible with ${vtypeToString(breakType[0])}`, pos, ); return { type: "error" }; @@ -598,35 +596,35 @@ const simpleUnaryOperations: { operand: VType; result?: VType; }[] = [ - { unaryType: "not", operand: { type: "bool" } }, -]; + { unaryType: "not", operand: { type: "bool" } }, + ]; const simpleBinaryOperations: { binaryType: string; operand: VType; result?: VType; }[] = [ - // arithmetic - { binaryType: "+", operand: { type: "int" } }, - { binaryType: "+", operand: { type: "string" } }, - { binaryType: "-", operand: { type: "int" } }, - { binaryType: "*", operand: { type: "int" } }, - { binaryType: "/", operand: { type: "int" } }, - // logical - { binaryType: "and", operand: { type: "bool" } }, - { binaryType: "or", operand: { type: "bool" } }, - // equality - { binaryType: "==", operand: { type: "null" }, result: { type: "bool" } }, - { binaryType: "==", operand: { type: "int" }, result: { type: "bool" } }, - { binaryType: "==", operand: { type: "string" }, result: { type: "bool" } }, - { binaryType: "==", operand: { type: "bool" }, result: { type: "bool" } }, - { binaryType: "!=", operand: { type: "null" }, result: { type: "bool" } }, - { binaryType: "!=", operand: { type: "int" }, result: { type: "bool" } }, - { binaryType: "!=", operand: { type: "string" }, result: { type: "bool" } }, - { binaryType: "!=", operand: { type: "bool" }, result: { type: "bool" } }, - // 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" } }, -]; + // arithmetic + { binaryType: "+", operand: { type: "int" } }, + { binaryType: "+", operand: { type: "string" } }, + { binaryType: "-", operand: { type: "int" } }, + { binaryType: "*", operand: { type: "int" } }, + { binaryType: "/", operand: { type: "int" } }, + // logical + { binaryType: "and", operand: { type: "bool" } }, + { binaryType: "or", operand: { type: "bool" } }, + // equality + { binaryType: "==", operand: { type: "null" }, result: { type: "bool" } }, + { binaryType: "==", operand: { type: "int" }, result: { type: "bool" } }, + { binaryType: "==", operand: { type: "string" }, result: { type: "bool" } }, + { binaryType: "==", operand: { type: "bool" }, result: { type: "bool" } }, + { binaryType: "!=", operand: { type: "null" }, result: { type: "bool" } }, + { binaryType: "!=", operand: { type: "int" }, result: { type: "bool" } }, + { binaryType: "!=", operand: { type: "string" }, result: { type: "bool" } }, + { binaryType: "!=", operand: { type: "bool" }, result: { type: "bool" } }, + // 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" } }, + ]; diff --git a/compiler/Lexer.ts b/compiler/lexer.ts similarity index 99% rename from compiler/Lexer.ts rename to compiler/lexer.ts index f7813dd..55f71bf 100644 --- a/compiler/Lexer.ts +++ b/compiler/lexer.ts @@ -1,4 +1,4 @@ -import { Pos, Token } from "./Token.ts"; +import { Pos, Token } from "./token.ts"; export class Lexer { private index = 0; diff --git a/compiler/Lowerer.ts b/compiler/lowerer.ts similarity index 99% rename from compiler/Lowerer.ts rename to compiler/lowerer.ts index 3557730..85c0a3e 100644 --- a/compiler/Lowerer.ts +++ b/compiler/lowerer.ts @@ -3,7 +3,7 @@ import { BinaryType, Expr, Stmt } from "./ast.ts"; import { LocalLeaf, Locals, LocalsFnRoot } from "./lowerer_locals.ts"; import { Ops } from "./mod.ts"; import { Assembler } from "./program_builder.ts"; -import { VType, vtypeToString } from "./vtypes.ts"; +import { VType, vtypeToString } from "./vtype.ts"; export class Lowerer { private program = new Assembler(); diff --git a/compiler/main.ts b/compiler/main.ts index ac550c8..b5e47b9 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -1,8 +1,8 @@ -import { Checker } from "./Checker.ts"; -import { Lexer } from "./Lexer.ts"; -import { Lowerer } from "./Lowerer.ts"; -import { Parser } from "./Parser.ts"; -import { Resolver } from "./Resolver.ts"; +import { Checker } from "./checker.ts"; +import { Lexer } from "./lexer.ts"; +import { Lowerer } from "./lowerer.ts"; +import { Parser } from "./parser.ts"; +import { Resolver } from "./resolver.ts"; const text = await Deno.readTextFile("example.slg"); diff --git a/compiler/mod.ts b/compiler/mod.ts index be72e5a..ee80a77 100644 --- a/compiler/mod.ts +++ b/compiler/mod.ts @@ -1,12 +1,12 @@ import { Stmt } from "./ast.ts"; -import { Lexer } from "./Lexer.ts"; -import { Parser } from "./Parser.ts"; +import { Lexer } from "./lexer.ts"; +import { Parser } from "./parser.ts"; -export * from "./Parser.ts"; +export * from "./parser.ts"; export * from "./ast.ts"; export * from "./arch.ts"; -export * from "./Lexer.ts"; -export * from "./Token.ts"; +export * from "./lexer.ts"; +export * from "./token.ts"; export async function compileWithDebug(filepath: string): Promise { const text = await Deno.readTextFile(filepath); diff --git a/compiler/Parser.ts b/compiler/parser.ts similarity index 99% rename from compiler/Parser.ts rename to compiler/parser.ts index 8c0fbf1..cce3303 100644 --- a/compiler/Parser.ts +++ b/compiler/parser.ts @@ -8,8 +8,8 @@ import { Stmt, StmtKind, } from "./ast.ts"; -import { Lexer } from "./Lexer.ts"; -import { Pos, Token } from "./Token.ts"; +import { Lexer } from "./lexer.ts"; +import { Pos, Token } from "./token.ts"; export class Parser { private currentToken: Token | null; @@ -501,7 +501,7 @@ export class Parser { const pos = this.pos(); if (this.test("ident")) { const ident = this.current().identValue!; - this.step() + this.step(); return this.etype({ type: "ident", value: ident }, pos); } if (this.test("[")) { diff --git a/compiler/Resolver.ts b/compiler/resolver.ts similarity index 99% rename from compiler/Resolver.ts rename to compiler/resolver.ts index 106bac5..1842806 100644 --- a/compiler/Resolver.ts +++ b/compiler/resolver.ts @@ -6,7 +6,7 @@ import { StaticSyms, Syms, } from "./resolver_syms.ts"; -import { Pos } from "./Token.ts"; +import { Pos } from "./token.ts"; export class Resolver { private root = new GlobalSyms(); diff --git a/compiler/Token.ts b/compiler/token.ts similarity index 100% rename from compiler/Token.ts rename to compiler/token.ts diff --git a/compiler/vtypes.ts b/compiler/vtype.ts similarity index 100% rename from compiler/vtypes.ts rename to compiler/vtype.ts