Example #1
0
void Printer::printBlockContents(BlockNode* node) {
    // functions delcarations
    Scope::FunctionIterator funIt(node->scope());
    while (funIt.hasNext()) {
        funIt.next()->node()->visit(this);
        out << '\n';
    }

	// variables declarations
    Scope::VarIterator varIt(node->scope());
    while (varIt.hasNext()) {
        AstVar* var = varIt.next();
        out << typeToName(var->type()) 
        	<< " " << var->name() << ";\n";
    }

    // nodes
    for (size_t i = 0; i < node->nodes(); ++i) {
        AstNode* subNode = node->nodeAt(i);
        subNode->visit(this);
        if (!subNode->isIfNode() && 
        	!subNode->isWhileNode() && 
        	!subNode->isForNode()) {
            out << ';';
        }
        out << '\n';
    }
}
Example #2
0
void ASTAnalyzer::printBlock (BlockNode* node) {
    for (unsigned int i = 0; i < node->nodes(); ++i) {
        AstNode* innerNode = node->nodeAt(i);
        innerNode->visit(this);
        if (!innerNode->isIfNode() && !innerNode->isWhileNode()
            && !innerNode->isForNode()) {
            output << ';';
        }
        output << '\n';
    }
}
Example #3
0
void SourceByASTPrinter::visitBlockNodeWithoutBraces(BlockNode *node){
	Scope::VarIterator vIt(node->scope());
	while(vIt.hasNext()) {
		AstVar *var = vIt.next();
		cout<<typeToName(var->type())<<" "<< var->name()<< ";"<<endl;
	}

	Scope::FunctionIterator fIt(node->scope());
	while(fIt.hasNext()) {
		fIt.next()->node()->visit(this);
	}

	int nodesNumber = node->nodes();
	for (int i = 0; i < nodesNumber; ++i) {
		AstNode *nodeAt = node->nodeAt(i);
		nodeAt->visit(this);
		if(!nodeAt->isForNode() && !nodeAt->isIfNode() && !nodeAt->isWhileNode())
			cout<<";"<<endl;
	}
}
Example #4
0
void ByteCodeVisitor::initLocals(BlockNode *blockNode) {
    if (blockNode == 0) return;
    initVars(blockNode->scope());
    for (uint32_t i = 0; i < blockNode->nodes(); ++i) {
        AstNode *n = blockNode->nodeAt(i);
        if (n->isBlockNode()) {
            initLocals(blockNode);
        } else if (n->isIfNode()) {
            IfNode *ifNode = n->asIfNode();
            initLocals(ifNode->thenBlock());
            initLocals(ifNode->elseBlock());
        } else if (n->isWhileNode()) {
            WhileNode *whileNode = n->asWhileNode();
            initLocals(whileNode->loopBlock());
        } else if (n->isForNode()) {
            ForNode *forNode = n->asForNode();
            initLocals(forNode->body());
        }
    }
    initChildFunctions(blockNode->scope());
}
Example #5
0
void TypeCheckerVisitor::visitBlockNode(BlockNode* node) {
	_current_scope = node->scope();
	Scope::FunctionIterator funcIt(_current_scope);
	while (funcIt.hasNext()) {
		AstFunction* func = funcIt.next();
		func->node()->visit(this);
	}
	int nodes_count = node->nodes();
	VarType commonType = VT_VOID;
	for (int i = 0; i != nodes_count; ++i) {
		AstNode* currentNode = node->nodeAt(i);
		currentNode->visit(this);
		bool isBlockNode = currentNode->isForNode() || currentNode->isWhileNode() || currentNode->isIfNode();
		VarType currentNodeType = getNodeType(currentNode);
		bool hasType = currentNodeType != VT_VOID;
		if ((isBlockNode && hasType) || (i == nodes_count - 1)) {
			commonType = getUpperCommonType(commonType, currentNodeType);
		}
	}
	setNodeType(node, commonType);
	_current_scope = node->scope()->parent();
}