Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/cashc/src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ export class ConstantModificationError extends CashScriptError {
}
}

export class InvalidModifierError extends CashScriptError { }

export class ArrayElementError extends CashScriptError {
constructor(
node: ArrayNode,
Expand Down
1 change: 1 addition & 0 deletions packages/cashc/src/ast/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class ParameterNode extends Node implements Named, Typed {
constructor(
public type: Type,
public name: string,
public modifiers: string[] = [],
) {
super();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cashc/src/ast/AstBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ export default class AstBuilder

visitParameter(ctx: ParameterContext): ParameterNode {
const type = parseType(ctx.typeName().getText());
const modifiers = ctx.modifier_list().map((modifier) => modifier.getText());
const name = ctx.Identifier().getText();
const parameter = new ParameterNode(type, name);
const parameter = new ParameterNode(type, name, modifiers);
parameter.location = Location.fromCtx(ctx);
return parameter;
}
Expand Down
1 change: 1 addition & 0 deletions packages/cashc/src/ast/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export enum Class {

export enum Modifier {
CONSTANT = 'constant',
UNUSED = 'unused',
}

export const GLOBAL_SYMBOL_TABLE = new SymbolTable();
Expand Down
2 changes: 2 additions & 0 deletions packages/cashc/src/ast/SymbolTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { functionReturnType } from '../utils.js';

export class Symbol {
references: IdentifierNode[] = [];
ignoreUnused: boolean = false;

private constructor(
public name: string,
Expand Down Expand Up @@ -90,6 +91,7 @@ export class SymbolTable {
unusedSymbols(): Symbol[] {
return Array.from(this.symbols)
.map((e) => e[1])
.filter((s) => !s.ignoreUnused)
.filter((s) => s.references.length === 0);
}
}
31 changes: 30 additions & 1 deletion packages/cashc/src/generation/GenerateTargetTraversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
ForNode,
} from '../ast/AST.js';
import AstTraversal from '../ast/AstTraversal.js';
import { GlobalFunction, Class } from '../ast/Globals.js';
import { GlobalFunction, Class, Modifier } from '../ast/Globals.js';
import { BinaryOperator } from '../ast/Operator.js';
import {
compileBinaryOp,
Expand Down Expand Up @@ -174,6 +174,7 @@ export default class GenerateTargetTraversal extends AstTraversal {
for (let i = node.parameters.length - 1; i >= 0; i -= 1) {
bodyTraversal.visit(node.parameters[i]);
}
bodyTraversal.dropUnusedParameters(node.parameters);

bodyTraversal.visit(node.body);
bodyTraversal.cleanGlobalFunctionStack(node);
Expand Down Expand Up @@ -216,6 +217,7 @@ export default class GenerateTargetTraversal extends AstTraversal {

// Keep track of constructor parameter count for instructor pointer calculation
this.constructorParameterCount = node.parameters.length;
this.dropUnusedParameters(node.parameters);

if (node.functions.length === 1) {
node.functions = this.visitList(node.functions) as FunctionDefinitionNode[];
Expand Down Expand Up @@ -271,6 +273,7 @@ export default class GenerateTargetTraversal extends AstTraversal {
this.currentFunction = node;

node.parameters = this.visitList(node.parameters) as ParameterNode[];
this.dropUnusedParameters(node.parameters);

if (this.compilerOptions.enforceFunctionParameterTypes) {
this.enforceFunctionParameterTypes(node);
Expand Down Expand Up @@ -338,6 +341,24 @@ export default class GenerateTargetTraversal extends AstTraversal {
this.tagScopeCleanup(tagStartIndex);
}

private dropUnusedParameters(parameters: ParameterNode[]): void {
parameters
.filter((parameter) => parameter.modifiers.includes(Modifier.UNUSED))
.sort((a, b) => this.getStackIndex(a.name) - this.getStackIndex(b.name))
.forEach((parameter) => {
const stackIndex = this.getStackIndex(parameter.name);
const locationData = { location: parameter.location, positionHint: PositionHint.START };

if (stackIndex > 0) {
this.emit(encodeInt(BigInt(stackIndex)), locationData);
this.emit(Op.OP_ROLL, locationData);
}

this.emit(Op.OP_DROP, locationData);
this.removeFromStack(stackIndex);
});
}

enforceFunctionParameterTypes(node: FunctionDefinitionNode): void {
node.parameters.forEach((parameter) => this.enforceFunctionParameterType(parameter));
}
Expand Down Expand Up @@ -378,6 +399,7 @@ export default class GenerateTargetTraversal extends AstTraversal {
}

shouldEnforceFunctionParameterType(node: ParameterNode): boolean {
if (node.modifiers.includes(Modifier.UNUSED)) return false;
if (node.type === PrimitiveType.BOOL) return true;
if (node.type instanceof BytesType && node.type.bound !== undefined) return true;
return false;
Expand All @@ -390,6 +412,13 @@ export default class GenerateTargetTraversal extends AstTraversal {

visitVariableDefinition(node: VariableDefinitionNode): Node {
node.expression = this.visit(node.expression);

if (node.modifier.includes(Modifier.UNUSED)) {
this.emit(Op.OP_DROP, { location: node.location, positionHint: PositionHint.END });
this.popFromStack();
return node;
}

this.popFromStack();
this.pushToStack(node.name);
return node;
Expand Down
3 changes: 2 additions & 1 deletion packages/cashc/src/grammar/CashScript.g4
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ parameterList
;

parameter
: typeName Identifier
: typeName modifier* Identifier
;

block
Expand Down Expand Up @@ -194,6 +194,7 @@ expression

modifier
: 'constant'
| 'unused'
;

literal
Expand Down
4 changes: 3 additions & 1 deletion packages/cashc/src/grammar/CashScript.interp

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions packages/cashc/src/grammar/CashScript.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,27 @@ T__60=61
T__61=62
T__62=63
T__63=64
VersionLiteral=65
BooleanLiteral=66
NumberUnit=67
NumberLiteral=68
NumberPart=69
ExponentPart=70
PrimitiveType=71
UnboundedBytes=72
BoundedBytes=73
Bound=74
StringLiteral=75
DateLiteral=76
HexLiteral=77
TxVar=78
UnsafeCast=79
NullaryOp=80
Identifier=81
WHITESPACE=82
COMMENT=83
LINE_COMMENT=84
T__64=65
VersionLiteral=66
BooleanLiteral=67
NumberUnit=68
NumberLiteral=69
NumberPart=70
ExponentPart=71
PrimitiveType=72
UnboundedBytes=73
BoundedBytes=74
Bound=75
StringLiteral=76
DateLiteral=77
HexLiteral=78
TxVar=79
UnsafeCast=80
NullaryOp=81
Identifier=82
WHITESPACE=83
COMMENT=84
LINE_COMMENT=85
'pragma'=1
';'=2
'cashscript'=3
Expand Down Expand Up @@ -146,4 +147,5 @@ LINE_COMMENT=84
'&&'=62
'||'=63
'constant'=64
'bytes'=72
'unused'=65
'bytes'=73
5 changes: 4 additions & 1 deletion packages/cashc/src/grammar/CashScriptLexer.interp

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions packages/cashc/src/grammar/CashScriptLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,27 @@ T__60=61
T__61=62
T__62=63
T__63=64
VersionLiteral=65
BooleanLiteral=66
NumberUnit=67
NumberLiteral=68
NumberPart=69
ExponentPart=70
PrimitiveType=71
UnboundedBytes=72
BoundedBytes=73
Bound=74
StringLiteral=75
DateLiteral=76
HexLiteral=77
TxVar=78
UnsafeCast=79
NullaryOp=80
Identifier=81
WHITESPACE=82
COMMENT=83
LINE_COMMENT=84
T__64=65
VersionLiteral=66
BooleanLiteral=67
NumberUnit=68
NumberLiteral=69
NumberPart=70
ExponentPart=71
PrimitiveType=72
UnboundedBytes=73
BoundedBytes=74
Bound=75
StringLiteral=76
DateLiteral=77
HexLiteral=78
TxVar=79
UnsafeCast=80
NullaryOp=81
Identifier=82
WHITESPACE=83
COMMENT=84
LINE_COMMENT=85
'pragma'=1
';'=2
'cashscript'=3
Expand Down Expand Up @@ -146,4 +147,5 @@ LINE_COMMENT=84
'&&'=62
'||'=63
'constant'=64
'bytes'=72
'unused'=65
'bytes'=73
Loading
Loading