1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "compiler.hpp"
#include "vm.hpp"
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/assert.hpp>
namespace client { namespace code_gen
{
void program::op(int a)
{
code.push_back(a);
}
void program::op(int a, int b)
{
code.push_back(a);
code.push_back(b);
}
void program::op(int a, int b, int c)
{
code.push_back(a);
code.push_back(b);
code.push_back(c);
}
int const* program::find_var(std::string const& name) const
{
std::map<std::string, int>::const_iterator i = variables.find(name);
if (i == variables.end())
return 0;
return &i->second;
}
void program::add_var(std::string const& name)
{
std::size_t n = variables.size();
variables[name] = n;
}
void program::print_variables(std::vector<int> const& stack) const
{
typedef std::pair<std::string, int> pair;
BOOST_FOREACH(pair const& p, variables)
{
std::cout << " " << p.first << ": " << stack[p.second] << std::endl;
}
}
void program::print_assembler() const
{
std::vector<int>::const_iterator pc = code.begin();
std::vector<std::string> locals(variables.size());
typedef std::pair<std::string, int> pair;
BOOST_FOREACH(pair const& p, variables)
{
locals[p.second] = p.first;
std::cout << "local "
<< p.first << ", @" << p.second << std::endl;
}
while (pc != code.end())
{
switch (*pc++)
{
case op_neg:
std::cout << "op_neg" << std::endl;
break;
case op_add:
std::cout << "op_add" << std::endl;
break;
case op_sub:
std::cout << "op_sub" << std::endl;
break;
case op_mul:
std::cout << "op_mul" << std::endl;
break;
case op_div:
std::cout << "op_div" << std::endl;
break;
case op_load:
std::cout << "op_load " << locals[*pc++] << std::endl;
break;
case op_store:
std::cout << "op_store " << locals[*pc++] << std::endl;
break;
case op_int:
std::cout << "op_int " << *pc++ << std::endl;
break;
case op_stk_adj:
std::cout << "op_stk_adj " << *pc++ << std::endl;
break;
}
}
}
bool compiler::operator()(unsigned int x) const
{
program.op(op_int, x);
return true;
}
bool compiler::operator()(ast::variable const& x) const
{
int const* p = program.find_var(x.name);
if (p == 0)
{
std::cout << x.id << std::endl;
error_handler(x.id, "Undeclared variable: " + x.name);
return false;
}
program.op(op_load, *p);
return true;
}
bool compiler::operator()(ast::operation const& x) const
{
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.operator_)
{
case '+': program.op(op_add); break;
case '-': program.op(op_sub); break;
case '*': program.op(op_mul); break;
case '/': program.op(op_div); break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::signed_ const& x) const
{
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.sign)
{
case '-': program.op(op_neg); break;
case '+': break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::expression const& x) const
{
if (!boost::apply_visitor(*this, x.first))
return false;
BOOST_FOREACH(ast::operation const& oper, x.rest)
{
if (!(*this)(oper))
return false;
}
return true;
}
bool compiler::operator()(ast::assignment const& x) const
{
if (!(*this)(x.rhs))
return false;
int const* p = program.find_var(x.lhs.name);
if (p == 0)
{
std::cout << x.lhs.id << std::endl;
error_handler(x.lhs.id, "Undeclared variable: " + x.lhs.name);
return false;
}
program.op(op_store, *p);
return true;
}
bool compiler::operator()(ast::variable_declaration const& x) const
{
int const* p = program.find_var(x.assign.lhs.name);
if (p != 0)
{
std::cout << x.assign.lhs.id << std::endl;
error_handler(x.assign.lhs.id, "Duplicate variable: " + x.assign.lhs.name);
return false;
}
bool r = (*this)(x.assign.rhs);
if (r) // don't add the variable if the RHS fails
{
program.add_var(x.assign.lhs.name);
program.op(op_store, *program.find_var(x.assign.lhs.name));
}
return r;
}
bool compiler::operator()(ast::statement_list const& x) const
{
program.clear();
// op_stk_adj 0 for now. we'll know how many variables we'll have later
program.op(op_stk_adj, 0);
BOOST_FOREACH(ast::statement const& s, x)
{
if (!boost::apply_visitor(*this, s))
{
program.clear();
return false;
}
}
program[1] = program.nvars(); // now store the actual number of variables
return true;
}
}}
|