Exemplo n.º 1
0
void ASTtoByteCodeTranslator::createFunMap(Scope* scope){
    Scope::FunctionIterator funIt(scope);

    while(funIt.hasNext()) {
        AstFunction* fun = funIt.next();
        if (fun->node()->body()->nodeAt(0)->isNativeCallNode())
            fun->node()->visit(this);
        else{
        	BytecodeFunction* bcFun = new BytecodeFunction(fun);
        	code->addFunction(bcFun);
        	funMap.insert(std::make_pair(fun, bcFun));
        	funStack.push(bcFun);
        	fun->node()->visit(this);
        	funStack.pop();
        }
    }
}
void BytecodeGenerationVisitor::visitBlockNode(BlockNode* node) {   // DONE
	Scope* prevScope = currentScope;
	currentScope = node->scope();
	
	Scope::VarIterator varsToSave(currentScope);
	while (varsToSave.hasNext()) {
		AstVar* v = varsToSave.next();
		bytecode->addInsn(insnByUntypedInsn[v->type()][UT_LOADVAR]);
		bytecode->addInt16(getVarId(v));
		locals.push_back(v);
	}
	
	Scope::VarIterator varsToInit(currentScope);
	while (varsToInit.hasNext()) {
		AstVar* v = varsToInit.next();
		bytecode->addInsn(insnByUntypedInsn[v->type()][UT_LOAD0]);
		bytecode->addInsn(insnByUntypedInsn[v->type()][UT_STOREVAR]);
		bytecode->addInt16(getVarId(v));
	}
	
	Scope::FunctionIterator functionsToAdd(node -> scope());
	while (functionsToAdd.hasNext()) {
		AstFunction *astFunction = functionsToAdd.next();
		BytecodeFunction *bytecodeFunction = new BytecodeFunction(astFunction);
		code -> addFunction(bytecodeFunction);
	}
	
	Scope::FunctionIterator functionsToBuild(node -> scope());
	while (functionsToBuild.hasNext()) {
		AstFunction *astFunction = functionsToBuild.next();
		astFunction->node()->visit(this);
	}

	for (unsigned int i = 0; i < (node -> nodes()); i++) {
		node -> nodeAt(i) -> visit(this);
		if (currentType != VT_VOID && !(node->nodeAt(i)->isReturnNode())) {
			bytecode -> addInsn(BC_POP);
		}
	}
	
	Scope::VarIterator varsToRestore(currentScope);
	stack<AstVar*> variables;
	while (varsToRestore.hasNext()) {
		variables.push(varsToRestore.next());
	}
	
	while (!variables.empty()) {
		AstVar* v = variables.top();
		bytecode->addInsn(insnByUntypedInsn[v->type()][UT_STOREVAR]);
		bytecode->addInt16(getVarId(v));
		locals.pop_back();
		variables.pop();
	}
	
	currentScope = prevScope;
	currentType = VT_VOID;
}
Exemplo n.º 3
0
void TypeChecker::visitFunDefs(BlockNode* node) {
    Scope::FunctionIterator funIt(node->scope());
    while(funIt.hasNext()) {
        AstFunction* fun = funIt.next();
        size_t dbg_cur_size = _funs.size();
        _funs.push(fun);
        fun->node()->visit(this);
        _funs.pop();
        assert(dbg_cur_size == _funs.size());
    }
}
Exemplo n.º 4
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();
}
Exemplo n.º 5
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));
  }
}
Exemplo n.º 6
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);
        }
    }
Exemplo n.º 7
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();
}