首页
/ Tiny C++ 解释器技术文档

Tiny C++ 解释器技术文档

2024-12-28 04:37:24作者:乔或婵

以下是关于如何安装、使用以及API文档的详细介绍。

1. 安装指南

Linux

  1. 确保安装了Qt 6工具包和编译器(GNU C++编译器、LLVM C++编译器或MSVC C++编译器)。
  2. 执行以下命令:
    $ qmake
    $ make
    $ sudo make install
    $ cpi -v
    cpi 2.2.0
    

Windows

  1. 确保安装了Qt 6工具包和Visual Studio 2022 Developer Command Prompt。
  2. 执行以下命令:
    > 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        (运行cpi命令)
    Usage: cpi.exe [options] [file] [-]
    Tiny C++ Interpreter.
    Runs in interactive mode by default.
    

2. 项目使用说明

交互模式

  1. 运行以下命令进入交互模式:
    $ cpi
    
  2. 输入代码并执行,例如:
    cpi> 3 << 23;        (位运算)
    25165824
    
    cpi> int a = 3;
    cpi> ~a;              (取反)
    -4
    cpi> a ^ 2;           (异或)
    1
    
    cpi> auto func = [](int n) { return n*n; };     (Lambda函数)
    cpi> func(3);
    9
    

执行模式

  1. 将C++源代码保存为hello.cpp
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello world\n";
        return 0;
    }
    
  2. 使用以下命令执行:
    $ cpi hello.cpp
    Hello world
    

编译选项

  1. 在代码中指定编译选项,例如链接数学库:

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    int main(int argc, char *argv[])
    {
        if (argc != 2) return 0;
    
        std::cout << sqrt(atoi(argv[1])) << std::endl;
        return 0;
    }
    // CompileOptions: -lm
    
  2. 使用以下命令执行:

    $ cpi sqrt.cpp 2
    1.41421
    

3. 项目API使用文档

cpi提供了一个简单的API,用于在交互模式下执行C++代码。以下是部分API列表:

  • .conf:显示各种设置的当前值。
  • .help:显示此帮助信息。
  • .rm LINENO:删除指定行号的代码。
  • .show:显示当前源代码。
  • .quit:退出程序。

4. 项目安装方式

安装方式已在“安装指南”部分详细说明。请按照上述步骤进行安装。

登录后查看全文
热门项目推荐