diff --git a/NewSource/Socordia.CodeAnalysis/AST/Declarations/FunctionDefinition.cs b/NewSource/Socordia.CodeAnalysis/AST/Declarations/FunctionDefinition.cs index a2caa1d3..00e60790 100644 --- a/NewSource/Socordia.CodeAnalysis/AST/Declarations/FunctionDefinition.cs +++ b/NewSource/Socordia.CodeAnalysis/AST/Declarations/FunctionDefinition.cs @@ -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(nameof(Signature))!; - public Block? Body => (Block)Children.First; + public Signature Signature => (Signature)Children[0]; + public Block? Body => (Block?)Children[1]; } \ No newline at end of file diff --git a/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/FunctionDeclaration.cs b/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/FunctionDefinitionParser.cs similarity index 100% rename from NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/FunctionDeclaration.cs rename to NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/FunctionDefinitionParser.cs diff --git a/NewSource/SocordiaC/Compilation/Utils.cs b/NewSource/SocordiaC/Compilation/Utils.cs index dcaffa47..a09463ae 100644 --- a/NewSource/SocordiaC/Compilation/Utils.cs +++ b/NewSource/SocordiaC/Compilation/Utils.cs @@ -1,4 +1,5 @@ using DistIL.AsmIO; +using DistIL.IR; using Socordia.CodeAnalysis.AST; using Socordia.CodeAnalysis.AST.Declarations; using Socordia.CodeAnalysis.AST.TypeNames; @@ -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 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) { @@ -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) diff --git a/NewSource/SocordiaC/Driver.cs b/NewSource/SocordiaC/Driver.cs index 66c037de..bc273a1d 100644 --- a/NewSource/SocordiaC/Driver.cs +++ b/NewSource/SocordiaC/Driver.cs @@ -50,7 +50,7 @@ public string GetNamespaceOf(AstNode node) public void Compile() { - var hasError = (List messages) => messages.Any(_ => _.Severity == MessageSeverity.Error); + var hasError = () => PrintErrorsStage.Errors.Count >= 0; var pipeline = Pipeline.Build( cfg => { @@ -58,6 +58,7 @@ public void Compile() cfg.Add(); cfg.Add(); cfg.Add(); + cfg.Add(); cfg.Add(); diff --git a/NewSource/SocordiaC/Stages/CompileFunctionsStage.cs b/NewSource/SocordiaC/Stages/CompileFunctionsStage.cs index 9a87720f..072c9519 100644 --- a/NewSource/SocordiaC/Stages/CompileFunctionsStage.cs +++ b/NewSource/SocordiaC/Stages/CompileFunctionsStage.cs @@ -14,7 +14,7 @@ public async Task HandleAsync(Driver context, Func> { var builder = new IRBuilder(def.Body!.CreateBlock()); - if (!node.Children.First.HasChildren) + if (!node.Children[1].HasChildren) { builder.Emit(new ReturnInst()); } diff --git a/NewSource/SocordiaC/Stages/ConvertToIrStage.cs b/NewSource/SocordiaC/Stages/ConvertToIrStage.cs index 0102ca21..dc95ec43 100644 --- a/NewSource/SocordiaC/Stages/ConvertToIrStage.cs +++ b/NewSource/SocordiaC/Stages/ConvertToIrStage.cs @@ -35,7 +35,6 @@ public async Task HandleAsync(Driver context, Func> } } - return await next.Invoke(context); } } \ No newline at end of file diff --git a/NewSource/SocordiaC/Stages/ParsingStage.cs b/NewSource/SocordiaC/Stages/ParsingStage.cs index 0f6229ed..0dee1841 100644 --- a/NewSource/SocordiaC/Stages/ParsingStage.cs +++ b/NewSource/SocordiaC/Stages/ParsingStage.cs @@ -18,7 +18,8 @@ public async Task 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); @@ -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) diff --git a/NewSource/SocordiaC/Stages/PrintErrorsStage.cs b/NewSource/SocordiaC/Stages/PrintErrorsStage.cs new file mode 100644 index 00000000..7bb37369 --- /dev/null +++ b/NewSource/SocordiaC/Stages/PrintErrorsStage.cs @@ -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 +{ + public static List Errors = []; + + public async Task HandleAsync(Driver context, Func> 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); + } +} \ No newline at end of file diff --git a/NewSource/SocordiaC/compilation.sc b/NewSource/SocordiaC/compilation.sc index 5ee7e1a2..59f9fc8d 100644 --- a/NewSource/SocordiaC/compilation.sc +++ b/NewSource/SocordiaC/compilation.sc @@ -19,6 +19,11 @@ func external() -> System.Text.StringBuilder } +func internal_type() -> Hello +{ + +} + class MyClass {