RePIG is pure syntactic sugar atop R47 RPN. Its name should never be interpreted as providing lipstick on a 🐷 because that would be rude and wholly unnecessary. No, this is a serious feature 🧐 and not for funzies at all.
If you wish a better explanation than that, accept that RePIG is decidedly not kosher, yet cheerfully dirty and light on its trotters.
Another interpretation might occur to you on recalling the old proverb:
If a pig flies, no one laughs if it fails to stick the landing.
So please be nice! ☺️
RePIG takes inspiration from RPL’s « ... » program notation and structured control forms, then compiles the result down to the same R47 RPN program IR that rejig uses internally.
The default output form is therefore plain RPN. With no plan for a RePIG/RPL output form,1 this cannot be expected to work:
rejig --fmt program.repig ❌
Sorry; you’re on your own when it comes to formatting RePIG code. Fortunately, rejig accepts free-form input for its basic UTF-8 path, atop which RePIG is built. Spaces, tabs, newlines, and carriage returns work equally for separating input words.
RePIG does not add capabilities to the R47 proper. If the calculator cannot do a thing, RePIG cannot make it do that thing. Critically, this means RePIG does not provide symbolic algebra manipulation (CAS), nor metaprogramming, and it never will. The sole overarching goal is to let R47 programmers write in a clearer source form, particularly those with RPL experience.
Syntax Inference
RePIG parsing works atop regular rejig UTF-8 input parsing.
The official extension for the language is .repig, but it starts parsing the file as with rejig --from utf8 until it finds a syntax clue that tells it to shift into RePIG mode.
The same is true of the generic UTF-8 file extensions .txt and .utf8, but I made an exception for the .p47u extension on the grounds that I invented it to mean “the UTF-8 form of an R47’s .p47 program file.” As a result, the RePIG compiler explicitly refuses to parse these RPL-style «blocks» when found in a file with that extension.
Basic Syntax
A RePIG program is enclosed in RPL-style angle quotes:2
«
1 2 +
»
The core language remains RPN-ish. Literals and operations execute left to right, pushing values and calling R47 operations much as ordinary RPN source does, using the same op-arg ordering. Whereas RPL would have you say…
42 'I' STO
…to set global variable ‘I’ to the value 42, RePIG requires you to say instead:
42 STO ‘I’
Both schemes require you to push 42 onto the stack first, but from there they differ. RPL follows the FORTH path by insisting that you provide all the arguments on the stack before calling STO whereas RePIG treats the storage location as an argument to the STO call itself, which then goes and collects the other argument from the stack. Viewed this way, RPL is more consistent. The upside is that my design choice in RePIG to have it take the RPN path will have a payoff, seen below.
The primary remnant of the FORTH-inspired postfix RPL processing in RePIG is the conditional clauses of the control structures:
«
IF X 2 == THEN // note FORTH-like postfix notation
‘YES’ ; R47-style string literal quoting
ELSE
"NO" @ HP-42S style quoting also allowed
END
» ‘IFTHEN’ STO # see below about LBL vs function objects
This example shows off the four different line-ending comment styles allowed in RePIG code. There are the three previously allowed for “rejig --from utf8” inputs, plus the RPL @ style. Real code should pick one of these and stick to as a matter of good style; two at most. Three is sloppy, and four is lunatic.
As for the substance of this example, the key thing to understand is that RePIG translates this to RPN at the end of input parsing. No special RePIG runtime is needed on your calculator to run the resulting program.
Note also that END inside a RePIG «block» does not mean the same thing as the R47 END opcode. RePIG code does not require an explicit END op; the compiler will provide it.
Literals
RePIG does not change or augment the rejig rules for interpretation of R47 style literals. Use the same syntax as in a .p47u file, even when the HP RPL legacy provides other methods. For instance, the HP-28S requires “(1,2)” syntax to denote the complex number 1+2𝒾; RePIG recognizes the latter, but not the legacy style.
Strings are another good example. RePIG supports both the R47’s single curly quoting style and the HP-42’s straight ASCII double quote style, but this is purely because rejig supports both. There is no special RPL-inspired rule at work here.
Identifiers
RePIG allows creation of named local variables using the RPL arrow syntax:3
«
→ a b «
RCL ‘a’ 𝑥²
RCL ‘b’ 𝑥²
+
√𝑥
STO ‘c’ DROP𝑥 VIEW ‘c’
»
» ‘Hypot’ STO
As you may guess from either the math or the name, this program takes the sides of a right triangle and computes its hypotenuse according to Pythagoras’ world-famous theorem: `a^2 + b^2 = c^2`
The arrow syntax causes RePIG to generate a program that consumes as many variables as follow; it stores them in R47 LocR variables and remembers the naming correspondence for later. Under the hood, those RCL statements are rewritten to reference those R.0𝑥 locals. Because this happens regardless of the name(s) given after the arrow, we say that these local variables “shadow” any globals that happen to have the same name.
This occurs regardless of whether you use R47 register or variable syntax to reference the name. In this way, it is possible to hide the stack registers from yourself until the subprogram ends, so be careful!
A good practice here is to use all-lowercase names for these local named variables, in a parallel to the R47’s scheme for local labels . The R47 does not presently have a way of creating named local variables, lowercase letter convention or no; all it has for now is the numbered sort, using that same R.𝑥𝑦 scheme we’ve hijacked to our own ends here. RePIG grants the R47 this highly-useful capability. Since there are zero registers and precious few built-in special variables named with a lowercase leading letter, this convention is likely to keep you out of trouble.
But beware: the ‘c’ variable in that same program is global! The STO command is sent straight through to the R47 layer untouched, so it is the R47’s rules that prevail here.
LSTO
RePIG solves this problem by providing the LSTO pseudo-op, akin to the one found in newRPL and in Free42. It extends this same mechanism for creating named local variables, but at a small usability cost: if you change the STO in the above example to LSTO, then call this program with 3 and 4 on the stack, the subsequent VIEW op will display R.02: 5 because the RePIG compiler rewrites that reference to ‘c’ as R.02. The R47 program execution loop has no idea that this has happened, causing it to report the underlying register name, not the named local variable that RePIG was given.
This is why the example above uses a named global variable: to allow the R47 to report the result using our preferred naming.
Note that unlike in RPL, our version of LSTO does not consume its argument from the stack. For consistency in the RPN body statement contexts where this can be used, it behaves like the RPN flavor of STO. The DROP𝑥 call in the example above is therefore still necessary to keep the result from being shown duplicated on the stack, should you change the STO to LSTO.
RePIG Is Not Tamper-Proof
Another possible trap is that nothing stops a RePIG program from referring directly to the underlying LocR registers employed by RePIG to hold these named local variables. RePIG diagnostics and documentation refer to the source-level name, not the local register mappings, but it is easy enough to study the resulting .p47u output to see these mappings. As a rule, you should not use LocR in a RePIG program to avoid interfering with this underlying mechanism.
At the same time, realize that the LocR allocation resets on every XEQ call, making it safe to call out to an R47 subroutine that uses LocR itself. Using GTO instead can create a problem since that doesn’t create an R47 stack frame, thus no separate LocR allocation.
Program Flow Control
One of the primary motivations behind the creation of RePIG is that until the STRUCT menu landed in September 2026, the R47 had little in the way of structured program flow control features.
See my articles on its branching and looping features for details — including coverage of the new facilities — but the key points to understand here are:
The RePIG parser takes precedence over R47 op interpretation. In all potential interpretation conflicts, the RePIG parser wins.
The only presently-defined R47
STRUCTops that do not collide are ones like the `FOR_x^y` variant of the plain R47FORop. Those with a distinguishing suffix get encoded in IR form on parsing, which is then emitted on RPN lowering, same as with any other RPN code inside an RePIG block. Colliding forms are shadowed by the RePIG parser when found inside a «block».(Technically, ops like
ENDIFdo not collide, but that’s no practical use when the parser shadows the openingIFand any mid-spanELSEops.)The RePIG compiler does not currently make use of any of these new
STRUCTfacilities in its code generator. It continues to lower an RPL-styleWHILEloop in terms of classic RPNISG/DSEtype looping ops. It is not a thin wrapper around the new nativeWHILEop.
Presently implemented are…
IF-THEN-ELSE
Let us revisit the example we saw above, but study it in more detail this time:
« IF X 2 == THEN
‘YES’
ELSE
‘NO’
END
» ‘IFTHEN’ STO
You immediately notice the keywords (IF, THEN, ELSE…) that aren’t part of HP RPN. What may be less obvious is that these create a block structure that overcomes the traditional RPN limitations. You aren’t limited to a single instruction to run in the if-true case, and you don’t need to make a GTO/GSB/XEQ call to provide “else” case processing.
Another detail to be aware of is that there is no necessary tie here between the logical test clause and the underlying calculator’s predefined tests. Not even the super-advanced R47 has a canned 𝑥=? 2 operation. It has to be synthesized from available ops, as we see in the rejig output:
LBL ‘IFTHEN’ ; alpha-STO at the end the block = LBL
RCL X ; make a copy; could be ENTER just as well
2 ; comparison value from IF condition
𝑥=? Y ; technically, this checks 2 = Y, not X = 2
GTO a ; local subroutine for the true case
DROP𝑥 ; RPL consumes its stack arguments;
DROP𝑥 ; it does not leave litter behind
0. ; false result from IF test
GTO b ; ELSE clause handling
LBL a ; THEN clause handling
DROP𝑥 ; we GTO'd over the cleanup above…
DROP𝑥 ; …must repeat it here
1. ; true result from IF test
LBL b ; common handling of true/false result
𝑥≠? 0. ; is this the TRUE case?
GTO c ; yes, go to actual handler
DROP𝑥 ; no; get rid of 0. IF test result first
GTO d ; go to false-case handler
LBL c ; true-case handler at last
DROP𝑥 ; get rid of 1. IF test result
‘YES’ ; what appears between THEN and ELSE
GTO e ; common end of condition
LBL d ; false-case handler
‘NO’ ; what appears between ELSE and END
LBL e ; fall through into common condition end
RTN ; implicit meaning of ‘»’ after LBL
END ; end of input program
If you’re thinking this is stupendously inefficient, you’re right, hand-optimized RPN will always be much tighter. If you wish to lose RPN programming contests gauged on lines of code, use the piggy-piggy RePIG; oink! The tradeoff is that RePIG code is much easier to read and write. You should not be expecting Clang levels of optimization from RePIG.
Or, in fact, any level of optimization!
I did warn you that this was quick and dirty little piglet, did I not? One commentator characterized RePIG code as having the PIG-STYle nature, and I who have perpetrated this affordance find myself unable to disagree. This rejig feature need only be useful to survive; beauty is not, alas, in-scope.
One big upside is that RePIG expands the range of operators to the general postfix scheme you see above. No longer are you limited to the built-in do-if-true tests, though you can still use them in these contexts:
42.+69.𝑖
IF ℂ? THEN
‘Yes, it's complex!’
END
Note that the ELSE clause is optional.
RePIG does not presently have anything like ELSEIF. You must manually chain the clauses:
IF first_cond THEN
⋮
ELSE
IF second_cond THEN
⋮
ELSE
IF third_cond THEN
⋮
END
END
END
CASE-THEN
Much like the switch statement in C-family languages, RePIG allows:
CASE
X 1 == THEN
‘ONE’
END
X 2 == THEN
‘TWO’
END
‘OTHER’
END
Do note that unlike C, each case does not need an explicit BREAK. RePIG does have a BREAK pseudo-op, but it will break out of an enclosing loop: WHILE-REPEAT, DO-UNTIL, or FOR-NEXT. If none such exists, BREAK will cause a compilation error.
This is how C’s continue works when used within a switch, which makes the parallel RePIG CONT pseudo-op more consistent in its behavior than what C-family languages provide.
(This should not be confused with the R47’s own CASE branching op. If you have to use both in a single program, it is best to make use of the fact that RePIG will not recognize its own feature outside «blocks», where you can therefore safely use the R47 op. I have not bothered testing whether you can use them both inside a block somehow; if you cannot, I am unsympathetic, and if you can, there is no guarantee that the construction won’t break in the future due to a keyword/operation conflict.)
WHILE-REPEAT
If you STO the value 5 into global register I and then run this…
0.
STO ‘iter’
WHILE I 3 > REPEAT
1.
STO+ ‘iter’
STO- I
DROP𝑥
END
VIEW ‘iter’
…it will report ‘iter’ = 2. because it:
- tests 5 and finds it greater than 3
- bumps the iteration counter
- decrements the loop control counter
- tests 4; still greater than 3
- repeat counter manipulation
- tests one last time and ends on finding
I=3 > 3false.
DO-UNTIL
If you keep the I=3 resulting from running the previous and then run this…
0.
STO ‘iter’
DO
1.
STO+ ‘iter’
STO- I
DROP𝑥
UNTIL I 0 <
END
VIEW ‘iter’
…your R47 will report ‘iter’ = 4. because the loop control test is now in the tail of the loop, causing it to check 2, 1, 0, and finally -1 before ending now that I<0 at last.
FOR-NEXT
Now for something a smidge more advanced. I present here the world’s first-ever4 RePIG solution to Project Euler Problem #1:
«
0. STO P ; zero our accumulator, global register P
3 999 FOR ‘i’ ; loop index is a local register unless ‘i’ is arrow-bound
IF ‘i’ 3 MOD 0 == ; first PEP #1 test
‘i’ 5 MOD 0 == ; second test
OR ; logic operator consumes both results
THEN ; if i%3 || i%5 { …
RCL ‘i’ ; no RPL-like bare vars; need explicit RCL
STO+ P ; same as "P 'i' + 'P' STO" in RPL
END
NEXT ; bump ‘i’ for next iteration of FOR loop
VIEW P ; show our (P)roblem's answer
» ‘PEP001’ STO
The primary difference between FOR-NEXT and the loop styles covered previously is that it manages the loop counter for you.
This code compiles down to classic HP RPN loop-control machinery: either ISG for count-up loops or DSE for count-down loops.
We touched on BREAK above. Here’s an overly complicated alternative to the above example:
«
0. STO P
0 1e9 FOR ‘i’
IF ‘i’ 3 MOD 0 == ‘i’ 5 MOD 0 == OR THEN
RCL ‘i’
STO+ P
IF 999 ‘i’ <= THEN BREAK END
END
NEXT
VIEW P
» ‘PEP001b’ STO
That first IF is on a single line now that I no longer have to leave room for comments. This makes it clearer that OR is an RPL-style logical test, given in the FORTH-like postfix order. I find this an admirable realignment in RPL, making everything more consistent.5
Do not confuse this Boolean logical operation with the R47’s bitwise OR op. This is being handled at the RePIG compiler level, which has the happy side effect that such code can at times lower to non-R47 machines.
The same conditional context is also what makes the bare quoted ‘i’ instances above mean “recall the value of variable ‘i’” rather than “push the string literal ‘i’ onto the stack.”
FOR-STEP
This is a simple extension of FOR-NEXT loops:
« π STO ‘I’ ; this global variable will be shadowed by…
1 8 FOR ‘I’ ; …this loop-scoped local of the same name
‘Iteration: ’
RCL+ ‘I’ ; depends on R47 style string concatenation
2 STEP ; count by twos
» ‘ForStep’ STO
The STEP keyword allows one to specify a different loop counter step value, from 2 to 99. 1 is also legal, but then you have an overly complicated FOR-NEXT loop.
This example also shows that loop control variables are local, scoped to the loop itself.6 The precious π cargo in the outer global ‘I’ variable is not harmed by the loop.
One detail worth knowing is that because this uses the classic HP c.eeess loop control scheme — counter, end value, and step size — RePIG code that accesses the loop’s current value causes it to generate an IP call to chop off the fractional part, giving the Integer Part you wanted.
Storage References
The FOR-NEXT example above brings us to another key difference between RPL and RePIG. In RPL, bare variable/register references use quotes to distinguish a reference to the storage element itself from what is stored there; I and 'I' mean different things in RPL, which is why this…
'I' 1 +
…produces the value I+1 on the stack. The single quotes cause an RPL machine to dereference I, putting its content onto the stack, where it can be affected by the + op.
We cannot allow that in RePIG body statements because the R47 already has two different uses for quotes. First, to distinguish registers from variables, and second, to mark string literals.
The RePIG equivalent to the above is therefore:
RCL I
1
+
Understand, the key difference between these examples is not the whitespace:7 it is the need for an explicit RCL. If it were otherwise, a bare ‘i’ statement would be ambiguous: it could be a request to pull the value of variable I onto the stack, or it could be an R47 string literal, “I”! Because we prefer R47 style for body statements, there is no ambiguity, which is why the ‘YES’ and ‘NO’ reports in the IF-THEN-ELSE example do what you expect.
Outside RPL-style condition clauses, quotes mean the same as in R47 flavored RPN:
42
STO G
STO ‘G’
That stores the same value in both the global G register and then in the global ‘G’ variable. For the same reason, a bare reference to ‘G’ doesn’t give the value on the stack as on HP RPL machines; in ordinary RePIG statements, you still need RCL or VIEW to make a storage location’s value visible on-screen. This is meant to maintain consistency with other rejig input formats; always remember, RePIG is syntactic sugar atop R47 flavor RPN, not a wholly new language like HP RPL.
But note that qualifier! Within condition clauses, RePIG allows use of RPL’s FORTH-style postfix evaluation rules, thus also RPL-style variable references, including dereferencing.
Recursion
RePIG neither improves nor harms the R47’s recursion capabilities; it uses what is provided. We see that in this classic recursive method for calculating the factorial of a number on the stack at call time:
« → x «
IF ‘x’ 1 > THEN
RCL ‘x’ ; put ‘x’ back onto stack; was eaten by arrow
RCL ‘x’ 1. - ; produce new ‘x’ argument, decremented
XEQ ‘FactR’ ; call ourselves recursively
× ; each recursive call will do this after…
ELSE ; …this clause starts unwinding the call stack
1. ; terminal value; works for 0! = 1! = 1
END
» » 'FactR' STO
Because the R47 defaults to SSIZE8 mode, this will work for arguments up to 7, at which point it starts yeeting intermediate results off the top of the stack, producing the wrong answer.
It may be instructive to see the same essential algorithm in RPL:
« → X «
IF X 1 > THEN X X 1 - FactR × ELSE 1 END
» » 'FactR' STO
Whitespace compression and stripped comments aside, this is shorter because of RPL’s method of distinguishing object references from their evaluated results, based on whether they are quoted or not.
A further key difference is that RPL-native machines have far bigger stacks than the R47, more typically limited by available RAM than by a hard-coded limit. This program will therefore not fail on a proper RPL-native machine until the numbers get huge.
No First-Class Functions
In proper RPL, you can say:
« + 2 / » 'AVERAGE' STO
This not only creates a new named subroutine called AVERAGE, it stores it to a variable of that name for further manipulation. Because RePIG targets RPN, without first-class functions, this is merely the convention for producing a labeled subroutine in our scheme:
LBL ‘AVERAGE’
+
2
÷
RTN
RePIG is explicitly not a LISP dialect!
Nevertheless, RePIG does support nested «blocks», each level creating a new local variable scope. When the output targets an R47 or Free42/Plus42, these are backed by platform locals, scoped to the call stack. For this reason, nested blocks are implemented in terms of XEQ, to allocate a fresh call stack:
« 1. LSTO ‘i’
« 2. LSTO ‘i’
« 3. LSTO ‘i’ »
RCL ‘i’
»
RCL ‘i’
» ‘NestBlk’ STO
You will see the {1,2,3} added to the stack on the way down, then the 2 and 1 recalled to the stack, showing that the innermost 3 never leaks past the block boundary.
Mixed RPL & RPN
RePIG code compiles down to RPN, and the RPL angle brackets may appear anywhere an RPN statement is allowed. These facts combine to make possible intermixing the styles within a single file:
LBL ‘EvenStk’
7
LocR 01
STO R.00
LBL a
RCL R.00
XEQ d
DSZ R.00
GTO a
REGS
RTN
« 2 × » d STO
If you build that, take note that the RePIG compiler is smart enough to realize you have already used LBL d and avoid a name collision. There’s no sense exposing our one-line helper as a global entrypoint, so we use the same mechanism RePIG itself does.
As an exercise for the reader, flip that around so that the loop is in RePIG style and the local d helper is good ol’ RPN. 🤓
Oh, what the heck, here it is. I’m such a big softy… 🤣
Lowering Restrictions
Because RePIG compiles its input to the same R47 form it uses when reading all other program types, it can in principle produce output for lesser (non-R47) machine types. Whether this succeeds comes down to…
- which ops and arg types you use in your RePIG input code;
- which of these forms the RePIG-to-R47 compiler happens to use when producing the linearized IR from the AST;
- how much space is available for RePIG’s profligate use of labels and registers; and
- which lowering facilities are available for your chosen selection of
rejig --outputand--syntax.
The rigidity of each of these barriers varies.
Take for example the third one, which we see in the way RePIG local variables translate to, e.g., an HP-11C output like --to rlm11c. RePIG implements local variables in terms of the R47 LocR feature, which the 11C lacked, but because we have variable name mapping to allow STO ‘Foo’ to become something the 11C can represent like STO A, these local R.𝑥𝑦 pattern R47 registers are force-fit into the 11C’s much smaller global register scheme. Compiling RePIG to HP-11C/15C code will therefore not fail on the total lack of local variables/registers but on the small number of global registers. (26 = R0-9 + R.0-.0 + A-E + I.)
The thirty-series outputs (--to dm32, and UTF-8 with --syntax dm32 / hp32sii / hp35s) use the same pattern: the LocR op is suppressed, and R.𝑥𝑦 targets get mapped into free spots in the platform’s A–Z range.
A harder barrier is using ops that do not exist on the target and cannot be emulated.
Take our cpx.repig, which centers on a test checking for a complex number. You might imagine that it would therefore fail to lower to the HP-11C, but succeed on the HP-15C. Alas, it fails on two separate levels:
The 15C does have a way of storing the complex literal at the top of the program, but it doesn’t have a do-if-true op equivalent to
ℂ?on the R47. You might replace that with a check of the R47’s semi-equivalentCPXRESflag, whichrejigknows to translate to HP-15C system flag 8. That doesn’t tell you that the number in X is complex, precisely, but that the stack is in complex mode, in which case all stack levels contain a complex result, even if it’s with a +0𝑖 portion.Having done this, you still get a conversion failure due to the use of a string literal for reporting the test’s result.
That brings us to this variation, which will convert via rejig cpx2.repig --to hp15c:
«
42.+69.𝑖
IF FS? ‘CPXRES’ THEN
1 ; do something more substantive here…
ELSE
0 ; …and here
END
» ‘CPXTest’ STO
If you then try converting that same program via --to rlm11c, it correctly complains, “complex numbers not supported for HP-11C output” on line 2, following the implicit LBL.
Another easy way to make rejig fail is to try lowering code that uses tests not provided on the target. The HP-11C has eight built-in logical tests, but most later HP RPN machines (including the 15C) have twelve. It is not RePIG’s place to second-guess the comparison you use in your code. Perhaps one day rejig might become smart enough to rework the code logic to use a smaller set of available comparison operators, but that day is not today.
Even in the happy path, the inefficiencies of RePIG are easier to tolerate in the more modern machine designs. You might be surprised how quickly RePIG can burn through the miserly allocations HP saw fit to provide back in the day.
Grammar
The canonical syntax definition is the Pigeon grammar file input/repig.peg. This file is meant to be readable to software developers, then extended by them to increase the scope of the RePIG language. This may then need to be followed by a corresponding addition to the lowering stage that compiles the new AST node to R47 IR.
Test Fixtures
The RePIG test inputs are paired files:
.repig: RePIG source.p47u: R47 UTF-8 program expected on compiling it
While those fixtures exist primarily to prevent regressions as the language evolves, they also serve as examples of what a RePIG construct means in R47 terms.
(You may now wish to return to the rejig home page.)
License
This work is © 2026 by Warren Young and is licensed under CC BY-NC-SA 4.0
- ^ I might allow RePIG blocks to be passed through unchanged, but the primary point I’m making is that I currently haven’t even a sketch of a plan to try writing a formatter for these RPL-like blocks themselves.
- ^
ASCII
<< ... >>is accepted as equivalent to these guillemets. - ^
RePIG also accepts ASCII arrows for this:
-> - ^ First-mover advantage being one of the privileges of inventing the language. 🤭
- ^ Perhaps one day I will create “ReFIG” to fully FORTH-ify the R47 without going so far as RPL, skipping the parts that turned it into a LISP dialect.
- ^
The same is true of
FOR-NEXTloops. - ^
Indeed,
rejigallows it to be written on a single line —RCL I 1 +— but I formatted it using three lines above to mimic traditional HP RPN style, as shown in the R47’s program entry mode.