Skip to content

Commit

Permalink
feat: Add resolving SimpleTypeName in same namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 26, 2024
1 parent 39a437c commit 6bdec5b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public class FunctionDefinition : Declaration
{
public FunctionDefinition(Signature signature, Block? body)
{
Properties.Set(nameof(Signature), signature);
Children.Add(signature);
Children.Add(body);
}

public Signature Signature => Properties.GetOrThrow<Signature>(nameof(Signature))!;
public Block? Body => (Block)Children.First;
public Signature Signature => (Signature)Children[0];
public Block? Body => (Block?)Children[1];
}
52 changes: 40 additions & 12 deletions NewSource/SocordiaC/Compilation/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DistIL.AsmIO;
using DistIL.IR;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.TypeNames;
Expand All @@ -18,21 +19,48 @@ public static string ToPascalCase(string name)
}

public static TypeDesc? GetTypeFromNode(TypeName node, TypeDef containingType)
{
var resolvedType = GetTypeFromNodeImpl(node, containingType);

if (resolvedType == null)
{
node.AddError($"Type '{node}' not found");
}

return resolvedType ?? PrimType.Void;
}

private static readonly Dictionary<string, TypeDesc> Primities = new()
{
["none"] = PrimType.Void,
["bool"] = PrimType.Bool,
["i8" ] = PrimType.Byte,
["i16" ] = PrimType.Int16,
["i32" ] = PrimType.Int32,
["i64" ] = PrimType.Int64,
["f32" ] = PrimType.Single,
["f64" ] = PrimType.Double,
};

private static TypeDesc? GetTypeFromNodeImpl(TypeName node, TypeDef containingType)
{
if (node is SimpleTypeName id)
{
return id.Name switch
if (Primities.TryGetValue(id.Name, out var prim))
{
"none" => PrimType.Void,
"bool" => PrimType.Bool,
"i8" => PrimType.Byte,
"i16" => PrimType.Int16,
"i32" => PrimType.Int32,
"i64" => PrimType.Int64,
"f32" => PrimType.Single,
"f64" => PrimType.Double,
var x => containingType.Name == x ? containingType : null
};
return prim;
}

var type = containingType.Module.FindType(containingType.Namespace, id.Name);
if (type != null)
{
return type;
}

if (containingType.Name == id.Name)
{
return containingType;
}
}
else if (node is QualifiedTypeName qname)
{
Expand All @@ -48,7 +76,7 @@ public static string ToPascalCase(string name)
}
}

throw new Exception("cannot get type from node");
return null;
}

public static TypeDefOrSpec? GetTypeFromNode(TypeName node, ModuleDef module)
Expand Down
3 changes: 2 additions & 1 deletion NewSource/SocordiaC/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ public string GetNamespaceOf(AstNode node)

public void Compile()
{
var hasError = (List<Message> messages) => messages.Any(_ => _.Severity == MessageSeverity.Error);
var hasError = () => PrintErrorsStage.Errors.Count >= 0;

var pipeline = Pipeline.Build<Driver, Driver>(
cfg => {
cfg.Add<ParsingStage>();
cfg.Add<SemanticCheckStage>();
cfg.Add<ConvertToIrStage>();
cfg.Add<CompileFunctionsStage>();
cfg.Add<PrintErrorsStage>();

cfg.Add<SaveModuleStage>();

Expand Down
2 changes: 1 addition & 1 deletion NewSource/SocordiaC/Stages/CompileFunctionsStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
{
var builder = new IRBuilder(def.Body!.CreateBlock());

if (!node.Children.First.HasChildren)
if (!node.Children[1].HasChildren)
{
builder.Emit(new ReturnInst());
}
Expand Down
1 change: 0 additions & 1 deletion NewSource/SocordiaC/Stages/ConvertToIrStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
}
}


return await next.Invoke(context);
}
}
9 changes: 4 additions & 5 deletions NewSource/SocordiaC/Stages/ParsingStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public async Task<Driver> HandleAsync(Driver context,

private static void ParseSourceFiles(Driver context)
{
Parallel.ForEachAsync(context.Settings.Sources, (filename, ct) => {
foreach (var filename in context.Settings.Sources)
{
if (File.Exists(filename))
{
var tree = CompilationUnit.FromFile(filename);
Expand All @@ -27,11 +28,9 @@ private static void ParseSourceFiles(Driver context)
}
else
{
context.Messages.Add(Message.Error($"File '{filename}' does not exists", (TextFilePosition)TextFilePosition.None));
// context.Messages.Add(Message.Error($"File '{filename}' does not exists", (TextFilePosition)TextFilePosition.None));
}

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

private static void ApplyTree(Driver context, CompilationUnit tree)
Expand Down
29 changes: 29 additions & 0 deletions NewSource/SocordiaC/Stages/PrintErrorsStage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Flo;
using MrKWatkins.Ast;
using MrKWatkins.Ast.Listening;
using MrKWatkins.Ast.Position;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.Parsing;
using Message = Socordia.CodeAnalysis.Parsing.Message;

namespace SocordiaC.Stages;

public sealed class PrintErrorsStage : IHandler<Driver, Driver>
{
public static List<string> Errors = [];

public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>> next)
{
foreach(var tree in context.Trees)
{
Errors.AddRange(MessageFormatter.FormatErrors(tree.Declarations, true));
}

foreach (var error in Errors)
{
Console.WriteLine(error);
}

return await next.Invoke(context);
}
}
5 changes: 5 additions & 0 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func external() -> System.Text.StringBuilder

}

func internal_type() -> Hello
{

}

class MyClass
{

Expand Down

0 comments on commit 6bdec5b

Please sign in to comment.