IOT>> cpi>> 返回
项目作者: treefrogframework

项目描述 :
Tiny c++ interpreter
高级语言: C++
项目地址: git://github.com/treefrogframework/cpi.git
创建时间: 2012-12-08T14:45:36Z
项目社区:https://github.com/treefrogframework/cpi

开源协议:

下载


Tiny C++ Interpreter

ActionsCI
License
Release

Cpi is a tiny interpreter for C++20 or C++23.

Requirements

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

Install

Linux:

  1. $ qmake
  2. $ make
  3. $ sudo make install
  4. $ cpi -v
  5. cpi 2.2.0

Windows (Command prompt for VS2022):

  1. **********************************************************************
  2. ** Visual Studio 2022 Developer Command Prompt v17.14.7
  3. ** Copyright (c) 2025 Microsoft Corporation
  4. **********************************************************************
  5. [vcvarsall.bat] Environment initialized for: 'x64'
  6. > C:\Qt\6.7.0\msvc2019_64\bin\qtenv2.bat
  7. Setting up environment for Qt usage...
  8. > cd (cpi root directory)
  9. > qmake
  10. > nmake
  11. > cpi.bat -h (Run cpi command)
  12. Usage: cpi.exe [options] [file] [-]
  13. Tiny C++ Interpreter.
  14. Runs in interactive mode by default.
  15. Options:
  16. -?, -h, --help Displays help on commandline options.
  17. --help-all Displays help, including generic Qt options.
  18. -v, --version Displays version information.
  19. Arguments:
  20. file File to compile.
  21. - Reads from stdin.

Interactive Mode

  1. $ cpi (Run cpi.bat in windows)
  2. cpi 2.2.0
  3. Type ".help" for more information.
  4. Loaded INI file: /home/foo/.config/cpi/cpi.conf
  5. cpi> 3 << 23; (Bitwise operation)
  6. 25165824
  7. cpi> int a = 3;
  8. cpi> ~a; (Complement)
  9. -4
  10. cpi> a ^ 2; (XOR)
  11. 1
  12. cpi> auto func = [](int n) { return n*n; }; (Lambda function)
  13. cpi> func(3);
  14. 9
  15. cpi> .quit ( or press ctrl+c )

Code can be pasted.

  1. $ cpi (Run cpi.bat in windows)
  2. cpi> #include <map> (Paste code here)
  3. #include <iostream>
  4. int main()
  5. {
  6. std::map<int, std::string> m = { {1, "one"}, {2, "two"} };
  7. if (auto it = m.find(2); it != m.end()) {
  8. std::cout << it->second << std::endl;
  9. }
  10. } (Press enter)
  11. two (The result of the executed output)

Executive mode

Save C++ source code as hello.cpp.

  1. #include <iostream>
  2. int main()
  3. {
  4. std::cout << "Hello world\n";
  5. return 0;
  6. }

Run cpi in command line.

  1. $ cpi hello.cpp
  2. Hello world

Immediately 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.

  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. int main(int argc, char *argv[])
  5. {
  6. if (argc != 2) return 0;
  7. std::cout << sqrt(atoi(argv[1])) << std::endl;
  8. return 0;
  9. }
  10. // CompileOptions: -lm
  1. $ cpi sqrt.cpp 2
  2. 1.41421
  3. $ cpi sqrt.cpp 3
  4. 1.7320

The following is sample code for generator using coroutine.

  1. #include <generator>
  2. #include <iostream>
  3. std::generator<int> num_generator(int from, int to)
  4. {
  5. for (int i = from; i <= to; i++) {
  6. co_yield i;
  7. }
  8. }
  9. int main()
  10. {
  11. long long sum = 0;
  12. for (auto v : num_generator(1, 1000000)) {
  13. sum += v;
  14. }
  15. std::cout << sum << std::endl;
  16. return 0;
  17. }
  18. // CompileOptions: -std=c++23
  1. $ cpi main.cpp
  2. 500000500000

Furthermore, pkg-config command can be used for CompileOptions.

  1. #include <cblas.h>
  2. #include <iostream>
  3. int main()
  4. {
  5. // 2x2 Matrix
  6. int M = 2, N = 2, K = 2;
  7. double A[4] = {1.0, 2.0, 3.0, 4.0};
  8. double B[4] = {5.0, 6.0, 7.0, 8.0};
  9. double C[4];
  10. // General Matrix-Matrix multiplication
  11. cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
  12. M, N, K,
  13. 1.0, A, K, B, N,
  14. 0.0, C, N);
  15. // Print results
  16. std::cout << "Result of A * B = C:" << std::endl;
  17. for (int i = 0; i < 2; ++i) {
  18. for (int j = 0; j < 2; ++j) {
  19. std::cout << C[i * 2 + j] << " ";
  20. }
  21. std::cout << std::endl;
  22. }
  23. return 0;
  24. }
  25. // CompileOptions: `pkg-config --cflags --libs openblas`
  1. $ cpi dgemm.cpp
  2. Result of A * B = C:
  3. 19 22
  4. 43 50

Qt application can also be run.

  1. #include <QApplication>
  2. #include <QLabel>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. QLabel label("Hello, world!");
  7. label.setAlignment(Qt::AlignCenter);
  8. label.setWindowTitle("Hello");
  9. label.resize(200, 100);
  10. label.show();
  11. return app.exec();
  12. }
  13. // CompileOptions: `pkg-config --cflags --libs Qt6Widgets`
  1. $ cpi helloworld_qt.cpp

Running like a scripting language

Adding a shebang, save as hello.cpps. No longer compiled in a C++ compiler successfully.

  1. #!/usr/bin/env cpi
  2. #include <iostream>
  3. int main()
  4. {
  5. std::cout << "Hello world\n";
  6. return 0;
  7. }
  1. $ chmod +x hello.cpps
  2. $ ./hello.cpps
  3. Hello world

Yes, a shell script. I named it CppScript.

Help

  1. cpi> .help
  2. .conf Display the current values for various settings.
  3. .help Display this help.
  4. .rm LINENO Remove the code of the specified line number.
  5. .show Show the current source code.
  6. .quit Exit this program.

Download

Download Page

Web Site

http://treefrogframework.github.io/cpi/