Skip to content

Commit

Permalink
feat: Add Parsing and SemanticCheck Stage
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 1, 2024
1 parent 2ac4afa commit 7159b5e
Show file tree
Hide file tree
Showing 118 changed files with 128 additions and 5,601 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@
<EnablePreviewFeatures>True</EnablePreviewFeatures>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Description>The Parser For Backlang</Description>
<PackageIcon>logo.png</PackageIcon>
<PackageProjectUrl>https://www.backlang.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/Backlang-Org/Backlang</RepositoryUrl>
<PackageTags>backlang,dotnet</PackageTags>
<IncludeSymbols>True</IncludeSymbols>
<RootNamespace>Backlang.CodeAnalysis</RootNamespace>
</PropertyGroup>


<ItemGroup>
<None Include="..\..\logo.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Loyc.Syntax" Version="30.1.3"/>
<PackageReference Include="System.Resources.ResourceManager" Version="4.3.0"/>
<PackageReference Include="System.Runtime.Experimental" Version="7.0.0-preview.2.22152.2"/>
</ItemGroup>

<ItemGroup>
Expand All @@ -44,6 +35,4 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<Import Project="$(SolutionDir)\Version.props"/>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Backlang.Driver;
using Loyc.Syntax;

namespace BacklangC.Core;

public static class LNodeDeconstructors
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Backlang.Codeanalysis.Core;
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using Loyc.Syntax;

namespace Backlang.Contracts.Semantic;
namespace BacklangC.Semantic.Checks;

internal class ImportCheck : ISemanticCheck
{
public void Check(CompilationUnit tree, CompilerContext context)
public void Check(CompilationUnit tree, Driver context)
{
for (var i = 0; i < tree.Body.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Backlang.Codeanalysis.Core;
using Backlang.Driver;
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using BacklangC.Core;
using Loyc.Syntax;

namespace Backlang.Contracts.Semantic;
namespace BacklangC.Semantic.Checks;

internal class InterfaceNameCheck : ISemanticCheck
{
public void Check(CompilationUnit tree, CompilerContext context)
public void Check(CompilationUnit tree, Driver context)
{
for (var i = 0; i < tree.Body.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Backlang.Codeanalysis.Core;
using Backlang.Codeanalysis.Parsing.AST;
using Loyc.Syntax;

namespace Backlang.Contracts.Semantic;
namespace BacklangC.Semantic.Checks;

internal class ModifierCheck : ISemanticCheck
{
public void Check(CompilationUnit tree, CompilerContext context)
public void Check(CompilationUnit tree, Driver context)
{
var nodesWithModifiers = tree.Body
.SelectMany(_ => _.DescendantsAndSelf()).Where(IsModifiableNode).ToArray();
Expand All @@ -15,7 +17,7 @@ public void Check(CompilationUnit tree, CompilerContext context)
}
}

private void CheckForInvalidModifierCombination(LNode node, CompilerContext context)
private void CheckForInvalidModifierCombination(LNode node, Driver context)
{
var attrs = node.Attrs;
var condition = (attrs.Contains(LNode.Id(CodeSymbols.Public)) && attrs.Contains(LNode.Id(CodeSymbols.Private)))
Expand All @@ -26,7 +28,7 @@ private void CheckForInvalidModifierCombination(LNode node, CompilerContext cont

if (condition)
{
context.AddError(node, ErrorID.InvalidModifierCombination);
//context.AddError(node, ErrorID.InvalidModifierCombination);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Backlang.Codeanalysis.Core;
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using Loyc.Syntax;

namespace Backlang.Contracts.Semantic;
namespace BacklangC.Semantic.Checks;

internal class ModuleDefinitionCheck : ISemanticCheck
{
public void Check(CompilationUnit tree, CompilerContext context)
public void Check(CompilationUnit tree, Driver context)
{
if (tree.Body.Count(_ => _.Calls(CodeSymbols.Namespace)) > 1)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Backlang.Codeanalysis.Core;
using Backlang.Driver;
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using BacklangC.Core;
using Loyc.Syntax;

namespace Backlang.Contracts.Semantic;
namespace BacklangC.Semantic.Checks;

internal class TypenameCheck : ISemanticCheck
{
public void Check(CompilationUnit tree, CompilerContext context)
public void Check(CompilationUnit tree, Driver context)
{
for (var i = 0; i < tree.Body.Count; i++)
{
Expand Down
8 changes: 8 additions & 0 deletions NewSource/BacklangC/Semantic/ISemanticCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Backlang.Codeanalysis.Parsing.AST;

namespace BacklangC.Semantic;

public interface ISemanticCheck
{
void Check(CompilationUnit tree, Driver context);
}
24 changes: 24 additions & 0 deletions NewSource/BacklangC/Semantic/SemanticChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Backlang.Codeanalysis.Parsing.AST;
using BacklangC.Semantic.Checks;

namespace BacklangC.Semantic;

public static class SemanticChecker
{
private static readonly List<ISemanticCheck> SemanticChecks =
[
new ModuleDefinitionCheck(),
new ImportCheck(),
new TypenameCheck(),
new ModifierCheck(),
new InterfaceNameCheck()
];

public static void Do(CompilationUnit tree, Driver context)
{
foreach (var check in SemanticChecks)
{
check.Check(tree, context);
}
}
}
42 changes: 42 additions & 0 deletions NewSource/BacklangC/Stages/ParsingStage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using Flo;
using Loyc.Syntax;

namespace BacklangC.Stages;

public sealed class ParsingStage : IHandler<Driver, Driver>
{
public async Task<Driver> HandleAsync(Driver context,
Func<Driver, Task<Driver>> next)
{
ParseSourceFiles(context);

return await next.Invoke(context);
}

private static void ParseSourceFiles(Driver context)
{
Parallel.ForEachAsync(context.Settings.Sources, (filename, ct) => {
if (File.Exists(filename))
{
var tree = CompilationUnit.FromFile(filename);

ApplyTree(context, tree);
}
else
{
context.Messages.Add(Message.Error($"File '{filename}' does not exists", SourceRange.Synthetic));
}

return ValueTask.CompletedTask;
}).Wait();
}

private static void ApplyTree(Driver context, CompilationUnit tree)
{
context.Trees.Add(tree);

context.Messages.AddRange(tree.Messages);
}
}
18 changes: 18 additions & 0 deletions NewSource/BacklangC/Stages/SemanticCheckStage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using BacklangC.Semantic;
using Flo;

namespace BacklangC.Stages;

public sealed class SemanticCheckStage : IHandler<Driver, Driver>
{
public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>> next)
{
Parallel.ForEachAsync(context.Trees, (tree, ct) => {
SemanticChecker.Do(tree, context);

return ValueTask.CompletedTask;
}).Wait();

return await next.Invoke(context);
}
}
4 changes: 4 additions & 0 deletions NewSource/BacklangC/compilation.back
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

func main() {

}
File renamed without changes
11 changes: 0 additions & 11 deletions Source/Backlang-Compiler/compilation.back

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions Source/Backlang.Codeanalysis/Core/Attributes/KeywordAttribute.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Source/Backlang.Codeanalysis/Core/Attributes/LexemeAttribute.cs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7159b5e

Please sign in to comment.