This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comments (with %), direct use of johnny instructions (with @)
- Loading branch information
1 parent
066717f
commit 5dc705f
Showing
5 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/jcompiler/compiler/extended/statements/Raw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package jcompiler.compiler.extended.statements; | ||
|
||
import jcompiler.Memory; | ||
import jcompiler.compiler.JohnnyInstruction; | ||
import jcompiler.compiler.Statement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Raw implements Statement { | ||
|
||
public static final String VAR_REGEX = "[a-zA-Z]+"; | ||
public static final String NUM_REGEX = "[0-9]+"; | ||
|
||
private final String code; | ||
|
||
public Raw(String code) { | ||
this.code = code; | ||
} | ||
|
||
String[] names = {"take", "add", "sub", "save", "jmp", "tst", "inc", "dec", "null", "hlt", "read", "out", "set"}; | ||
|
||
@Override | ||
public JohnnyInstruction[] compile() { | ||
String[] splitCode = code.substring(1).split(" "); | ||
|
||
int hi = 0; | ||
int lo = 0; | ||
|
||
if (splitCode[0].matches(NUM_REGEX)) { | ||
hi = Integer.parseInt(splitCode[0]); | ||
} else { | ||
for (int i = 0; i < names.length; i++) { | ||
if (splitCode[0].toLowerCase().equals(names[i])) { | ||
hi = i + 1; | ||
break; | ||
} | ||
} | ||
if (hi == 0) { | ||
throw new IllegalArgumentException("Could not find instruction at statement " + code); | ||
} | ||
} | ||
|
||
|
||
boolean isAddress; | ||
List<JohnnyInstruction> instructionList = new ArrayList<>(); | ||
if (splitCode[1].matches(NUM_REGEX) || (splitCode[1].startsWith("$") && splitCode[1].substring(1).matches(NUM_REGEX))) { | ||
isAddress = splitCode[1].startsWith("$"); | ||
|
||
lo = isAddress ? Integer.parseInt(splitCode[1].substring(1)) : Integer.parseInt(splitCode[1]); | ||
|
||
} else { | ||
String varName = splitCode[1]; | ||
if (!Memory.variableExists(varName)) { | ||
throw new IllegalStateException("Variable " + varName + " does not exist!"); | ||
} | ||
lo = Memory.resolveId(varName); | ||
isAddress = true; | ||
} | ||
|
||
instructionList.add(new jcompiler.compiler.standard.johnnyinstructions.Raw(hi, lo, isAddress)); | ||
|
||
return instructionList.toArray(new JohnnyInstruction[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/jcompiler/compiler/standard/johnnyinstructions/Raw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package jcompiler.compiler.standard.johnnyinstructions; | ||
|
||
import jcompiler.compiler.DataAccessor; | ||
import jcompiler.compiler.JohnnyInstruction; | ||
|
||
public class Raw extends JohnnyInstruction implements DataAccessor { | ||
|
||
private final int hi; | ||
private int lo; | ||
private final boolean isAddress; | ||
|
||
public Raw(int hi, int lo, boolean isAddress) { | ||
this.hi = hi; | ||
this.lo = lo; | ||
this.isAddress = isAddress; | ||
} | ||
|
||
@Override | ||
public void shiftAddress(int shiftAmount) { | ||
lo = isAddress ? lo + shiftAmount : lo; | ||
} | ||
|
||
@Override | ||
public int getH() { | ||
return hi; | ||
} | ||
|
||
@Override | ||
public int getL() { | ||
return lo; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/jcompiler/compiler/standard/statements/Raw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package jcompiler.compiler.standard.statements; | ||
|
||
import jcompiler.Memory; | ||
import jcompiler.compiler.JohnnyInstruction; | ||
import jcompiler.compiler.Statement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Raw implements Statement { | ||
|
||
public static final String VAR_REGEX = "[a-zA-Z]+"; | ||
public static final String NUM_REGEX = "[0-9]+"; | ||
|
||
private final String code; | ||
|
||
public Raw(String code) { | ||
this.code = code; | ||
} | ||
|
||
String[] names = {"take", "add", "sub", "save", "jmp", "tst", "inc", "dec", "null", "hlt"}; | ||
|
||
@Override | ||
public JohnnyInstruction[] compile() { | ||
String[] splitCode = code.substring(1).split(" "); | ||
|
||
int hi = 0; | ||
int lo = 0; | ||
|
||
if (splitCode[0].matches(NUM_REGEX)) { | ||
hi = Integer.parseInt(splitCode[0]); | ||
} else { | ||
for (int i = 0; i < names.length; i++) { | ||
if (splitCode[0].toLowerCase().equals(names[i])) { | ||
hi = i + 1; | ||
break; | ||
} | ||
} | ||
if (hi == 0) { | ||
throw new IllegalArgumentException("Could not find instruction at statement " + code); | ||
} | ||
} | ||
|
||
|
||
boolean isAddress; | ||
List<JohnnyInstruction> instructionList = new ArrayList<>(); | ||
if (splitCode[1].matches(NUM_REGEX) || (splitCode[1].startsWith("$") && splitCode[1].substring(1).matches(NUM_REGEX))) { | ||
isAddress = splitCode[1].startsWith("$"); | ||
|
||
lo = isAddress ? Integer.parseInt(splitCode[1].substring(1)) : Integer.parseInt(splitCode[1]); | ||
|
||
} else { | ||
String varName = splitCode[1]; | ||
if (!Memory.variableExists(varName)) { | ||
throw new IllegalStateException("Variable " + varName + " does not exist!"); | ||
} | ||
lo = Memory.resolveId(varName); | ||
isAddress = true; | ||
} | ||
|
||
instructionList.add(new jcompiler.compiler.standard.johnnyinstructions.Raw(hi, lo, isAddress)); | ||
|
||
return instructionList.toArray(new JohnnyInstruction[0]); | ||
} | ||
} |