A Detour Into Pipes

26 October, 2026
All pages

It seems I overlooked some things in the last post. Such as how to catch error codes. This is part of the bigger picture of calling external programs, including features like handling standard in and standard out.

Most shell scripting languages use a special variable, frequently $?, to hold the last return code. There are other languages that do this, too. For a language where this is infrequent, a special variable is not that bad an idea, but I'm not sold on it. Converting the RUN command into a RUN() function, enabling any return code to be assigned to whatever variable you like might be a better solution.

However, this brings up the characteristic that external commands frequently have both an error code (and typically a 0 to mean there was no error) and output. The latter is usually called "standard out" and is a stream of binary data. This forms the core of how you can "pipe" the output of one command into the input of another in popular scripting languages. I don't think we should lose that ability in this one, so that means that | can't be used for any other language construct. Probably. In fact, given it would parse as a statement separator, that initially makes sense. But due to my intention to use a proper EBNF grammar, I think it is possible to use | as an expression operator in some contexts, providing it is inside some suitable delimited expression, such as with ( ).

This also brings up the use of > for directing standard out to a file. And <. And >>. My first impulse is to make sure these are defined in the grammar, and defined as at the end of a statement.

But that also brings up an interesting question: should internal statements support these?

For a subprogram call, I think the answer is probably yes. Though one immediate question is how to handle when standard out is redirected inside a subprogram. At first thought, maybe it could be captured in both places. But a step back reveals that that doesn't make sense. If you don't capture the output of the subprogram, what happens to any output that it creates with PRINT? It goes to standard out, of course. But output inside the subprogram that is captured does not get sent so, so why should explicitly capturing the subprogram's standard out catch it? Simple: it shouldn't. So that behaves the same for external programs as for subprograms.

What about internal commands, like a FOR loop? That's more difficult to decide and I don't have an answer at this time.

Another point almost sidelined is how to parse the filenames. On the command line, a filename will be an unquoted string, just like the command. But that prohibits variable substitution or any kind of string expression - unless we go to quoted mode. So in quoted mode, and this in any script, it's a string expression. Which means a literal name must be in quotes. But also means the name can be constructed with a string expression, including variables. That sounds like a win to me!

(I also mentioned last time about using / as expression operator. Well, one trick I've seen in some versions of SmallTalk is that / can concatenate two strings and make sure the delimiter character is correct. That is, if the string before it ends with a "/" then the operator won't add one. So "path/to/" / "file" would produce "path/to/file". But so would "path/to" / "file".)

Something else to touch on briefly is that we probably want some more native kind of stream support in the language. As in, open a file, get a stream, kind of thing. Specifying a variable containing a stream to the redirection clauses would be quite powerful. Exactly how to do that is a topic for another post, though.

One last overlooked item is handling an unqualified command name. If we're calling a program in a known location, it will probably be called with a path prefix. But if it is one of the hundreds of commands normally callable from the command line, well it won't. Because it will be in the PATH. Of course, this will only work when the program is in scripting mode. But it does bring up the question of precedence. That is, will a subprogram of the same name take precedence over an external command that can be found by searching all entries in the PATH environment variable? Maybe. Maybe not. I can envisage a need for some complex rules for when a subprogram in imported code might not override that... and times when it does. But I need to think some more about that.