Skip to content

Commit

Permalink
fix: a whole bunch of class stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Oct 1, 2023
1 parent c63e103 commit eba7951
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
18 changes: 12 additions & 6 deletions examples/lexer/src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ export class ArrayStepper<T extends unknown = unknown> {
protected position = 0;

public constructor(
protected readonly input: ArrayLike<T>
protected readonly input: ArrayLike<T>
) {}

protected peek(offset = 1): T | undefined {
const peekPosition = this.position + offset;
return peekPosition + 1 > this.input.length ? undefined : this.input[peekPosition];
const peekPosition = this.position + offset;
return peekPosition + 1 > this.input.length ? undefined : this.input[peekPosition];
}

protected get isFinished(): boolean {
return this.position + 1 > this.input.length;
return this.position + 1 > this.input.length;
}

protected get current(): T {
return this.peek(0)!;
return this.peek(0)!;
}
}

Expand All @@ -95,12 +95,18 @@ const NUMERIC = /^[0-9]$/;
const WHITESPACE = /\s+/;

export class Lexer extends ArrayStepper<string> {
private lastLocation: Location;

private line = 1;
private column = 1;
private lastLocation = new Location(this.line, this.column);
private currentLexemeCharacters: string[] = [];
private readonly tokens: Token[] = []

public constructor(source: string) {
super(source);
this.lastLocation = new Location(this.line, this.column);
}

public tokenize(): Token[] {
while (!this.isFinished)
this.lex();
Expand Down
3 changes: 2 additions & 1 deletion src/code-generator-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const enum Context {
FunctionBody,
SwitchStatement,
EnumBody,
ClassBody
ClassBody,
TypeParameters
}

export interface MetaValues extends Record<string, unknown> {
Expand Down
29 changes: 16 additions & 13 deletions src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,15 @@ export default class CodeGenerator extends StringBuilder {

if (typeParameters) {
this.append("(");

const enclosingContext = this.meta.currentContext;
this.meta.currentContext = Context.TypeParameters;

for (const typeParam of typeParameters)
this.walk(typeParam.name);

this.append(")");
this.meta.currentContext = enclosingContext;
}

const mixins: ExpressionWithTypeArguments[] = [];
Expand Down Expand Up @@ -1256,7 +1261,8 @@ export default class CodeGenerator extends StringBuilder {
body?: Block
) {

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

Expand All @@ -1265,9 +1271,7 @@ export default class CodeGenerator extends StringBuilder {
this.append("private ");

this.append("def ");

const isStatic = modifiers?.map(mod => mod.kind)?.includes(SyntaxKind.StaticKeyword);
if (isStatic)
if (modifiers?.map(mod => mod.kind)?.includes(SyntaxKind.StaticKeyword))
this.append("self.");

if (typeof name === "string")
Expand All @@ -1278,17 +1282,19 @@ export default class CodeGenerator extends StringBuilder {
if (parameters.length > 0) {
this.append("(");
for (const param of parameters) {
const modifierTypes = param.modifiers?.map(mod => mod.kind) ?? [];
if (modifierTypes.includes(SyntaxKind.PublicKeyword))
const modifierKinds = param.modifiers?.map(mod => mod.kind) ?? [];
if (modifierKinds.includes(SyntaxKind.PublicKeyword)) {
this.append("@");
this.meta.publicClassProperties.push(param);
else if (modifierTypes.includes(SyntaxKind.ProtectedKeyword))
} else if (modifierKinds.includes(SyntaxKind.ProtectedKeyword)) {
this.append("@");
this.meta.protectedClassProperties.push({
name: param.name,
type: param.type
});
else if (modifierTypes.includes(SyntaxKind.StaticKeyword))
} else if (modifierKinds.includes(SyntaxKind.StaticKeyword))
this.append("@@");
else if (modifierTypes.includes(SyntaxKind.ReadonlyKeyword) || modifierTypes.includes(SyntaxKind.PrivateKeyword))
else if (modifierKinds.includes(SyntaxKind.PrivateKeyword) || modifierKinds.includes(SyntaxKind.ReadonlyKeyword))
this.append("@");

this.walk(param);
Expand Down Expand Up @@ -1393,10 +1399,6 @@ export default class CodeGenerator extends StringBuilder {
this.append(isProperty ? "property " : "");
break;
}
case SyntaxKind.ReadonlyKeyword: {
this.append("getter ");
break;
}
case SyntaxKind.StaticKeyword: {
this.append(isProperty ? "@@" : "");
break;
Expand All @@ -1409,6 +1411,7 @@ export default class CodeGenerator extends StringBuilder {
this.pushFlag("Export");
break;
}
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.DeclareKeyword: {
break;
}
Expand Down

0 comments on commit eba7951

Please sign in to comment.