Giter VIP home page Giter VIP logo

cpi's Introduction

Tiny C++ Interpreter

Cpi is a tiny interpreter for C++11 code.

Requirements

  • Qt qmake build system
  • Compiler - GNU C++ compiler or LLVM C++ compiler

Install

  $ qmake
  $ make
  $ sudo make install

Executive mode

Save C++ source code as hello.cpp.

#include <iostream>

int main()
{
    std::cout << "Hello world\n";
    return 0;
}

Run cpi in command line.

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

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2) return 0;

    cout << sqrt(atoi(argv[1])) << endl;
    return 0;
}
// CompileOptions: -lm
  $ 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.

#!/usr/bin/env cpi
#include <iostream>

int main()
{
    std::cout << "Hello world\n";
    return 0;
}
  $ chmod +x hello.cpps
  $ ./hello.cpps
  Hello world

Yes, a shell script. I named it CppScript.

Interactive Mode

  $ cpi
  Cpi 2.0.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 )

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

Web Site

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

cpi's People

Contributors

aoym avatar treefrogframework avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.