Beispiel #1
0
void AstPrinterVisitor::visitBlockNode(BlockNode* node) {
    if (!isMainScope(node->scope())) {
        _output << "{" << endl;
    }

    Scope::VarIterator varIt(node->scope());
    while(varIt.hasNext()) {
        AstVar* var = varIt.next();
        printVarType(var->type());
        _output << " " << var->name();
        printSemicolon();
    }

    Scope::FunctionIterator funcIt(node->scope());
    while(funcIt.hasNext()) {
        FunctionNode* func = funcIt.next()->node();
        func->visit(this);
    }

    for (uint32_t i = 0; i < node->nodes(); ++i) {
        node->nodeAt(i)->visit(this);
        if (!(node->nodeAt(i)->isIfNode()
              || node->nodeAt(i)->isWhileNode()
              || node->nodeAt(i)->isForNode()
              || node->nodeAt(i)->isReturnNode()
              || node->nodeAt(i)->isBlockNode())) {
            printSemicolon();
        }
    }
    if (!isMainScope(node->scope())) {
        _output << "}" << endl;
    }
}
Beispiel #2
0
void Ast2SrcVisitor::initScope(Scope* scope) {
    std::string indent(_indent, ' ');

    Scope::VarIterator varIt(scope);
    while (varIt.hasNext()) {
        AstVar* var = varIt.next();
        _out << indent << mathvm::typeToName(var->type()) << " "<< var->name() << ";" << std::endl;
    }

    Scope::FunctionIterator funcIt(scope);
    while (funcIt.hasNext()) {
        funcIt.next()->node()->visit(this);
        _out << std::endl;
    }
}
Beispiel #3
0
    void printScope(Scope * scope) {
        Scope::VarIterator varIt(scope); 

        while(varIt.hasNext()) {
            AstVar* var = varIt.next();

            os << indent();
            os << typeToName(var->type()) << " " << var->name() << ";" << endl;
        }

        Scope::FunctionIterator funcIt(scope);
        while(funcIt.hasNext()) {
            AstFunction* func = funcIt.next();
            
            os << indent();
            func->node()->visit(this);
        }
    }
Beispiel #4
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();
}