|
| 1 | +/* |
| 2 | + * CPUInstruction.java |
| 3 | + */ |
| 4 | + |
| 5 | +package nsl.instruction; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.EnumSet; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Set; |
| 13 | +import nsl.*; |
| 14 | +import nsl.expression.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Jan |
| 18 | + */ |
| 19 | +public class CPUInstruction extends AssembleExpression { |
| 20 | + public static final String name = "CPU"; |
| 21 | + |
| 22 | + private static final Set<String> VALID_CPUS = new HashSet<>(Arrays.asList("x86", "amd64")); |
| 23 | + |
| 24 | + private final Expression cpu; |
| 25 | + |
| 26 | + /** |
| 27 | + * Class constructor. |
| 28 | + * |
| 29 | + * @param returns the number of values to return |
| 30 | + */ |
| 31 | + public CPUInstruction(int returns) { |
| 32 | + if (!ScriptParser.inGlobalContext()) |
| 33 | + throw new NslContextException(EnumSet.of(NslContext.Global), name); |
| 34 | + if (returns > 0) throw new NslReturnValueException(name); |
| 35 | + |
| 36 | + ArrayList<Expression> paramsList = Expression.matchList(); |
| 37 | + if (paramsList.size() != 1) throw new NslArgumentException(name, 1); |
| 38 | + |
| 39 | + this.cpu = paramsList.get(0); |
| 40 | + if (!ExpressionType.isString(this.cpu)) |
| 41 | + throw new NslArgumentException(name, 1, ExpressionType.String); |
| 42 | + |
| 43 | + String cpuValue = this.cpu.getStringValue(); |
| 44 | + if (!VALID_CPUS.contains(cpuValue)) { |
| 45 | + throw new NslException( |
| 46 | + String.format( |
| 47 | + "%s: Invalid CPU architecture \"%s\". Valid values are: %s", |
| 48 | + name, cpuValue, String.join(", ", VALID_CPUS)), |
| 49 | + true); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** Assembles the source code. */ |
| 54 | + @Override |
| 55 | + public void assemble() throws IOException { |
| 56 | + AssembleExpression.assembleIfRequired(this.cpu); |
| 57 | + ScriptParser.writeLine(name + " " + this.cpu); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Assembles the source code. |
| 62 | + * |
| 63 | + * @param var the variable to assign the value to |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public void assemble(Register var) throws IOException { |
| 67 | + throw new UnsupportedOperationException("Not supported."); |
| 68 | + } |
| 69 | +} |
0 commit comments