-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Codegen verification * Test verification on travis * Revert test
- Loading branch information
Showing
10 changed files
with
182 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
import { writeFile } from "fs"; | ||
import { CommonOperatorCodeGenOptions, OperatorCodeGenOptions } from "./common"; | ||
import { genCommonTypes } from "./genCommon"; | ||
import { genOperatorTests } from "./genTests"; | ||
import { genOperatorTypes } from "./genTypes"; | ||
import { codeGenSpec } from "./spec"; | ||
|
||
const PATH_PREFIX = "src/exponent/"; | ||
const PATH_PREFIX = "src/exponent"; | ||
|
||
export function emitCommonTypes(options: CommonOperatorCodeGenOptions) { | ||
emitFile("common.ts", genCommonTypes(options)); | ||
export interface EmitPlan { | ||
path: string; | ||
source: string; | ||
} | ||
|
||
export function emitOperator(options: OperatorCodeGenOptions) { | ||
const { fileNamePrefix } = options; | ||
emitFile(`${fileNamePrefix}.ts`, genOperatorTypes(options)); | ||
emitFile(`__test__/${fileNamePrefix}Spec.ts`, genOperatorTests(options)); | ||
export function emit(callback?: () => void) { | ||
const emits: EmitPlan[] = getEmitPlans(); | ||
|
||
let index = -1; | ||
const nextEmit = () => { | ||
index++; | ||
const isLastEmit = index === emits.length - 1; | ||
emitFile(emits[index], isLastEmit ? callback : nextEmit); | ||
}; | ||
nextEmit(); | ||
} | ||
|
||
export function getEmitPlans(): EmitPlan[] { | ||
const { operators, ...common } = codeGenSpec; | ||
const emits: EmitPlan[] = [{ path: `${PATH_PREFIX}/common.ts`, source: genCommonTypes(codeGenSpec) }]; | ||
operators.forEach(operator => { | ||
const operatorSpec = { ...operator, ...common }; | ||
const { fileNamePrefix } = operator; | ||
emits.push( | ||
{ path: `${PATH_PREFIX}/${fileNamePrefix}.ts`, source: genOperatorTypes(operatorSpec) }, | ||
{ path: `${PATH_PREFIX}/__test__/${fileNamePrefix}Spec.ts`, source: genOperatorTests(operatorSpec) }, | ||
); | ||
}); | ||
return emits; | ||
} | ||
|
||
function emitFile(path: string, content: string) { | ||
writeFile(PATH_PREFIX + path, content, error => { | ||
function emitFile({ path, source }: EmitPlan, callback?: () => void) { | ||
writeFile(path, source, error => { | ||
if (error) { | ||
console.error(`There was an error writing to ${path}`); | ||
} else { | ||
console.log(`Generated ${path}`); | ||
if (callback) { | ||
callback(); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { emit } from "./emit"; | ||
|
||
emit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { CodeGenSpec } from "./common"; | ||
|
||
const maxExponent = 5; | ||
|
||
export const codeGenSpec: CodeGenSpec = { | ||
minExponent: -maxExponent, | ||
maxExponent, | ||
operators: [ | ||
{ | ||
fileNamePrefix: "addition", | ||
uncurriedTypeNamePrefix: "Add", | ||
curriedTypeNamePrefix: "Add", | ||
testTypeNamePrefix: "Sum", | ||
specialCases: { | ||
[0]: "R", | ||
}, | ||
compute: (left, right) => left + right, | ||
}, | ||
{ | ||
fileNamePrefix: "multiplication", | ||
uncurriedTypeNamePrefix: "Multiply", | ||
curriedTypeNamePrefix: "MultiplyBy", | ||
testTypeNamePrefix: "Product", | ||
specialCases: { | ||
[0]: "0", | ||
[1]: "R", | ||
}, | ||
compute: (left, right) => left * right, | ||
}, | ||
{ | ||
fileNamePrefix: "division", | ||
uncurriedTypeNamePrefix: "Divide", | ||
curriedTypeNamePrefix: "DividedBy", | ||
testTypeNamePrefix: "Quotient", | ||
specialCases: { | ||
[0]: "(R extends 0 ? ArithmeticError : 0)", | ||
}, | ||
compute: (left, right) => left / right, | ||
}, | ||
], | ||
}; |
Oops, something went wrong.