Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt at generating AST-related code #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
dist
*fixtures*
dist-test
dist-ts
dist-ts

*-generated.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dist-ts/
.netlify

.log/
/packages/delisp-core/src/syntax-generated.ts
193 changes: 193 additions & 0 deletions packages/delisp-core/generate-syntax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import fs from "fs";
import path from "path";

// TS Types

interface TSRawType {
tag: "primitive";
raw: string;
}
interface TSArrayType {
tag: "array";
elements: TSType;
}

interface TSRecordField {
key: string;
type: TSType;
}

interface TSRecordType {
tag: "record";
fields: TSRecordField[];
}
type TSType = TSRawType | TSArrayType | TSRecordType;

function tstype(raw: string): TSRawType {
return { tag: "primitive", raw };
}

function tsArray(t: TSType): TSArrayType {
return { tag: "array", elements: t };
}

function tsRecord(fields: TSRecordType["fields"]): TSRecordType {
return { tag: "record", fields };
}

function tsContains(E: TSRawType, t: TSType): boolean {
switch (t.tag) {
case "primitive":
return t.raw === E.raw;
case "array":
return tsContains(E, t.elements);
case "record":
return t.fields.some((f) => tsContains(E, f.type));
}
}

function printTSType(t: TSType): string {
switch (t.tag) {
case "primitive":
return t.raw;
case "array":
return `Array<${printTSType(t.elements)}>`;
case "record":
return `{
${t.fields
.map((f) => f.key + ": " + printTSType(f.type) + ";")
.join("\n")}
}`;
}
}

// ADT

interface ADTAlternative {
tag: string;
record: TSRecordType;
}

interface ADT {
tag: "adt";
name: string;
generics: TSRawType[];
alternatives: ADTAlternative[];
type: TSRawType;
}

function tsADT(
name: string,
generics: TSRawType[],
alternatives: {
[tag: string]: {
[field: string]: TSType;
};
}
): ADT {
return {
tag: "adt",
name,
generics,
alternatives: Object.keys(alternatives).map((tag) => {
return {
tag,
record: tsRecord(
Object.keys(alternatives[tag]).map((key) => ({
key,
type: alternatives[tag][key],
}))
),
};
}),
type: tstype(name),
};
}

function printADT(adt: ADT): string {
const alternativeData = (alt: ADTAlternative) => {
const generics = adt.generics.map((t) =>
tsContains(t, alt.record) ? t.raw : "_" + t.raw
);
const name = `S${alt.tag}F`;
const nameWithGenerics = `${name}<${generics.join(", ")}>`;
return {
name,
generics,
nameWithGenerics: generics.length === 0 ? name : nameWithGenerics,
};
};

const alternatives = adt.alternatives
.map((alt) => {
const altData = alternativeData(alt);

const taggedRecord = tsRecord([
{
key: "tag",
type: tstype(`"${alt.tag[0].toLowerCase() + alt.tag.slice(1)}"`),
},
...alt.record.fields,
]);

return `
export interface ${altData.nameWithGenerics}
${printTSType(taggedRecord)}
`;
})
.join("\n");

const generics =
adt.generics.length === 0
? ""
: `<${adt.generics.map((g) => g.raw).join(", ")}>`;

const union = `
export type ${printTSType(adt.type)}${generics} = ${adt.alternatives
.map(alternativeData)
.map((d) => `${d.name}${generics}`)
.join(" | ")};
`;

return `
${alternatives}
${union}
`;
}

const E = tstype("E");

const Identifier = tsADT("Identifier", [], {
Identifier: { name: tstype("string"), location: tstype("Location") },
});

const LambdaList = tsADT("LambdaList", [], {
LambdaList: {
positionalArguments: tsArray(Identifier.type),
location: tstype("Location"),
},
});

const Expression = tsADT("FutureExpression", [E], {
Number: { value: tstype("number") },
String: { value: tstype("string") },
Boolean: { value: tstype("boolean") },
None: {},
Conditional: {
condition: E,
consequent: E,
alternative: E,
},
Function: { lambdaList: LambdaList.type, body: tsArray(E) },
});

const output = `
// This file is generated
import { Location } from "./input";

${printADT(Identifier)}
${printADT(LambdaList)}
${printADT(Expression)}
`;

fs.writeFileSync(path.join(__dirname, "src/syntax-generated.ts"), output);
5 changes: 3 additions & 2 deletions packages/delisp-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"main": "dist/src/index.js",
"license": "MIT",
"scripts": {
"build": "tsc --build tsconfig.json",
"build": "yarn run generate-syntax && tsc --build tsconfig.json",
"build:all": "tsc --build tsconfig.all.json",
"test": "jest"
"test": "jest",
"generate-syntax": "ts-node ./generate-syntax.ts"
},
"files": [
"dist/"
Expand Down
2 changes: 1 addition & 1 deletion packages/delisp-core/src/macroexpand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function macroexpandLambda(lambda: S.SFunction): S.Expression {
node: {
tag: "function",
lambdaList: {
tag: "function-lambda-list",
tag: "lambdaList",
positionalArguments: [
{ tag: "identifier", name: "*context*", location: lambda.location },
...lambdaList.positionalArguments,
Expand Down
2 changes: 1 addition & 1 deletion packages/delisp-core/src/syntax-convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function parseLambdaList(ll: ASExpr): S.LambdaList {
});

return {
tag: "function-lambda-list",
tag: "lambdaList",
positionalArguments: symbols.map(parseIdentifier),
location: ll.location,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/delisp-core/src/syntax-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export function wrapInLambda(e: S.Expression): S.Expression {
node: {
tag: "function",
lambdaList: {
tag: "function-lambda-list",
tag: "lambdaList",
positionalArguments: [],
location: e.location,
},
Expand Down
77 changes: 21 additions & 56 deletions packages/delisp-core/src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
//

import { Location } from "./input";
import { TypeWithWildcards } from "./type-wildcards";
import { Type } from "./types";

//
// Expressions
//
import {
Identifier,
LambdaList,
SBooleanF,
SConditionalF,
SFunctionF,
SNoneF,
SNumberF,
SStringF,
} from "./syntax-generated";
import { TypeWithWildcards } from "./type-wildcards";
import { Type } from "./types";

interface SNumberF {
tag: "number";
value: number;
}

interface SStringF {
tag: "string";
value: string;
}

interface SBooleanF {
tag: "boolean";
value: boolean;
}

interface SNoneF {
tag: "none";
}

export interface Identifier {
tag: "identifier";
name: SVar;
location: Location;
}
export { Identifier, LambdaList };

type SVar = string;
interface SVariableReferenceF {
Expand All @@ -45,13 +31,6 @@ interface SVariableReferenceF {
closedFunctionEffect?: Type;
}

interface SConditionalF<E> {
tag: "conditional";
condition: E;
consequent: E;
alternative: E;
}

interface SFunctionCallF<E> {
tag: "function-call";
fn: E;
Expand All @@ -62,20 +41,6 @@ interface SFunctionCallF<E> {
arguments: E[];
}

export interface LambdaList {
tag: "function-lambda-list";
// The positional arguments for the lambda-list. This is
// internal.
positionalArguments: Identifier[];
location: Location;
}

interface SFunctionF<E> {
tag: "function";
lambdaList: LambdaList;
body: E[];
}

interface SVectorConstructorF<E> {
tag: "vector";
values: E[];
Expand Down Expand Up @@ -159,10 +124,10 @@ interface SUnknownF<_E> {
}

type AnyExpressionF<I = {}, E = Expression<I>> =
| SNumberF
| SStringF
| SBooleanF
| SNoneF
| SNumberF<E>
| SStringF<E>
| SBooleanF<E>
| SNoneF<E>
| SVariableReferenceF
| SConditionalF<E>
| SFunctionCallF<E>
Expand Down Expand Up @@ -193,10 +158,10 @@ export interface Expression<I = {}>
export interface SVariableReference<I = {}>
extends Node<I, SVariableReferenceF> {}

export interface SNumber<I = {}> extends Node<I, SNumberF> {}
export interface SString<I = {}> extends Node<I, SStringF> {}
export interface SBoolean<I = {}> extends Node<I, SBooleanF> {}
export interface SNone<I = {}> extends Node<I, SNoneF> {}
export interface SNumber<I = {}> extends Node<I, SNumberF<Expression<I>>> {}
export interface SString<I = {}> extends Node<I, SStringF<Expression<I>>> {}
export interface SBoolean<I = {}> extends Node<I, SBooleanF<Expression<I>>> {}
export interface SNone<I = {}> extends Node<I, SNoneF<Expression<I>>> {}

export interface SConditional<I = {}>
extends Node<I, SConditionalF<Expression<I>>> {}
Expand Down
1 change: 1 addition & 0 deletions packages/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"lib": ["ES2019"],
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
Expand Down