-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First try on a Python compile target
- Loading branch information
1 parent
93b59c9
commit 6fc3313
Showing
58 changed files
with
2,994 additions
and
344 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
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
73 changes: 73 additions & 0 deletions
73
src/main/java/com/sixtyfour/cbmnative/python/OptimizerPy.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,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
95
src/main/java/com/sixtyfour/cbmnative/python/PlatformPy.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,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
115
src/main/java/com/sixtyfour/cbmnative/python/Pythonizer.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,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(); | ||
} | ||
|
||
} |
Oops, something went wrong.