All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.sparkium.brainfxck.action.impl.XAction Maven / Gradle / Ivy

The newest version!
package com.github.bloodshura.sparkium.brainfxck.action.impl;

import com.github.bloodshura.ignitium.charset.sequence.CharSet;
import com.github.bloodshura.ignitium.collection.map.XMap;
import com.github.bloodshura.sparkium.brainfxck.BrainfuckContext;
import com.github.bloodshura.sparkium.brainfxck.BrainfuckInterpreter;
import com.github.bloodshura.sparkium.brainfxck.BrainfuckMemory;
import com.github.bloodshura.sparkium.brainfxck.action.Action;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
 * This class represents a custom action, which can be used on a {@link BrainfuckInterpreter}.
 * 

This class is always changing, so only use it for test purposes. */ public class XAction implements Action { /** * A {@link CharSet} instance containing all the characters which are manipulated by this action. */ public static final CharSet CHAR_SET = new CharSet('$', ';', '\\'); /** * The flag name used for storing the escaping state of reprocessings. * * @see BrainfuckContext#getFlags() */ public static final String FLAG_ESCAPE = "lastEscaped"; /** * The flag name used for storing reprocessings. * * @see BrainfuckContext#getFlags() */ public static final String FLAG_REPROCESS = "reprocess"; @Override public boolean act(char ch, @Nonnull BrainfuckContext context) throws IOException { BrainfuckInterpreter interpreter = context.getInterpreter(); BrainfuckMemory memory = interpreter.getMemory(); XMap flags = context.getFlags(); int skipLevel = (int) flags.get(DefaultAction.FLAG_SKIP, 0); if (skipLevel > 0) { return false; } boolean lastEscape = flags.containsKey(FLAG_ESCAPE); int reprocessCount = (int) flags.get(FLAG_REPROCESS, 0); if (ch == '\\') { flags.set(FLAG_ESCAPE, true); } else if (lastEscape) { flags.removeKey(FLAG_ESCAPE); if (ch >= '0' && ch <= '9') { flags.set(FLAG_REPROCESS, reprocessCount + Character.getNumericValue(ch)); } } else if (reprocessCount > 0) { flags.set(FLAG_REPROCESS, 0); for (int i = 0; i < reprocessCount; i++) { context.interpret(ch); } } else if (ch == '$') { interpreter.getLogger().print(memory.getPointer()); } else if (ch == ';') { interpreter.getLogger().print(memory.getValue()); } else { return false; } return true; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy