Esempio n. 1
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());
    }
}
Esempio n. 2
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);
	}
}
Esempio n. 3
0
void BytecodeGenerator::secondRun(Scope * scope) {
  Scope::FunctionIterator it(scope);
  while (it.hasNext()) {
    AstFunction *func = it.next();
    BytecodeFunction *bcf =
        (BytecodeFunction*) code->functionByName(func->name());
    fBC = bcf->bytecode();
    currentFun = bcf->id();
    returnType = func->returnType();
    func->node()->body()->visit(this);
    bcf->setLocalsNumber(localsCounter[currentFun]);
  }

  for (size_t i = 0; i < scope->childScopeNumber(); ++i) {
    secondRun(scope->childScopeAt(i));
  }
}
Esempio n. 4
0
void BytecodeTranslatorVisitor::visitCallNode(CallNode* node) {
    onVisitNode(node);

    AstFunction* f = scope()->lookupFunction(node->name());
    if (!f) ERROR("Unknown function " + f->name());
    checkSignature(node, f);

    for (uint16_t i = 0; i < node->parametersNumber(); i++)
        visitTyped(node->parameterAt(i), f->parameterType(i));

    if (isNative(f))
        EMIT(BC_CALLNATIVE);
    else
        EMIT(BC_CALL);

    EMIT_ID(getFunctionId(f));

    pushType(f->returnType());
}
Esempio n. 5
0
void TypeChecker::visitCallNode(CallNode* node) {
    size_t nodePos = node->position();
    string const callName = node->name();
    Scope* curScope = currentScope();
    AstFunction* funToCall = curScope->lookupFunction(callName);
    if(funToCall == 0) {
        throw ExceptionWithPos(undeclaredFunMsg(callName), nodePos);
    }
    for(size_t i = 0; i != node->parametersNumber(); ++i) {
        node->parameterAt(i)->visit(this);
        VarType actualArgType = nodeType(node->parameterAt(i));
        VarType expectedArgType = funToCall->parameterType(i);
        if(actualArgType != expectedArgType &&
           !isIntDoublePair(actualArgType, expectedArgType))
        {
            throw ExceptionWithPos(wrongArgTypeMsg(callName, i), nodePos);
        }
    }
    node->setInfo(new VarType(funToCall->returnType()));
}
Esempio n. 6
0
void Generator::visitCallNode(CallNode *node)
{
	AstFunction *function = lookup_function(node->name());
	uint16_t id = lookup_function(function)->id();
	for (uint32_t i = 0; i != node->parametersNumber(); ++i)
	{
	    uint32_t pos = node->parametersNumber() - i - 1;
	    switch (function->parameterType(pos))
	    {
	    case VT_INT:
	        eval_int(node->parameterAt(pos));
	        break;
        case VT_DOUBLE:
	        eval_double(node->parameterAt(pos));
	        break;
        case VT_STRING:
	        eval_string(node->parameterAt(pos));
	        break;
        default: assert(0);
	    }
	}
	bytecode()->addInsn(BC_CALL);
	bytecode()->addInt16(id);
	switch (function->returnType())
	{
	case VT_INT:
	    bytecode()->addInsn(BC_LOADIVAR0);
	    break;
	case VT_DOUBLE:
	    bytecode()->addInsn(BC_LOADDVAR0);
	    break;
	case VT_STRING:
	    bytecode()->addInsn(BC_LOADSVAR0);
	    break;
    default: break;
	}
}
void TypeInferenceVisitor::visitCallNode(CallNode* node) {
	node->visitChildren(this);
	AstFunction* function = _scopes.top()->lookupFunction(node->name());
	_types[node] = function->returnType();
}