chore(octoasm): scaffold project with token and diagnostic types
This commit is contained in:
1 parent
05b0cfafbd
commit
d3cf619571
3 files changed
+124
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<RootNamespace>Chip8.OctoAsm</RootNamespace>
|
||||||
|
<AssemblyName>Chip8.OctoAsm</AssemblyName>
|
||||||
|
<LangVersion>10</LangVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Chip8.Core\Chip8.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Chip8.OctoAsm;
|
||||||
|
|
||||||
|
public enum OctoSeverity
|
||||||
|
{
|
||||||
|
Error,
|
||||||
|
Warning,
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct OctoDiagnostic
|
||||||
|
{
|
||||||
|
public OctoDiagnostic(OctoSeverity severity, string message, SourcePosition position)
|
||||||
|
{
|
||||||
|
this.severity = severity;
|
||||||
|
this.message = message;
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OctoSeverity severity { get; }
|
||||||
|
public string message { get; }
|
||||||
|
public SourcePosition position { get; }
|
||||||
|
|
||||||
|
public int line => position.line;
|
||||||
|
public int column => position.column;
|
||||||
|
|
||||||
|
public override string ToString() =>
|
||||||
|
severity.ToString().ToLowerInvariant() + " at " + position + ": " + message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class OctoCompileException : Exception
|
||||||
|
{
|
||||||
|
public OctoCompileException(OctoDiagnostic diagnostic)
|
||||||
|
: this(new[] { diagnostic })
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public OctoCompileException(IReadOnlyList<OctoDiagnostic> diagnostics)
|
||||||
|
: base(describe(diagnostics))
|
||||||
|
{
|
||||||
|
this.diagnostics = diagnostics;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<OctoDiagnostic> diagnostics { get; }
|
||||||
|
|
||||||
|
public OctoDiagnostic diagnostic => diagnostics[0];
|
||||||
|
|
||||||
|
static string describe(IReadOnlyList<OctoDiagnostic> diagnostics)
|
||||||
|
{
|
||||||
|
if (diagnostics.Count == 1)
|
||||||
|
{
|
||||||
|
return diagnostics[0].ToString();
|
||||||
|
}
|
||||||
|
return diagnostics.Count + " Octo compile errors; first: " + diagnostics[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
namespace Chip8.OctoAsm;
|
||||||
|
|
||||||
|
public enum OctoTokenKind
|
||||||
|
{
|
||||||
|
Word,
|
||||||
|
Number,
|
||||||
|
OpenBrace,
|
||||||
|
CloseBrace,
|
||||||
|
OpenParen,
|
||||||
|
CloseParen,
|
||||||
|
EndOfFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct SourcePosition
|
||||||
|
{
|
||||||
|
public SourcePosition(int line, int column)
|
||||||
|
{
|
||||||
|
this.line = line;
|
||||||
|
this.column = column;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int line { get; }
|
||||||
|
public int column { get; }
|
||||||
|
|
||||||
|
public override string ToString() => "line " + line + ", col " + column;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct OctoToken
|
||||||
|
{
|
||||||
|
public OctoToken(OctoTokenKind kind, string text, int value, SourcePosition position)
|
||||||
|
{
|
||||||
|
this.kind = kind;
|
||||||
|
this.text = text;
|
||||||
|
this.value = value;
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OctoTokenKind kind { get; }
|
||||||
|
public string text { get; }
|
||||||
|
public int value { get; }
|
||||||
|
public SourcePosition position { get; }
|
||||||
|
|
||||||
|
public bool isWord(string word) => kind == OctoTokenKind.Word && text == word;
|
||||||
|
|
||||||
|
public override string ToString() => kind switch
|
||||||
|
{
|
||||||
|
OctoTokenKind.EndOfFile => "<eof>",
|
||||||
|
OctoTokenKind.Number => "number(" + value + ")",
|
||||||
|
_ => "'" + text + "'",
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user