EnhancedMatrix buildMatrixAux( antlr::RefAST &refAST ) {

	switch( refAST->getType() ) {

		case MExprParserTokenTypes::SEM :
		{
			//std::cerr << "SEM" << std::endl;
			antlr::RefAST child1 = refAST->getFirstChild();
			EnhancedMatrix enhancedMatrix1 = buildMatrixAux( child1 );
			antlr::RefAST child2 = child1->getNextSibling();
			EnhancedMatrix enhancedMatrix2 = buildMatrixAux( child2 );
			return enhancedMatrix1.colJoin( enhancedMatrix2 );
		}

		case MExprParserTokenTypes::WS  :
		case MExprParserTokenTypes::CMA :
		{
			//std::cerr << "WS|CMA" << std::endl;
			antlr::RefAST child = refAST->getFirstChild();
			if ( child == antlr::nullAST ) return EnhancedMatrix();

			EnhancedMatrix enhancedMatrix = buildMatrixAux( child );
			while(  ( child = child->getNextSibling() )  !=  antlr::nullAST  ) {
				enhancedMatrix = enhancedMatrix.rowJoin(  buildMatrixAux( child )  );
			}
			return enhancedMatrix;
		}

		case MExprParserTokenTypes::LBRACK :
		{
			//std::cerr << "LBRACK" << std::endl;
			antlr::RefAST child = refAST->getFirstChild();
			if ( child == antlr::nullAST ) return EnhancedMatrix();

			EnhancedMatrix enhancedMatrix = buildMatrixAux( child );
			while(  ( child = child->getNextSibling() )  !=  antlr::nullAST  ) {
				enhancedMatrix = enhancedMatrix.rowJoin(  buildMatrixAux( child )  );
			}
			return enhancedMatrix;
		}

		case MExprParserTokenTypes::ADD :
		case MExprParserTokenTypes::SUB :
		case MExprParserTokenTypes::MUL :
		case MExprParserTokenTypes::DIV :
		case MExprParserTokenTypes::MOD :
		case MExprParserTokenTypes::VAR :
		case MExprParserTokenTypes::NUM :
		{
			//std::cerr << "VALUE" << std::endl;
			double value = getValue( refAST );
			return EnhancedMatrix( value );
		}
	}
	return EnhancedMatrix();
}
Пример #2
0
int eeeprintDotTree(antlr::RefAST tree, std::ostream& out, antlr::Parser& p) {
	int me = ++_dot_node;
	out << me << " [label=\"" << p.getTokenName(tree->getType())
		<< "\\n" << tree->getText() << "\"];" << std::endl;
	antlr::RefAST c = tree->getFirstChild();
	while (antlr::nullAST != c) {
		int child = printDotTree(c, out, p);
		out << me << " -> " << child << ";" << std::endl;
		c = c->getNextSibling();
	}
	return me;
}
Пример #3
0
void eeeprintTree(antlr::RefAST tree, std::ostream& out, antlr::Parser& p, int indent) {
	int j = indent;
	std::string i = ""; while (j-- > 0) i += "  ";
	if (tree->getFirstChild()) {
		out << i << "( " << tree->toString() << " <" <<
			p.getTokenName(tree->getType()) << ">" << std::endl;
		printTree(tree->getFirstChild(), out, p, indent+1);
		out << i << ")" << std::endl;
	} else {
		out << i << tree->toString() << " <" << 
			p.getTokenName(tree->getType()) << ">" << std::endl;
	}
	if (tree->getNextSibling()) {
		printTree(tree->getNextSibling(), out, p, indent);
	}
}
double getValue( antlr::RefAST &refAST ) {

	switch( refAST->getType() ) {

		case MExprParserTokenTypes::ADD :
		case MExprParserTokenTypes::SUB :
		{
			std::string op = refAST->getText();

			antlr::RefAST child1 = refAST->getFirstChild();
			double value1 = getValue( child1 );
			if (  ISNAN( value1 )  ) return std::numeric_limits< double >::quiet_NaN();

			antlr::RefAST child2 = child1->getNextSibling();
			if ( child2 == antlr::nullAST ) {
				return op == "+" ? value1 : -value1;
			}
			double value2 = getValue( child2 );
			if (  ISNAN( value2 )  ) return std::numeric_limits< double >::quiet_NaN();

			return op == "+" ? value1 + value2 : value1 - value2;
		}

		case MExprParserTokenTypes::MUL :
		case MExprParserTokenTypes::DIV :
		case MExprParserTokenTypes::MOD :
		{
			std::string op = refAST->getText();
			antlr::RefAST child1 = refAST->getFirstChild();
			double value1 = getValue( child1 );
			if (  ISNAN( value1 )  ) return std::numeric_limits< double >::quiet_NaN();

			antlr::RefAST child2 = child1->getNextSibling();
			double value2 = getValue( child2 );
			if (  ISNAN( value2 )  ) return std::numeric_limits< double >::quiet_NaN();

			if ( op == "*" ) return value1 * value2;
			if ( op == "/" ) return value1 / value2;

			// MOD (%)
			if ( value2 == 0 ) return std::numeric_limits< double >::quiet_NaN();
			return static_cast< int >( value1 ) % static_cast< int >( value2 );
		}

		case MExprParserTokenTypes::VAR :
		{
			std::string op = refAST->getText();
			if ( op == "pi" ) return M_PI;
			if ( op == "e" ) return M_E;
			return std::numeric_limits< double >::quiet_NaN();
		}

		case MExprParserTokenTypes::NUM :
		{
			std::string num = refAST->getText();
			return atof( num.c_str() );
		}

	}

	return std::numeric_limits< double >::quiet_NaN();
}