Skip to content

Commit

Permalink
refactor: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 26, 2024
1 parent e7d6f73 commit e0988db
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AssemblyParser : Parser
{
protected override void InitLexer(LexerConfig lexer)
{
lexer.IgnoreWhitespace();
lexer.Ignore(" ", "\t", "\r");

lexer.Ignore(new MultiLineCommentIgnoreMatcher("/*", "*/"));
lexer.Ignore(new SingleLineCommentIgnoreMatcher("#"));
Expand All @@ -19,6 +19,7 @@ protected override void InitLexer(LexerConfig lexer)
lexer.MatchString("'", "'", allowEscapeChars: false, allowUnicodeChars: false);

lexer.AddSymbols("(", ")", ",", "{", "}", "/*", "*/");
lexer.AddMatcher(new NewLineMatcher());
}

protected override void InitParser(ParserDefinition def)
Expand All @@ -28,7 +29,7 @@ protected override void InitParser(ParserDefinition def)
def.Register(Number, new NumberParselet());
def.Register(Name, new InstructionParselet());
def.Register("macro", new MacroParselet());
def.Register(String, new StringLiteralParselet());
def.Register(PredefinedSymbols.String, new StringLiteralParselet());

def.Prefix("&", tag: "address");
def.Prefix("$", tag: "labelref");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MimaSim.Core;
using System.Diagnostics;
using MimaSim.Core;
using MimaSim.Core.Parsing;

namespace MimaSim.MIMA.Parsing.Parsers.Assembler;
Expand All @@ -8,6 +9,19 @@ public class AssemblySourceTextTranslator : ISourceTextTranslator
public byte[] ToRaw(string input, ref DiagnosticBag diagnostics)
{
var parser = new AssemblyParser();

parser.Lexer.SetSource(input);
while (parser.Lexer.IsNotAtEnd())
{
var token = parser.Lexer.Next().ToString();

if (token == "\n")
{
token = "\\n";
}

Debug.WriteLine(token);
}
var ast = parser.Parse(input);

var visitor = new AssemblyVisitor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Silverfly;
using Silverfly.Lexing;

namespace MimaSim.MIMA.Parsing.Parsers.Assembler;

public class NewLineMatcher : IMatcher
{
public bool Match(Lexer lexer, char c) => c == '\n' && lexer.IsContext<InstructionContext>();

public Token Build(Lexer lexer, ref int index, ref int column, ref int line)
{
while (lexer.IsNotAtEnd() && lexer.Peek() == '\n')
{
lexer.Advance();
}

return new Token("\n", new ReadOnlyMemory<char>(['\n']), line, column, lexer.Document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public AstNode Parse(Parser parser, Token token)
{
using var context = parser.Lexer.OpenContext<InstructionContext>();

var args = parser.ParseSeperated(",");
var args = parser.ParseSeperated(",", terminator: "\n");
return new InstructionNode(token, args)
.WithRange(token, parser.LookAhead());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public AstNode Parse(Parser parser, Token token)
parser.Consume("{");
var body = parser.ParseList(terminators: "}");

return new MacroNode(nameToken, parameters, body);
return new MacroNode(nameToken, parameters, body)
.WithRange(token, parser.LookAhead());
}

public ImmutableList<AstNode> ParseArgs(Parser parser, Symbol separator, params Symbol[] terminators)
Expand Down

0 comments on commit e0988db

Please sign in to comment.