NodePointer Parser::factor() { NodePointer result; SymbolType type = currentSymbol_.symbolType_; if (TheOperatorFactory::getInstance().isOperator(type)) { if (type == SUBSTRACTION) { type = MINUS; } OperatorPointer theOperator = TheOperatorFactory::getInstance().create(type); if (!theOperator->isUnary()) { throw ErrorMessage("Unary operator expected", getLineNumber()); } getNextSymbol(); NodePointer subExpressionTree = expression(theOperator->precedence()); result = NodePointer(new Expression(theOperator, NodePointerInserter()(subExpressionTree), getLineNumber())); } else if (type == LEFT_PARENTHESIS) { getNextSymbol(); NodePointer subExpressionTree = expression(); accept(RIGHT_PARENTHESIS); result = subExpressionTree; } else if (type == IDENTIFIER || type == GLOBAL) { result = NodePointer(new Variable(currentSymbol_.value_.toString(), tables_, type == GLOBAL)); getNextSymbol(); } else if (type == ARGUMENT) { result = NodePointer(new Argument(tables_.subroutineStack_, currentSymbol_.value_, getLineNumber())); getNextSymbol(); } else if (type == CONSTANT) { result = NodePointer(new Constant(currentSymbol_.value_)); getNextSymbol(); } else if (type == CALL) { return procedureCall(); } else { throw ErrorMessage("Unexpected symbol " + symbolToString(currentSymbol_), getLineNumber()); } return result; }
void SpiralScene::setupObjects() { int numSpheres = 100; double sphereScaling = .6; Angle angleBetweenSpheres = Angle::degrees(25); double depthBetweenSpheresBase = 1.2; double depthBetweenSpheresMultiple = 1.10; int firstSphereDepth = 10; double radius = 2; NodePointer rootNode(new TransformNode(getCamera().getTransform())); MaterialNodePointer material(new MaterialNode(rootNode)); Color white(1, 1, 1); material->setAmbient(white * .1); material->setDiffuse(white * .5); material->setSpecular(white); material->setShininess(20); NodePointer firstDepthTranslation(new TranslationNode(0, 0, -firstSphereDepth, material)); NodePointer lastDepthTranslation = firstDepthTranslation; for (int i = 0; i < numSpheres; i++) { Angle rotationAngle = angleBetweenSpheres * i; NodePointer rotation(new RotationNode(rotationAngle, Vector3d::UnitZ(), lastDepthTranslation)); NodePointer radiusTranslation(new TranslationNode(radius, 0, 0, rotation)); NodePointer sphereScalingNode(new ScalingNode(sphereScaling, sphereScaling, sphereScaling, radiusTranslation)); RayObjectPointer sphere(new Sphere(sphereScalingNode)); addObject(sphere); double depthBetweenSpheres = depthBetweenSpheresBase * pow(depthBetweenSpheresMultiple, i); lastDepthTranslation = NodePointer(new TranslationNode(0, 0, -depthBetweenSpheres, lastDepthTranslation)); } }
void Node::setChildren( const std::vector< std::shared_ptr< Node > >& theChildren ) { children.clear(); for ( auto it = theChildren.begin(); it != theChildren.end(); ++it ) { Node* child = new Node(); ( * child ) = ( * ( * it ) ); children.push_back( NodePointer( child ) ); } }
NodePointer Parser::localVariable() { accept(MY); if (currentSymbol_.symbolType_ == IDENTIFIER) { return NodePointer(new LocalVariable(currentSymbol_.value_.toString(), tables_)); } else { throw ErrorMessage("Variable expected", getLineNumber()); } }
NodePointer Parser::procedureDefinition() { getNextSymbol(); std::string name = "&" + currentSymbol_.value_.toString(); accept(DEFINITION); NodePointer definition = blockStatement(); if (tables_.subroutineTable_.find(name) != tables_.subroutineTable_.end()) { throw ErrorMessage("Multiple definition of subroutine " + name, getLineNumber()); } tables_.subroutineTable_[name] = definition; return NodePointer(new EmptyNode()); }
NodePointer Parser::getNextParseTree() { if (!currentParseTree_) { getNextSymbol(); } if (currentSymbol_.symbolType_ != END) { currentParseTree_ = statement(); } else { currentParseTree_ = NodePointer(); } return currentParseTree_; }
NodePointer Parser::procedureCall() { NodeArray arguments; SymbolIndex definitionIndex = currentSymbol_.value_.toString(); getNextSymbol(); accept(LEFT_PARENTHESIS); if (currentSymbol_.symbolType_ != RIGHT_PARENTHESIS) { arguments.push_back(expression()); while (currentSymbol_.symbolType_ == COMMA) { getNextSymbol(); arguments.push_back(expression()); } } accept(RIGHT_PARENTHESIS); NodePointer subroutineCall = NodePointer(new SubroutineCall(tables_, definitionIndex, arguments, getLineNumber())); return subroutineCall; }
NodePointer Parser::expression(int precedence) { NodePointer expressionTree = factor(); while (true) { SymbolType type = currentSymbol_.symbolType_; if (TheOperatorFactory::getInstance().isOperator(type)) { OperatorPointer theOperator = TheOperatorFactory::getInstance().create(type); if (!theOperator->isBinary()) { throw ErrorMessage("Binary operator expected", getLineNumber()); } int newPrecedence = theOperator->precedence(); if (newPrecedence < precedence) { break; } if (theOperator->assiociativity() == LEFT) { newPrecedence++; } getNextSymbol(); NodePointer subExpressionTree = expression(newPrecedence); expressionTree = NodePointer(new Expression(theOperator, NodePointerInserter()(expressionTree)(subExpressionTree), getLineNumber())); } else { break; } } return expressionTree; }