9

I am trying to understand the syntax of .bst-files using the Guide Tame the Beast. For starters, I want to understand the example of the multiplication function on page 39 (The functions skip$ and if$ are explained on pages 33 to 35):

The first assignment in the function looks like this:

'a :=

Which is described as "We store the first value".

I know about stack-oriented programming and reverse polish notation, but I never actually coded it myself. What does the apostrophe do here? Is it related to "storing"? Sometomes I also see functions (such as skip$) with a preceding apostrophe. What is the meaning of this?

1 Answers1

9

The ' here means that rather than trying to evaluate a here, BibTeX will use the name itself. It the construct

'a :=

there will be some integer on the stack (let's call it 1), so we actually have

#1 'a :=

This will assign variable a as 1. If we didn't have the ', BibTeX would try to evaluate a here. As a will have been declared as an integer, that would mean that the value would be placed into the stack here. So something like

#1 'a :=
#2 a =
   { } % True code
   { } % False code
 if$

works (and here is false): the second use of a inserts the value into the sack so we can do the comparison.


The ' syntax is also used to avoid needing to have brace groups around a one step branch in a conditional. It's used in constructs such as

[test]
  'skip$ % True branch
  { }    % False branch
if$

most commonly with skip$ (do nothing) or pop$ (pop the top item from the stack). Without the ' and the { } group, BibTeX would try to execute the function at the wrong time, and odd errors would result. (Here of course one could just use a brace group for the desired outcome.) The underlying concept is the same as the first case: put the quoted thing on the stack.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    Well, in the example a and b are declared to be integer variables, but this doesn't really change the description. – egreg Aug 02 '17 at 16:14
  • Ok so if I understood correctly, it's equivalent to the single equal sign "=" for an assqignment vs double equals "==" for an evaluation? – AlphaOmega Aug 02 '17 at 16:18
  • @egreg I'd missed that this came from a set up where a is an integer: I've adjusted – Joseph Wright Aug 02 '17 at 16:23
  • 1
    @AlphaOmega Well not really as BibTeX uses = for the test and := for the assignment, so those distinctions also apply – Joseph Wright Aug 02 '17 at 16:24
  • I see, and what exactly does the hashtag do? Does #1 simply mean to put 1 on top of the stack? – AlphaOmega Aug 02 '17 at 16:25
  • 3
    @AlphaOmega At the risk of explaining all of BibTeX in one answer + comments: strings are added to the stack by quoting in the form "...", integers are added to the stack in the form #.... – Joseph Wright Aug 02 '17 at 16:26