Exemplo n.º 1
0
void
ZDriver::writeStateTable( std::ostream &out )
{
	size_t i, j, nState, nAct, nTotal;

	nState = StateTable::get()->getNumStates();

	// Count all actions
	nTotal = 0;
	for ( i = 0; i < nState; ++i )
	{
		State		*stp = StateTable::get()->getNthState(i);
		ActionList	&ap = stp->getActions();

		nAct = ap.getNumActions();
		for ( j = 0; j < nAct; ++j )
		{
			Action &act = ap.getNthAction( j );
			if ( ! act.isIgnoreType() )
				nTotal++;
			if ( act.getLookAhead() == SymbolTable::get()->getDefaultName() )
				nTotal++;
		}
	}

	out << "static int theStateTable[" << nTotal << "][4] =" << endl();
	out << "{" << endl();

	for ( i = 0; i < nState; ++i )
	{
		State		*stp = StateTable::get()->getNthState(i);
		ActionList	&ap = stp->getActions();

		nAct = ap.getNumActions();
		for ( j = 0; j < nAct; ++j )
		{
			Action &act = ap.getNthAction( j );

			if ( ! act.isIgnoreType() )
			{
				out << "    // State " << i << endl();
				out << "    { " << i << ", " << act.getLookAheadSymbol()->getIndex() << ", ";
				emitAction( act, out );
				out << " }," << endl();
			}

			if ( act.getLookAhead() == SymbolTable::get()->getDefaultName() )
			{
				out << "    // State " << i << " default action" << endl();
				out << "    { " << i << ", " << -1 << ", ";
				emitAction( act, out );
				out << " }," << endl();
			}
		}
	}
	out << "};" << endl();
}
Exemplo n.º 2
0
/// Push an event into the input system
void InputSystem::updateWithEvent(const Event& event)
{
	// A key was pressed, let's check if it casts an action
	if (event.type == Event::KeyPressed)
	{
		keyboardState[event.key.code] = true;

		for (std::size_t i = 0; i < bindings.actionMappings.size(); ++i)
		{
			if (bindings.actionMappings[i].isTriggeredBy(event.key.code))
			{
				emitAction(bindings.actionMappings[i].actionName, AInputComponent::Pressed);
			}
		}
	}
	else if (event.type == Event::KeyReleased)
	{
		keyboardState[event.key.code] = false;
	}
}
Exemplo n.º 3
0
void ControlBodyTranslator::processApply(const P4::ApplyMethod* method) {
    builder->emitIndent();
    auto table = control->getTable(method->object->getName().name);
    BUG_CHECK(table != nullptr, "No table for %1%", method->expr);

    P4::ParameterSubstitution binding;
    cstring actionVariableName;
    if (!saveAction.empty()) {
        actionVariableName = saveAction.at(saveAction.size() - 1);
        if (!actionVariableName.isNullOrEmpty()) {
            builder->appendFormat("enum %s %s;\n",
                                  table->actionEnumName.c_str(), actionVariableName.c_str());
            builder->emitIndent();
        }
    }
    builder->blockStart();

    BUG_CHECK(method->expr->arguments->size() == 0, "%1%: table apply with arguments", method);
    cstring keyname = "key";
    if (table->keyGenerator != nullptr) {
        builder->emitIndent();
        builder->appendLine("/* construct key */");
        builder->emitIndent();
        builder->appendFormat("struct %s %s = {}", table->keyTypeName.c_str(), keyname.c_str());
        builder->endOfStatement(true);
        table->emitKey(builder, keyname);
    }
    builder->emitIndent();
    builder->appendLine("/* value */");
    builder->emitIndent();
    cstring valueName = "value";
    builder->appendFormat("struct %s *%s = NULL", table->valueTypeName.c_str(), valueName.c_str());
    builder->endOfStatement(true);

    if (table->keyGenerator != nullptr) {
        builder->emitIndent();
        builder->appendLine("/* perform lookup */");
        builder->emitIndent();
        builder->target->emitTableLookup(builder, table->dataMapName, keyname, valueName);
        builder->endOfStatement(true);
    }

    builder->emitIndent();
    builder->appendFormat("if (%s == NULL) ", valueName.c_str());
    builder->blockStart();

    builder->emitIndent();
    builder->appendLine("/* miss; find default action */");
    builder->emitIndent();
    builder->appendFormat("%s = 0", control->hitVariable.c_str());
    builder->endOfStatement(true);

    builder->emitIndent();
    builder->target->emitTableLookup(builder, table->defaultActionMapName,
                                     control->program->zeroKey, valueName);
    builder->endOfStatement(true);
    builder->blockEnd(false);
    builder->append(" else ");
    builder->blockStart();
    builder->emitIndent();
    builder->appendFormat("%s = 1", control->hitVariable.c_str());
    builder->endOfStatement(true);
    builder->blockEnd(true);

    builder->emitIndent();
    builder->appendFormat("if (%s != NULL) ", valueName.c_str());
    builder->blockStart();
    builder->emitIndent();
    builder->appendLine("/* run action */");
    table->emitAction(builder, valueName);
    if (!actionVariableName.isNullOrEmpty()) {
        builder->emitIndent();
        builder->appendFormat("%s = %s->action",
                              actionVariableName.c_str(), valueName.c_str());
        builder->endOfStatement(true);
    }
    toDereference.clear();

    builder->blockEnd(true);
    builder->emitIndent();
    builder->appendFormat("else return %s", builder->target->abortReturnCode().c_str());
    builder->endOfStatement(true);

    builder->blockEnd(true);
}