#ifndef PARSER_SEMANTIC_ACTION_H #define PARSER_SEMANTIC_ACTION_H #include #include #include #include #include "generated/location.hh" namespace example { struct ParserSemanticValue { typedef ParserSemanticValue this_type; typedef int i32_type; typedef double f64_type; typedef std::string str_type; typedef int expr_type; typedef std::list expr_list_type; //deque? 末尾挿入の際に反復子が無効になっても良いかどうか(ポインタや参照は無効にならない)。 typedef boost::variant variant_type; variant_type var; // getter i32_type &as_i32() { return get_var_as();} f64_type &as_f64() { return get_var_as();} str_type &as_string() { return get_var_as();} expr_type &as_expr() { return get_var_as();} expr_list_type &as_expr_list() { return get_var_as();} template inline U &get_var_as() { if(U *p = boost::get(&var)){ return *p; } else{ var = U(); //use default constructor return boost::get(var); } } // setter this_type &operator=(i32_type v) { var = v; return *this;} this_type &operator=(const f64_type &v) { var = v; return *this;} this_type &operator=(const str_type &v) { var = v; return *this;} }; class ParserSemanticAction { public: typedef ParserSemanticValue::i32_type i32_type; typedef ParserSemanticValue::f64_type f64_type; typedef ParserSemanticValue::str_type str_type; typedef ParserSemanticValue::expr_type expr_type; typedef ParserSemanticValue::expr_list_type expr_list_type; typedef ParserSemanticValue semantic_type; typedef location location_type; void move_expr(expr_type &dst, expr_type &src); void op_comma(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_mul(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_div(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_mod(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_add(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_sub(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_shr(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_shl(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_bitand(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_bitxor(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_assign_bitor(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cond(expr_type &dst, expr_type &cond, expr_type &left, expr_type &right); void op_logor(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_logand(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_bitor(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_bitxor(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_bitand(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_eq(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_ne(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_lt(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_gt(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_le(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_cmp_ge(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_shl(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_shr(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_add(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_sub(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_mul(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_div(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_mod(expr_type &dst, expr_type &lhs, expr_type &rhs); void op_preinc(expr_type &dst, expr_type &operand); void op_predec(expr_type &dst, expr_type &operand); void op_minus(expr_type &dst, expr_type &operand); void op_plus(expr_type &dst, expr_type &operand); void op_bitnot(expr_type &dst, expr_type &operand); void op_lognot(expr_type &dst, expr_type &operand); void op_subscript(expr_type &dst,expr_type &base, expr_type &offset); void op_funcall(expr_type &dst,expr_type &func, expr_list_type &args); void op_member(expr_type &dst, expr_type &base, str_type &member); void op_postinc(expr_type &dst, expr_type &operand); void op_postdec(expr_type &dst, expr_type &operand); void create_expr_list(expr_list_type &dst); void move_expr_list(expr_list_type &dst, expr_list_type &src); void append_expr_list(expr_list_type &dst, expr_type &elem); void ident(expr_type &dst, str_type &id); void literal_i32(expr_type &dst, i32_type &value); void literal_f64(expr_type &dst, f64_type &value); void literal_str(expr_type &dst, str_type &value); void error(const location_type &loc, const std::string &msg); }; inline void ParserSemanticAction::move_expr(expr_type &dst, expr_type &src) { std::swap(dst, src); } inline void ParserSemanticAction::op_comma(expr_type &dst, expr_type &lhs, expr_type &rhs) { dst = rhs; } inline void ParserSemanticAction::op_assign(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_mul(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_div(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_mod(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_add(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_sub(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_shr(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_shl(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_bitand(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_bitxor(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_assign_bitor(expr_type &dst, expr_type &lhs, expr_type &rhs){} inline void ParserSemanticAction::op_cond(expr_type &dst, expr_type &cond, expr_type &left, expr_type &right){dst = cond ? left : right;} inline void ParserSemanticAction::op_logor(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs || rhs;} inline void ParserSemanticAction::op_logand(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs && rhs;} inline void ParserSemanticAction::op_bitor(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs | rhs;} inline void ParserSemanticAction::op_bitxor(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs ^ rhs;} inline void ParserSemanticAction::op_bitand(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs & rhs;} inline void ParserSemanticAction::op_cmp_eq(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs == rhs;} inline void ParserSemanticAction::op_cmp_ne(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs != rhs;} inline void ParserSemanticAction::op_cmp_lt(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs < rhs;} inline void ParserSemanticAction::op_cmp_gt(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs > rhs;} inline void ParserSemanticAction::op_cmp_le(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs <= rhs;} inline void ParserSemanticAction::op_cmp_ge(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs >= rhs;} inline void ParserSemanticAction::op_shl(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs << rhs;} inline void ParserSemanticAction::op_shr(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs >> rhs;} inline void ParserSemanticAction::op_add(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs + rhs;} inline void ParserSemanticAction::op_sub(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs - rhs;} inline void ParserSemanticAction::op_mul(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs * rhs;} inline void ParserSemanticAction::op_div(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs / rhs;} inline void ParserSemanticAction::op_mod(expr_type &dst, expr_type &lhs, expr_type &rhs){dst = lhs % rhs;} inline void ParserSemanticAction::op_preinc(expr_type &dst, expr_type &operand){dst = operand + 1;} inline void ParserSemanticAction::op_predec(expr_type &dst, expr_type &operand){dst = operand - 1;} inline void ParserSemanticAction::op_minus(expr_type &dst, expr_type &operand){dst = -operand;} inline void ParserSemanticAction::op_plus(expr_type &dst, expr_type &operand){dst = +operand;} inline void ParserSemanticAction::op_bitnot(expr_type &dst, expr_type &operand){dst = ~operand;} inline void ParserSemanticAction::op_lognot(expr_type &dst, expr_type &operand){dst = !operand ? 1 : 0;} inline void ParserSemanticAction::op_subscript(expr_type &dst,expr_type &base, expr_type &offset){} inline void ParserSemanticAction::op_funcall(expr_type &dst,expr_type &func, expr_list_type &args){} inline void ParserSemanticAction::op_member(expr_type &dst, expr_type &base, str_type &member){} inline void ParserSemanticAction::op_postinc(expr_type &dst, expr_type &operand){dst = operand + 1;} inline void ParserSemanticAction::op_postdec(expr_type &dst, expr_type &operand){dst = operand - 1;} inline void ParserSemanticAction::create_expr_list(expr_list_type &dst){} inline void ParserSemanticAction::move_expr_list(expr_list_type &dst, expr_list_type &src){} inline void ParserSemanticAction::append_expr_list(expr_list_type &dst, expr_type &elem){} inline void ParserSemanticAction::ident(expr_type &dst, str_type &id){} inline void ParserSemanticAction::literal_i32(expr_type &dst, i32_type &value){ dst = value;} inline void ParserSemanticAction::literal_f64(expr_type &dst, f64_type &value){} inline void ParserSemanticAction::literal_str(expr_type &dst, str_type &value){} inline void ParserSemanticAction::error(const location_type &loc, const std::string &msg) { std::cerr << msg << std::endl; } } #endif