From a82e3abc907ba4284c0e13f6668a2d37eba31a49 Mon Sep 17 00:00:00 2001 From: crabonature Date: Sat, 10 Feb 2018 12:42:31 +0100 Subject: [PATCH 1/2] Persistent side effects with Dir --- src/icr/command_stack.cr | 23 ++++++++++++++++++++++- src/icr/console.cr | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/icr/command_stack.cr b/src/icr/command_stack.cr index c5a1dba..df127e7 100644 --- a/src/icr/command_stack.cr +++ b/src/icr/command_stack.cr @@ -32,6 +32,8 @@ module Icr type = :struct when .match /^alias\s/ type = :alias + when .match with_persistent_side_effect + type = :side_effect when .match /^[A-Z]([A-Za-z0-9_]+)?\s*=[^=~]/ type = :constant_assignment when .match /^macro\s/ @@ -39,7 +41,7 @@ module Icr else type = :regular end - @commands << Command.new(type, command) + do_push(Command.new(type, command)) end # Pop the last command. It's used in cases if the last command results into error. @@ -47,6 +49,10 @@ module Icr @commands.pop end + def printable_execution_result? + [:regular, :side_effect].includes?(@commands.last.type) + end + # Generate crystal source code, based on the command in the stack. def to_code code = @@ -64,6 +70,7 @@ module Icr def __icr_exec__ #{code(:regular, 1)} + #{code(:side_effect, 1)} end puts "#{DELIMITER}\#{__icr_exec__.inspect}" @@ -75,5 +82,19 @@ module Icr cmds = @commands.select { |cmd| cmd.type == command_type }.map &.value cmds.map { |cmd| (" " * indent_level) + cmd }.join("\n") end + + private def do_push(command) + pop_when_last_was_side_effect unless @commands.empty? + @commands << command + end + + # Pop the last command if the last command got side effects. + private def pop_when_last_was_side_effect + @commands.pop if @commands.last.type == :side_effect + end + + private def with_persistent_side_effect + /^Dir\.rmdir|^Dir\.mkdir_p|^Dir\.mkdir/ + end end end diff --git a/src/icr/console.cr b/src/icr/console.cr index 6d457f5..9821a3c 100644 --- a/src/icr/console.cr +++ b/src/icr/console.cr @@ -152,7 +152,7 @@ module Icr end private def print_execution_result? - @command_stack.commands.last.type == :regular + @command_stack.printable_execution_result? end private def __exit__ From 012bbd6c48b23b5986e79155fb1a69e252255611 Mon Sep 17 00:00:00 2001 From: crabonature Date: Sun, 11 Feb 2018 13:39:41 +0100 Subject: [PATCH 2/2] Persistent side effects with File and FileUtils --- src/icr/command_stack.cr | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/icr/command_stack.cr b/src/icr/command_stack.cr index df127e7..e2e2fe2 100644 --- a/src/icr/command_stack.cr +++ b/src/icr/command_stack.cr @@ -94,7 +94,11 @@ module Icr end private def with_persistent_side_effect - /^Dir\.rmdir|^Dir\.mkdir_p|^Dir\.mkdir/ + # Dir.mkdir(), Dir.mkdir_p(), Dir.rmdir(), + # File.delete(), File.link(), File.rename(), File.symlink(), + # FileUtils.mkdir(), FileUtils.mkdir_p(), FileUtils.mv(), + # FileUtils.rm, FileUtils.rm_r(), FileUtils.rm_rf(), FileUtils.rmdir() + /^Dir\.(mkdir|rmdir)|^FileUtils\.(mkdir|mv|rm)|^File\.(delete|link|rename|symlink)/ end end end