C/C++ 交互式解释器

3
我想知道是否有类似于Python解释器的C/C++等效工具:
$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "   test\n\n ".strip()
'test'
>>> import re
>>> re.search('([\d\.]*).tgz', 'package-2.1.5.tgz').group(1)
'2.1.5'

到目前为止,我一直使用一个虚拟的bash脚本来实现这个目的。它不是交互式的,但它可以防止创建一个带有main等内容的C++文件来检查单个或一些命令的结果。

我相信我们可以在gdb或eclipse中以某种方式完成它,或者它存在于某个隐藏的软件包中,因此如果您知道该问题周围的有趣信息,我将不胜感激。谢谢,祝您愉快。

 $ cat cInterpreter.sh
 #!/bin/bash

function usage {
  cat <<EOF
USAGE:
$0 '<semi-colon separated includes>' '<semi-colon separated commands>'

EXAMPLE:
$0 'arpa/inet.h' 'printf("%04x\n", htons(5294));'

EOF
}

if [ $# -ne 2 ] || [ "$1" = "--help" ]; then
  usage
  exit 0
fi

includes=$1
commands=$2

g++ -Wall -x c++ - -o /tmp/cInterpreter.bin <<EOF &&
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

$(tr ';' '\n' <<< $includes | xargs -I{} echo "#include <{}>")

int main() {
  $commands;
  return 0;
}

EOF
 /tmp/cInterpreter.bin

$ cInterpreter.sh 'arpa/inet.h' 'printf("%04x\n", htons(5294));'
ae14
3个回答

6
你有Cling,它不完美,但它是一个C++的REPL。
参考链接:http://blog.coldflake.com/posts/On-the-fly-C++/
git clone http://root.cern.ch/git/llvm.git src
cd src
git checkout cling-patches
cd tools
git clone http://root.cern.ch/git/cling.git
git clone http://root.cern.ch/git/clang.git
cd clang
git checkout cling-patches

cd ../..
./configure --enable-cxx11
make
sudo make install

$ cling -Wc++11-extensions -std=c++11

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <arpa/inet.h>
[cling]$ htons(5294)
(uint16_t) 44564
[cling]$ printf("%04x\n", htons(5294))
input_line_5:2:2: error: use of undeclared identifier 'printf'
 printf("%04x\n", htons(5294))
 ^
[cling]$ #include <stdio.h>
[cling]$ printf("%04x\n", htons(5294))
ae14

谢谢,看起来很有前途! - user1556814

2

1

RCRL (read-compile-run-loop) 是 C++ 的一个替代品,可用于取代 cling。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接