Skip to content

Commit

Permalink
fix: yul function definition error
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Jan 25, 2024
1 parent 556bd4a commit 18cdebd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ast/yul/yul-function-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class YulFunctionDefinition extends BaseNode {
body: YulBlock | null = null;
constructor(ctx: YulFunctionDefinitionContext, visitor: SolidityParserVisitor<any>) {
super(ctx, visitor);
this.name = ctx._YulIdentifier!.text!;
this.name = ctx.YulIdentifier(0)!.getText();
this.parameters = ctx._arguments.map((arg) => arg.text!);
this.returnParameters = ctx._returnParameters.map((arg) => arg.text!);
this.body = ctx._body?.accept(visitor) ?? null;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/__snapshots__/prettier.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ contract AbiEncode {
_salt
)
}
function pos(length) -> pos {
function allocate(length) -> pos {
pos := mload(0x40)
}
}
Expand Down
48 changes: 28 additions & 20 deletions src/tests/yul.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,51 @@ import { test, expect } from 'vitest';

test('assemblyStatement', () => {
expect(createParse((p) => p.assemblyStatement())(`assembly { let x := 123 }`)).toMatchObject({
yulStatements: [{ type: 'YulVariableDeclaration', identifiers: ['x'] }],
yulStatements: [{ expression: { type: 'YulVariableDeclaration', identifiers: ['x'] } }],
});
expect(
createParse((p) => p.assemblyStatement())(`assembly { f.b, foo, bar := mul(x, y) }`),
).toMatchObject({
yulStatements: [{ type: 'YulAssignment', paths: ['f.b', 'foo', 'bar'] }],
yulStatements: [{ expression: { type: 'YulAssignment', paths: ['f.b', 'foo', 'bar'] } }],
});
expect(createParse((p) => p.assemblyStatement())(`assembly { mul(x, y) }`)).toMatchObject({
yulStatements: [{ type: 'YulFunctionCall', identifier: 'mul', expressions: ['x', 'y'] }],
yulStatements: [
{ expression: { type: 'YulFunctionCall', identifier: 'mul', expressions: ['x', 'y'] } },
],
});
expect(createParse((p) => p.assemblyStatement())(`assembly { if foo {} }`)).toMatchObject({
yulStatements: [{ type: 'YulIfStatement', condition: 'foo', body: {} }],
yulStatements: [{ expression: { type: 'YulIfStatement', condition: 'foo', body: {} } }],
});
expect(createParse((p) => p.assemblyStatement())(`assembly { for {} bar {} {} }`)).toMatchObject({
yulStatements: [
{
type: 'YulForStatement',
initializationBlock: {},
conditionExpression: 'bar',
loopBlock: {},
body: {},
expression: {
type: 'YulForStatement',
initializationBlock: {},
conditionExpression: 'bar',
loopBlock: {},
body: {},
},
},
],
});
expect(
createParse((p) => p.assemblyStatement())(`assembly { leave break continue }`),
).toMatchObject({
yulStatements: ['leave', 'break', 'continue'],
yulStatements: [{ expression: 'leave' }, { expression: 'break' }, { expression: 'continue' }],
});
expect(
createParse((p) => p.assemblyStatement())(`assembly { switch x case 0 {} default {} }`),
).toMatchObject({
yulStatements: [
{
type: 'YulSwitchStatement',
expression: 'x',
switchCases: [{ case: '0' }],
default: true,
body: {},
expression: {
type: 'YulSwitchStatement',
expression: 'x',
switchCases: [{ case: '0' }],
default: true,
body: {},
},
},
],
});
Expand All @@ -50,11 +56,13 @@ test('assemblyStatement', () => {
).toMatchObject({
yulStatements: [
{
type: 'YulFunctionDefinition',
name: 'foo',
parameters: ['x', 'y'],
returnParameters: ['a', 'b'],
body: {},
expression: {
type: 'YulFunctionDefinition',
name: 'foo',
parameters: ['x', 'y'],
returnParameters: ['a', 'b'],
body: {},
},
},
],
});
Expand Down

0 comments on commit 18cdebd

Please sign in to comment.