Skip to content

Commit

Permalink
Testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed Sep 26, 2023
1 parent bedf476 commit 599641b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
3 changes: 1 addition & 2 deletions app/gui2/parser-codegen/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/gui2/parser-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"scripts": {
"generate-ast-schema": "rm -rf generated && mkdir generated && cargo run -p enso-parser-schema > generated/ast-schema.json",
"preinstall": "npm run generate-ast-schema",
"start": "tsc && node dist/codegen.js generated/ast-schema.json generated/ast.ts"
"start": "tsc --build && node dist/codegen.js generated/ast-schema.json generated/ast.ts"
},
"keywords": [],
"devDependencies": {
"dependencies": {
"typescript": "^5.2.2"
}
}
26 changes: 15 additions & 11 deletions app/gui2/parser-codegen/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import * as fs from 'fs'
import * as ts from 'typescript'
import { factory as tsf } from 'typescript'

type Schema = {
types: [string: Schema.Type]
}
module Schema {
export type Type = {
name: string
Expand All @@ -25,6 +22,12 @@ module Schema {
export type Result = { class: 'result'; type0: TypeRef; type1: TypeRef }
export type PrimitiveType = 'bool' | 'u32' | 'u64' | 'i32' | 'i64' | 'char' | 'string'
}
type TypeGraph = {
[id: string]: Schema.Type
}
type Schema = {
types: TypeGraph
}

function fromSnake(ident: string, to: 'camel' | 'pascal', prefix?: string): string {
const segments = []
Expand Down Expand Up @@ -196,12 +199,13 @@ class Type {
this.size = size
}

static new(ref: Schema.TypeRef, types: [string: Schema.Type]): Type {
static new(ref: Schema.TypeRef, types: TypeGraph): Type {
const c = ref.class
switch (c) {
case 'type':
const ty = types[ref.id]
const parent = types[ty.parent]
const parentId = ty.parent
const parent = ty.parent != null ? types[ty.parent] : null
const typeName = namespacedName(ty.name, parent?.name)
const type = tsf.createTypeReferenceNode(typeName)
if (Object.entries(ty.discriminants).length !== 0) {
Expand Down Expand Up @@ -286,7 +290,7 @@ function seekCursor(cursor: ts.Expression, offset: number): ts.Expression {
function makeGetter(
fieldName: string,
fieldData: Schema.Field,
types: [string: Schema.Type],
types: TypeGraph,
): ts.GetAccessorDeclaration {
const type = Type.new(fieldData.type, types)
return tsf.createGetAccessorDeclaration(
Expand Down Expand Up @@ -314,7 +318,7 @@ function createAssignmentStatement(left: ts.Expression, right: ts.Expression): t
)
}

function makeConcreteType(id: string, types: [string: Schema.Type]): ts.ClassDeclaration {
function makeConcreteType(id: string, types: TypeGraph): ts.ClassDeclaration {
const ident = tsf.createIdentifier(toPascal(types[id].name))
const paramIdent = tsf.createIdentifier('cursor')
const cursorParam = tsf.createParameterDeclaration(
Expand Down Expand Up @@ -387,7 +391,7 @@ function makeClass(
name: ts.Identifier,
members: ts.ClassElement[],
id: string,
types: [string: Schema.Type],
types: TypeGraph,
): ts.ClassDeclaration {
const ty = types[id]
return tsf.createClassDeclaration(
Expand Down Expand Up @@ -419,7 +423,7 @@ function makeChildType(
base: ts.Identifier,
id: string,
discrim: string,
types: [string: Schema.Type],
types: TypeGraph,
): ChildType {
const ty: Schema.Type = types[id]
const name = toPascal(ty.name)
Expand Down Expand Up @@ -555,7 +559,7 @@ function abstractTypeDeserializer(
)
}

function makeAbstractType(id: string, types: [string: Schema.Type]) {
function makeAbstractType(id: string, types: TypeGraph) {
const ty = types[id]
const name = toPascal(ty.name)
const ident = tsf.createIdentifier(name)
Expand Down Expand Up @@ -654,7 +658,7 @@ emit(
undefined,
tsf.createNamedImports(
Array.from(Object.entries(support), ([name, _value]) =>
tsf.createImportSpecifier(undefined, undefined, tsf.createIdentifier(name)),
tsf.createImportSpecifier(false, undefined, tsf.createIdentifier(name)),
),
),
),
Expand Down
5 changes: 3 additions & 2 deletions app/gui2/parser-codegen/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"]
"lib": ["es2015"],
}

0 comments on commit 599641b

Please sign in to comment.