Skip to content

Commit

Permalink
Added command line define option
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Apr 17, 2024
1 parent bcb2b7c commit 4e561f2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -443,4 +445,56 @@ public void propertyChange(PropertyChangeEvent evt) {
Assertions.assertArrayEquals(data, subject.preferences.roots);
}

@Test
void testSpin1Defines() throws Exception {
Preferences subject = new Preferences();

Map<String, String> defines = new HashMap<>();
defines.put("KEY", "VALUE");
defines.put("EMPTY_KEY", "");
subject.setSpin1Defines(defines);

ByteArrayOutputStream os = new ByteArrayOutputStream();

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.setSerializationInclusion(Include.NON_DEFAULT);
mapper.writeValue(os, subject.preferences);

Assertions.assertEquals(""
+ "{\n"
+ " \"spin1Defines\" : {\n"
+ " \"EMPTY_KEY\" : \"\",\n"
+ " \"KEY\" : \"VALUE\"\n"
+ " }\n"
+ "}", os.toString().replaceAll("\\r\\n", "\n"));
}

@Test
void testSpin2Defines() throws Exception {
Preferences subject = new Preferences();

Map<String, String> defines = new HashMap<>();
defines.put("KEY", "VALUE");
defines.put("EMPTY_KEY", "");
subject.setSpin2Defines(defines);

ByteArrayOutputStream os = new ByteArrayOutputStream();

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.setSerializationInclusion(Include.NON_DEFAULT);
mapper.writeValue(os, subject.preferences);

Assertions.assertEquals(""
+ "{\n"
+ " \"spin2Defines\" : {\n"
+ " \"EMPTY_KEY\" : \"\",\n"
+ " \"KEY\" : \"VALUE\"\n"
+ " }\n"
+ "}", os.toString().replaceAll("\\r\\n", "\n"));
}

}
24 changes: 24 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/EditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -57,13 +58,16 @@
import com.maccasoft.propeller.model.ObjectsNode;
import com.maccasoft.propeller.model.SourceProvider;
import com.maccasoft.propeller.model.Token;
import com.maccasoft.propeller.model.TokenStream;
import com.maccasoft.propeller.model.VariablesNode;
import com.maccasoft.propeller.spin1.Spin1Compiler;
import com.maccasoft.propeller.spin1.Spin1Formatter;
import com.maccasoft.propeller.spin1.Spin1TokenMarker;
import com.maccasoft.propeller.spin1.Spin1TokenStream;
import com.maccasoft.propeller.spin2.Spin2Compiler;
import com.maccasoft.propeller.spin2.Spin2Formatter;
import com.maccasoft.propeller.spin2.Spin2TokenMarker;
import com.maccasoft.propeller.spin2.Spin2TokenStream;
import com.maccasoft.propeller.spinc.CTokenMarker;
import com.maccasoft.propeller.spinc.Spin1CCompiler;
import com.maccasoft.propeller.spinc.Spin2CCompiler;
Expand Down Expand Up @@ -493,10 +497,30 @@ public void run() {
if (tabItemText.toLowerCase().endsWith(".spin")) {
compiler = new Spin1Compiler(preferences.getSpin1CaseSensitiveSymbols(), true);
compiler.setSourceProvider(new EditorTabSourceProvider(preferences.getSpin1LibraryPath()));

for (Entry<String, String> entry : preferences.getSpin1Defines().entrySet()) {
Token token;
TokenStream stream = new Spin1TokenStream(entry.getValue());
List<Token> list = new ArrayList<>();
while ((token = stream.nextToken()).type != Token.EOF) {
list.add(token);
}
compiler.addDefine(entry.getKey(), list);
}
}
else if (tabItemText.toLowerCase().endsWith(".spin2")) {
compiler = new Spin2Compiler(preferences.getSpin2CaseSensitiveSymbols());
compiler.setSourceProvider(new EditorTabSourceProvider(preferences.getSpin2LibraryPath()));

for (Entry<String, String> entry : preferences.getSpin2Defines().entrySet()) {
Token token;
TokenStream stream = new Spin2TokenStream(entry.getValue());
List<Token> list = new ArrayList<>();
while ((token = stream.nextToken()).type != Token.EOF) {
list.add(token);
}
compiler.addDefine(entry.getKey(), list);
}
}
else if (tabItemText.toLowerCase().endsWith(".c")) {
for (Node node : root.getChilds()) {
Expand Down
36 changes: 36 additions & 0 deletions modules/spin-tools/src/com/maccasoft/propeller/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ public SerializedPreferences() {
public String[] spin1LibraryPath;
public boolean spin1CaseSensitiveSymbols;
public String spin1Template;
@JsonInclude(Include.NON_ABSENT)
public Map<String, String> spin1Defines;

public String[] spin2LibraryPath;
public boolean spin2CaseSensitiveSymbols;
public boolean spin2ClockSetter;
public String spin2Template;
@JsonInclude(Include.NON_ABSENT)
public Map<String, String> spin2Defines;

public List<String> lru;

Expand Down Expand Up @@ -581,6 +585,22 @@ public void setSpin1CaseSensitiveSymbols(boolean spin1CaseSensitiveSymbols) {
changeSupport.firePropertyChange(PROP_SPIN1_CASE_SENSITIVE_SYMBOLS, preferences.spin1CaseSensitiveSymbols, preferences.spin1CaseSensitiveSymbols = spin1CaseSensitiveSymbols);
}

public Map<String, String> getSpin1Defines() {
return preferences.spin1Defines != null ? preferences.spin1Defines : new HashMap<>();
}

public void setSpin1Defines(Map<String, String> spin1Defines) {
if (spin1Defines.isEmpty()) {
preferences.spin1Defines = null;
}
else {
if (preferences.spin1Defines == null) {
preferences.spin1Defines = new HashMap<>();
}
preferences.spin1Defines.putAll(spin1Defines);
}
}

public File[] getSpin2LibraryPath() {
if (preferences.spin2LibraryPath != null) {
List<File> l = new ArrayList<>();
Expand Down Expand Up @@ -634,6 +654,22 @@ public void setSpin2Template(File spin2Template) {
preferences.spin2Template = spin2Template != null ? spin2Template.getAbsolutePath() : null;
}

public Map<String, String> getSpin2Defines() {
return preferences.spin2Defines != null ? preferences.spin2Defines : new HashMap<>();
}

public void setSpin2Defines(Map<String, String> spin2Defines) {
if (spin2Defines.isEmpty()) {
preferences.spin2Defines = null;
}
else {
if (preferences.spin2Defines == null) {
preferences.spin2Defines = new HashMap<>();
}
preferences.spin2Defines.putAll(spin2Defines);
}
}

public int[] getTabStops(Class<?> clazz) {
int[] result = null;

Expand Down

0 comments on commit 4e561f2

Please sign in to comment.