From 10bd72e858a8ffaacafa296dbbc429dc73b0111c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 18 Jan 2019 14:22:17 -0800 Subject: Added `tree` back as a parameter to compile actions, which helps with better error reporting, e.g. for (fail) (no arguments). Overall better error reporting now. Also added shorthand ("Action" tree with ...) for (SyntaxTree {.type = "Action", .1 = ...}). --- lib/shell/init.nom | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'lib/shell/init.nom') diff --git a/lib/shell/init.nom b/lib/shell/init.nom index d58894e..13dde56 100644 --- a/lib/shell/init.nom +++ b/lib/shell/init.nom @@ -1,12 +1,26 @@ # This file defines some actions for running shell commands. - + external: - (=sh $cmd) means: - lua> (" - local result = io.popen(\$cmd) - local contents = result:read("*a") - result:close() - return contents - ") - $(sh> $) = $os.execute + (at $callsite =sh $cmd) means: + $f = ($io.popen $cmd) + $contents = ($f, read "*a") + [$ok, $return_type, $return] = ($f, close) + unless $ok: + if ($return_type == "exit"): + at $callsite fail "Command failure: Command `\$cmd` failed with exit code \$return" + ..else: + at $callsite fail "Command failure: Command `\$cmd` was terminated by signal \$return" + return $contents + + (at $callsite sh> $cmd) means: + [$ok, $return_type, $return] = ($os.execute $cmd) + unless $ok: + if ($return_type == "exit"): + at $callsite fail "Command failure: Command `\$cmd` failed with exit code \$return" + ..else: + at $callsite fail "Command failure: Command `\$cmd` was terminated by signal \$return" + + # Attach callsite information for better error reporting + (=sh $cmd) compiles to (\(at ("Text" tree with "\($cmd.source)") =sh $cmd) as lua) + (sh> $cmd) compiles to (\(at ("Text" tree with "\($cmd.source)") sh> $cmd) as lua) -- cgit v1.2.3