| 1 | /*============================================================================= |
|---|---|
| 2 | Copyright (c) 2001-2014 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | #include "compiler.hpp" |
| 8 | #include "vm.hpp" |
| 9 | |
| 10 | namespace client |
| 11 | { |
| 12 | void compiler::operator()(ast::nil) const |
| 13 | { |
| 14 | BOOST_ASSERT(0); |
| 15 | } |
| 16 | |
| 17 | void compiler::operator()(unsigned int n) const |
| 18 | { |
| 19 | code.push_back(x: op_int); |
| 20 | code.push_back(x: n); |
| 21 | } |
| 22 | |
| 23 | void compiler::operator()(ast::operation const& x) const |
| 24 | { |
| 25 | boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 26 | switch (x.operator_) |
| 27 | { |
| 28 | case '+': code.push_back(x: op_add); break; |
| 29 | case '-': code.push_back(x: op_sub); break; |
| 30 | case '*': code.push_back(x: op_mul); break; |
| 31 | case '/': code.push_back(x: op_div); break; |
| 32 | default: BOOST_ASSERT(0); break; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void compiler::operator()(ast::signed_ const& x) const |
| 37 | { |
| 38 | boost::apply_visitor(visitor: *this, visitable: x.operand_); |
| 39 | switch (x.sign) |
| 40 | { |
| 41 | case '-': code.push_back(x: op_neg); break; |
| 42 | case '+': break; |
| 43 | default: BOOST_ASSERT(0); break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void compiler::operator()(ast::expression const& x) const |
| 48 | { |
| 49 | boost::apply_visitor(visitor: *this, visitable: x.first); |
| 50 | for (ast::operation const& oper : x.rest) |
| 51 | { |
| 52 | (*this)(oper); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 |
