Skip to content

Commit 4b75f23

Browse files
authored
feat: add support for CPU instruction (#7)
1 parent adfd565 commit 4b75f23

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

src/nsl/statement/Statement.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public static AssembleExpression matchInstruction(int returns) {
9393
return new ComponentTextInstruction(returns);
9494
if (ScriptParser.tokenizer.match(CopyFilesInstruction.name))
9595
return new CopyFilesInstruction(returns);
96+
if (ScriptParser.tokenizer.match(CPUInstruction.name)) return new CPUInstruction(returns);
9697
if (ScriptParser.tokenizer.match(CRCCheckInstruction.name))
9798
return new CRCCheckInstruction(returns);
9899
if (ScriptParser.tokenizer.match(CreateDirectoryInstruction.name))

0 commit comments

Comments
 (0)