void TypeInferenceVisitor::visitBinaryOpNode(BinaryOpNode* node) {
	AstNode* left = node->left();
	AstNode* right = node->right();
	left->visit(this);
	right->visit(this);
	_types[node] = maxType(_types[left], _types[right]);
}
Example #2
0
void TypeCheckerVisitor::visitBinaryOpNode(BinaryOpNode* node) {
	AstNode* left = node->left();
	AstNode* right = node->right();
	left->visit(this);
	right->visit(this);
	setNodeType(node, getOperationResultType(node->kind(), left, right));
}
Example #3
0
void ByteCodeVisitor::visitCallNode(mathvm::CallNode *node) {
    unsigned long stackSize = currStack->size();
    AstFunction *astFunction = currScope->lookupFunction(node->name(), true);

    AstFunctionInfo *functionInfo = (AstFunctionInfo *) astFunction->info();
    Scope *parameterScope = astFunction->scope();

    if (node->parametersNumber() != parameterScope->variablesCount()) {
        error("parameters number mistmach in calling %s", node->name().c_str());
    }
    vector<VarType> declaredParameters;
    declaredParameters.reserve(parameterScope->variablesCount());
    Scope::VarIterator varIterator(parameterScope);
    while (varIterator.hasNext()) {
        declaredParameters.push_back(varIterator.next()->type());
    }

    vector<VarType>::reverse_iterator it = declaredParameters.rbegin();

    for (uint32_t i = node->parametersNumber(); i > 0; --i, ++it) {
        AstNode *n = node->parameterAt(i - 1);
        n->visit(this);
        cast(topStack(), (*it));
    }

    insn(BC_CALL);
    typed(functionInfo->function->id());
    if (astFunction->returnType() != VT_VOID) {
        pushStack(astFunction->returnType());
    }
}
Example #4
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 #5
0
        virtual void visitBlockNode(BlockNode * node) {
            if (currentTabNum >= 0) {
                out<< "{"<< endl;
            }
            ++currentTabNum;

            visitScope(node->scope());
            for (size_t i = 0; i < node->nodes(); ++i) {
                addIndent();
                AstNode * currentNode = node->nodeAt(i);
                currentNode->visit(this);
                if (currentNode->isCallNode() ||
                        currentNode->isLoadNode() ||
                        currentNode->isNativeCallNode() ||
                        currentNode->isPrintNode() ||
                        currentNode->isReturnNode() ||
                        currentNode->isStoreNode()) {

                    out<< ";"<< endl;
                }
            }

            --currentTabNum;
            if (currentTabNum >= 0) {
                addIndent();
                out<< "}"<< endl;
            }


        }
Example #6
0
void TypeCheckerVisitor::visitStoreNode(StoreNode* node) {
	AstNode* value = node->value();
	value->visit(this);
	const AstVar* var = node->var();
	if (isAssignable(var->type(), getNodeType(value))) {
		setNodeType(node, var->type());
	} else {
		setErrorMessage(node, "Bad type");
	}
}
Example #7
0
void PrintVisitor::visitPrintNode(PrintNode *node) {
    cout << "print(";
    for (uint32_t i = 0; i < node->operands(); i++) {
        if (i > 0) cout << ", ";
        AstNode *pNode = node->operandAt(i);
        pNode->visit(this);
    }
    cout << ")";
    lastSym = ')';
}
Example #8
0
void ast_printer::visitPrintNode( PrintNode* node ) {
    stream_ << "print(";

    for (unsigned int i = 0; i < node->operands(); ++i) {
        if (i > 0) stream_ << ", ";
        AstNode *pNode = node->operandAt(i);
        pNode->visit(this);
    }
    stream_ << ")";
    needSemicolon = true;
}
Example #9
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 #10
0
void AstPrinter::visitIfNode(IfNode* node) {
	print ("if (");
    node->ifExpr()->visit(this);
    print(")" );
    node->thenBlock()->visit(this);
    AstNode* elseBlock = node->elseBlock();
    if (elseBlock) {
        print(" else ");
        elseBlock->visit(this);
    }
}
Example #11
0
void ByteCodeVisitor::visitBlockNode(mathvm::BlockNode *node) {
    currScope = node->scope();
    for (uint32_t i = 0; i < node->nodes(); ++i) {
        AstNode *n = node->nodeAt(i);
        n->visit(this);
    }
    Scope::FunctionIterator functionIterator(currScope);
    while (functionIterator.hasNext()) {
        AstFunction *astFunction = functionIterator.next();
        visitFunctionNode(astFunction->node());
    }
    currScope = currScope->parent();
}
Example #12
0
void BytecodeGenerator::visitPrintNode(PrintNode * node) {
  for (size_t i = 0; i < node->operands(); i++) {
    AstNode *operand = node->operandAt(i);
    operand->visit(this);
    switch (TOSType) {
    case VT_INT: fBC->addInsn(BC_IPRINT); break;
    case VT_DOUBLE: fBC->addInsn(BC_DPRINT); break;
    case VT_STRING: fBC->addInsn(BC_SPRINT); break;
    default:
    	throw BCGenerationException("Unknown type");
    }
  }
}
Example #13
0
  void insideBlock(BlockNode* node) {
    variableDeclaration(node->scope());
    functionDeclaration(node->scope());

    for (uint32_t i = 0; i < node->nodes(); i++) {
      indent();
      AstNode* current = node->nodeAt(i);
      current->visit(this);
      if (current->isCallNode() || current->isBinaryOpNode() ||
          current->isUnaryOpNode() || current->isStringLiteralNode() ||
          current->isDoubleLiteralNode() || current->isIntLiteralNode() ||
          current->isLoadNode())
        _output << ";";
      _output << endl;
    }
  }
Example #14
0
void TypeCheckerVisitor::visitCallNode(CallNode* node) {
	uint32_t params_count = node->parametersNumber();
	AstFunction* refFunc = _current_scope->lookupFunction(node->name(), true);
	bool matchesRefedFunction = (refFunc!= NULL) && (refFunc->parametersNumber() == params_count);
	for (uint32_t i = 0; i < params_count; i++) {
		AstNode* param = node->parameterAt(i);
		param->visit(this);
		if (matchesRefedFunction && !isAssignable(refFunc->parameterType(i), getNodeType(param))) {
			setErrorMessage(param, "Wrong type parameter");
		}
	}
	if (matchesRefedFunction) {
		setNodeType(node, refFunc->returnType());
	} else {
		setNodeType(node, VT_INVALID);
	}
}
Example #15
0
void TypeChecker::visitReturnNode(ReturnNode* node) {
    assert(_funs.size() != 0);
    VarType expectedRetType = _funs.top()->returnType();
    AstNode* retExpr = node->returnExpr();
    if(retExpr != 0) {
        retExpr->visit(this);
        VarType actualRetType = nodeType(retExpr);
        if(actualRetType != expectedRetType &&
           !isIntDoublePair(actualRetType, expectedRetType))
        {
            throw ExceptionWithPos(invalidRetExprTypeMsg(expectedRetType, actualRetType),
                                   node->position());
        }
    } else if(expectedRetType != VT_VOID) {
        throw ExceptionWithPos(retExprExpectedMsg(), node->position());
    }
}
Example #16
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 #17
0
    virtual void visitBlockNode(BlockNode *node)
    {
        bool topLevel = node->scope()->parent()->parent() == NULL;
        if (!topLevel) {
            out << "{" << endl;
        }

        printVars(node->scope());
        for (uint32_t i = 0; i < node->nodes(); i++) {
            AstNode *currentNode = node->nodeAt(i);
            currentNode->visit(this);
            if (currentNode->isLoadNode() || currentNode->isStoreNode() ||
                currentNode->isReturnNode() || currentNode->isCallNode() ||
                currentNode->isNativeCallNode() || currentNode->isPrintNode()) {
                out << ";" << endl;
            }
        }
        if (!topLevel) {
            out << "}" << endl;
        }
    }
Example #18
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();
}
Example #19
0
void TypeCheckerVisitor::visitUnaryOpNode(UnaryOpNode* node) {
	AstNode* operand = node->operand();
	operand->visit(this);
	setNodeType(node, getOperationResultType(node->kind(), operand));
}