Saturday, February 14, 2009

Using CINT - C/C++ Interpreter - An experiment with vector

Let do something with vector
cint.exe> L vector
cint.exe> {vector<int> temps;}
cint.exe> {temps.push_back(6);}
cint.exe> {temps.push_back(4);}
cint.exe> {temps.push_back(5);}
cint.exe> {temps.size();}
(const unsigned int)3
cint.exe> {temps[0];}
(int)6
cint.exe> {temps[1];}
(int)4
cint.exe> {temps[2];}
(int)5
cint.exe>
So it works well, now there is time to try some algorithm
cint.exe> L algorithm
cint.exe> {sort(temps.begin(),temps.end());}
Error: Template Function value_type(first) is not defined in current scope  algo.h(735)
!!!Dictionary position rewound... !!!Error recovered!!!
cint.exe>
It seems that there is some problem with STL lib coming with CINT in Windows. Looking at STL header files, I found that Function value_type is declared in _iterator.h surrounded by a condition for whatever reason
#if (G__GNUC>=3)

#if (G__GNUC_VER>=3001) 
...
template  
inline T* value_type(const vector::iterator&) {return (T*)(0);}
..
#endif
#endif
To fix it is fairly simple
d:\cint-5.16.19\cint.exe
cint : C/C++ interpreter  (mailing list 'cint@root.cern.ch')
   Copyright(c) : 1995~2005 Masaharu Goto (gotom@hanno.jp)
   revision     : 5.16.19, March 16, 2007 by M.Goto

No main() function found in given source file. Interactive interface started.
'h':help, 'q':quit, '{statements;}' or 'p [expr]' to evaluate

cint.exe> {#define G__GNUC 4}
cint.exe> {#define G__GNUC_VER 4001}
cint.exe> L vector
cint.exe> L algorithm
cint.exe> {vector<int> temps;}
cint.exe> {temps.push_back(6);}
cint.exe> {temps.push_back(4);}
cint.exe> {temps.push_back(5);}
cint.exe> {temps.size();}
(const unsigned int)3
cint.exe> {temps[0];}
(int)6
cint.exe> {temps[1];}
(int)4
cint.exe> {temps[2];}
(int)5
cint.exe> {sort(temps.begin(),temps.end());}
cint.exe> {temps[0];}
(int)4
cint.exe> {temps[1];}
(int)5
cint.exe> {temps[2];}
(int)6
cint.exe>

Using CINT - C/C++ Interpreter - Basic Commands

Load,Unload,files
L that load files into interpreter, and file that list these loaded files.
cint.exe> L iostream
cint.exe> files
  0 fp=0x781c1bd0 lines=13   file="iostream"
  1 fp=0x781c1bf0 lines=479  file="iostream.h"
  2 fp=0x781c1c10 lines=11  *file="iosenum.h"
  3 fp=0x781c1c30 lines=34   file="bool.h"
  4 fp=0x781c1c50 lines=173 *file="_iostream"
If we use #include in curly bracket, we will get the same result
cint.exe> {#include <iostream>}
cint.exe> files
  0 fp=0x781c1bf0 lines=13   file="iostream"
  1 fp=0x781c1c10 lines=479  file="iostream.h"
  2 fp=0x781c1c30 lines=11  *file="iosenum.h"
  3 fp=0x781c1c50 lines=34   file="bool.h"
  4 fp=0x781c1c70 lines=173 *file="_iostream"
U command unload a file, a C/C++ header file can include other header files, which in turn can include additional header files and so all. CINT is good enough and maintain this dependencies so if we unload one file, others referenced files may get unloaded also.
cint.exe> L string
cint.exe> files
  0 fp=0x781c1bd0 lines=11   file="string"
  1 fp=0x781c1bf0 lines=72   file="_string"
  2 fp=0x       0 lines=0    file="string.dll"
  3 fp=0x781c1c10 lines=13   file="iostream"
  4 fp=0x781c1c30 lines=479  file="iostream.h"
  5 fp=0x781c1c50 lines=11  *file="iosenum.h"
  6 fp=0x781c1c70 lines=34   file="bool.h"
  7 fp=0x781c1c90 lines=173 *file="_iostream"
cint.exe> U string
cint.exe> files
reset
reset reset interpreter environment and unload all files
cint.exe> {int a=1;}
cint.exe> {a;}
(int)1
cint.exe> reset
cint.exe> {a;}
Error: Symbol a is not defined in current scope  (tmpfile)(1)
!!!Dictionary position rewound... !!!Error recovered!!!
cint.exe> L vector
cint.exe> files
  0 fp=0x781c1bd0 lines=11   file="vector"
  1 fp=0x781c1bf0 lines=18   file="_vector"
  2 fp=0x       0 lines=0    file="vector.dll"
  3 fp=0x       0 lines=0    file="vectorbool.dll"
  4 fp=0x781c1c10 lines=313  file="_vector.h"
  5 fp=0x781c1c30 lines=282  file="function.h"
  6 fp=0x781c1c50 lines=34   file="bool.h"
  7 fp=0x781c1c70 lines=235  file="algobase.h"
  8 fp=0x781c1c90 lines=55   file="_pair.h"
  9 fp=0x781c1cb0 lines=17   file="_iterator"
 10 fp=0x781c1cd0 lines=668  file="_iterator.h"
 11 fp=0x781c1cf0 lines=6    file="stddef.h"
 12 fp=0x781c1d10 lines=479  file="iostream.h"
 13 fp=0x781c1d30 lines=11  *file="iosenum.h"
 14 fp=0x781c1d50 lines=173 *file="_iostream"
 15 fp=0x781c1d70 lines=177  file="defalloc.h"
 16 fp=0x781c1d90 lines=34   file="new.h"
 17 fp=0x781c1db0 lines=34   file="stdio.h"
 18 fp=0x       0 lines=0    file="stdfunc.dll"
 19 fp=0x781c1dd0 lines=14   file="stdlib.h"
 20 fp=0x  476800 lines=19   file="limits.h"

-- Press return for more -- (input [number] of lines, Cont,Step,More)
cint.exe> reset
cint.exe> files
cint.exe>

Wednesday, February 11, 2009

Using CINT - C/C++ Interpreter - Getting Started

One of things I favor scripting languages (Ruby, Python) over none scripting (JAVA, C/C++) is there is no need to follow time consuming cycle of write-compile-run. Even with the best IDE, I still feels the pain of typing command, hit key combination, waiting for computer doing. This is especially true when I need to explore some API, test some ideas. So when I discovered CINT, a C/C++ Interpreter, I gave it a try. With CINT, we can type C/C++ code directly in it's interactive CINT console and get the result. CINT is available for Windows and many UNIX platform(s) some LINUX clone has CINT pre-installed. We start CINT console by type e.g. (on Windows)
d:\cint-5.16.19\cint.exe
cint.exe>{#include <iostream>
cint.exe>{cout << "Hello World\n";}  
Hello World (class ostream)2085223112 
C/C++ code shall be type inside curly bracket {...}, CINT doesn't allow to define function in interactive console
> { int func999() {return 999;} }
Limitation: Function can not be defined in a command line or a tempfile
You need to write it in a source file (tmpfile)(1)
!!!Dictionary position rewound... !!!Error recovered!!!
So we have to create separate file e.g.
# func999.cpp
int func999() {
return 999;
}
,load it in the console and call a function inside
cint.exe>L func999.cpp
cint.exe>{func999();}
Some of the most frequently used commands includes
L  - load file in to the console
U  - unload the file
file - show which files are currently loaded
reset - reset memory of CINT console to initial state,unload all files
q - quit CINT console