main.cpp就一个main函数,倒是直接明了
main函数大概的流程:
- 读代码文件
- 使用spirit将代码解析成抽象语法树
- 使用compiler把语法树编译成字节码
- 从编译后的字节中找出main函数
- 为main函数传入参数(通过虚拟栈传递)
- 使用VM执行字节码
- 输出main函数的返回值
一点点把玩其代码:
1. 读"代码文件":
点击(此处)折叠或打开
- std::string source_code; // We will read the contents here.
- in.unsetf(std::ios::skipws); // No white space
- std::copy(
- std::istream_iterator<char>(in),
- std::istream_iterator<char>(),
- std::back_inserter(source_code));
居然用copy、istream_iterator、back_inserter来读文件,文节青年的代码啊
像我等普通青年,估计就用二进制方式打开文件,判断一下文件长度,直接分析好内存,一次read搞定了
2. 解析语法树
点击(此处)折叠或打开
- bool success = phrase_parse(iter, end, +function, skipper, ast);
其中iter和end给出了解析的字符串的起始位置
+function表示解析的语法规则。注意其中加号是不能省的,它表示匹配function一次或多次。spirit重载了+号*号,以和BNF范式统一,不过由于C++语言的限制,它们只能放在规则之前,而不是像传统一样写在规则之后。
skipper表示使用的跳过语法规则。命中skipper的字符串部分将直接跳过,不参与解析。
后面的代码如下。比较直接,不再赘述
点击(此处)折叠或打开
- if (success && iter == end)
- {
- if (compiler(ast))
- {
- boost::shared_ptr<client::code_gen::function>
- p = compiler.find_function("main");
- if (!p)
- return 1;
-
- int nargs = argc-2;
- if (p->nargs() != nargs)
- {
- std::cerr << "Error: main function requires " << p->nargs() << " arguments." << std::endl;
- std::cerr << nargs << "supplied." << std::endl;
- return 1;
- }
-
- std::cout << "Success\n";
- std::cout << "-------------------------\n";
- std::cout << "Assembler----------------\n\n";
- compiler.print_assembler();
-
- // Push the arguments into our stack
- for (int i = 0; i < nargs; ++i)
- vm.get_stack()[i] = boost::lexical_cast<int>(argv[i+2]);
-
- // Call the interpreter
- int r = vm.execute(compiler.get_code());
-
- std::cout << "-------------------------\n";
- std::cout << "Result: " << r << std::endl;
- std::cout << "-------------------------\n\n";
- }
- else
- {
- std::cout << "Compile failure\n";
- }
- }
- else
- {
- std::cout << "Parse failure\n";
- }
注意:编译后的字节码是放在compiler对象里的。但虚拟栈是由vm对象提供的。
有点奇怪function为什么用shared_ptr来返回,貌似完全没有share的必要。或者仅仅是为了避免少写compiler的析构函数?
另一个奇怪之处是从compiler获取了main函数的指针,但仅仅用来判断传入参数的个数。vm.execute并没有指定从main函数开始执行。那么程序是怎么跳到main函数去执行的呢?直觉猜想编译出的代码里起始位置应该有直接jump到main函数的代码。翻了一下compiler.cpp,果然!
点击(此处)折叠或打开
- bool compiler::operator()(ast::function_list const& x)
- {
- // Jump to the main function
- code.push_back(op_jump);
- code.push_back(0); // we will fill this in later when we finish compiling
- // and we know where the main function is
-
- BOOST_FOREACH(ast::function const& f, x)
- {
- if (!(*this)(f))
- {
- code.clear();
- return false;
- }
- }
- // find the main function
- boost::shared_ptr<code_gen::function> p =
- find_function("main");
-
- if (!p) // main function not found
- {
- std::cerr << "Error: main function not defined" << std::endl;
- return false;
- }
- code[1] = p->get_address()-1; // jump to this (main function) address
-
- return true;
- }
其中又有一个boost的奇技淫巧的应用:BOOST_FOREACH,在<boost/foreach.hpp>中,用于简化遍历容器
不过C++11中已经从语言层面对此进行了支持,语法上略有不同:
点击(此处)折叠或打开
- int arr[] = {1, 2, 3, 4, 5};
- for (int& i : arr)
- {
- cout << i << endl;
- }
main.cpp解析完了,明天看看ast.hpp