Is there a tool to ask the computer how it can understand code?

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Uri Blass
Posts: 8248
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Is there a tool to ask the computer how it can understand code?

Post by Uri Blass » Thu Nov 08, 2018 5:25 am

I would like simply to be able to find easily what the computer needs to understand a code without many hours and today I need some hours even to understand a simple function like engine_info() in stockfish.

I take the following line in stockfish's code that is the first line in main.cpp line 37

Code: Select all

std::cout << engine_info() << std::endl;
I would like every line to have fathers that if the computer understand them it has no problem to understand the line and I want the computer to show me the fathers of every line if I ask it(I think that it should be something that can be automatically done by a compiler and I do not ask for explanation in human language)

I want information like the following about stockfish and the question is if there is a tool to give the information without searching for a long time in the code.

fathers of line 37 in main.cpp are
1)line 21 of main.cpp

Code: Select all

#include <iostream>
2)line 27 of main.cpp

Code: Select all

#include "tt.h"
3)line 24 of tt.h

Code: Select all

#include "misc.h"
4)line 32 of misc.h

Code: Select all

const std::string engine_info(bool to_uci = false);
5)lines 124-144 of misc.cpp

Code: Select all

const string engine_info(bool to_uci) {

  const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
  string month, day, year;
  stringstream ss, date(__DATE__); // From compiler, format is "Sep 21 2008"

  ss << "Stockfish " << Version << setfill('0');

  if (Version.empty())
  {
      date >> month >> day >> year;
      ss << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
  }

  ss << (Is64Bit ? " 64" : "")
     << (HasPext ? " BMI2" : (HasPopCnt ? " POPCNT" : ""))
     << (to_uci  ? "\nid author ": " by ")
     << "T. Romstad, M. Costalba, J. Kiiski, G. Linscott";

  return ss.str();
}
father of line 124 in misc.cpp is
line 27 of misc.cpp

Code: Select all

#include <string>

mar
Posts: 1888
Joined: Fri Nov 26, 2010 1:00 pm
Full name: Martin Sedlak

Re: Is there a tool to ask the computer how it can understand code?

Post by mar » Thu Nov 08, 2018 12:46 pm

What you want is called "goto definition" in IDEs/Editors which support this feature.
Martin Sedlak

tpoppins
Posts: 773
Joined: Tue Nov 24, 2015 8:11 pm
Location: upstate

Re: Is there a tool to ask the computer how it can understand code?

Post by tpoppins » Fri Nov 09, 2018 5:56 am

e.g., in Code::Blocks right-clicking on a function's/variable's name offers the following options:
  • Find declaration of
  • Find implementation of
  • Find occurrences of
  • Find functions called by
  • Find functions calling
  • Find references of
Tirsa Poppins
CCRL

Uri Blass
Posts: 8248
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: Is there a tool to ask the computer how it can understand code?

Post by Uri Blass » Fri Nov 09, 2018 8:07 am

thanks
another question.

Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).

mar
Posts: 1888
Joined: Fri Nov 26, 2010 1:00 pm
Full name: Martin Sedlak

Re: Is there a tool to ask the computer how it can understand code?

Post by mar » Fri Nov 09, 2018 12:33 pm

Uri Blass wrote:
Fri Nov 09, 2018 8:07 am
Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).
Probably because std is a namespace so it picks the next symbol? How would you jump to namespace anyway? Where does it "begin"? There would be many files.
You should learn a bit about C++ before trying to understand C++ code.
Martin Sedlak

phhnguyen
Posts: 292
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

Re: Is there a tool to ask the computer how it can understand code?

Post by phhnguyen » Fri Nov 09, 2018 1:57 pm

Uri Blass wrote:
Fri Nov 09, 2018 8:07 am
thanks
another question.

Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).
I guess because of imperfect IDEs, so complicated and bad design of standard library (yes, it has a bad reputation). A programmer should be disappointed if he moves from Java to work with std one since it lacks information and the hierarchy is so bad (compared with Java).

My way (sure it may be not a good way) to use std lib is that I treat it as a "blackbox" or simply as a set of C functions. It means I learn how to use them without looking deeper into their code. Thus I am quite rare to click on std or its functions for looking their definitions. Whenever I need to know which functions / includes or how to use I will google. I am sure human explanations / examples are much easier and quicker to understand than those code.

BTW, look like you are learning Stockfish / C++ from middle of nowhere. I suggest you to start yourself a short course about C++, write some simple programs, then spend time to read about general programming which is a bit hard for beginners but Stockfish is using heavily. For the first time, learn to use std library as a blackbox since Stockfish uses just only some.

Dann Corbit
Posts: 9067
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: Is there a tool to ask the computer how it can understand code?

Post by Dann Corbit » Fri Nov 09, 2018 8:19 pm

mar wrote:
Fri Nov 09, 2018 12:33 pm
{snip}
You should learn a bit about C++ before trying to understand C++ code.
Don't those two things go hand in hand?

But it probably would be a good idea to read Bjarne Stroustrup's books.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.

Sven
Posts: 3706
Joined: Thu May 15, 2008 7:57 pm
Location: Berlin, Germany
Full name: Sven Schüle
Contact:

Re: Is there a tool to ask the computer how it can understand code?

Post by Sven » Fri Nov 09, 2018 10:19 pm

phhnguyen wrote:
Fri Nov 09, 2018 1:57 pm
BTW, look like you are learning Stockfish / C++ from middle of nowhere. I suggest you to start yourself a short course about C++, write some simple programs, then spend time to read about general programming which is a bit hard for beginners but Stockfish is using heavily. For the first time, learn to use std library as a blackbox since Stockfish uses just only some.
I can't judge about Uri's skills in C++. But just as a side note I'd like to add that Uri is not a beginner in the area of programming in general (he is the author of Movei which is a 2600+ engine and was written in C a couple of years ago). Apart from that I fully agree to your other points regarding Stockfish and about understanding parts of the standard C++ library.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)

Uri Blass
Posts: 8248
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: Is there a tool to ask the computer how it can understand code?

Post by Uri Blass » Fri Nov 09, 2018 11:42 pm

I am the author of movei but I got a lot of help from other people with the non mathematical part of the program and I also started by copying some structures from tscp but not working chess code(and my move generator is clearly different than the move generator of other programs)

I do not like to start to learn C++ before trying to understand C++ code because of the following reasons:
1)I am not interested in things that are not needed for understanding the code(spending time on them means that I need to spend more time)
2)I prefer to learn something only when I need it(otherwise it is not an efficient way to use my time because I may read something that I do not need in the next month forget it and later I need to read it again).

BeyondCritics
Posts: 335
Joined: Sat May 05, 2012 12:48 pm
Location: Bergheim

Re: Is there a tool to ask the computer how it can understand code?

Post by BeyondCritics » Sat Nov 10, 2018 11:01 am

If you want to accomplish something efficiently, the first thing is obviously to get your tools straight. Think of all the time you have definitely lost by now, tinkering around for nothing!
Anyway: Any(!) modern IDE should do what you have asked for, including Visual Studio, Visual Studio Code and Eclipse.
Since hacking stockfish is definitely non commercial, you can get a free license from https://www.sourcetrail.com/, i like their product.
You can also try https://sourcegraph.com and maybe their chrome plugin.

Post Reply