From 7b26ed73e4083afd76a3fadb4f45f257795877c0 Mon Sep 17 00:00:00 2001 From: Drew Youngwerth Date: Mon, 11 Nov 2024 13:51:35 -0800 Subject: [PATCH] Cleanup --- src/parser/lexer.ts | 4 ++-- src/parser/syntax-macros/functional-notation.ts | 12 +++++++++--- src/parser/token.ts | 2 +- src/semantics/resolution/resolve-entities.ts | 2 +- src/semantics/resolution/resolve-impl.ts | 2 +- src/semantics/resolution/resolve-object-type.ts | 2 +- src/syntax-objects/implementation.ts | 2 +- src/syntax-objects/index.ts | 2 +- src/syntax-objects/{ => lib}/helpers.ts | 4 ++-- 9 files changed, 19 insertions(+), 13 deletions(-) rename src/syntax-objects/{ => lib}/helpers.ts (74%) diff --git a/src/parser/lexer.ts b/src/parser/lexer.ts index 886dec0d..401c5473 100644 --- a/src/parser/lexer.ts +++ b/src/parser/lexer.ts @@ -68,7 +68,7 @@ export const lexer = (chars: CharStream): Token => { const consumeOperator = (chars: CharStream, token: Token) => { while (isOpChar(chars.next)) { if (token.value === ">" && (chars.next === ">" || chars.next === ":")) { - break; // Ugly hack to support generics, means >> is not a valid operator + break; // Ugly hack to support generics, means >> is not a valid operator. At least until we write a custom parser for the generics reader macro. } token.addChar(chars.consumeChar()); @@ -93,7 +93,7 @@ const nextIsNumber = (chars: CharStream) => (isDigitSign(chars.next) && isDigit(chars.at(1) ?? "")); const consumeSpaces = (chars: CharStream, token: Token) => { - while (chars.next === " " && token.span < 2) { + while (chars.next === " " && token.length < 2) { token.addChar(chars.consumeChar()); } }; diff --git a/src/parser/syntax-macros/functional-notation.ts b/src/parser/syntax-macros/functional-notation.ts index 61e032c0..adfc6fdd 100644 --- a/src/parser/syntax-macros/functional-notation.ts +++ b/src/parser/syntax-macros/functional-notation.ts @@ -1,6 +1,12 @@ import { idIs, isOp } from "../grammar.js"; import { Expr, List, ListValue } from "../../syntax-objects/index.js"; +// Note: The current version of this function was modified by GPT o1. +// I wrote the original version an have made modifications to the version +// produced by o1. The intent was to have o1 improve the performance. But +// now I'm not sure the added complexity produced by o1 was worth the cost. +// Might still be worth re-writing again to something similar to the original. + export const functionalNotation = (list: List): List => { const array = list.toArray(); let isTuple = false; @@ -46,12 +52,12 @@ type Accumulator = { result: ListValue[]; skip: number }; const handleNextExpression = ( acc: Accumulator, expr: Expr, - nextExpr: Expr, + nextExpr: List, array: Expr[], index: number ) => { - if ((nextExpr as List).calls("generics")) { - const generics = nextExpr as List; + if (nextExpr.calls("generics")) { + const generics = nextExpr; const nextNextExpr = array[index + 2]; if (nextNextExpr && nextNextExpr.isList()) { acc.result.push(processGenerics(expr, generics, nextNextExpr as List)); diff --git a/src/parser/token.ts b/src/parser/token.ts index 2385b6f9..68bdd7d2 100644 --- a/src/parser/token.ts +++ b/src/parser/token.ts @@ -10,7 +10,7 @@ export class Token { this.location = location; } - get span() { + get length() { return this.value.length; } diff --git a/src/semantics/resolution/resolve-entities.ts b/src/semantics/resolution/resolve-entities.ts index fbfccd8c..1152e192 100644 --- a/src/semantics/resolution/resolve-entities.ts +++ b/src/semantics/resolution/resolve-entities.ts @@ -1,6 +1,6 @@ import { Block } from "../../syntax-objects/block.js"; import { Expr } from "../../syntax-objects/expr.js"; -import { nop } from "../../syntax-objects/helpers.js"; +import { nop } from "../../syntax-objects/lib/helpers.js"; import { List } from "../../syntax-objects/list.js"; import { VoidModule } from "../../syntax-objects/module.js"; import { ObjectLiteral } from "../../syntax-objects/object-literal.js"; diff --git a/src/semantics/resolution/resolve-impl.ts b/src/semantics/resolution/resolve-impl.ts index 8105d531..5e9d9618 100644 --- a/src/semantics/resolution/resolve-impl.ts +++ b/src/semantics/resolution/resolve-impl.ts @@ -1,4 +1,4 @@ -import { nop } from "../../syntax-objects/helpers.js"; +import { nop } from "../../syntax-objects/lib/helpers.js"; import { Implementation } from "../../syntax-objects/implementation.js"; import { ObjectType, TypeAlias } from "../../syntax-objects/types.js"; import { getExprType } from "./get-expr-type.js"; diff --git a/src/semantics/resolution/resolve-object-type.ts b/src/semantics/resolution/resolve-object-type.ts index ec39d1c6..4fc2c4ad 100644 --- a/src/semantics/resolution/resolve-object-type.ts +++ b/src/semantics/resolution/resolve-object-type.ts @@ -1,5 +1,5 @@ import { Call } from "../../syntax-objects/call.js"; -import { nop } from "../../syntax-objects/helpers.js"; +import { nop } from "../../syntax-objects/lib/helpers.js"; import { List } from "../../syntax-objects/list.js"; import { ObjectType, diff --git a/src/syntax-objects/implementation.ts b/src/syntax-objects/implementation.ts index 81efa685..ca022df9 100644 --- a/src/syntax-objects/implementation.ts +++ b/src/syntax-objects/implementation.ts @@ -1,6 +1,6 @@ import { Expr } from "./expr.js"; import { Fn } from "./fn.js"; -import { nop } from "./helpers.js"; +import { nop } from "./lib/helpers.js"; import { Identifier } from "./identifier.js"; import { ChildList } from "./lib/child-list.js"; import { Child } from "./lib/child.js"; diff --git a/src/syntax-objects/index.ts b/src/syntax-objects/index.ts index 3a82ab0a..654d669b 100644 --- a/src/syntax-objects/index.ts +++ b/src/syntax-objects/index.ts @@ -2,7 +2,7 @@ export * from "./syntax.js"; export * from "./bool.js"; export * from "./expr.js"; export * from "./float.js"; -export * from "./helpers.js"; +export * from "./lib/helpers.js"; export * from "./identifier.js"; export * from "./int.js"; export * from "./list.js"; diff --git a/src/syntax-objects/helpers.ts b/src/syntax-objects/lib/helpers.ts similarity index 74% rename from src/syntax-objects/helpers.ts rename to src/syntax-objects/lib/helpers.ts index 60de5b79..e8dae433 100644 --- a/src/syntax-objects/helpers.ts +++ b/src/syntax-objects/lib/helpers.ts @@ -1,5 +1,5 @@ -import { Nop } from "./nop.js"; -import { Whitespace } from "./whitespace.js"; +import { Nop } from "../nop.js"; +import { Whitespace } from "../whitespace.js"; export const newLine = () => new Whitespace({ value: "\n" });