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;
}
Example #2
0
// private:
// visitBlockNodeImpl
void BytecodeGenerator::visitVarDecls(BlockNode* node) {
    Bytecode* bc = _state.currentBcToFill();
    Scope::VarIterator varIt(node->scope());
    while(varIt.hasNext()) {
        AstVar* var = varIt.next();
        uint16_t varId = _state.currentCtxAddVar(var->name(), node->position());
        Instruction bcLoad0 = typedInsn(var->type(), BC_ILOAD0, BC_DLOAD0, BC_SLOAD0);
        bc->addInsn(bcLoad0);
        Instruction bcStore = typedInsn(var->type(), BC_STOREIVAR, BC_STOREDVAR, BC_STORESVAR);
        genBcInsnWithId(bc, bcStore, varId);
    }
}
void BytecodeGenerationVisitor::visitCallNode(CallNode* node) {
	const Signature& sign = code->functionByName(node->name())->signature();
	AstFunction* f = currentScope->lookupFunction(node->name());
	
	for (unsigned int i = 0; i < node->parametersNumber(); i++) {
		AstVar* v = f->scope()->lookupVariable(sign[i + 1].second);
		uint16_t varId = getVarId(v);
		bytecode->addInsn(insnByUntypedInsn[v->type()][UT_LOADVAR]);
		bytecode->addInt16(varId);
	}
	
	for (unsigned int i = 0; i < node->parametersNumber(); i++) {
		node->parameterAt(i)->visit(this);
		if (currentType == VT_INT && sign[i + 1].first == VT_DOUBLE) {
			bytecode->addInsn(BC_I2D);
		} else if (currentType == VT_DOUBLE && sign[i + 1].first == VT_INT) {
			bytecode->addInsn(BC_D2I);
		}
	}
	
	for (int i = node->parametersNumber(); i > 0; i--) {
		AstVar* v = f->scope()->lookupVariable(sign[i].second);
		uint16_t varId = getVarId(v);
		bytecode->addInsn(insnByUntypedInsn[sign[i].first][UT_STOREVAR]);
		bytecode->addInt16(varId);
	}
	
	bytecode->addInsn(BC_CALL);
	bytecode->addInt16(code->functionByName(node->name())->id());
	
	if (sign[0].first == VT_VOID) {
		for (unsigned int i = node->parametersNumber(); i > 0; i--) {
			AstVar* v = f->scope()->lookupVariable(sign[i].second);
			uint16_t varId = getVarId(v);
			bytecode->addInsn(insnByUntypedInsn[v->type()][UT_STOREVAR]);
			bytecode->addInt16(varId);
		}
	} else {
		for (unsigned int i = node->parametersNumber(); i > 0; i--) {
			AstVar* v = f->scope()->lookupVariable(sign[i].second);
			uint16_t varId = getVarId(v);
			bytecode->addInsn(BC_SWAP);
			bytecode->addInsn(insnByUntypedInsn[v->type()][UT_STOREVAR]);
			bytecode->addInt16(varId);
		}
	}
	
	currentType = sign[0].first;
}
void ASTtoByteCodeTranslator::visitFunctionNode(FunctionNode* node) {
	uint16_t varId = 0;

	for(size_t i = 0; i < node->parametersNumber(); ++i) {
		AstVar *var = node->body()->scope()->lookupVariable(node->parameterName(node->parametersNumber() - i - 1));

		varMap[var] = varId;
		Bytecode *bytecode = funStack.top()->bytecode();
		switch (var->type()) {
		    case VT_INT:
		    	bytecode->addInsn(BC_STOREIVAR);
		        break;
		    case VT_DOUBLE:
		    	bytecode->addInsn(BC_STOREDVAR);
		        break;
		    case VT_STRING:
		    	bytecode->addInsn(BC_STORESVAR);
		        break;
		    default:
		    	bytecode->addInsn(BC_INVALID);
		        break;
		}
		bytecode->addUInt16(varId++);
	}
	node->body()->visit(this);
}
Example #5
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;
    }
}
Example #6
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 #7
0
void ByteCodeVisitor::visitFunctionNode(mathvm::FunctionNode *node) {
    BytecodeFunction *dump = currentBytecodeFunction;
    if (currScope) {
        AstFunction *function = currScope->lookupFunction(node->name());
        if (!function) {
            error("undeclared function %s", node->name().c_str());
        }
        AstFunctionInfo *astFunctionInfo = (AstFunctionInfo *) function->info();
        currentBytecodeFunction = astFunctionInfo->function;
    } else {
        currentBytecodeFunction = dynamic_cast<BytecodeFunction *>(interpreterCode->functionById((uint16_t) 0));
    }
    std::vector<VarType> newStack;
    currStack = &newStack;

    Scope::VarIterator varIterator(node->body()->scope()->parent());
    while (varIterator.hasNext()) {
        AstVar *var = varIterator.next();
        pushStack(var->type());//ensure that variables on stack is ok
        store(var);
    }

    visitBlockNode(node->body());

    currentBytecodeFunction = dump;
    currScope = currScope->parent();//jump parameters scope
}
Example #8
0
 void printVars(Scope *scope)
 {
     Scope::VarIterator iter(scope);
     while (iter.hasNext()) {
         AstVar *astVar = iter.next();
         out << typeToName(astVar->type()) << " " << astVar->name() << ";" << endl;
     }
 }
void TypeInferenceVisitor::processFunction(AstFunction* function) {
	Scope* scope = function->scope();
	Scope::VarIterator vit(scope);
	while(vit.hasNext()) {
		AstVar* var = vit.next();
		_types[var] = var->type();
	}
	function->node()->visit(this);
}
Example #10
0
 void variableDeclaration(Scope* scope) {
   Scope::VarIterator iter(scope);
   while (iter.hasNext()) {
     AstVar* x = iter.next();
     indent();
     _output << typeToName(x->type()) << " "
             << x->name() << ";"
             << endl;
   }
 }
Example #11
0
void ASTAnalyzer::printScopeDeclarations (Scope* scope) {
    Scope::FunctionIterator fuctions(scope);
    while (fuctions.hasNext()) {
        fuctions.next()->node()->visit(this);
    }
    Scope::VarIterator vars(scope);
    while (vars.hasNext()) {
        AstVar* var = vars.next();
        output << typeToName(var->type()) << " " << var->name() << ";\n";
    }
}
Example #12
0
        void visitScope(Scope * scope) {
            Scope::VarIterator varIter(scope);
            while (varIter.hasNext()) {
                addIndent();
                AstVar * var = varIter.next();
                out<< typeToName(var->type())<< " "<< var->name()<< ";"<< endl;

            }
            Scope::FunctionIterator funcIter(scope);
            while (funcIter.hasNext()) {

                funcIter.next()->node()->visit(this);
            }
        }
Example #13
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;
    }
}
Example #14
0
void PrettyPrinter::printScope(Scope *scope)
{
	std::string indentation(m_indent, ' ');
	//why constructor doesn't get const pointer?
	Scope::VarIterator ivar(scope);
	//Java-style iterators in C++?
	while (ivar.hasNext())
	{
		AstVar *var = ivar.next();
		m_out << indentation << typeToName(var->type()) << " "
		      << var->name() << ";" << std::endl;
	}
	
	Scope::FunctionIterator ifun(scope);
	while (ifun.hasNext()) ifun.next()->node()->visit(this);
}
Example #15
0
void AstPrinter::printScopeDeclarations(Scope* scope) {
    Scope::VarIterator varIterator(scope);
    while(varIterator.hasNext()) {
        AstVar *var = varIterator.next();
        print(typeToName(var->type()));
        print(" ");
        print(var->name());
        print(";\n");
    }
    
    Scope::FunctionIterator funcIterator(scope);
    while(funcIterator.hasNext()) {
        AstFunction *func = funcIterator.next();
		printFunction(func);
    }
    print("\n");
}
Example #16
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);
        }
    }
    void BytecodeVisitor::visitBlockNode(BlockNode *node) {
        LOG_Visitor("visitBlockNode");

        Scope::VarIterator variableIterator(node->scope());
        while (variableIterator.hasNext()) {
            AstVar *var = variableIterator.next();
            context->introduceVariable(var->type(), var->name());
        }
        Scope::FunctionIterator functionIterator(node->scope());
        while (functionIterator.hasNext()) {
            context->introduceFunction(new BytecodeFunction(functionIterator.next()));
        }

        node->visitChildren(this);

        functionIterator = Scope::FunctionIterator(node->scope());
        while (functionIterator.hasNext()) {
            visitFunctionNode(functionIterator.next()->node());
        }
    }
Example #18
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;
	}
}