Skip to content

Commit

Permalink
minor (#663): added better code generation for typeof-expression.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzholzbauer committed Jul 25, 2024
1 parent 41df507 commit 11e74af
Show file tree
Hide file tree
Showing 18 changed files with 3,981 additions and 2,838 deletions.
11 changes: 11 additions & 0 deletions kipper/core/src/compiler/ast/ast-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import type {
ClassPropertyDeclarationContext,
ClassMethodDeclarationContext,
ClassConstructorDeclarationContext,
TypeofExpressionContext,
} from "../lexer-parser";
import { InterfaceMemberDeclarationContext } from "../lexer-parser";
import type { KipperProgramContext } from "../program-ctx";
Expand Down Expand Up @@ -771,6 +772,16 @@ export class KipperFileASTGenerator implements KipperParserListener, ParseTreeLi
public exitActualBitwiseShiftExpression: (ctx: ActualBitwiseShiftExpressionContext) => void =
this.handleExitingTreeNode;

/**
* Enter a parse tree produced by the KipperParser.typeofExpression
*/
public enterTypeofExpression: (ctx: TypeofExpressionContext) => void = this.handleEnteringTreeNode;

/**
* Exit a parse tree produced by the KipperParser.typeofExpression
*/
public exitTypeofExpression: (ctx: TypeofExpressionContext) => void = this.handleExitingTreeNode;

// NOTE:
// We are ignoring the 'conditionalExpression' rule, and only going to handle the rule
// 'actualConditionalExpression', which implements a more precise 'conditionalExpression' rule.
Expand Down
6 changes: 4 additions & 2 deletions kipper/core/src/compiler/ast/mapping/ast-node-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ import {
SliceNotationMemberAccessExpressionContext,
StringPrimaryExpressionContext,
SwitchStatementContext,
TangledPrimaryExpressionContext, TypeofExpressionContext,
TangledPrimaryExpressionContext,
TypeofExpressionContext,
TypeofTypeSpecifierExpressionContext,
VariableDeclarationContext,
VoidOrNullOrUndefinedPrimaryExpressionContext,
Expand All @@ -68,7 +69,8 @@ import type {
ASTStatementKind,
ASTStatementRuleName,
} from "../common";
import { Declaration, Expression, Statement, TypeofExpression } from "../nodes";
import type { Declaration, Expression, Statement } from "../nodes";
import { TypeofExpression } from "../nodes";
import {
AdditiveExpression,
ArrayPrimaryExpression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import type { ASTNodeMapper } from "../../../mapping";
* Union type of all possible {@link ParserASTNode.kind} values for a constructable {@link PostfixExpression} AST node.
* @since 0.10.0
*/
export type ASTPostfixExpressionKind = typeof ParseRuleKindMapping.RULE_incrementOrDecrementPostfixExpression | typeof ParseRuleKindMapping.RULE_typeofExpression;
export type ASTPostfixExpressionKind =
| typeof ParseRuleKindMapping.RULE_incrementOrDecrementPostfixExpression
| typeof ParseRuleKindMapping.RULE_typeofExpression;

/**
* Union type of all possible {@link ParserASTNode.kind} context classes for a constructable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import type { KipperPostfixOperator } from "../../../../../const";
* typeof(49); // "__kipper.builtIn.num"
* typeof("Hello, World!"); // "__kipper.builtIn.str"
*/
export class TypeofExpression extends PostfixExpression<
TypeofExpressionSemantics,
TypeofExpressionTypeSemantics
> {
export class TypeofExpression extends PostfixExpression<TypeofExpressionSemantics, TypeofExpressionTypeSemantics> {
/**
* The static kind for this AST Node.
* @since 0.12.0
Expand Down Expand Up @@ -83,7 +80,7 @@ export class TypeofExpression extends PostfixExpression<
}

public hasSideEffects(): boolean {
return true; // This expression has side effects as it modifies the value of the operand
return false; // This expression has side effects as it modifies the value of the operand
}

/**
Expand Down Expand Up @@ -117,7 +114,7 @@ export class TypeofExpression extends PostfixExpression<
*/
public async primarySemanticTypeChecking(): Promise<void> {
this.typeSemantics = {
// This will always be a number
// This will always be of type "type" as the typeof operator always returns a type
evaluatedType: BuiltInTypes.type,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import type { Reference } from "../../../../../semantics";
* @since 0.8.0
*/
export interface TypeofTypeSpecifierExpressionSemantics extends TypeSpecifierExpressionSemantics {
ref: Reference
ref: Reference;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import type { CompilableASTNode } from "../../../../compilable-ast-node";
import { TypeSpecifierExpression } from "../type-specifier-expression";
import type { TypeofTypeSpecifierExpressionContext } from "../../../../../lexer-parser";
import { KindParseRuleMapping, ParseRuleKindMapping } from "../../../../../lexer-parser";
import {
BuiltInTypes,
RawType,
} from "../../../../../semantics";
import { BuiltInTypes, RawType } from "../../../../../semantics";

/**
* Typeof type specifier expression, which represents a runtime typeof expression evaluating the type of a value.
Expand Down Expand Up @@ -93,7 +90,7 @@ export class TypeofTypeSpecifierExpression extends TypeSpecifierExpression<
ref: {
refTarget: ref,
srcExpr: this,
}
},
};
}

Expand Down
5 changes: 1 addition & 4 deletions kipper/core/src/compiler/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,7 @@ export const kipperUnaryModifierOperators: Array<KipperUnaryModifierOperator> =
* All available unary operators in Kipper, which can be used to modify the value of an expression.
* @since 0.9.0
*/
export type KipperUnaryOperator =
| KipperUnaryModifierOperator
| KipperPostfixOperator
| KipperBitwiseNotOperator;
export type KipperUnaryOperator = KipperUnaryModifierOperator | KipperPostfixOperator | KipperBitwiseNotOperator;

/**
* All available unary operators in Kipper, which can be used to modify the value of an expression.
Expand Down
Loading

0 comments on commit 11e74af

Please sign in to comment.