Skip to content

Commit

Permalink
feat: Adds expression for accessing the argument of variant match cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Mar 25, 2022
1 parent ba19cff commit 6a5c03e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tezwell/smartts-sdk",
"version": "0.7.1",
"version": "0.7.2",
"description": "SmartTS SDK is a metaprogramming framework for building Tezos smart contracts from Javascript.",
"keywords": [
"Tezos",
Expand Down
10 changes: 3 additions & 7 deletions src/core/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { LineInfo } from '../misc/utils';
import StatementAtom from './enums/statement';
import { Expression } from './expression';
import ExpressionAtom from './enums/expression';
import { Proxied, proxy } from '../misc/proxy';
import { Add, GetVariable, Iterator, LessThanOrEqual } from '../expression';
import { Proxied } from '../misc/proxy';
import { Add, GetVariable, Iterator, LessThanOrEqual, VariantCaseArgument } from '../expression';
import { NewVariable, SetValue } from '../statement';

export class Statement implements IStatement {
Expand Down Expand Up @@ -87,7 +87,7 @@ export class VariantMatchStatement implements IStatement {
variantArgumentName?: string,
): this {
variantArgumentName ||= `${this.argumentName}_${branch}`;
const variantArgument = this.variantArgument(variantArgumentName, this.line);
const variantArgument = VariantCaseArgument(variantArgumentName, this.line);
this.cases[branch] = { argumentName: variantArgumentName, statements: buildStatements(variantArgument) };
return this;
}
Expand All @@ -96,10 +96,6 @@ export class VariantMatchStatement implements IStatement {
return new Expression(ExpressionAtom.cases_arg, `"${argumentName}"`, line);
}

private variantArgument(argumentName: string, line: LineInfo) {
return proxy(new Expression(ExpressionAtom.variant_arg, `"${argumentName}"`, line), Expression.proxyHandler);
}

[Symbol.toPrimitive]() {
const caseArgument = this.caseArgument(this.argumentName, this.line);
const matchCases = Object.keys(this.cases).map((branch) => {
Expand Down
17 changes: 17 additions & 0 deletions src/expression/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ export const LambdaArgument = (
Expression.proxyHandler,
);

/**
* Get the argument of a variant match case.
*
* ```typescript
* VariantCaseArgument("action_1")
* ```
*
* @category | Variable Accessors
*
* @param name The name of the argument
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const VariantCaseArgument = (name: string, line = new LineInfo()) =>
proxy(new Expression<any>(ExpressionAtom.variant_arg, `"${name}"`, line), Expression.proxyHandler);

/**
* Get operations list from the stack or an empty list otherwise.
*
Expand Down

0 comments on commit 6a5c03e

Please sign in to comment.