# Calc: 2+2=4. In the base code, where is the 4 represented?

2-plus-2.ods (9.3 KB)
In 2-plus-2.ods if both the cursor is on A3 and View, show formula is Off, A3 shows 4.
.

.
Since we can see 4 on the screen, we know 4 is in the video memory. Since we know it is in the video memory, we know it is in the RAM. But we want to know where it is in the base code.
.
If we had similar grade school experiences, we can remember word problems like “If Bob gave you 2 apples and Mary gave you two apples, how many apples would you have?” Following the question, There would be a space for your answer. In algebra there would be a level of abstraction. We might read “If a=2 and b=2, where a+b=c, what is the value of c?”
.
We know programmers use abstractions to hold values – sometimes null, sometimes not. They could write “If a=2 and b=2, where a+b=FunctionResult, what is the value of FunctionResult?”

In our case, FunctionResult = 4. The programmer probably did not name the abstraction containing this function result “FunctionResult,” but he did give it some name. If we knew where the 4 was represented in the base code, we should be able to find what the programmer named his abstraction for the values generated by the function.
.
Our question is “In the base code, where is the 4 represented?” I do not know. Could you tell us?

Who is we?
On typical concept is called a variable in several programming languages.

Not in a typical procedural language like C, C++. In declarative languages like prologue you have some kind of this approach, but you will usually not find your “human language” syntax.

No - an address is sufficient.

Nowhere. In your case the 4 is contained in cell with human readable address A4 on some unknown sheet of one open-Calc-File (together with a reference to the formula wich can recalculate another value instead of 4, if the two other cells or the formula is changed.)
.
A spreadsheet does not deal directly with variable. The code generates a meta-language to adress several grids, combine values via formulas and modify this on top with several macro-languages.

No - but out of curiosity. What would you do, if I could tell you somethig like “line 42 in files simplecalc.cpp”

1 Like

Wanderer, thanks for responding. As you say, syntax varies from language to language; programmers do use, at least, both variables and addresses as abstractions to hold or locate data.
.
It might have been clearer for me to write, “You and I know programmers use abstractions to hold values – sometimes null, sometimes not. They could write “If a=2 and b=2, where a+b=FunctionResult, what is the value of FunctionResult?”
.
In our case, FunctionResult = 4. The programmer probably did not name the abstraction containing this function result ‘FunctionResult,’ but he did use an abstraction to identify it. If one knew where the 4 was represented in the runtime environment and in the base code (where one would put a breakpoint to halt code execution at the point of interest), one should be able to find the specific abstraction used to hold or locate the value generated by the function.”
.
Our question is “In the runtime environment of 2-plus-2.ods with the active cell being A3, how can you and I find the abstraction used to hold or locate the value 4?"
.
Thank you for encouraging more exact expressions in writing.
.

Aren’t you right? As I understand it, once a working formula is entered into a spreadsheet cell, it is parsed and tokenized and may be recalculated when a precedent changes. There are also constructs, expressions, which are recalculated at runtime. Many references also are given a definite meaning only at runtime.
.
In the Calc API FormulaParser service, to hold or locate the value 4, I have 't noticed anything useful for finding the abstraction used. Similarly, lacking a breakpoint, I don’t know how to check the source code for possible creation of an appropriate abstraction. What I am wondering is whether there is something better than a memory dump to find the data and its abstraction. Is there an expression, a formula, a macro-language, a meta-language addressing a grid, a reference, a token, or something else?
.

When transferred to another spreadsheet document, for example, a working formula (which can be considered a string) may become a dead string. Making that dead string a working formula again would increase the power of calc by one level of abstraction.

Of course not. Because the formula parser does not (and does not have to) even know the result the formula would produce, it just parses the formula and transforms it into a sequence of tokens that (after some conversion/transformation) can be stored at a formula cell and then be processed by the interpreter to calculate of which the formula cell can obtain the result and remember.

So, to your question “In the base code, where is the 4 represented?” (what base even? core?), it’s stored in an instance of ScFormulaResult that is a member of the formula cell, but I doubt that will help you anything.

1 Like

It “may become” but not necessarily. If you copy and paste between 2 spreadsheets usually the formula are detectec an “survive” as formula. Same, if you save as odc/xlsx and import again. Even for text/csv there are options to retain formula for learned people.
.
So it seems Calc/Excel etc have reached your next level years ago.
.
Actually I have no clue where your destination is and I doubt you read a lot of “base code”. But have fun with your ideas…

Thank you, Wanderer. You are right. I am new to LibreOffice and I do not code in either C++ or java. Also, I am not using either “copy and paste” or “text/csv” or "saving as odc/xlsx and importing again. In this particular case the formula does not survive the transfer. I apologize for not being clearer.

Wanderer, here, belatedly, is a spreadsheet which shows why the situation develops. _DocRecipient_Variable_A_2.ods has the fonts with saturated colors. In _E76.ods, the less important font colors are faded. zz_DocR_E76_larger.png is an image of the most pertinent spreadsheet cells.
.
_DocRecipient_Variable_A_2.ods (39.9 KB)
.
_E76.ods (37.2 KB)
.

Thank you, erAck (de), for explaining the FormulaParser and mentioning ScFormulaResult. I downloaded core-master.zip, and happened across OpenGrok. As I understand it, the source code is now on my machine; the next step is to locate the relevant files. If incorrect, please let me know.

Wanderer, you are right. For clarity, I’d reword the title, if I can to “Calc: 2+2=4. At runtime, which, say, address points to the 4?” You also correctly realized that in C++, I am functionally illiterate. For your comfort, Lupp(1), Villeroy(2), and erACK(3) agree I am a novice. However, this is a cooperative effort. With experts like you, them, and others like caolanm and stbergmann(4), shouldn’t all of you collectively be able to locate the necessary information (v.i.) relatively easily.
.
(1) Could TYPEIN() compliment the FORMULA() function? ["TYPEIN()" was "TRANSFER()" in the original question.] - #17 by Bruce2
(2) Should we add a Novice Programmer FAQ Page?
(3) Calc: 2+2=4. In the base code, where is the 4 represented? - #3 by Bruce2
(4) Contributors to LibreOffice/core · GitHub

“Recent versions of LibreOffice caches spreadsheet formula results into its ODF file(1).” In the core, where may one find code for using an ODF file, for finding, say, the address of the 4, for the interpreter, for meta-language generation, and for expressions used in this area?
.
(1) Formula

Nobody knows, and usually nobody cares.
.
Computers use abstract rules. Compare it to an Airport. Today we put 2 suitcases from flight lo7.4.3 on lorry A1 and 2 suitcases from fight lo7.3.2 on lorry A2 together, pack all of them together in lorry A3 to bring them to their final destination all this is done in Terminal1. Tomorrow Terminal1 is busy, so we route the flights over Terminal2.
Now you come and ask where on the construction map of the Airport is the postal address of my suitcase.
It is simply not on the map. There may be streets and parking areas, but the location of the lorry is not on the map.
As long as the luggage arrive nobody cares.
.
And what will you do, if anybody can tell you (with help of a debugger) an hex address like 0x2f348a4f for the storage of the numeric represention of 4 on January 30 in 2023 on his machine?

(Edit: Typos)

1 Like

should raise an Exception, because …g isn’t valid symbol in hex-representations

1 Like

I shouldn’t write hex on a mobile phone…
I corrected the text.

One of your problems is that you don’t know what you are talking about. You jump on terms and merge them together to produce a conglomerate that does not make any sense at all.

It may be hard for anyone to tell you that you are completely off-track, but that seems to be the case.

ODF is the OpenDocument File format, cached there means nothing else than a document generator writes the formula cell’s result to the document, so a reading application can avoid recalculation if possible. There is no “address of the 4”, not “for the interpreter”, not “for meta-language generation” (whatever that might be), and not “for expressions used in this area” (nebulous).

Great clarifying analogy. You give an excellent description of what we usually do. In our present situation, however, we are interested in the characters “=,” “A1,” " ," “+,” " ," and “A2” only as strings in “A3.”
.
In the analogy, consider that your two suitcases from lorry A1 and my two from lorry A2 were loaded onto lorry A3 and trundled off to the correct destination. And now we are on our trip together.
.
Our problem is that now we can only read the instructions given to the driver of lorry A3 at Terminal1 in Airport1 about our bags on lorries A1 and A2 and where to send lorry A3 with the combined luggage. What you and I want to do is to use those same instructions at Terminal1 in Airport2. But we can not yet do this, as we can only see and read the instructions concerning Terminal1 in Airport1. We need to find the instructions’ location and put them into a form which we can issue to the A3 lorry driver at Terminal1 in Airport2. That, by analogy, is the goal of the project you and I have before us.

The goal you and I have is creating the inverse of FUNCTION().
.
In the source code, where did the programmer decide what to display in A3? He could display a value (4), a formula (=A1 + A2), or an error message (###, #VALUE!,. . .).
.
For convenience of discussion, could we assume that in the case of 2-plus-2.ods cell A3, with View, Show Formula: On, the address for the storage of the string representation of “=A1 + A2” on a machine is 0x2f348a4f? We need to program 3 steps. First, to find 0x2f348a4f. Second, to set a variable equal to the string at 0x2f348a4f. And third, to push the string “=A1 + A2” into an empty, designated cell. Since this will be a relatively few lines of code the change to calc will be incremental.
.
In the source code, the programmer had a reference to identify the formula of interest. We want to find that reference.
.
At runtime, the formula “=A1 + A2” would be at 0x2f348a4f and we could use a debugger to view it in the memory as bits. After we interpreted those bits, we would see it as the string "=A1 + A2.” Since we can see this string, we know that the programmer has used it, saved it and had a reference to it in the source code. Considering general trems, my vocabulary lacks a general term to name this type of reference. What would be the general term you would use to name this type of reference?
.
Considering specific terms, if the programmer named this reference “FormulaOfInterest,” we could find it in the source code by searching on the term “FormulaOfInterest.” In the source code, how do we find the programmer’s reference to the formula of interest when we do not know what term he used to identify it?

You are right. This is a perplexing conglomerate of terms about each of which I need to learn more. Didn’t, in Hamlet Act 2, scene 2, Polonius face a similar situation? You may well reach a different conclusion than his – which was "Though this be madness, yet there is method in’t. " ( Yes, I both empathize with you and wish I know more and could explain things more clearly. As a physician, I have spent a lot of time explaining medical terms and concepts which are foreign to most people’s experience.)

In the source, might we investigate .uno:ToggleFormula, IsShowFormula, and mbShowFormulas?

Could the number of lines to be examined have been reduced from about 7 million to around 2,000 (about a dozen files)?
.
CalcCommands.xcu
D:. . .\LO\sc_LO\core-master\officecfg\registry\data\org\openoffice\Office\UI\CalcCommands.xcu
FID_TOGGLEFORMULA
.uno:ToggleFormula
.
TableFormula.idl
D:. . .\LO\sc_LO\core-master\offapi\com\sun\star\text\textfield\TableFormula.idl
/** contains the formula. */ [property] string Formula;
.
DomainMapper_Impl.cxx
D:. . .\LO\sc_LO\core-master\writerfilter\source\dmapper\DomainMapper_Impl.cxx
grab-bag the original and converted formula
PROP_CELL_FORMULA_CONVERTED
CELL_GRAB_BAG
.
txtflde.cxx
D:. . .LO\sc_LO\core-master\xmloff\source\text\txtflde.cxx
XML_FORMULA
XML_TABLE_FORMULA
.
xeview.cxx
D:. . .\LO\sc_LO\core-master\sc\source\filter\excel\xeview.cxx
EXC_WIN2_SHOWFORMULAS
VOPT_FORMULAS
.
xiview.cxx
D:. . .\LO\sc_LO\core-master\sc\source\filter\excel\xiview.cxx
EXC_WIN2_SHOWFORMULAS
VOPT_FORMULAS
.
FormulaCompiler.cxx
D:. . .\LO\sc_LO\core-master\formula\source\core\api\FormulaCompiler.cxx
FormulaToken
CreateStringFromTokenArray( OUString& rFormula )
createSequenceOfFormulaTokens()
.
Formula.cxx
D:. . .\LO\sc_LO\core-master\formula\source\ui\dlg\formula.cxx
getCurrentFormula()
m_sFormula
.
SOFormulaParser.java
D:. . .\LO\sc_LO\core-master\reportbuilder\java\org\libreoffice\report\pentaho\SOFormulaParser.java
formula
.
xetable.hxx
D:. . .\LO\sc_LO\core-master\sc\source\filter\inc\xetable.hxx
XclTokenArrayRef CreateCellTokenArray
XclExpRangeFmlaBase
.
formulaparser.cxx
D:. . .\LO\sc_LO\core-master\sc\source\filter\oox\formulaparser.cxx
OUString()
.
formulabase.cxx
D:. . .\LO\sc_LO\core-master\sc\source\filter\oox\formulabase.cxx