From 9392b02e16f545d6520f3f14d21ba06a3922ef04 Mon Sep 17 00:00:00 2001 From: Marco Maccaferri Date: Wed, 2 Jan 2019 11:19:44 +0100 Subject: [PATCH] Added line delimiters and directive prefix preferences --- examples/HELLOBIN.ASM | 20 ++--- examples/HELLOCPM.ASM | 62 +++++++-------- src/com/maccasoft/tools/Application.java | 19 ++++- src/com/maccasoft/tools/Preferences.java | 19 +++++ .../maccasoft/tools/PreferencesDialog.java | 24 ++++++ src/com/maccasoft/tools/SourceFormatter.java | 75 +++++++++++++++++-- .../maccasoft/tools/editor/EditorUtil.java | 4 + .../maccasoft/tools/editor/SourceEditor.java | 6 +- 8 files changed, 178 insertions(+), 51 deletions(-) diff --git a/examples/HELLOBIN.ASM b/examples/HELLOBIN.ASM index f3080f8..3d9c681 100644 --- a/examples/HELLOBIN.ASM +++ b/examples/HELLOBIN.ASM @@ -1,21 +1,21 @@ ; -; Hello World .BIN for MONITOR environment -; +; Hello World .BIN for MONITOR environment +; ; Upload as Intel HEX then type G5000 to run. ; -; --------------------------------------------------------------------------- -; Constant definitions +; --------------------------------------------------------------------------- +; Constant definitions CHPUT .EQU 08H -; --------------------------------------------------------------------------- -; Compilation address +; --------------------------------------------------------------------------- +; Compilation address .ORG 5000H -; --------------------------------------------------------------------------- -; Program code entry point +; --------------------------------------------------------------------------- +; Program code entry point EXECUTE LD HL, HELLOWORLD @@ -26,9 +26,9 @@ LOOP LD A, (HL) INC HL JR LOOP -; --------------------------------------------------------------------------- +; --------------------------------------------------------------------------- ; Data - + HELLOWORLD .DB "Hello world!", 13, 10, 0 .END diff --git a/examples/HELLOCPM.ASM b/examples/HELLOCPM.ASM index 04338fe..5a79e48 100644 --- a/examples/HELLOCPM.ASM +++ b/examples/HELLOCPM.ASM @@ -1,31 +1,31 @@ -; -; Hello World .COM for CP/M environment -; -; Upload as CP/M binary then type HELLOCPM to run. -; - -; --------------------------------------------------------------------------- -; Constant definitions - -BDOS .EQU 05H -STROUT .EQU 09H - -; --------------------------------------------------------------------------- -; Compilation address, standard CP/M entry point - - .ORG 0100H - -; --------------------------------------------------------------------------- -; Program code entry point - -EXECUTE LD DE, HELLOWORLD - LD C, STROUT - CALL BDOS - RET - -; --------------------------------------------------------------------------- -; Data - -HELLOWORLD .DB "Hello world!", 13, 10, "$" - - .END +; +; Hello World .COM for CP/M environment +; +; Upload as CP/M binary then type HELLOCPM to run. +; + +; --------------------------------------------------------------------------- +; Constant definitions + +BDOS .EQU 05H +STROUT .EQU 09H + +; --------------------------------------------------------------------------- +; Compilation address, standard CP/M entry point + + .ORG 0100H + +; --------------------------------------------------------------------------- +; Program code entry point + +EXECUTE LD DE, HELLOWORLD + LD C, STROUT + CALL BDOS + RET + +; --------------------------------------------------------------------------- +; Data + +HELLOWORLD .DB "Hello world!", 13, 10, "$" + + .END diff --git a/src/com/maccasoft/tools/Application.java b/src/com/maccasoft/tools/Application.java index f8f7e1a..ec90155 100644 --- a/src/com/maccasoft/tools/Application.java +++ b/src/com/maccasoft/tools/Application.java @@ -865,6 +865,7 @@ public class Application { formatter.setCommentColumn(preferences.getCommentColumn()); formatter.setLabelCase(preferences.getLabelCase()); formatter.setMnemonicCase(preferences.getMnemonicCase()); + formatter.setDirectivePrefix(preferences.getDirectivePrefix()); tab.getEditor().replaceText(formatter.format()); @@ -1622,10 +1623,11 @@ public class Application { if (file.exists()) { try { + String lineDelimiter = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new FileReader(file)); while ((line = reader.readLine()) != null) { sb.append(line); - sb.append("\n"); + sb.append(lineDelimiter); } reader.close(); } catch (Exception e) { @@ -1778,8 +1780,21 @@ public class Application { file = new File(fileName); } + String text = tab.getEditor().getText(); + switch (preferences.getLineDelimiters()) { + case 0: + text = text.replaceAll("(\r\n|\n|\r)", System.getProperty("line.separator")); + break; + case 1: + text = text.replaceAll("(\r\n|\n|\r)", "\r\n"); + break; + case 2: + text = text.replaceAll("(\r\n|\n|\r)", "\n"); + break; + } + Writer os = new OutputStreamWriter(new FileOutputStream(file)); - os.write(tab.getEditor().getText()); + os.write(text); os.close(); if (saveAs || tab.getFile() == null) { diff --git a/src/com/maccasoft/tools/Preferences.java b/src/com/maccasoft/tools/Preferences.java index 7b515bb..1b255dd 100644 --- a/src/com/maccasoft/tools/Preferences.java +++ b/src/com/maccasoft/tools/Preferences.java @@ -64,6 +64,7 @@ public class Preferences { String editorFont; boolean showLineNumbers; boolean reloadOpenTabs; + int lineDelimiters; String[] openTabs; String selectedTab; @@ -72,6 +73,7 @@ public class Preferences { int commentColumn; int labelCase; int mnemonicCase; + int directivePrefix; boolean useTabstops; int tabWidth; @@ -95,6 +97,7 @@ public class Preferences { Preferences() { showLineNumbers = true; reloadOpenTabs = true; + lineDelimiters = 1; tabWidth = 4; mnemonicColumn = 16; @@ -172,6 +175,14 @@ public class Preferences { this.reloadOpenTabs = reloadOpenTabs; } + public int getLineDelimiters() { + return lineDelimiters; + } + + public void setLineDelimiters(int lineDelimiters) { + this.lineDelimiters = lineDelimiters; + } + public String[] getOpenTabs() { return openTabs; } @@ -228,6 +239,14 @@ public class Preferences { this.mnemonicCase = mnemonicCase; } + public int getDirectivePrefix() { + return directivePrefix; + } + + public void setDirectivePrefix(int directivePrefix) { + this.directivePrefix = directivePrefix; + } + public boolean isUseTabstops() { return useTabstops; } diff --git a/src/com/maccasoft/tools/PreferencesDialog.java b/src/com/maccasoft/tools/PreferencesDialog.java index 8335037..f0fe80d 100644 --- a/src/com/maccasoft/tools/PreferencesDialog.java +++ b/src/com/maccasoft/tools/PreferencesDialog.java @@ -46,6 +46,7 @@ public class PreferencesDialog extends Dialog { Button rootRemove; Button rootMoveUp; Button rootMoveDown; + Combo lineDelimiters; Text editorFont; Button editorFontBrowse; @@ -59,6 +60,7 @@ public class PreferencesDialog extends Dialog { Text commentColumn; Combo labelCase; Combo mnemonicCase; + Combo directivePrefix; List includes; Button includesAdd; @@ -262,6 +264,16 @@ public class PreferencesDialog extends Dialog { reloadOpenTabs.setText("Reload open tabs"); reloadOpenTabs.setSelection(preferences.isReloadOpenTabs()); + label = new Label(composite, SWT.NONE); + label.setText("Line delimiters"); + lineDelimiters = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); + lineDelimiters.setItems(new String[] { + "Platform", + "Windows (CR/LF)", + "Linux (LF)", + }); + lineDelimiters.select(preferences.getLineDelimiters()); + addSeparator(composite); label = new Label(composite, SWT.NONE); @@ -543,6 +555,16 @@ public class PreferencesDialog extends Dialog { "Lower", }); mnemonicCase.select(preferences.getMnemonicCase()); + + label = new Label(composite, SWT.NONE); + label.setText("Directive prefix"); + directivePrefix = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); + directivePrefix.setItems(new String[] { + "No change", + "Add dot", + "Remove dot", + }); + directivePrefix.select(preferences.getDirectivePrefix()); } void addSeparator(Composite parent) { @@ -560,12 +582,14 @@ public class PreferencesDialog extends Dialog { preferences.setTabWidth(Integer.valueOf(tabWidth.getText())); preferences.setUseTabstops(useTabstops.getSelection()); preferences.setReloadOpenTabs(reloadOpenTabs.getSelection()); + preferences.setLineDelimiters(lineDelimiters.getSelectionIndex()); preferences.setMnemonicColumn(Integer.valueOf(mnemonicColumn.getText())); preferences.setArgumentColumn(Integer.valueOf(argumentColumn.getText())); preferences.setCommentColumn(Integer.valueOf(commentColumn.getText())); preferences.setLabelCase(labelCase.getSelectionIndex()); preferences.setMnemonicCase(mnemonicCase.getSelectionIndex()); + preferences.setDirectivePrefix(directivePrefix.getSelectionIndex()); preferences.setIncludes(includes.getItems()); preferences.setGenerateBinary(generateBinary.getSelection()); diff --git a/src/com/maccasoft/tools/SourceFormatter.java b/src/com/maccasoft/tools/SourceFormatter.java index 701efe6..a1bb156 100644 --- a/src/com/maccasoft/tools/SourceFormatter.java +++ b/src/com/maccasoft/tools/SourceFormatter.java @@ -10,6 +10,9 @@ package com.maccasoft.tools; +import java.util.HashSet; +import java.util.Set; + import nl.grauw.glass.AssemblyException; import nl.grauw.glass.Line; import nl.grauw.glass.Source; @@ -33,6 +36,8 @@ public class SourceFormatter { public static final int NO_CHANGE = 0; public static final int TO_UPPER = 1; public static final int TO_LOWER = 2; + public static final int ADD_DOT = 1; + public static final int REMOVE_DOT = 2; Source source; @@ -42,12 +47,43 @@ public class SourceFormatter { int labelCase; int mnemonicCase; + int directivePrefix; int column; StringBuilder sb; + Set directives; + public SourceFormatter(Source source) { this.source = source; + + this.directives = new HashSet(); + this.directives.add("byte"); + this.directives.add("db"); + this.directives.add("dd"); + this.directives.add("ds"); + this.directives.add("dw"); + this.directives.add("else"); + this.directives.add("end"); + this.directives.add("endif"); + this.directives.add("endm"); + this.directives.add("endp"); + this.directives.add("ends"); + this.directives.add("equ"); + this.directives.add("error"); + this.directives.add("fill"); + this.directives.add("if"); + this.directives.add("incbin"); + this.directives.add("include"); + this.directives.add("irp"); + this.directives.add("macro"); + this.directives.add("org"); + this.directives.add("proc"); + this.directives.add("rept"); + this.directives.add("section"); + this.directives.add("text"); + this.directives.add("word"); + this.directives.add("warning"); } public int getMnemonicColumn() { @@ -90,6 +126,14 @@ public class SourceFormatter { this.mnemonicCase = mnemonicCase; } + public int getDirectivePrefix() { + return directivePrefix; + } + + public void setDirectivePrefix(int directivePrefix) { + this.directivePrefix = directivePrefix; + } + public String format() { sb = new StringBuilder(); format(source); @@ -128,17 +172,29 @@ public class SourceFormatter { sb.append(' '); } + String s = line.getMnemonic(); + if (isDirective(s)) { + if (directivePrefix == ADD_DOT) { + if (!s.startsWith(".")) { + s = "." + s; + } + } + else if (directivePrefix == REMOVE_DOT) { + if (s.startsWith(".")) { + s = s.substring(1); + } + } + } if (mnemonicCase == TO_UPPER) { - sb.append(line.getMnemonic().toUpperCase()); + sb.append(s.toUpperCase()); } else if (mnemonicCase == TO_LOWER) { - sb.append(line.getMnemonic().toLowerCase()); + sb.append(s.toLowerCase()); } else { - sb.append(line.getMnemonic()); + sb.append(s); } - - column += line.getMnemonic().length(); + column += s.length(); if (line.getArguments() != null) { while (column < argumentColumn) { @@ -148,7 +204,7 @@ public class SourceFormatter { if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') { sb.append(' '); } - String s = formatExpression(line.getArguments()); + s = formatExpression(line.getArguments()); sb.append(s); column += s.length(); } @@ -259,4 +315,11 @@ public class SourceFormatter { return e.toString(); } + boolean isDirective(String s) { + if (s.startsWith(".")) { + return directives.contains(s.substring(1).toLowerCase()); + } + return directives.contains(s.toLowerCase()); + } + } diff --git a/src/com/maccasoft/tools/editor/EditorUtil.java b/src/com/maccasoft/tools/editor/EditorUtil.java index 742571b..8aec11e 100644 --- a/src/com/maccasoft/tools/editor/EditorUtil.java +++ b/src/com/maccasoft/tools/editor/EditorUtil.java @@ -45,4 +45,8 @@ public class EditorUtil { return text; } + + public static String trimLines(String text) { + return text.replaceAll("[ \\t]+(\r\n|\n|\r)", "$1"); + } } diff --git a/src/com/maccasoft/tools/editor/SourceEditor.java b/src/com/maccasoft/tools/editor/SourceEditor.java index 0bbba5d..a976e5f 100644 --- a/src/com/maccasoft/tools/editor/SourceEditor.java +++ b/src/com/maccasoft/tools/editor/SourceEditor.java @@ -581,7 +581,7 @@ public class SourceEditor { ignoreUndo = true; ignoreRedo = true; - text = text.replaceAll("[ \\t]+(\r\n|\n|\r)", "$1"); + text = EditorUtil.trimLines(text); text = EditorUtil.replaceTabs(text, this.text.getTabs()); currentLine = 0; @@ -601,7 +601,9 @@ public class SourceEditor { int line = this.text.getLineAtOffset(offset); int topindex = line - this.text.getTopIndex(); + text = EditorUtil.trimLines(text); text = EditorUtil.replaceTabs(text, this.text.getTabs()); + this.text.setText(text); if (line > this.text.getLineCount()) { @@ -613,7 +615,7 @@ public class SourceEditor { public String getText() { String s = text.getText(); - String s2 = s.replaceAll("[ \\t]+(\r\n|\n|\r)", "$1"); + String s2 = EditorUtil.trimLines(s); if (!s2.equals(s)) { int offset = text.getCaretOffset(); int line = text.getLineAtOffset(offset);