commit initial

This commit is contained in:
baishi
2026-05-30 07:59:28 +08:00
commit cbefad339f
39 changed files with 7736 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\TinyCC.Core\TinyCC.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,10 @@
namespace TinyCC.Tests;
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}

View File

@@ -0,0 +1,86 @@
using TinyCC.Core;
namespace TinyCC.Tests;
public class LexerTests
{
private readonly IErrorReporter _errorReporter;
public LexerTests()
{
_errorReporter = new ErrorReporter();
}
[Fact]
public void Tokenize_SimpleExpression_ReturnsTokens()
{
var source = "int x = 3 + 4;";
var lexer = new Lexer(source, "test.c", _errorReporter);
var tokens = lexer.Tokenize().ToList();
Assert.False(_errorReporter.HasErrors);
Assert.Contains(tokens, t => t.Type == TokenType.Int);
Assert.Contains(tokens, t => t.Type == TokenType.Identifier && t.Lexeme == "x");
Assert.Contains(tokens, t => t.Type == TokenType.Assign);
Assert.Contains(tokens, t => t.Type == TokenType.IntLiteral && Convert.ToInt64(t.Value!) == 3);
Assert.Contains(tokens, t => t.Type == TokenType.IntLiteral && Convert.ToInt64(t.Value!) == 4);
Assert.Contains(tokens, t => t.Type == TokenType.Plus);
Assert.Contains(tokens, t => t.Type == TokenType.Semicolon);
}
[Fact]
public void Tokenize_FunctionDefinition_ReturnsTokens()
{
var source = "int add(int a, int b) { return a + b; }";
var lexer = new Lexer(source, "test.c", _errorReporter);
var tokens = lexer.Tokenize().ToList();
Assert.False(_errorReporter.HasErrors);
Assert.Contains(tokens, t => t.Type == TokenType.Int);
Assert.Contains(tokens, t => t.Type == TokenType.Identifier && t.Lexeme == "add");
Assert.Contains(tokens, t => t.Type == TokenType.LeftParen);
Assert.Contains(tokens, t => t.Type == TokenType.RightParen);
Assert.Contains(tokens, t => t.Type == TokenType.LeftBrace);
Assert.Contains(tokens, t => t.Type == TokenType.Return);
Assert.Contains(tokens, t => t.Type == TokenType.RightBrace);
}
[Fact]
public void Tokenize_SkipsComments()
{
var source = "int x; // this is a comment\nint y;";
var lexer = new Lexer(source, "test.c", _errorReporter);
var tokens = lexer.Tokenize().ToList();
Assert.False(_errorReporter.HasErrors);
var identifiers = tokens.Where(t => t.Type == TokenType.Identifier).ToList();
Assert.Equal(2, identifiers.Count);
Assert.Equal("x", identifiers[0].Lexeme);
Assert.Equal("y", identifiers[1].Lexeme);
}
}
public class ParserTests
{
private readonly IErrorReporter _errorReporter;
public ParserTests()
{
_errorReporter = new ErrorReporter();
}
[Fact]
public void Parse_SimpleFunction_ReturnsAst()
{
var source = "int add(int a, int b) { return a + b; }";
var lexer = new Lexer(source, "test.c", _errorReporter);
var tokens = lexer.Tokenize().ToList();
var parser = new Parser(tokens, _errorReporter);
var ast = parser.Parse();
Assert.False(_errorReporter.HasErrors);
Assert.NotNull(ast);
Assert.IsType<ProgramNode>(ast);
}
}