Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 13, 2024
1 parent d686a19 commit 25f2fe3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 94 deletions.
16 changes: 7 additions & 9 deletions src/lib/fast-shift-array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { at } from "vitest/dist/chunks/reporters.C_zwCd4j.js";

// TODO: Add map, filter, reduce, amd findIndex
export class FastShiftArray<T> {
private items: T[];
Expand All @@ -18,15 +20,6 @@ export class FastShiftArray<T> {
return value;
}

unshift(...items: T[]): number {
if (items.length === 0) return this.length;
this.headIndex = Math.max(this.headIndex - items.length, 0);
for (let i = 0; i < items.length; i++) {
this.items[this.headIndex + i] = items[i];
}
return this.length;
}

push(...items: T[]): number {
this.items.push(...items);
return this.length;
Expand All @@ -37,6 +30,11 @@ export class FastShiftArray<T> {
return this.items.pop();
}

unshift(...items: T[]): number {
this.items.splice(this.headIndex, 0, ...items);
return this.length;
}

at(index: number): T | undefined {
if (index < 0) {
index = this.length + index;
Expand Down
4 changes: 2 additions & 2 deletions src/semantics/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const registerModule = ({
}

if (!existingModule && (name === "index" || name === "mod")) {
parentModule.unshift(...ast.toArray());
parentModule.unshift(...ast.toArray().reverse());
return;
}

Expand All @@ -73,7 +73,7 @@ const registerModule = ({
}

if (existingModule && !rest.length) {
module.unshift(...ast.toArray());
module.unshift(...ast.toArray().reverse());
}

if (!rest.length) return;
Expand Down
35 changes: 23 additions & 12 deletions src/syntax-objects/lib/child-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export class ChildList<T extends Expr = Expr> {
return this.store.length;
}

private registerExpr(expr: T) {
expr.parent = this.parent;
if (expr instanceof NamedEntity) {
this.parent.registerEntity(expr);
}
}

at(index: number): Expr | undefined {
return this.store.at(index);
}
Expand Down Expand Up @@ -73,11 +80,7 @@ export class ChildList<T extends Expr = Expr> {
}

set(index: number, expr: T) {
expr.parent = this.parent;
if (expr instanceof NamedEntity) {
this.parent.registerEntity(expr);
}

this.registerExpr(expr);
this.store.set(index, expr);
return this;
}
Expand Down Expand Up @@ -116,14 +119,9 @@ export class ChildList<T extends Expr = Expr> {

push(...expr: T[]) {
expr.forEach((ex) => {
ex.parent = this.parent;
if (ex instanceof NamedEntity) {
this.parent.registerEntity(ex);
}

this.registerExpr(ex);
this.store.push(ex);
});

return this;
}

Expand All @@ -132,7 +130,7 @@ export class ChildList<T extends Expr = Expr> {
}

insert(expr: T, at = 0) {
expr.parent = this.parent;
this.registerExpr(expr);
this.store.splice(at, 0, expr);
return this;
}
Expand Down Expand Up @@ -186,6 +184,19 @@ export class ChildList<T extends Expr = Expr> {
return this.store.slice(start, end);
}

shift(): T | undefined {
return this.store.shift();
}

unshift(...expr: T[]) {
expr.forEach((ex) => {
this.registerExpr(ex);
this.store.unshift(ex);
});

return this;
}

toArray(): T[] {
return this.store.toArray();
}
Expand Down
47 changes: 23 additions & 24 deletions src/syntax-objects/list.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { FastShiftArray } from "../lib/fast-shift-array.js";
import { Expr } from "./expr.js";
import { Float } from "./float.js";
import { getIdStr } from "./lib/get-id-str.js";
import { Id, Identifier } from "./identifier.js";
import { Int } from "./int.js";
import { NamedEntity } from "./named-entity.js";
import { Syntax, SyntaxMetadata } from "./syntax.js";
import { ChildList } from "./lib/child-list.js";

export class List extends Syntax {
readonly syntaxType = "list";
/** True when the list was defined by the user using parenthesis i.e. (hey, there) */
mayBeTuple?: boolean;
store: FastShiftArray<Expr> = new FastShiftArray();
#store = new ChildList(undefined, this);

constructor(
opts:
Expand All @@ -35,23 +35,23 @@ export class List extends Syntax {
}

get children() {
return this.store.toArray();
return this.toArray();
}

get hasChildren() {
return !!this.store.length;
return !!this.#store.length;
}

get length() {
return this.store.length;
return this.#store.length;
}

at(index: number): Expr | undefined {
return this.store.at(index);
return this.#store.at(index);
}

exprAt(index: number): Expr {
const expr = this.store.at(index);
const expr = this.#store.at(index);
if (!expr) {
throw new Error(`No expr at ${index}`);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ export class List extends Syntax {
set(index: number, expr: Expr | string) {
const result = typeof expr === "string" ? Identifier.from(expr) : expr;
result.parent = this;
this.store.set(index, result);
this.#store.set(index, result);
return this;
}

Expand All @@ -98,42 +98,42 @@ export class List extends Syntax {
}

consume(): Expr {
const next = this.store.shift();
const next = this.#store.shift();
if (!next) throw new Error("No remaining expressions");
return next;
}

first(): Expr | undefined {
return this.store.at(0);
return this.#store.at(0);
}

last(): Expr | undefined {
return this.store.at(-1);
return this.#store.at(-1);
}

/** Returns all but the first element in an array */
argsArray(): Expr[] {
return this.store.toArray().slice(1);
return this.#store.toArray().slice(1);
}

pop(): Expr | undefined {
return this.store.pop();
return this.#store.pop();
}

push(...expr: ListValue[]) {
expr.forEach((ex) => {
if (typeof ex === "string") {
this.store.push(new Identifier({ value: ex, parent: this }));
this.#store.push(new Identifier({ value: ex, parent: this }));
return;
}

if (typeof ex === "number" && Number.isInteger(ex)) {
this.store.push(new Int({ value: ex, parent: this }));
this.#store.push(new Int({ value: ex, parent: this }));
return;
}

if (typeof ex === "number") {
this.store.push(new Float({ value: ex, parent: this }));
this.#store.push(new Float({ value: ex, parent: this }));
return;
}

Expand All @@ -149,11 +149,11 @@ export class List extends Syntax {
}

if (ex.isList() && ex.calls("splice_quote")) {
this.store.push(...ex.argsArray());
this.#store.push(...ex.argsArray());
return;
}

this.store.push(ex);
this.#store.push(ex);
});

return this;
Expand All @@ -165,13 +165,12 @@ export class List extends Syntax {

insert(expr: Expr | string, at = 0) {
const result = typeof expr === "string" ? Identifier.from(expr) : expr;
result.parent = this;
this.store.splice(at, 0, result);
this.#store.insert(result, at);
return this;
}

remove(index: number, count = 1) {
this.store.splice(index, count);
this.#store.remove(index, count);
return this;
}

Expand Down Expand Up @@ -210,16 +209,16 @@ export class List extends Syntax {
slice(start?: number, end?: number): List {
return new List({
...super.getCloneOpts(),
value: this.store.slice(start, end),
value: this.#store.slice(start, end).toArray(),
});
}

sliceAsArray(start?: number, end?: number) {
return this.store.slice(start, end);
return this.children.slice(start, end);
}

toArray(): Expr[] {
return this.store.toArray();
return this.#store.toArray();
}

toJSON() {
Expand Down
64 changes: 17 additions & 47 deletions src/syntax-objects/module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Expr } from "./expr.js";
import { Float } from "./float.js";
import { Id, Identifier } from "./identifier.js";
import { Int } from "./int.js";
import { Id } from "./identifier.js";
import { ChildList } from "./lib/child-list.js";
import { LexicalContext } from "./lib/lexical-context.js";
import { List, ListValue } from "./list.js";
import {
NamedEntity,
ScopedNamedEntity,
ScopedNamedEntityOpts,
} from "./named-entity.js";

export type VoidModuleOpts = ScopedNamedEntityOpts & {
value?: ListValue[];
value?: Expr[];
phase?: number;
isIndex?: boolean;
exports?: LexicalContext;
Expand All @@ -23,7 +21,7 @@ export class VoidModule extends ScopedNamedEntity {
readonly isRoot: boolean = false;
/** This module is the entry point of the user src code */
isIndex = false;
value: Expr[] = [];
#value = new ChildList(undefined, this);
/**
* 0 = init,
* 1 = expanding regular macros,
Expand All @@ -41,6 +39,15 @@ export class VoidModule extends ScopedNamedEntity {
this.isIndex = opts.isIndex ?? false;
}

get value() {
return this.#value.toArray();
}

set value(value: Expr[]) {
this.#value = new ChildList(undefined, this);
this.push(...value);
}

registerExport(entity: NamedEntity, alias?: string) {
this.exports.registerEntity(entity, alias);
}
Expand Down Expand Up @@ -104,50 +111,13 @@ export class VoidModule extends ScopedNamedEntity {
];
}

push(...expr: ListValue[]) {
expr.forEach((ex) => {
if (typeof ex === "string") {
this.value.push(new Identifier({ value: ex, parent: this }));
return;
}

if (ex instanceof Array) {
this.push(new List({ value: ex, parent: this }));
return;
}

if (typeof ex === "number" && Number.isInteger(ex)) {
this.value.push(new Int({ value: ex, parent: this }));
return;
}

if (typeof ex === "number") {
this.value.push(new Float({ value: ex, parent: this }));
return;
}

ex.parent = this;

if (ex instanceof NamedEntity) {
this.registerEntity(ex);
}

if (ex.isList() && ex.calls("splice_quote")) {
this.value.push(...ex.argsArray());
return;
}

this.value.push(ex);
});

push(...expr: Expr[]) {
this.#value.push(...expr);
return this;
}

unshift(...expr: ListValue[]) {
const old = this.value;
this.value = [];
this.push(...expr);
this.push(...old);
unshift(...expr: Expr[]) {
this.#value.unshift(...expr);
return this;
}
}
Expand Down

0 comments on commit 25f2fe3

Please sign in to comment.