Skip to content

Commit

Permalink
feat: Add Mnemnonics To Assembler Highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 11, 2024
1 parent c2ae9df commit 0b249c2
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 44 deletions.
73 changes: 37 additions & 36 deletions src/MimaSim/MimaSim.Desktop/run.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/MimaSim/MimaSim/Behaviors/HighlightingBindingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using AvaloniaEdit;
using AvaloniaEdit.Highlighting;

namespace MimaSim.Behaviors;

public class HighlightingBindingBehavior : Behavior<TextEditor>
{
private TextEditor _textEditor;
Expand Down
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ShowLineNumbers="True">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding Source, Mode=TwoWay}"/>

<behaviors:HighlightingBindingBehavior Highlighting="{Binding Highlighting, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</avaloniaEdit:TextEditor>

Expand Down
4 changes: 2 additions & 2 deletions src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ private void InitializeComponent()

private void InitHighlighting()
{
this.Find<TextEditor>("editor").SyntaxHighlighting = LoadHighlighting("Hochsprache", ".hoch", "Highligting_Hochsprache");

LoadHighlighting("Maschinencode", ".hex", "Highligting_Hex");
LoadHighlighting("Assembler", ".asm", "Highligting_Assembler");
LoadHighlighting("Hochsprache", ".hoch", "Highligting_Hochsprache");
}

private IHighlightingDefinition LoadHighlighting(string name, string extension, string filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void InitLexer(LexerConfig lexer)
lexer.IgnoreWhitespace();

lexer.Ignore(new MultiLineCommentIgnoreMatcher("/*", "*/"));
lexer.Ignore(new SingleLineCommentIgnoreMatcher("//"));
lexer.Ignore(new SingleLineCommentIgnoreMatcher("#"));

lexer.MatchNumber(true, false);
lexer.AddMatcher(new EnumMatcher<Registers>("#register"));
Expand Down
34 changes: 31 additions & 3 deletions src/MimaSim/MimaSim/Resources/Highligting_Assembler.xshd
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,41 @@

<!-- This is the main ruleset. -->
<RuleSet>
<Span color="Comment" begin="//" />
<Span color="Comment" begin="#" />
<Span color="Comment" multiline="true" begin="/\*" end="\*/" />

<Keywords fontWeight="bold" foreground="Black">
<Keywords fontWeight="bold" foreground="Blue">
<Word>nop</Word>
<Word>exit</Word>

<Word>mov</Word>
<Word>load</Word>
<Word>cmpe</Word>

<Word>cmplt</Word>
<Word>cmple</Word>
<Word>cmpeq</Word>
<Word>cmpge</Word>
<Word>cmpgt</Word>
<Word>cmpne</Word>
<Word>cmpn</Word>

<Word>mul</Word>
<Word>add</Word>
<Word>sub</Word>
<Word>div</Word>
<Word>inc</Word>
<Word>dec</Word>

<Word>not</Word>
<Word>and</Word>
<Word>or</Word>
<Word>xor</Word>

<Word>lshift</Word>
<Word>rshift</Word>

<Word>jmp</Word>
<Word>jpmc</Word>
</Keywords>

<!-- Digits -->
Expand Down
21 changes: 21 additions & 0 deletions src/MimaSim/MimaSim/Resources/Highligting_Hex.xshd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Blue" />

<!-- This is the main ruleset. -->
<RuleSet>
<Span color="Comment" begin="//" />
<Span color="Comment" multiline="true" begin="/\*" end="\*/" />

<!-- Digits -->
<Rule foreground="DarkBlue">
\b0[xX][0-9a-fA-F]+ # hex number
| \b
( \d+(\.[0-9]+)? #number with optional floating point
| \.[0-9]+ #or just starting with floating point
)
([eE][+-]?[0-9]+)? # optional exponent
</Rule>
</RuleSet>
</SyntaxDefinition>
13 changes: 12 additions & 1 deletion src/MimaSim/MimaSim/ViewModels/ExecutionTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public LanguageName SelectedLanguage
SampleNames = Locator.Current.GetService<SampleLoader>().GetSampleNamesFor(_selectedLanguage).ToArray();

SelectedSample = null;
Highlighting = HighlightingManager.Instance.GetDefinitionByExtension(".hoch");
Highlighting = HighlightingManager.Instance.GetDefinitionByExtension(GetExtensionForLanguage(value));
}
}

Expand Down Expand Up @@ -204,4 +204,15 @@ public ExecutionTabViewModel()
var cache = Locator.Current.GetService<ICache>();
_source = cache!.Get<string>("input")!;
}

private string GetExtensionForLanguage(LanguageName language)
{
return language switch
{
LanguageName.Hochsprache => ".hoch",
LanguageName.Assembly => ".asm",
LanguageName.Maschinencode => ".hex",
_ => ".asm"
};
}
}

0 comments on commit 0b249c2

Please sign in to comment.