From 20c30f3164cd6ddcfc1c990efd6820ff4a0801e6 Mon Sep 17 00:00:00 2001 From: fvictorio Date: Sat, 5 Jan 2019 15:44:42 -0300 Subject: [PATCH 1/3] Add support for AssemblyIf --- src/printer.js | 7 ++++++ tests/Assembly/Assembly.sol | 10 ++++++++ .../Assembly/__snapshots__/jsfmt.spec.js.snap | 25 +++++++++++++++++++ tests/Assembly/jsfmt.spec.js | 1 + 4 files changed, 43 insertions(+) create mode 100644 tests/Assembly/Assembly.sol create mode 100644 tests/Assembly/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/Assembly/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index 87cda58a8..f2b0d3258 100644 --- a/src/printer.js +++ b/src/printer.js @@ -483,6 +483,13 @@ function genericPrint(path, options, print) { ':=', path.call(print, 'expression') ]); + case 'AssemblyIf': + return concat([ + 'if ', + path.call(print, 'condition'), + ' ', + path.call(print, 'body') + ]); case 'FunctionTypeName': { const returns = returnTypes => { if (returnTypes.length > 0) { diff --git a/tests/Assembly/Assembly.sol b/tests/Assembly/Assembly.sol new file mode 100644 index 000000000..dd3de2b6d --- /dev/null +++ b/tests/Assembly/Assembly.sol @@ -0,0 +1,10 @@ +contract Assembly { + function ifAssembly() { + assembly { + if + returndatasize { + success := 0 + } + } + } +} diff --git a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..b04102a00 --- /dev/null +++ b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Assembly.sol 1`] = ` +contract Assembly { + function ifAssembly() { + assembly { + if + returndatasize { + success := 0 + } + } + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +contract Assembly { + function ifAssembly() { + assembly { + if returndatasize { + success := 0 + } + } + } +} + +`; diff --git a/tests/Assembly/jsfmt.spec.js b/tests/Assembly/jsfmt.spec.js new file mode 100644 index 000000000..989047bcc --- /dev/null +++ b/tests/Assembly/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); From e1a8b587619ac79d7204b890fa190210ad938271 Mon Sep 17 00:00:00 2001 From: fvictorio Date: Sat, 5 Jan 2019 16:01:09 -0300 Subject: [PATCH 2/3] Print assembly cases blocks --- src/printer.js | 2 +- tests/Assembly/Assembly.sol | 12 ++++++++++ .../Assembly/__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index f2b0d3258..ed43563f0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -469,7 +469,7 @@ function genericPrint(path, options, print) { } else { doc = concat(['case ', path.call(print, 'value')]); } - return join(' ', [doc, '{}']); + return join(' ', [doc, path.call(print, 'block')]); case 'AssemblyLocalDefinition': return join(' ', [ 'let', diff --git a/tests/Assembly/Assembly.sol b/tests/Assembly/Assembly.sol index dd3de2b6d..ab7525203 100644 --- a/tests/Assembly/Assembly.sol +++ b/tests/Assembly/Assembly.sol @@ -7,4 +7,16 @@ contract Assembly { } } } + + function caseAssembly() { + assembly { + switch value + case 0 { + mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) + } + case 1 { + mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) + } + } + } } diff --git a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap index b04102a00..c14f08b7c 100644 --- a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap +++ b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap @@ -10,6 +10,18 @@ contract Assembly { } } } + + function caseAssembly() { + assembly { + switch value + case 0 { + mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) + } + case 1 { + mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) + } + } + } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ contract Assembly { @@ -20,6 +32,18 @@ contract Assembly { } } } + + function caseAssembly() { + assembly { + switch value + case 0 { + mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) + } + case 1 { + mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) + } + } + } } `; From cc2482bb078c5cafc4ab3bbf8b5c1747e3acd621 Mon Sep 17 00:00:00 2001 From: fvictorio Date: Sun, 6 Jan 2019 15:35:37 -0300 Subject: [PATCH 3/3] Add support for AssemblyFor --- src/printer.js | 11 ++++++++++- tests/Assembly/Assembly.sol | 6 ++++++ .../Assembly/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index e1bc7fd89..2876c2466 100644 --- a/src/printer.js +++ b/src/printer.js @@ -471,7 +471,7 @@ function genericPrint(path, options, print) { return concat([ '{', indent(hardline), - indent(join(line, path.map(print, 'operations'))), + indent(printPreservingEmptyLines(path, 'operations', options, print)), hardline, '}' ]); @@ -522,6 +522,15 @@ function genericPrint(path, options, print) { ' ', path.call(print, 'body') ]); + case 'AssemblyFor': { + return join(' ', [ + 'for', + path.call(print, 'pre'), + path.call(print, 'condition'), + path.call(print, 'post'), + path.call(print, 'body') + ]); + } case 'FunctionTypeName': { const returns = returnTypes => { if (returnTypes.length > 0) { diff --git a/tests/Assembly/Assembly.sol b/tests/Assembly/Assembly.sol index ab7525203..64d0255ad 100644 --- a/tests/Assembly/Assembly.sol +++ b/tests/Assembly/Assembly.sol @@ -19,4 +19,10 @@ contract Assembly { } } } + + function forAssembly() { + assembly { +for { let i := 0 } lt(i, x) { i := add(i, 1) } { y := mul(2, y) } + } + } } diff --git a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap index c14f08b7c..cc1d413fe 100644 --- a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap +++ b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap @@ -22,6 +22,12 @@ contract Assembly { } } } + + function forAssembly() { + assembly { +for { let i := 0 } lt(i, x) { i := add(i, 1) } { y := mul(2, y) } + } + } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ contract Assembly { @@ -44,6 +50,18 @@ contract Assembly { } } } + + function forAssembly() { + assembly { + for { + let i := 0 + } lt(i, x) { + i := add(i, 1) + } { + y := mul(2, y) + } + } + } } `;