Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node_modules
52 changes: 26 additions & 26 deletions src/codegen/expressions/arrow-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
BlockStatement,
FunctionParameter,
SourceLocation,
BinaryNode,
ReturnStatement,
IfStatement,
Expression,
} from "../../ast/types.js";
import {
ClosureAnalyzer,
Expand Down Expand Up @@ -272,7 +276,7 @@ export class ArrowFunctionExpressionGenerator extends BaseGenerator {
return "string";
}
if (bodyTyped.type === "binary") {
const binExpr = body as { type: string; op: string; left: unknown; right: unknown };
const binExpr = body as BinaryNode;
if (binExpr.op === "+") {
const leftType = this.inferReturnTypeFromBody(binExpr.left as ArrowFunctionNode["body"]);
const rightType = this.inferReturnTypeFromBody(binExpr.right as ArrowFunctionNode["body"]);
Expand Down Expand Up @@ -310,42 +314,38 @@ export class ArrowFunctionExpressionGenerator extends BaseGenerator {
}
}
if (bodyTyped.type === "block") {
const blockTyped = body as { type: string; statements: unknown[] };
const blockTyped = body as BlockStatement;
const blockStatements = blockTyped.statements;
if (blockStatements) {
for (let i = 0; i < blockStatements.length; i++) {
const stmt = blockStatements[i];
const stmtTyped = stmt as { type: string; value: unknown };
if (stmtTyped.type === "return" && stmtTyped.value) {
const returnValue = stmtTyped.value;
const returnValueTyped = returnValue as { type: string };
if (returnValueTyped.type === "object") {
return "object";
}
if (
returnValueTyped.type === "string" ||
returnValueTyped.type === "string_literal" ||
returnValueTyped.type === "template_literal"
) {
return "string";
const stmtBase = stmt as { type: string };
if (stmtBase.type === "return") {
const stmtTyped = stmt as ReturnStatement;
if (stmtTyped.value) {
const returnValueTyped = stmtTyped.value as { type: string };
if (returnValueTyped.type === "object") {
return "object";
}
if (
returnValueTyped.type === "string" ||
returnValueTyped.type === "string_literal" ||
returnValueTyped.type === "template_literal"
) {
return "string";
}
}
}
if (stmtTyped.type === "if") {
const ifStmt = stmt as {
type: string;
condition: unknown;
thenBlock: unknown;
elseBlock: unknown;
};
const thenBlock = ifStmt.thenBlock as { type: string; statements: unknown[] };
if (stmtBase.type === "if") {
const ifStmt = stmt as IfStatement;
const thenBlock = ifStmt.thenBlock;
const thenBlockStatements = thenBlock ? thenBlock.statements : null;
if (thenBlockStatements) {
for (let j = 0; j < thenBlockStatements.length; j++) {
const innerStmt = thenBlockStatements[j];
const innerStmtTyped = innerStmt as { type: string; value: unknown };
const innerStmtTyped = innerStmt as ReturnStatement;
if (innerStmtTyped.type === "return" && innerStmtTyped.value) {
const innerReturnValue = innerStmtTyped.value;
const innerReturnValueTyped = innerReturnValue as { type: string };
const innerReturnValueTyped = innerStmtTyped.value as { type: string };
if (innerReturnValueTyped.type === "object") {
return "object";
}
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/expressions/conditionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* - Phi node to merge results
*/

import { ConditionalExpressionNode, Expression } from "../../ast/types.js";
import { ConditionalExpressionNode, Expression, ArrayNode } from "../../ast/types.js";
import { IGeneratorContext } from "../infrastructure/generator-context.js";

export class ConditionalExpressionGenerator {
Expand Down Expand Up @@ -69,7 +69,7 @@ export class ConditionalExpressionGenerator {

this.emit(`${falseLabel}:`);
const savedExpectedType = this.ctx.getExpectedArrayElementType();
const falseExprTyped = expr.alternate as { type: string; elements?: Expression[] };
const falseExprTyped = expr.alternate as ArrayNode;
if (
falseExprTyped.type === "array" &&
(!falseExprTyped.elements || falseExprTyped.elements.length === 0)
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/expressions/literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ export class LiteralExpressionGenerator {
throw new Error("new RegExp() requires at least 1 argument");
}

const patternArg = args[0] as { type: string; value?: string };
const flagsArg = args.length > 1 ? (args[1] as { type: string; value?: string }) : null;
const patternArg = args[0] as StringNode;
const flagsArg = args.length > 1 ? (args[1] as StringNode) : null;

let flags = "";
if (flagsArg && flagsArg.type === "string" && flagsArg.value !== undefined) {
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/expressions/method-calls/class-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
InterfaceField,
FunctionNode,
FunctionParameter,
IndexAccessNode,
} from "../../../ast/types.js";
import type { MethodCallGeneratorContext } from "../method-calls.js";

Expand Down Expand Up @@ -672,7 +673,7 @@ export function handleClassMethods(
}
}
} else if (exprObjBase.type === "index_access") {
const indexAccess = expr.object as { type: string; object: Expression; index: Expression };
const indexAccess = expr.object as IndexAccessNode;
const baseExpr = indexAccess.object;
const baseExprBase = baseExpr as ExprBase;
if (baseExprBase.type === "member_access") {
Expand Down
11 changes: 5 additions & 6 deletions src/codegen/expressions/method-calls/console.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expression, MethodCallNode } from "../../../ast/types.js";
import { Expression, MethodCallNode, StringNode } from "../../../ast/types.js";
import type { MethodCallGeneratorContext } from "../method-calls.js";

function emitPrint(
Expand Down Expand Up @@ -163,16 +163,15 @@ function emitSingleArg(
arg: Expression,
params: string[],
): void {
const argTyped = arg as { type: string; value: string | number };

if (argTyped.type === "string") {
const strValue = argTyped.value as string;
if (arg.type === "string") {
const strNode = arg as StringNode;
const strValue = strNode.value;
const strConstPtr = ctx.stringGen.doCreateStringConstant(strValue);
emitPrintStrNoNl(ctx, useStderr, strConstPtr);
return;
}

if (argTyped.type === "number") {
if (arg.type === "number") {
const argValue = ctx.generateExpression(arg, params);
emitPrintNumNoNl(ctx, useStderr, argValue);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/expressions/method-calls/string-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ export function handleReplace(
const replaceArg = expr.args[1];

if (searchArg.type === "regex") {
const regexNode = searchArg as { type: string; pattern: string; flags: string };
const regexNode = searchArg as RegexNode;
const isGlobal = regexNode.flags.indexOf("g") !== -1;
const searchStr = ctx.stringGen.doGenerateGlobalString(regexNode.pattern);
const replaceStr = ctx.generateExpression(replaceArg, params);
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/expressions/operators/binary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expression, SourceLocation } from "../../../ast/types.js";
import { Expression, SourceLocation, NumberNode } from "../../../ast/types.js";
import type { IStringGenerator } from "../../infrastructure/generator-context.js";

interface ControlFlowGeneratorLike {
Expand Down Expand Up @@ -142,7 +142,7 @@ export class BinaryExpressionGenerator {
}

private isKnownInteger(expr: Expression): boolean {
const exprTyped = expr as { type: string; value?: number };
const exprTyped = expr as NumberNode;
if (exprTyped.type === "number" && typeof exprTyped.value === "number") {
return Number.isInteger(exprTyped.value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/expressions/operators/unary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class UnaryExpressionGenerator {
if (operand.type !== "variable") {
return this.ctx.emitError("Post-increment/decrement requires a variable operand");
}
const operandVar = operand as { type: string; name: string };
const operandVar = operand as VariableNode;
const varName = operandVar.name;
const allocaReg = this.ctx.getVariableAlloca(varName);
if (!allocaReg) {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class UnaryExpressionGenerator {
if (operand.type !== "variable") {
return this.ctx.emitError("Pre-increment/decrement requires a variable operand");
}
const operandVarPre = operand as { type: string; name: string };
const operandVarPre = operand as VariableNode;
const varName = operandVarPre.name;
const allocaReg = this.ctx.getVariableAlloca(varName);
if (!allocaReg) {
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/expressions/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* - With interpolation: `Hello ${name}` -> concatenate "Hello " with name value
*/

import { Expression, TemplateLiteralNode } from "../../ast/types.js";
import { Expression, TemplateLiteralNode, StringNode } from "../../ast/types.js";
import { IGeneratorContext } from "../infrastructure/generator-context.js";
import {
createStringConstant,
Expand Down Expand Up @@ -59,7 +59,7 @@ export class TemplateLiteralGenerator {
}

if (expr.parts.length === 1) {
const firstPart = expr.parts[0] as { type: string; value?: string };
const firstPart = expr.parts[0] as StringNode;
if (firstPart.type === "string") {
return this.ctx.stringGen.doCreateStringConstant(firstPart.value || "");
}
Expand All @@ -72,7 +72,7 @@ export class TemplateLiteralGenerator {
const part = expr.parts[_tpi];
let partValue: string;

const partAsObj = part as { type: string; value?: string };
const partAsObj = part as StringNode;
if (partAsObj.type === "string") {
partValue = this.ctx.stringGen.doCreateStringConstant(partAsObj.value || "");
} else {
Expand Down
Loading
Loading