Skip to content

Commit

Permalink
fix: many small things
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Oct 1, 2023
1 parent a023da3 commit 0542ef4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 38 deletions.
6 changes: 3 additions & 3 deletions examples/lexer/src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class Lexer extends ArrayStepper<string> {
return this.addToken(Syntax.EQUAL, undefined, true);
}

case '"':
case "\"":
case "'":
return this.readString();

Expand Down Expand Up @@ -254,8 +254,8 @@ export class Lexer extends ArrayStepper<string> {
}


private addToken<T extends ValueType = ValueType>(type: Syntax, value?: T, advance = false): void {
if (advance)
private addToken<T extends ValueType = ValueType>(type: Syntax, value?: T, doAdvance = false): void {
if (doAdvance)
this.advance();

const locationSpan = new LocationSpan(this.lastLocation, this.currentLocation);
Expand Down
17 changes: 11 additions & 6 deletions src/code-generator-meta.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { BindingName, ParameterDeclaration, PropertyName, TypeNode } from "typescript";
import Constants from "./constants";




export const enum Context {
Global,
SwitchStatement,
Block,
FunctionBody,
Block
SwitchStatement,
EnumBody,
ClassBody
}

export interface MetaValues extends Record<string, unknown> {
Expand All @@ -17,8 +22,8 @@ export interface MetaValues extends Record<string, unknown> {
arrowFunctionName?: string;
publicClassProperties: ParameterDeclaration[];
protectedClassProperties: { name: BindingName | PropertyName; type?: TypeNode }[];
allFunctionIdentifiers: string[];
asyncFunctionIdentifiers: string[];
allFunctionIdentifiers: Set<string>;
asyncFunctionIdentifiers: Set<string>;
spreadParameter: boolean;
// bindingCount: number;
}
Expand All @@ -32,8 +37,8 @@ export const DEFAULT_META: MetaValues = {
arrowFunctionName: undefined,
publicClassProperties: [],
protectedClassProperties: [],
allFunctionIdentifiers: [...Constants.REVERSE_ARGS_GLOBAL_FUNCTIONS, "parseInt", "parseFloat"],
asyncFunctionIdentifiers: [],
allFunctionIdentifiers: new Set<string>([...Constants.REVERSE_ARGS_GLOBAL_FUNCTIONS, "parseInt", "parseFloat"]),
asyncFunctionIdentifiers: new Set<string>(),
spreadParameter: false
// bindingCount: 0
};
111 changes: 82 additions & 29 deletions src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,20 @@ export default class CodeGenerator extends StringBuilder {
const access = <PropertyAccessExpression>node;
const objectText = access.expression.getText(this.sourceNode);
const propertyNameText = access.name.getText(this.sourceNode);
if (this.accessMacros.matchesCompleteReplace(objectText, propertyNameText)) {
this.accessMacros.completeReplace(objectText, propertyNameText);
break; // don't continue
}
const accessingThis = objectText === "this";
if (this.accessMacros.matchesCompleteReplace(objectText, propertyNameText))
return this.accessMacros.completeReplace(objectText, propertyNameText);

this.walk(access.expression);
this.append(access.expression.kind === SyntaxKind.ThisKeyword ? "" : ".");
if (this.accessMacros.matchesKeyReplace(propertyNameText)) {
this.accessMacros.keyReplace(propertyNameText);
break; // don't continue
if (accessingThis && this.meta.allFunctionIdentifiers.has(propertyNameText)) {
this.popLastPart();
this.append("self")
}

this.append(this.peekLastPart() === "@" ? "" : ".");
if (this.accessMacros.matchesKeyReplace(propertyNameText))
return this.accessMacros.keyReplace(propertyNameText);

this.walk(access.name);
if (this.accessMacros.matchesExtension(propertyNameText))
this.accessMacros.addExtension(propertyNameText);
Expand All @@ -261,23 +263,30 @@ export default class CodeGenerator extends StringBuilder {

const objectText = access.expression.getText(this.sourceNode);
const indexText = access.argumentExpression.getText(this.sourceNode);
if (this.accessMacros.matchesCompleteReplace(objectText, indexText.substring(1, -2))) {
this.accessMacros.completeReplace(objectText, indexText.substring(1, -2));
const indexStringContent = indexText.substring(1, -2);
const accessingThis = objectText === "this";
if (accessingThis && this.meta.allFunctionIdentifiers.has(indexStringContent)) {
this.popLastPart();
this.append("self");
}

if (this.accessMacros.matchesCompleteReplace(objectText, indexStringContent)) {
this.accessMacros.completeReplace(objectText, indexStringContent);
break; // don't continue
}

this.walk(access.expression);
if (this.accessMacros.matchesKeyReplace(indexText.substring(1, -2))) {
if (this.accessMacros.matchesKeyReplace(indexStringContent)) {
this.append(".");
this.accessMacros.keyReplace(indexText.substring(1, -2));
this.accessMacros.keyReplace(indexStringContent);
break; // don't continue
}

this.append("[");
this.walk(access.argumentExpression);
this.append("]");
if (this.accessMacros.matchesExtension(indexText.substring(1, -2)))
this.accessMacros.addExtension(indexText.substring(1, -2));
if (this.accessMacros.matchesExtension(indexStringContent))
this.accessMacros.addExtension(indexStringContent);

break;
}
Expand All @@ -303,7 +312,7 @@ export default class CodeGenerator extends StringBuilder {
if (functionName[0] !== functionName[0].toLowerCase())
return this.error(call.expression, "Function names cannot begin with capital letters.", "FunctionBeganWithCapital");

if (this.meta.currentContext === Context.Global && this.meta.asyncFunctionIdentifiers.includes(functionName))
if (this.meta.currentContext === Context.Global && this.meta.asyncFunctionIdentifiers.has(functionName))
this.append("await ");

const reverseGlobalArgs = Constants.REVERSE_ARGS_GLOBAL_FUNCTIONS.includes(functionName) && !isPropertyAccess;
Expand Down Expand Up @@ -487,13 +496,16 @@ export default class CodeGenerator extends StringBuilder {
this.pushIndentation();
this.newLine();

const enclosingContext = this.meta.currentContext;
this.meta.currentContext = Context.EnumBody;
for (const member of declaration.members)
this.walk(member);

this.popIndentation();
this.newLine();
this.append("end");
this.newLine();
this.meta.currentContext = enclosingContext;
break;
}
case SyntaxKind.EnumMember: {
Expand Down Expand Up @@ -537,6 +549,10 @@ export default class CodeGenerator extends StringBuilder {
const declaration = <ClassDeclaration>node;
this.appendClassDeclaration(
() => {
for (const member of declaration.members) // add names first
if (member.kind === SyntaxKind.MethodDeclaration)
this.meta.allFunctionIdentifiers.add((<Identifier>member.name).text);

for (const member of declaration.members)
this.walk(member);
},
Expand Down Expand Up @@ -564,6 +580,7 @@ export default class CodeGenerator extends StringBuilder {
this.walkType(signature.type);
}

this.newLine();
break;
}
case SyntaxKind.PropertyDeclaration: {
Expand Down Expand Up @@ -706,6 +723,9 @@ export default class CodeGenerator extends StringBuilder {
if (!bodyIsBlock)
this.popIndentation();

if (this.peekLastPart().includes("\n"))
this.popLastPart();

this.newLine();
this.append("end");
this.newLine();
Expand Down Expand Up @@ -733,6 +753,9 @@ export default class CodeGenerator extends StringBuilder {
if (!bodyIsBlock)
this.popIndentation();

if (this.peekLastPart().includes("\n"))
this.popLastPart();

this.newLine();
this.append("end");
this.newLine();
Expand Down Expand Up @@ -760,10 +783,13 @@ export default class CodeGenerator extends StringBuilder {
if (!thenBodyIsBlock)
this.popIndentation();

if (ifStatement.elseStatement) {
if (this.peekLastPart().includes("\n"))
this.popLastPart();

if (ifStatement.elseStatement) {
const elseBranchIsIf = ifStatement.elseStatement.kind === SyntaxKind.IfStatement;
this.newLine();
this.append(ifStatement.elseStatement.kind === SyntaxKind.IfStatement ? "els" : "else");
this.append("else");

const elseBodyIsBlock = ifStatement.elseStatement.kind === SyntaxKind.Block;
if (!elseBodyIsBlock) {
Expand All @@ -776,6 +802,9 @@ export default class CodeGenerator extends StringBuilder {
this.popIndentation();
}

if (this.peekLastPart().includes("\n"))
this.popLastPart();

this.newLine();
this.append("end");
this.newLine();
Expand Down Expand Up @@ -809,16 +838,21 @@ export default class CodeGenerator extends StringBuilder {
this.walk(tryStatement.finallyBlock);
this.popLastPart();
this.popIndentation();
this.newLine();
}

if (this.peekLastPart().includes("\n"))
this.popLastPart();

this.newLine();
this.append("end");
this.newLine();
break;
}
case SyntaxKind.ThrowStatement: {
const throwStatement = <ThrowStatement>node;
this.append("raise ");
this.walk(throwStatement.expression);
this.newLine();
break;
}
case SyntaxKind.SwitchStatement: {
Expand Down Expand Up @@ -917,7 +951,7 @@ export default class CodeGenerator extends StringBuilder {
break;
}
case SyntaxKind.RegularExpressionLiteral: {
this.append(`/${(<RegularExpressionLiteral>node).text}/`);
this.append(`TsRegex.new(${(<RegularExpressionLiteral>node).text})`);
break;
}
case SyntaxKind.TemplateExpression: {
Expand All @@ -935,7 +969,7 @@ export default class CodeGenerator extends StringBuilder {
break;
}
case SyntaxKind.StringLiteral: {
this.append(`"${(<StringLiteral>node).text}"`);
this.append((<StringLiteral>node).getFullText(this.sourceNode).trim());
break;
}
case SyntaxKind.TrueKeyword: {
Expand All @@ -954,16 +988,24 @@ export default class CodeGenerator extends StringBuilder {
const caseBlock = <CaseBlock>node;
this.newLine();

let lastEmpty = false;
for (const clause of caseBlock.clauses) {
if (isCaseClause(clause)) {
this.append("when ");
if (!lastEmpty)
this.append("when ");

if (lastEmpty)
this.append(", ");

this.walk(clause.expression);
} else
this.append("else");

this.appendBlock(clause);
this.popLastPart();
this.newLine();
lastEmpty = clause.statements.length === 0;
if (!lastEmpty) {
this.appendBlock(clause);
this.newLine();
}
}

break;
Expand Down Expand Up @@ -1064,6 +1106,8 @@ export default class CodeGenerator extends StringBuilder {
this.newLine();
}

const enclosingContext = this.meta.currentContext;
this.meta.currentContext = Context.ClassBody;
walkMembers();
if (this.meta.publicClassProperties.length > 0)
this.newLine();
Expand Down Expand Up @@ -1109,6 +1153,7 @@ export default class CodeGenerator extends StringBuilder {
this.newLine();
this.append("end");
this.newLine();
this.meta.currentContext = enclosingContext
}

private appendBlock<T extends Node & { statements: NodeArray<Statement> }>(node: T, module = false): void {
Expand Down Expand Up @@ -1137,7 +1182,7 @@ export default class CodeGenerator extends StringBuilder {

this.append("(");
for (const arg of callArguments)
if (arg.kind === SyntaxKind.Identifier && this.meta.allFunctionIdentifiers.includes((<Identifier>arg).text)) {
if (arg.kind === SyntaxKind.Identifier && this.meta.allFunctionIdentifiers.has((<Identifier>arg).text)) {
if (providedBlock || providedArrowFunction)
this.error(arg, "Functions may only have one function argument.", "MultipleFunctionsPassed");

Expand Down Expand Up @@ -1188,6 +1233,7 @@ export default class CodeGenerator extends StringBuilder {
}

private handleExporting(): void {
if (this.meta.currentContext !== Context.Global) return;
const isExported = this.consumeFlag("Export");
if (!isExported)
this.append("private ");
Expand All @@ -1210,7 +1256,7 @@ export default class CodeGenerator extends StringBuilder {
body?: Block
) {

this.meta.allFunctionIdentifiers.push(typeof name === "string" ? name : name.text);
this.meta.allFunctionIdentifiers.add(typeof name === "string" ? name : name.text);
this.walkModifierList(modifiers?.values(), false);
const modifierKinds = modifiers?.map(mod => mod.kind);

Expand Down Expand Up @@ -1268,7 +1314,7 @@ export default class CodeGenerator extends StringBuilder {

const isAsync = this.consumeFlag("Async");
if (isAsync) {
this.meta.asyncFunctionIdentifiers.push(typeof name === "string" ? name : name.text);
this.meta.asyncFunctionIdentifiers.add(typeof name === "string" ? name : name.text);
this.pushIndentation();
this.newLine();
this.append("async! do");
Expand Down Expand Up @@ -1484,8 +1530,15 @@ export default class CodeGenerator extends StringBuilder {
}

private getMappedIdentifier(text: string): string {
return text
.replace(/Error/, "Exception");
const replaced = text
.replace(/Error/, "Exception")
.replace(/undefined/, "nil")
.replace(/null/, "nil");

if (/^[A-Z_]+$/.test(replaced) && this.meta.currentContext === Context.Global)
return replaced.toLowerCase();
else
return replaced;
}

private getMappedType(text: string): string {
Expand Down

0 comments on commit 0542ef4

Please sign in to comment.