Cpi is a tiny interpreter for C++20 or C++23.
The following softwares are needed to build and execute cpi. The compiler is used as interpreter of cpi internally.
- Qt tookit version 6
- Compiler - GNU C++ compiler, LLVM C++ compiler or MSVC C++ compiler
Linux:
$ qmake
$ make
$ sudo make install
$ cpi -v
cpi 2.2.0Windows (Command prompt for VS2022):
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.14.7
** Copyright (c) 2025 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
> C:\Qt\6.7.0\msvc2019_64\bin\qtenv2.bat
Setting up environment for Qt usage...
>cd (cpi root directory)
> qmake
> nmake
> cpi.bat -h (Run cpi command)
Usage: cpi.exe [options] [file] [-]
Tiny C++ Interpreter.
Runs in interactive mode by default.
Options:
-?, -h, --help Displays help on commandline options.
--help-all Displays help, including generic Qt options.
-v, --version Displays version information.
Arguments:
file File to compile.
- Reads from stdin. $ cpi (Run cpi.bat in windows)
cpi 2.2.0
Type ".help" for more information.
Loaded INI file: /home/foo/.config/cpi/cpi.conf
cpi> 3 << 23; (Bitwise operation)
25165824
cpi> int a = 3;
cpi> ~a; (Complement)
-4
cpi> a ^ 2; (XOR)
1
cpi> auto func = [](int n) { return n*n; }; (Lambda function)
cpi> func(3);
9
cpi> .quit ( or press ctrl+c )
Code can be pasted.
$ cpi (Run cpi.bat in windows)
cpi> #include <map> (Paste code here)
#include<iostream>intmain()
{
std::map<int, std::string> m = { {1, "one"}, {2, "two"} };
if (auto it = m.find(2); it != m.end()) {
std::cout << it->second << std::endl;
}
} (Press enter)
two (The result of the executed output)Save C++ source code as hello.cpp.
#include<iostream>intmain()
{
std::cout << "Hello world\n";
return0;
}Run cpi in command line.
$ cpi hello.cpp
Hello worldImmediately compiled and executed! Almost a script language, but the source file is also C++ program which a compiler can compile successfully.
Next code outputs a square root of input argument. Specify options for compiler or linker with "CompileOptions: " word. In this example, linking math library specified by "-lm" option.
#include<iostream>
#include<cmath>
#include<cstdlib>intmain(int argc, char *argv[])
{
if (argc != 2) return0;
std::cout << sqrt(atoi(argv[1])) << std::endl;
return0;
}
// CompileOptions: -lm $ cpi sqrt.cpp 2
1.41421
$ cpi sqrt.cpp 3
1.7320The following is sample code for generator using coroutine.
#include<generator>
#include<iostream>
std::generator<int> num_generator(int from, int to)
{
for (int i = from; i <= to; i++) {
co_yield i;
}
}
intmain()
{
longlong sum = 0;
for (auto v : num_generator(1, 1000000)) {
sum += v;
}
std::cout << sum << std::endl;
return0;
}
// CompileOptions: -std=c++23 $ cpi main.cpp
500000500000Furthermore, pkg-config command can be used for CompileOptions.
#include<cblas.h>
#include<iostream>intmain()
{
// 2x2 Matrixint M = 2, N = 2, K = 2;
double A[4] = {1.0, 2.0, 3.0, 4.0};
double B[4] = {5.0, 6.0, 7.0, 8.0};
double C[4];
// General Matrix-Matrix multiplicationcblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
M, N, K,
1.0, A, K, B, N,
0.0, C, N);
// Print results
std::cout << "Result of A * B = C:" << std::endl;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
std::cout << C[i * 2 + j] << "";
}
std::cout << std::endl;
}
return0;
}
// CompileOptions: `pkg-config --cflags --libs openblas` $ cpi dgemm.cpp
Result of A * B = C:
19 22
43 50Qt application can also be run.
#include<QApplication>
#include<QLabel>intmain(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label("Hello, world!");
label.setAlignment(Qt::AlignCenter);
label.setWindowTitle("Hello");
label.resize(200, 100);
label.show();
return app.exec();
}
// CompileOptions: `pkg-config --cflags --libs Qt6Widgets` -Wl,-rpath,`pkg-config --variable=libdir Qt6Widgets` $ cpi helloworld_qt.cppAdding a shebang, save as hello.cpps. No longer compiled in a C++ compiler successfully.
#!/usr/bin/env cpi
#include<iostream>intmain()
{
std::cout << "Hello world\n";
return0;
} $ chmod +x hello.cpps
$ ./hello.cpps
Hello worldYes, a shell script. I named it CppScript.
cpi> .help
.conf Display the current values for various settings.
.help Display this help.
.rm LINENO Remove the code of the specified line number.
.show Show the current source code.
.quit Exit this program.