Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessing and Math Update #27

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,54 @@ public HighProgramCompiler compile() {
asm = asm.substring(1);
}
code = code + "\n" + asm;
} else if(split[0].equals("add")) {
if(protectedVariables.contains(split[1])) {
error("access violation!");
}

if(!isInt(split[2])) {
code = code + "\n" +
"ADDMM "+variableAddresses.get(split[1])+" "+variableAddresses.get(split[2]);
} else {
code = code + "\n" +
"ADDM "+variableAddresses.get(split[1])+" "+parseInt(split[2]);
}
} else if(split[0].equals("sub")) {
if(protectedVariables.contains(split[1])) {
error("access violation!");
}

if(!isInt(split[2])) {
code = code + "\n" +
"SUBMM "+variableAddresses.get(split[1])+" "+variableAddresses.get(split[2]);
} else {
code = code + "\n" +
"SUBM "+variableAddresses.get(split[1])+" "+parseInt(split[2]);
}
} else if(split[0].equals("mul")) {
if(protectedVariables.contains(split[1])) {
error("access violation!");
}

if(!isInt(split[2])) {
code = code + "\n" +
"MULMM "+variableAddresses.get(split[1])+" "+variableAddresses.get(split[2]);
} else {
code = code + "\n" +
"MULM "+variableAddresses.get(split[1])+" "+parseInt(split[2]);
}
} else if(split[0].equals("div")) {
if(protectedVariables.contains(split[1])) {
error("access violation!");
}

if(!isInt(split[2])) {
code = code + "\n" +
"DIVMM "+variableAddresses.get(split[1])+" "+variableAddresses.get(split[2]);
} else {
code = code + "\n" +
"DIVM "+variableAddresses.get(split[1])+" "+parseInt(split[2]);
}
} else {
if (compileProcessor != null) {
String a = compileProcessor.generateSchesemForInstruction(compileContext, split);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,150 @@
package de.emilschlampp.scheCPU.high.preprocessing;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class HighLangPreprocessor {
private PreprocessorEnvironment preprocessorEnvironment = new PreprocessorEnvironment();

private final String program;
private String result;

public HighLangPreprocessor(String program) {
this.program = program;
}

public HighLangPreprocessor preprocess() {
List<String> programSplit = new ArrayList<>(Arrays.asList(program.split("\n")));
Map<String, Boolean> definedMacros = new HashMap<>();

List<String> newProgram = new ArrayList<>();

for (int i = 0; i < programSplit.size(); i++) {
String line = programSplit.get(i);

if(line.startsWith("~")) {
String command = line.substring(1);

String[] split = command.split(" ");

if(split[0].equals("include")) {
InputStream in = null;

if(!preprocessorEnvironment.isAllowFileInclusion()) {
throw new RuntimeException("File inclusion not allowed!");
}

if(preprocessorEnvironment.isFileInclusionWhiteList()) {
if(!preprocessorEnvironment.getFileInclusionWhitelist().contains(split[1])) {
throw new RuntimeException("Non-whitelisted file!");
}
}

try {
in = preprocessorEnvironment.getFileInputStreamCreator().apply(split[1]);
} catch (IOException throwable) {
throw new RuntimeException("Error while opening the include file!", throwable);
}
if(in == null) {
throw new RuntimeException("The include file does not exist!");
}

Scanner scanner = new Scanner(in);

int cur = 0;
while (scanner.hasNextLine()) {
programSplit.add(i+1+cur, scanner.nextLine());
cur++;
}
} else if(split[0].equals("define")) {
if(split.length == 2) {
definedMacros.put(split[1], true);
}
} else if(split[0].equals("undef")) {
if(split.length == 2) {
definedMacros.remove(split[1]);
}
} else if(split[0].equals("ifdef")) {
if(split.length != 2) {
throw new RuntimeException("Invalid arg len!");
}

String macro = split[1];

if(!definedMacros.containsKey(macro)) {
int bracesCount = 1;

while (bracesCount > 0 && i < programSplit.size() -1) {
i++;
String nextLine = programSplit.get(i);
if(nextLine.startsWith("~ifdef") || nextLine.startsWith("~ifndef")) {
bracesCount++;
} else if(nextLine.startsWith("~endif")) {
bracesCount--;
}
}
}
} else if(split[0].equals("ifndef")) {
if(split.length != 2) {
throw new RuntimeException("Invalid arg len!");
}

String macro = split[1];

if(definedMacros.containsKey(macro)) {
int bracesCount = 1;

while (bracesCount > 0 && i < programSplit.size() -1) {
i++;
String nextLine = programSplit.get(i);
if(nextLine.startsWith("~ifdef") || nextLine.startsWith("~ifndef")) {
bracesCount++;
} else if(nextLine.startsWith("~endif")) {
bracesCount--;
}
}
}
} else if(split[0].equals("endif")) {

}
} else {
newProgram.add(line);
}

//programSplit.add(line);
}

StringBuilder res = new StringBuilder();
boolean a = false;
for (String s : newProgram) {
res.append(s).append("\n");
a = true;
}

if(a) {
this.result = res.substring(0, res.length()-1);
} else {
this.result = res.toString();
}

return this;
}

public String getProgram() {
return program;
}

public String getResult() {
return result;
}

public PreprocessorEnvironment getPreprocessorEnvironment() {
return preprocessorEnvironment;
}

public HighLangPreprocessor setPreprocessorEnvironment(PreprocessorEnvironment preprocessorEnvironment) {
this.preprocessorEnvironment = preprocessorEnvironment;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.emilschlampp.scheCPU.high.preprocessing;

import de.emilschlampp.scheCPU.util.ThrowingFunction;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class PreprocessorEnvironment {
private boolean allowFileInclusion = true;
private boolean fileInclusionWhiteList = false;
private List<String> fileInclusionWhitelist = new ArrayList<>();

private ThrowingFunction<String, InputStream, IOException> fileInputStreamCreator = FileInputStream::new; // Support for virtual file systems (like databases or fio-discs)


public boolean isAllowFileInclusion() {
return allowFileInclusion;
}

public PreprocessorEnvironment setAllowFileInclusion(boolean allowFileInclusion) {
this.allowFileInclusion = allowFileInclusion;
return this;
}

public boolean isFileInclusionWhiteList() {
return fileInclusionWhiteList;
}

public PreprocessorEnvironment setFileInclusionWhiteList(boolean fileInclusionWhiteList) {
this.fileInclusionWhiteList = fileInclusionWhiteList;
return this;
}

public List<String> getFileInclusionWhitelist() {
return fileInclusionWhitelist;
}

public PreprocessorEnvironment setFileInclusionWhitelist(List<String> fileInclusionWhitelist) {
this.fileInclusionWhitelist = fileInclusionWhitelist;
return this;
}

public ThrowingFunction<String, InputStream, IOException> getFileInputStreamCreator() {
return fileInputStreamCreator;
}

public PreprocessorEnvironment setFileInputStreamCreator(ThrowingFunction<String, InputStream, IOException> fileInputStreamCreator) {
this.fileInputStreamCreator = fileInputStreamCreator;
return this;
}
}

This file was deleted.

Loading