Skip to content

Commit

Permalink
feat: Add Macro Expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 2, 2024
1 parent d62325e commit a7526f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Backlang.Driver;
using Backlang.Codeanalysis.Parsing.AST;
using Loyc.Syntax;

namespace BacklangC.Core;

public static class ExtensionUtils
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using LeMP;
using System.Text;
using System.Text.RegularExpressions;
using Backlang.Codeanalysis.Parsing;
using Backlang.Codeanalysis.Parsing.AST;
using LeMP;
using Loyc;
using Loyc.Syntax;

namespace Backlang.Driver.InternalMacros;
namespace BacklangC.Core;

[ContainsMacros]
public static class SyntacticMacros
Expand Down Expand Up @@ -126,16 +131,16 @@ private static LNode ExpandOperator(LNode @operator, IMacroContext context)
};
var modChanged = @operator.WithAttrs(newAttrs);
var fnName = @operator.Args[1];
var compContext = (CompilerContext)context.ScopedProperties["Context"];
var compContext = (Driver)context.ScopedProperties["Context"];

if (fnName is var (_, (_, name)) && OpMap.ContainsKey(name.Name.Name))
{
var op = OpMap[name.Name.Name];

if (@operator[2].ArgCount != op.ArgumentCount)
{
compContext.AddError(@operator,
$"Cannot overload operator, parameter count mismatch. {op.ArgumentCount} parameters expected");
// compContext.AddError(@operator,
// $"Cannot overload operator, parameter count mismatch. {op.ArgumentCount} parameters expected");
}

var newTarget = SyntaxTree.Type("op_" + op.OperatorName, LNode.List()).WithRange(fnName.Range);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using Backlang.Core.Macros;
using Backlang.Driver.InternalMacros;
using System.Runtime.Loader;
using Backlang.Codeanalysis.Parsing;
using Backlang.Core.Macros;
using BacklangC.Core;
using Flo;
using LeMP;
using LeMP.Prelude;
using Loyc;
using Loyc.Collections;
using System.Runtime.Loader;
using Loyc.Syntax;

namespace Backlang.Driver.Compiling.Stages.ExpandingStages;
namespace BacklangC.Stages;

public sealed class ExpandMacrosStage : IHandler<CompilerContext, CompilerContext>
public sealed class ExpandMacrosStage : IHandler<Driver, Driver>
{
private readonly MacroProcessor _macroProcessor;

Expand All @@ -23,19 +26,16 @@ public ExpandMacrosStage()
_macroProcessor.PreOpenedNamespaces.Add((Symbol)typeof(SyntacticMacros).Namespace);
}

public async Task<CompilerContext> HandleAsync(CompilerContext context,
Func<CompilerContext, Task<CompilerContext>> next)
public async Task<Driver> HandleAsync(Driver context,
Func<Driver, Task<Driver>> next)
{
context.CompilationTarget.BeforeExpandMacros(_macroProcessor); //Only calls once

if (context.MacroReferences != null)
if (context.Settings.MacroReferences != null)
{
var loadContext = new AssemblyLoadContext("Macros");
foreach (var ml in context.MacroReferences)
foreach (var ml in context.Settings.MacroReferences)
{
var basePath = new FileInfo(context.ProjectFile).Directory.FullName;
var directory = new FileInfo(context.ResultingOutputPath).Directory.FullName;
var assembly = loadContext.LoadFromAssemblyPath(Path.Combine(basePath, directory, ml));
var directory = new FileInfo(context.Settings.OutputPath).Directory?.FullName;
var assembly = loadContext.LoadFromAssemblyPath(Path.Combine(directory, ml));

if (assembly == null)
{
Expand All @@ -48,7 +48,6 @@ public async Task<CompilerContext> HandleAsync(CompilerContext context,
}
}

_macroProcessor.DefaultScopedProperties.Add("Target", context.Options.Target);
_macroProcessor.DefaultScopedProperties.Add("Context", context);

foreach (var tree in context.Trees)
Expand Down

0 comments on commit a7526f3

Please sign in to comment.