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 dc7ab5a973bcca63371b3b2c57f05664253cf4f5 Mon Sep 17 00:00:00 2001 From: fvictorio Date: Mon, 7 Jan 2019 07:48:03 -0300 Subject: [PATCH 3/3] Indent switch cases in assembly code --- src/printer.js | 7 ++++++- tests/Assembly/__snapshots__/jsfmt.spec.js.snap | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/printer.js b/src/printer.js index e1bc7fd89..5d3c75337 100644 --- a/src/printer.js +++ b/src/printer.js @@ -494,7 +494,12 @@ function genericPrint(path, options, print) { return node.value; case 'AssemblySwitch': doc = join(hardline, path.map(print, 'cases')); - return concat(['switch ', path.call(print, 'expression'), hardline, doc]); + return concat([ + 'switch ', + path.call(print, 'expression'), + indent(hardline), + indent(doc) + ]); case 'AssemblyCase': if (node.default) { doc = concat(['default']); diff --git a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap index c14f08b7c..1743ebafe 100644 --- a/tests/Assembly/__snapshots__/jsfmt.spec.js.snap +++ b/tests/Assembly/__snapshots__/jsfmt.spec.js.snap @@ -36,12 +36,12 @@ contract Assembly { function caseAssembly() { assembly { switch value - case 0 { - mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) - } - case 1 { - mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) - } + case 0 { + mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) + } + case 1 { + mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) + } } } }