From f085e8e14bf410e7ba3a2cec43b3c63a89e00c33 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Tue, 31 Dec 2024 20:56:32 +0000 Subject: [PATCH] Execute expressions properly This way we can actually capture their execution status --- lib/IUtils/Compiler.rakumod | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/IUtils/Compiler.rakumod b/lib/IUtils/Compiler.rakumod index 90054c6..a4cddad 100644 --- a/lib/IUtils/Compiler.rakumod +++ b/lib/IUtils/Compiler.rakumod @@ -52,6 +52,21 @@ class IdrisError is Exception { } } +#| An error coming from a compiled expression +class ExpressionError is Exception { + has Str $.out; + has Str $.err; + has Int $.exit-code; + has Str $.expr; + + method message { + qq:to/END/; + Error running expression: $.expr + Command exited with $.exit-code + END + } +} + #| Invoke an idris command sub idris-run(*@cmd) is export { my $proc = run "idris2", @cmd, :out, :err; @@ -69,5 +84,17 @@ sub idris-run(*@cmd) is export { # TODO: Special handling for IO Bool to make this eaiser #| Exec the expression with the given name in the given file sub idris-exec($expr, $file) is export { - idris-run '--find-ipkg', '--exec', $expr, $file + # Have idris compile an executable for the expression, + idris-run '--find-ipkg', '--client', ":c iutils_out $expr", $file; + # Run the expression + my $proc = run 'build/exec/iutils_out', :out, :err; + my $out = $proc.out.slurp(:close); + my $err = $proc.err.slurp(:close); + unless $proc { + ExpressionError.new( + out => $out, err => $err, + exit-code => $proc.exitcode, expr => $expr + ).throw; + } + return $out; }