Skip to content

Commit

Permalink
Prefix Fix (#43)
Browse files Browse the repository at this point in the history
* WIP

* Revert unused change
  • Loading branch information
drew-y authored Sep 13, 2024
1 parent b494c47 commit 0a59dae
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 181 deletions.
36 changes: 36 additions & 0 deletions src/parser/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,42 @@ exports[`parser can parse a file into a syntax expanded ast 1`] = `
10,
3,
],
[
"mul",
[
"&",
"self",
],
[
":",
"other",
[
"Vec",
[
"generics",
"T",
],
],
],
],
[
"mul",
[
"&",
"self",
],
[
":",
"other",
[
"Vec",
[
"generics",
"T",
],
],
],
],
[
"let",
[
Expand Down
178 changes: 0 additions & 178 deletions src/parser/__tests__/fixtures/desugarred-ast.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/parser/__tests__/fixtures/void-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ with: () => 0
10
+ 3
mul(&self, other: Vec<T>)
mul(
&self,
other: Vec<T>
)
let a = array
.reduce(0) (acc, val) =>
acc + val
Expand Down
1 change: 1 addition & 0 deletions src/parser/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const prefixOps: OpMap = new Map([
["~", 7],
["%", 7],
["$", 7],
["@", 7],
["$@", 7],
["...", 5],
]);
Expand Down
12 changes: 9 additions & 3 deletions src/parser/syntax-macros/interpret-whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Expr, List } from "../../syntax-objects/index.js";
export const interpretWhitespace = (list: List, indentLevel?: number): List => {
const transformed = new List({ ...list.metadata });

let hadComma = false;
while (list.hasChildren) {
const child = elideParens(list, indentLevel);
if (child?.isList() && child.length === 0) continue;
addSibling(child, transformed);
addSibling(child, transformed, hadComma);
hadComma = nextIsComma(list);
}

if (transformed.length === 1 && transformed.first()?.isList()) {
Expand Down Expand Up @@ -166,6 +168,10 @@ const consumeLeadingWhitespace = (list: List) => {

const isNewline = (v?: Expr) => v?.isWhitespace() && v.isNewline;
const isIndent = (v: Expr) => v.isWhitespace() && v.isIndent;
const nextIsComma = (list: List) => {
const next = list.first();
return !!(next?.isIdentifier() && next.is(","));
};

const isNamedParameter = (v: List) => {
const identifier = v.at(0);
Expand All @@ -184,10 +190,10 @@ const isNamedParameter = (v: List) => {
return true;
};

const addSibling = (child: Expr, siblings: List) => {
const addSibling = (child: Expr, siblings: List, hadComma?: boolean) => {
const olderSibling = siblings.at(-1);

if (!child.isList()) {
if (!child.isList() || hadComma) {
siblings.push(child);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Parameter } from "./parameter.js";
import { NamedEntity, NamedEntityOpts } from "./named-entity.js";
import { Id, Identifier } from "./identifier.js";
import { getIdStr } from "./get-id-str.js";
import { LexicalContext } from "./lexical-context.js";

export type Type =
| PrimitiveType
Expand Down Expand Up @@ -162,6 +163,7 @@ export type ObjectField = { name: string; typeExpr: Expr; type?: Type };

export class ObjectType extends BaseType {
readonly kindOfType = "object";
namespace: LexicalContext = new LexicalContext();
typeParameters?: Identifier[];
appliedTypeArgs?: Type[];
genericInstances?: ObjectType[];
Expand Down

0 comments on commit 0a59dae

Please sign in to comment.