Skip to content

Commit

Permalink
First try on a Python compile target
Browse files Browse the repository at this point in the history
  • Loading branch information
EgonOlsen71 committed Jul 26, 2024
1 parent 93b59c9 commit 6fc3313
Show file tree
Hide file tree
Showing 58 changed files with 2,994 additions and 344 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/sixtyfour/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,36 @@ public static byte[] loadBlob(InputStream is) {
* @return the string[] the program splitted into it's lines
*/
public static String[] loadProgram(InputStream prg) {
return loadProgram(prg, false);
}

/**
* Load a program from a text file into a String[] array. It assumes that the
* text is UTF-8 encoded. Line separator is \n.
*
* @param prg the program's input stream
* @param noTrim don't trim input
* @return the string[] the program splitted into it's lines
*/
public static String[] loadProgram(InputStream prg, boolean noTrim) {
List<String> lines = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(prg, "UTF-8"))) {
while (br.ready()) {
String line = br.readLine();
if (line != null && line.trim().length() > 0) {
lines.add(line.trim());
if (noTrim) {
lines.add(line);
} else {
if (line != null && line.trim().length() > 0) {
lines.add(line.trim());
}
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to load program file!", e);
}
return lines.toArray(new String[lines.size()]);
}

/**
* Load a program from a text file into a String[] array. It assumes that the
* text is UTF-8 encoded. Line separator is \n.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* The transformer for the Powerscript/-shell target platform. It generates
* Powerscript code linked together with a Powerscript runtime. The result can
* either be run in a Powershell.
* be run in a Powershell.
*
* @author EgonOlsen
*
Expand Down Expand Up @@ -133,7 +133,7 @@ public List<String> transform(CompilerConfig config, MemoryConfig memConfig, Mac

return res;
}

private List<String> replaceBrackets(List<String> lines) {
return lines.stream().map(p -> replaceInternal(p)).collect(Collectors.toList());
}
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/sixtyfour/cbmnative/python/OptimizerPy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.sixtyfour.cbmnative.python;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.sixtyfour.cbmnative.Optimizer;
import com.sixtyfour.cbmnative.PlatformProvider;
import com.sixtyfour.cbmnative.ProgressListener;
import com.sixtyfour.config.CompilerConfig;

/**
* An optimizer implementation for the Python target platform.
* Nothing in here for now...
*
* @author EgonOlsen
*
*/
public class OptimizerPy implements Optimizer {


@Override
public List<String> optimize(CompilerConfig config, PlatformProvider platform, List<String> code,
ProgressListener pg) {
List<String> res = new ArrayList<>();
Set<String> globals = new HashSet<>();
boolean inDef = false;
int insertAt = -1;

for (String line:code) {
if (line.contains("\n")) {
res.addAll(Arrays.asList(line.split("\n")));
} else {
res.add(line);
}
}

code = res;
res = new ArrayList<>();

for (String line:code) {
if (line.startsWith("def ") || line.isEmpty()) {
if (inDef) {
if (insertAt!=-1) {
for (String global:globals) {
res.add(insertAt+1, global);
}
}
globals.clear();
}
inDef = line.startsWith("def ");
if (inDef) {
insertAt = res.size();
} else {
insertAt = -1;
}
}
if (inDef) {
if (line.trim().startsWith("global ")) {
globals.add(line);
} else {
res.add(line);
}
} else {
res.add(line);
}
}
return res;
}

}
95 changes: 95 additions & 0 deletions src/main/java/com/sixtyfour/cbmnative/python/PlatformPy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.sixtyfour.cbmnative.python;

import com.sixtyfour.cbmnative.Generator;
import com.sixtyfour.cbmnative.Optimizer;
import com.sixtyfour.cbmnative.PlatformProvider;
import com.sixtyfour.cbmnative.Transformer;
import com.sixtyfour.cbmnative.Unlinker;
import com.sixtyfour.config.CompilerConfig;

/**
* A PlatformProvider implementation for the Python target platform.
*
* @author EgonOlsen
*
*/
public class PlatformPy implements PlatformProvider {

@Override
public int getStackSize() {
return 100;
}

@Override
public int getForStackSize() {
return 100;
}

@Override
public Optimizer getOptimizer() {
return new OptimizerPy();
}

@Override
public Transformer getTransformer() {
return new TransformerPy();
}

@Override
public Unlinker getUnlinker() {
return null;
}

@Override
public boolean useLooseTypes() {
return true;
}

@Override
public int getMaxHeaderAddress() {
return 0;
}

@Override
public int getBaseAddress() {
return 0;
}

@Override
public void overrideConfig(CompilerConfig conf) {
conf.setLoopOptimizations(false);
conf.setIntOptimizations(false);
conf.setShiftOptimizations(false);
}

@Override
public Generator getGenerator(String orgLine) {
return null;
}

@Override
public int getBasicMemoryEndAddress() {
return 0;
}

@Override
public int getBasicBufferAddress() {
return 0;
}

@Override
public boolean supportsCompression() {
return false;
}

@Override
public boolean supportsBigRam() {
return false;
}

@Override
public boolean supportsVarRelocation() {
return false;
}

}
115 changes: 115 additions & 0 deletions src/main/java/com/sixtyfour/cbmnative/python/Pythonizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.sixtyfour.cbmnative.python;

import java.util.ArrayList;
import java.util.List;

/**
* Handles indentions and other Python related shenanigans
*
* @author EgonOlsen
*
*/
public class Pythonizer {

private int level=0;

public Pythonizer() {
//
}

public Pythonizer(int level) {
this.level=level;
}

public void indent() {
level++;
}

public void outdent() {
level--;
}

public String process(String cmd) {
cmd = removeDollar(cmd);
StringBuilder sb = new StringBuilder();
for (int i=0; i<level; i++) {
sb.append(" ");
}
sb.append(cmd);
return sb.toString();

}

public List<String> unifyIndentions(List<String> code) {
List<String> res = new ArrayList<>();
for (String line:code) {
line=line.replace("\t", " ");
res.add(line);
}
return res;
}

public String processGlobal(String cmd) {
cmd = removeDollar(cmd);
StringBuilder sb = new StringBuilder();
if (!cmd.startsWith("def ")) {
for (int i=0; i<level; i++) {
sb.append(" ");
}
}
if (!cmd.contains("=") || cmd.contains("==")) {
sb.append(cmd);
} else {
String[] parts = cmd.split("=");
String add = sb.toString();
String p1 = extractVarName(parts[1]);
if (!isNumber(p1) && !p1.startsWith("\"") && !p1.isEmpty()) {
sb.append("global ").append(p1).append("\n").append(add);
}
sb.append("global ").append(parts[0]).append("\n").append(add).append(cmd);
}
return sb.toString();
}

private String extractVarName(String cmd) {
int pos = cmd.indexOf(".");
int pos2 = cmd.indexOf("[");
if (pos2==-1 && pos==-1) {
return cmd;
}
if (pos2==-1) {
return cmd.substring(0, pos);
}
if (pos!=-1) {
return cmd.substring(0, Math.min(pos, pos2));
}
return cmd.substring(0, pos2);
}

private boolean isNumber(String line) {
try {
Float.parseFloat(line.trim());
return true;
} catch (Exception e) {
return false;
}
}

private String removeDollar(String cmd) {
StringBuilder ret = new StringBuilder();
boolean inStr = false;
for (int i=0; i<cmd.length(); i++) {
char c=cmd.charAt(i);
if (c=='"') {
inStr=!inStr;
}
if ((c=='%' || c=='$') && !inStr) {
ret.append(c=='$'?"_STRVAR":"_INTVAR");
} else {
ret.append(c);
}
}
return ret.toString();
}

}
Loading

0 comments on commit 6fc3313

Please sign in to comment.