Example #1
0
static Symbol* task_proc(NodeFunction* pFunc, ParseInfo* pinfo, std::vector<Symbol*>* pvecSyms)
{
	if(!pFunc || !pinfo) return 0;

	SymbolTable *pTable = new SymbolTable();
	SymbolArray arrArgs;
	arrArgs.SetDontDel(1);

	const NodeFunction *pThreadFunc = 0;
	RuntimeInfo *pruninfo2 = new RuntimeInfo();
	pThreadFunc = (NodeFunction*)pFunc/*->clone()->optimize()*/;

	if(pvecSyms) arrArgs.GetArr() = *pvecSyms;
	arrArgs.UpdateIndices();

	pTable->InsertSymbol(T_STR"<args>", &arrArgs);
	Symbol* pRet = pThreadFunc->eval(*pinfo, *pruninfo2, pTable);
	pTable->RemoveSymbolNoDelete(T_STR"<ret>");
	pTable->RemoveSymbolNoDelete(T_STR"<args>");

	if(pTable) delete pTable;
	if(pvecSyms) delete_symbols(pvecSyms);
	//if(pThreadFunc) delete pThreadFunc;
	delete pruninfo2;

	return pRet;
}
Example #2
0
// execute script
static inline int script_main(int argc, char** argv)
{
	if(argc<=1)
	{
		usage(argv[0]);
		return -1;
	}

	bool bShowSymbols = 0;
	bool bInteractive = 0;
	unsigned int uiDebugLevel = 3;
#ifndef NDEBUG
	uiDebugLevel = 4;
#endif
	unsigned int iStartArg = 1;
	for(iStartArg=1; iStartArg<unsigned(argc); ++iStartArg)
	{
		t_string strArg = STR_TO_WSTR(argv[iStartArg]);
		tl::trim(strArg);

		// end of arguments to hermelin
		if(strArg[0] != T_STR'-')
			break;

		if(strArg=="-s" || strArg == "--symbols")
			bShowSymbols = 1;
		else if(strArg=="-i" || strArg == "--interactive")
			bInteractive = 1;
		else if(strArg=="-h" || strArg == "--help")
			{ usage(argv[0]); return 0; }

		else if(strArg=="-d0") uiDebugLevel = 0;
		else if(strArg=="-d1") uiDebugLevel = 1;
		else if(strArg=="-d2") uiDebugLevel = 2;
		else if(strArg=="-d3") uiDebugLevel = 3;
		else if(strArg=="-d4") uiDebugLevel = 4;
	}

	const std::array<tl::Log*, 5> arrLogs{{&tl::log_crit, &tl::log_err, &tl::log_warn, &tl::log_info, &tl::log_debug}};
	for(unsigned int iLog=0; iLog<arrLogs.size(); ++iLog)
		arrLogs[iLog]->SetEnabled(uiDebugLevel>=iLog);

	// debug in script.yy needs to be set
	yydebug = (uiDebugLevel>=4);

	if(bInteractive)
		return interactive(bShowSymbols, uiDebugLevel);


	if(iStartArg >= unsigned(argc))
	{
		tl::log_err("No input file given.");
		return -1;
	}



	// loading of input file
	const char* pcFile = argv[iStartArg];
	t_string strFile = STR_TO_WSTR(pcFile);

	t_char* pcInput = load_file(pcFile);
	if(!pcInput)
		return -2;


	ParseObj par;
	ParseInfo info;
	RuntimeInfo runinfo;

	info.bEnableDebug = (uiDebugLevel>=4);


	// lexing
	par.strCurFile = strFile;
	par.pLexer = new Lexer(pcInput, strFile.c_str());

	delete[] pcInput;
	pcInput = 0;

	if(!par.pLexer->IsOk())
	{
		tl::log_err("Lexer returned with errors.");
		return -3;
	}

	init_global_syms(info.pGlobalSyms);


	// parsing
	int iParseRet = yyparse(&par);

	delete par.pLexer;
	par.pLexer = 0;

	if(iParseRet != 0)
	{
		tl::log_err("Parser returned with error code ", iParseRet, ".");
		return -4;
	}


	// optimizing
	par.pRoot = par.pRoot->optimize();



	// executing
	SymbolArray *parrMainArgs = new SymbolArray();
	for(int iArg=iStartArg; iArg<argc; ++iArg)
	{
		SymbolString *pSymArg = new SymbolString();
		pSymArg->SetVal(STR_TO_WSTR(argv[iArg]));
		parrMainArgs->GetArr().push_back(pSymArg);
	}
	//std::vector<Symbol*> vecMainArgs = { &arrMainArgs };

	SymbolTable *pTableSup = new SymbolTable();

	info.pmapModules->insert(ParseInfo::t_mods::value_type(strFile, par.pRoot));
	runinfo.strExecFkt = T_STR"main";
	//info.pvecExecArg = &vecMainArgs;
	runinfo.strInitScrFile = strFile;

	SymbolArray arrMainArgs;
	arrMainArgs.GetArr().push_back(parrMainArgs);
	pTableSup->InsertSymbol(T_STR"<args>", &arrMainArgs);
	par.pRoot->eval(info, runinfo, pTableSup);
	pTableSup->RemoveSymbolNoDelete(T_STR"<args>");
	delete pTableSup;


	if(bShowSymbols)
	{
		tl::log_info("================================================================================");
		tl::log_info("Global symbols:");
		info.pGlobalSyms->print();

		std::ostringstream ostrFkts;
		for(const NodeFunction* pFunc : info.vecFuncs)
			ostrFkts << pFunc->GetName() << ", ";
		tl::log_info("Script functions: ", ostrFkts.str());


		const t_mapFkts* pExtFkts = get_ext_calls();

		std::ostringstream ostrSysFkts;
		for(const auto& fktpair : *pExtFkts)
			ostrSysFkts << fktpair.first << ", ";
		tl::log_info("System functions: ", ostrSysFkts.str());
		tl::log_info("================================================================================");
	}

	return 0;
}