Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e committed Aug 19, 2019
1 parent 5aef82d commit 4189f1d
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 54 deletions.
3 changes: 0 additions & 3 deletions Backer/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ module.exports = {
"browser": true,
"node": true,
"es6": true
},
"parserOptions": {
"project": "./tsconfig.json"
}
}
5 changes: 3 additions & 2 deletions Backer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"artistry-code-style": "0.0.2",
"eslint": "^6.1.0",
"eslint-plugin-filenames": "^1.3.2",
"liltest": "0.0.5",
"prettier-eslint": "^9.0.0",
"ts-node": "^8.3.0",
"typescript": "^3.5.3",
"prettier-eslint": "^9.0.0"
"typescript": "^3.5.3"
}
}
38 changes: 32 additions & 6 deletions Backer/src/X.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as X from "../../Truth/Core/X";
import { outdent } from "../../Truth/CoreTests/Framework/TestUtil";
import { IR } from "./compiler/IR";
import { IR } from "./compiler/Ir";
import { JSEmitter } from "./compiler/JavaScriptEmitter";
import { Writer } from "./writer/Writer";
import * as fs from "fs";
import { Placeholder } from "./writer/Placeholder";

function main()
{
const source = outdent`
String
Number
Test User
User
Name: String
Age: Number
Expand All @@ -27,10 +30,33 @@ function main()

const prog = new X.Program();
const doc = prog.documents.create(source);

const result = IR.parseTruth(doc);
JSEmitter(result);

const path = __dirname + "/file.txt";
const fd = fs.openSync(path, "w+");

const writer = new Writer({
write(data, position)
{
console.log(position, data);
fs.writeSync(fd, data, position, "utf-8");
},
truncate(length)
{
fs.truncateSync(path, length);
}
});

const id1 = new Placeholder("World");
const id2 = new Placeholder("XXX");

writer.insert("Hllo ");
writer.insert("e", 1);
writer.insertIdentifier(id1);
writer.insert("!\n");

writer.insertIdentifier(id2);

id1.edit("Parsa");
}

main();
setInterval(() => null, 1e4);
97 changes: 97 additions & 0 deletions Backer/src/compiler/DiffService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { IR } from "./Ir";
import { Emitter } from "./Emitter";

export class DiffService
{
constructor(
public readonly ir: IR.Document,
public readonly emitter: Emitter
) {}
}

export enum DiffKind {
ChildrenAdded,
ChildrenRemoved,
ChildrenRenamed,
InheritanceAdded,
InheritanceRemoved,
DeclarationAdded,
DeclarationRemoved
}

interface DiffBase {
readonly kind: DiffKind;
}

export type Diff =
| ChildrenAdded
| ChildrenRemoved
| ChildrenRenamed
| InheritanceAdded
| InheritanceRemoved
| DeclarationAdded
| DeclarationRemoved;

export class ChildrenAdded implements DiffBase
{
public readonly kind = DiffKind.ChildrenAdded;

constructor(
public readonly base: IR.Declaration,
public readonly child: IR.Declaration
) {}
}

export class ChildrenRemoved implements DiffBase
{
public readonly kind = DiffKind.ChildrenRemoved;

constructor(
public readonly base: IR.Declaration,
public readonly child: IR.Declaration
) {}
}

export class ChildrenRenamed implements DiffBase
{
public readonly kind = DiffKind.ChildrenRenamed;

constructor(
public readonly base: IR.Declaration,
public readonly child: IR.Declaration
) {}
}

export class InheritanceAdded implements DiffBase
{
public readonly kind = DiffKind.InheritanceAdded;

constructor(
public readonly base: IR.Declaration,
public readonly name: string
) {}
}

export class InheritanceRemoved implements DiffBase
{
public readonly kind = DiffKind.InheritanceRemoved;

constructor(
public readonly base: IR.Declaration,
public readonly name: string
) {}
}

export class DeclarationAdded implements DiffBase
{
public readonly kind = DiffKind.DeclarationAdded;

constructor(public readonly declaration: IR.Declaration) {}
}

export class DeclarationRemoved implements DiffBase
{
public readonly kind = DiffKind.DeclarationRemoved;

constructor(public readonly declaration: IR.Declaration) {}
}
25 changes: 25 additions & 0 deletions Backer/src/compiler/Emitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IR } from "./Ir";
import { Writer } from "../writer/Writer";
import * as D from "./DiffService";

export abstract class Emitter
{
constructor(
public readonly ir: IR.Document,
public readonly writer: Writer
) {}

abstract [D.DiffKind.ChildrenAdded](diff: D.ChildrenAdded): void;

abstract [D.DiffKind.ChildrenRemoved](diff: D.ChildrenRemoved): void;

abstract [D.DiffKind.ChildrenRenamed](diff: D.ChildrenRenamed): void;

abstract [D.DiffKind.InheritanceAdded](diff: D.InheritanceAdded): void;

abstract [D.DiffKind.InheritanceRemoved](diff: D.InheritanceRemoved): void;

abstract [D.DiffKind.DeclarationAdded](diff: D.DeclarationAdded): void;

abstract [D.DiffKind.DeclarationRemoved](diff: D.DeclarationRemoved): void;
}
29 changes: 22 additions & 7 deletions Backer/src/compiler/IR.ts → Backer/src/compiler/Ir.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as X from "../../../Truth/Core/X";
import { camelize } from "./util";
import { camelize } from "./Util";

/**
* This namespace contains all the functionalities and tools required to
* deal with the Truth Intermediate representation.
*
*
* @internal
*/
export namespace IR {
Expand All @@ -26,6 +26,11 @@ export namespace IR {
parent?: Declaration;
};

export interface Representation {
statements: X.Statement[];
declarations: Document;
}

/**
* Used in toIR to store deceleration stack entities.
* @internal
Expand All @@ -40,11 +45,16 @@ export namespace IR {
* @param doc A Truth document.
* @returns The generated representation.
*/
export function parseTruth(doc: X.Document): Document {
export function parseTruth(doc: X.Document): Representation
{
const statements: X.Statement[] = [];
const result = new Set<Declaration>();
let declarationStack: DeclarationInfo[] = [];

for (const statement of doc.eachStatement()) {
for (const statement of doc.eachStatement())
{
statements.push(statement);

if (statement.isWhitespace || statement.isComment || statement.isNoop)
continue;

Expand All @@ -67,14 +77,16 @@ export namespace IR {

const declarationStackEntity = { indent, declaration };

if (indent === 0) {
if (indent === 0)
{
declarationStack = [declarationStackEntity];
result.add(declaration);
continue;
}

let lastDeclarationInfo: DeclarationInfo;
do {
do
{
lastDeclarationInfo = declarationStack.pop()!;
} while (lastDeclarationInfo.indent >= indent);

Expand All @@ -89,6 +101,9 @@ export namespace IR {
parent.declarationName = parent.parent.declarationName + parent.name;
}

return [...result];
return {
statements,
declarations: [...result]
};
}
}
61 changes: 34 additions & 27 deletions Backer/src/compiler/JavaScriptEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import { IR } from "./IR";
import { IR } from "./Ir";
import { outdent } from "../../../Truth/CoreTests/Framework/TestUtil";

export function JSEmitter(declarations: IR.Document): void {
let result: string = `const TruthFactory = require("truth-factory");`;
export function JSEmitter(declarations: IR.Document): void
{
let result: string = `const TruthFactory = require("truth-factory");`;

const isSpecial = (name: string): boolean => {
return name === "String" || name === "Number" || name === "Boolean";
}
const isSpecial = (name: string): boolean =>
{
return name === "String" || name === "Number" || name === "Boolean";
};

const quote = (name: string): string => isSpecial(name) ? name : `"${name}"`;
const quote = (name: string): string => isSpecial(name) ? name : `"${name}"`;

const type2str = (field: IR.Declaration): string => {
if (field.declarationName !== field.name) {
return quote(field.declarationName);
}
const type2str = (field: IR.Declaration): string =>
{
if (field.declarationName !== field.name)
{
return quote(field.declarationName);
}

if (field.inheritedFrom.length === 0) {
return "TruthFactor.any";
}
if (field.inheritedFrom.length === 0)
{
return "TruthFactor.any";
}

if (field.inheritedFrom.length === 1) {
return quote(field.inheritedFrom[0]);
}
if (field.inheritedFrom.length === 1)
{
return quote(field.inheritedFrom[0]);
}

return `TruthFactory.union(${field.inheritedFrom.map(quote).join()})`;
};
return `TruthFactory.union(${field.inheritedFrom.map(quote).join()})`;
};

for (const declaration of declarations) {
if (isSpecial(declaration.name)) continue;
for (const declaration of declarations)
{
if (isSpecial(declaration.name)) continue;

const code = outdent`
const code = outdent`
const ${declaration.declarationName} = TruthFactory(
"${declaration.declarationName}",
[${declaration.inheritedFrom.map(name => `"${name}"`).join()}],
Expand All @@ -39,10 +46,10 @@ export function JSEmitter(declarations: IR.Document): void {
);
`;

result += "\n" + code;
}
result += "\n" + code;
}

result += `\nTruthFactory.resolve();`
result += `\nTruthFactory.resolve();`;

console.log(result);
}
console.log(result);
}
6 changes: 4 additions & 2 deletions Backer/src/compiler/util.ts → Backer/src/compiler/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @param {string} str The input string.
* @returns string The input in upperCameCase
*/
export function camelize(str: string): string {
export function camelize(str: string): string
{
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, word => word.toUpperCase())
.replace(/\s+/g, "");
Expand All @@ -14,7 +15,8 @@ export function camelize(str: string): string {
* @param {string} str The input string.
* @returns string The input in lowerCamelCase.
*/
export function lowerCamelize(str: string): string {
export function lowerCamelize(str: string): string
{
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) =>
index === 0 ? word.toLowerCase() : word.toUpperCase()
Expand Down
7 changes: 0 additions & 7 deletions Backer/src/compiler/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions Backer/src/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello Parsa!
XXX
Loading

0 comments on commit 4189f1d

Please sign in to comment.