Example #1
0
void FGotoCodeGen::writeExec()
{
	testEofUsed = false;
	outLabelUsed = false;

	out << L"	{\n";

	if ( redFsm->anyRegCurStateRef() )
		out << L"	int _ps = 0;\n";

	if ( redFsm->anyConditions() )
		out << L"	" << WIDE_ALPH_TYPE() << L" _widec;\n";

	if ( !noEnd ) {
		testEofUsed = true;
		out << 
			L"	if ( " << P() << L" == " << PE() << L" )\n"
			L"		goto _test_eof;\n";
	}

	if ( redFsm->errState != 0 ) {
		outLabelUsed = true;
		out << 
			L"	if ( " << vCS() << L" == " << redFsm->errState->id << L" )\n"
			L"		goto _out;\n";
	}

	out << L"_resume:\n";

	if ( redFsm->anyFromStateActions() ) {
		out <<
			L"	switch ( " << FSA() << L"[" << vCS() << L"] ) {\n";
			FROM_STATE_ACTION_SWITCH();
			SWITCH_DEFAULT() <<
			L"	}\n"
			L"\n";
	}

	out << 
		L"	switch ( " << vCS() << L" ) {\n";
		STATE_GOTOS();
		SWITCH_DEFAULT() <<
		L"	}\n"
		L"\n";
		TRANSITIONS() << 
		L"\n";

	if ( redFsm->anyRegActions() )
		EXEC_ACTIONS() << L"\n";

	out << L"_again:\n";

	if ( redFsm->anyToStateActions() ) {
		out <<
			L"	switch ( " << TSA() << L"[" << vCS() << L"] ) {\n";
			TO_STATE_ACTION_SWITCH();
			SWITCH_DEFAULT() <<
			L"	}\n"
			L"\n";
	}

	if ( redFsm->errState != 0 ) {
		outLabelUsed = true;
		out << 
			L"	if ( " << vCS() << L" == " << redFsm->errState->id << L" )\n"
			L"		goto _out;\n";
	}

	if ( !noEnd ) {
		out << 
			L"	if ( ++" << P() << L" != " << PE() << L" )\n"
			L"		goto _resume;\n";
	}
	else {
		out << 
			L"	" << P() << L" += 1;\n"
			L"	goto _resume;\n";
	}

	if ( testEofUsed )
		out << L"	_test_eof: {}\n";

	if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
		out <<
			L"	if ( " << P() << L" == " << vEOF() << L" )\n"
			L"	{\n";

		if ( redFsm->anyEofTrans() ) {
			out <<
				L"	switch ( " << vCS() << L" ) {\n";

			for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
				if ( st->eofTrans != 0 )
					out << L"	case " << st->id << L": goto tr" << st->eofTrans->id << L";\n";
			}

			SWITCH_DEFAULT() <<
				L"	}\n";
		}

		if ( redFsm->anyEofActions() ) {
			out <<
				L"	switch ( " << EA() << L"[" << vCS() << L"] ) {\n";
				EOF_ACTION_SWITCH();
				SWITCH_DEFAULT() <<
				L"	}\n";
		}

		out <<
			L"	}\n"
			L"\n";
	}

	if ( outLabelUsed )
		out << L"	_out: {}\n";

	out << L"	}\n";
}
Example #2
0
void OCamlFGotoCodeGen::writeExec()
{
	testEofUsed = false;
	outLabelUsed = false;

	out << "	begin\n";

	if ( redFsm->anyRegCurStateRef() )
		out << "	let _ps = ref 0 in\n";

	if ( redFsm->anyConditions() )
		out << "	let _widec : " << WIDE_ALPH_TYPE() << " = ref 0 in\n";

	out << "\n";
  out << "\tlet rec do_start () =\n";
	if ( !noEnd ) {
		testEofUsed = true;
		out << 
			"	if " << P() << " = " << PE() << " then\n"
			"		do_test_eof ()\n"
      "\telse\n";
	}

	if ( redFsm->errState != 0 ) {
		outLabelUsed = true;
		out << 
			"	if " << vCS() << " = " << redFsm->errState->id << " then\n"
			"		do_out ()\n"
      "\telse\n";
	}
  out << "\tdo_resume ()\n";

	out << "and do_resume () =\n";

	if ( redFsm->anyFromStateActions() ) {
		out <<
			"	begin match " << AT(FSA(),vCS()) << " with\n";
			FROM_STATE_ACTION_SWITCH();
			SWITCH_DEFAULT() <<
			"	end;\n"
			"\n";
	}

	out << 
		"	begin match " << vCS() << " with\n";
		STATE_GOTOS();
		SWITCH_DEFAULT() <<
		"	end\n"
		"\n";
		TRANSITIONS() << 
		"\n";

	if ( redFsm->anyRegActions() )
		EXEC_ACTIONS() << "\n";

	out << "\tand do_again () =\n";

	if ( redFsm->anyToStateActions() ) {
		out <<
			" begin match " << AT(TSA(), vCS()) << " with\n";
			TO_STATE_ACTION_SWITCH();
			SWITCH_DEFAULT() <<
			"	end;\n"
			"\n";
	}

	if ( redFsm->errState != 0 ) {
		outLabelUsed = true;
		out << 
			"	match " << vCS() << " with\n" <<
      "	| " << redFsm->errState->id << " -> do_out ()\n"
      "	| _ ->\n";
	}

	out << "\t" << P() << " <- " << P() << " + 1;\n";

	if ( !noEnd ) {
		out << 
			"	if " << P() << " <> " << PE() << " then\n"
			"		do_resume ()\n"
      "\telse do_test_eof ()\n";
	}
	else {
		out << 
			"	do_resume ()\n";
	}

//	if ( testEofUsed )
  out << "and do_test_eof () =\n";

	if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
		out <<
			"	if " << P() << " = " << vEOF() << " then\n"
			"	begin\n";

		if ( redFsm->anyEofTrans() ) {
			out <<
				"	match " << vCS() << " with\n";

			for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
				if ( st->eofTrans != 0 )
					out << "	| " << st->id << " -> tr" << st->eofTrans->id << " ()\n";
			}

			SWITCH_DEFAULT() << ";\n"; // fall through
		}

		if ( redFsm->anyEofActions() ) {
			out <<
				"	try match " << AT(EA(), vCS()) << " with\n";
				EOF_ACTION_SWITCH();
				SWITCH_DEFAULT() <<
				"	\n"
				"	with Goto_again -> do_again () \n";
		}

		out <<
			"	end\n"
			"\n";
	}
  else
    out << "\t()\n";

	if ( outLabelUsed )
		out << "\tand do_out () = ()\n";

  out << "\tin do_start ()\n";
	out << "	end;\n";
}