Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/treefrogframework/cpi
Tiny c++ interpreter
https://github.com/treefrogframework/cpi
c-plus-plus cpp cpp14 cpp17 cpp20 interpreter
Last synced: 26 days ago
JSON representation
Tiny c++ interpreter
- Host: GitHub
- URL: https://github.com/treefrogframework/cpi
- Owner: treefrogframework
- License: mit
- Created: 2012-12-08T14:45:36.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2024-04-15T05:33:26.000Z (7 months ago)
- Last Synced: 2024-05-02T01:05:35.382Z (6 months ago)
- Topics: c-plus-plus, cpp, cpp14, cpp17, cpp20, interpreter
- Language: C++
- Homepage: https://treefrogframework.github.io/cpi/
- Size: 102 KB
- Stars: 190
- Watchers: 14
- Forks: 34
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Tiny C++ Interpreter
[![ActionsCI](https://github.com/treefrogframework/cpi/actions/workflows/actions.yml/badge.svg)](https://github.com/treefrogframework/cpi/actions/workflows/actions.yml)
[![License](https://img.shields.io/badge/license-MIT-blue)](https://opensource.org/licenses/MIT)
[![Release](https://img.shields.io/github/v/release/treefrogframework/cpi.svg)](https://github.com/treefrogframework/cpi/releases)Cpi is a tiny interpreter for C++17 or C++20.
## 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:
```sh
$ qmake
$ make
$ sudo make install
$ cpi -v
cpi 2.1.0
```Windows (Command prompt for VS2022):
```bat
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.9.6
** Copyright (c) 2022 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.
```## Interactive Mode
```
$ cpi (Run cpi.bat in windows)
cpi 2.1.0
Type ".help" for more information.
Loaded INI file: /home/foo/.config/cpi/cpi.confcpi> 3 << 23; (Bitwise operation)
25165824cpi> int a = 3;
cpi> ~a; (Complement)
-4
cpi> a ^ 2; (XOR)
1cpi> auto func = [](int n) { return n*n; }; (Lambda function)
cpi> func(3);
9cpi> .quit ( or press ctrl+c )
```Code can be pasted.
```cpp
$ cpi (Run cpi.bat in windows)
cpi> #include (Paste code here)
#includeint main()
{
std::map 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)
```## Executive mode
Save C++ source code as *hello.cpp*.```cpp
#includeint main()
{
std::cout << "Hello world\n";
return 0;
}
```Run cpi in command line.
```sh
$ cpi hello.cpp
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.```cpp
#include
#include
#includeint main(int argc, char *argv[])
{
if (argc != 2) return 0;std::cout << sqrt(atoi(argv[1])) << std::endl;
return 0;
}
// CompileOptions: -lm
``````sh
$ cpi sqrt.cpp 2
1.41421$ cpi sqrt.cpp 3
1.7320
```#### Running like a scripting language
Adding a shebang, save as *hello.cpps*. No longer compiled in a C++ compiler successfully.```cpp
#!/usr/bin/env cpi
#includeint main()
{
std::cout << "Hello world\n";
return 0;
}
``````sh
$ chmod +x hello.cpps
$ ./hello.cpps
Hello world
```Yes, a shell script. I named it CppScript.
## Help
```
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.
```## Download
[Download Page](https://github.com/treefrogframework/cpi/releases)## Web Site
http://treefrogframework.github.io/cpi/