Path: blob/a-new-beginning/SharedDependencies/Sources/nihstro/parser_assembly/compare.cpp
2 views
// Copyright 2014 Tony Wasserka1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11// * Neither the name of the owner nor the names of its contributors may12// be used to endorse or promote products derived from this software13// without specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS16// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT17// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR18// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT19// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.262728// Enable this for detailed XML overview of parser results29// #define BOOST_SPIRIT_DEBUG3031#include <boost/fusion/include/adapt_struct.hpp>32#include <boost/phoenix/core/reference.hpp>33#include <boost/spirit/include/qi.hpp>3435#include "nihstro/parser_assembly.h"36#include "nihstro/parser_assembly_private.h"3738#include "nihstro/shader_binary.h"39#include "nihstro/shader_bytecode.h"4041namespace spirit = boost::spirit;42namespace qi = boost::spirit::qi;43namespace ascii = boost::spirit::qi::ascii;44namespace phoenix = boost::phoenix;4546using spirit::_1;47using spirit::_2;48using spirit::_3;49using spirit::_4;5051using namespace nihstro;5253// Adapt parser data structures for use with boost::spirit5455/*BOOST_FUSION_ADAPT_STRUCT(56IntegerWithSign,57(int, sign)58(unsigned, value)59)60*/61BOOST_FUSION_ADAPT_STRUCT(62CompareInstruction,63(OpCode, opcode)64(std::vector<Expression>, arguments)65(std::vector<Instruction::Common::CompareOpType::Op>, ops)66)6768template<>69CompareParser<ParserIterator>::CompareParser(const ParserContext& context)70: CompareParser::base_type(instruction),71common(context),72opcodes_compare(common.opcodes_compare),73expression(common.expression),74end_of_statement(common.end_of_statement),75diagnostics(common.diagnostics) {7677// TODO: Will this properly match >= ?78compare_ops.add79( "==", CompareOp::Equal )80( "!=", CompareOp::NotEqual )81( "<", CompareOp::LessThan )82( "<=", CompareOp::LessEqual )83( ">", CompareOp::GreaterThan )84( ">=", CompareOp::GreaterEqual );8586// Setup rules8788auto comma_rule = qi::lit(',');8990opcode = qi::no_case[qi::lexeme[opcodes_compare >> &ascii::space]];91compare_op = qi::lexeme[compare_ops];9293// cmp src1, src2, op1, op294// TODO: Also allow "cmp src1 op1 src2, src1 op2 src2"95two_ops = compare_op > comma_rule > compare_op;96two_expressions = expression > comma_rule > expression;97instr[0] = opcode > two_expressions > comma_rule > two_ops;9899instruction = instr[0] > end_of_statement;100101// Error handling102// BOOST_SPIRIT_DEBUG_NODE(instr[0]);103// BOOST_SPIRIT_DEBUG_NODE(instruction);104105// qi::on_error<qi::fail>(instruction, error_handler(phoenix::ref(diagnostics), _1, _2, _3, _4));106}107108109