Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Sep 6, 2024
1 parent 4f45e16 commit c5e8bd6
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ runtime/sacred.vm/zig-cache/
runtime/sacred.vm/zig-out/

bench/

ReleaseAot/
1 change: 0 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"sdk": {
"version": "9.0.0",
"allowPrerelease": true
}
}
33 changes: 33 additions & 0 deletions lib/vein.std/std/test/numeric.test.vein
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,41 @@ private static class NumericTest
Assert.equal(12 * two, 24);
Assert.equal(15 * two, 30);
}
[test]
public static fib_test(): void {
Fib(15);
return;
}

public static Fib(n: i32): i32
{
if (n < 2)
{
return n;
}
else
{
let n1 = Fib(n - 1);
let n2 = Fib(n - 2);
let n3 = n1 + n2;
return n3;
}
}
/*public static Fib(x: i32): i32 {
if (x == 0) {
return 0;
}

let prev = 0;
let next = 1;
for (let i = 1; i < x; i++)
{
let sum = prev + next;
prev = next;
next = sum;
}
return next;
}*/

[test]
public static i16_mod(): void
Expand Down
9 changes: 9 additions & 0 deletions runtime/common/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public static void ForEach<T>(this IEnumerable<T> enumerator, Action<T> actor)
{
foreach (var v in enumerator) actor(v);
}

public static IEnumerable<T> Pipe<T>(this IEnumerable<T> enumerator, Action<T> actor)
{
foreach (var v in enumerator)
{
actor(v);
yield return v;
}
}
}
9 changes: 4 additions & 5 deletions tools/compiler/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace vein;

using System.Text;
using compilation;
using MoreLinq;
using static Spectre.Console.AnsiConsole;


Expand All @@ -23,16 +22,16 @@ public static void Info(string text, BaseSyntax posed, DocumentDeclaration doc)

}

public static void EnqueueErrorsRange(IEnumerable<CompilationEventData> s) => s.Pipe(x => State.errors.Enqueue(x)).Consume();
public static void EnqueueInfosRange(IEnumerable<CompilationEventData> s) => s.Pipe(x => State.infos.Enqueue(x)).Consume();
public static void EnqueueWarnsRange(IEnumerable<CompilationEventData> s) => s.Pipe(x => State.warnings.Enqueue(x)).Consume();
public static void EnqueueErrorsRange(IEnumerable<CompilationEventData> s) => s.ForEach(x => State.errors.Enqueue(x));
public static void EnqueueInfosRange(IEnumerable<CompilationEventData> s) => s.ForEach(x => State.infos.Enqueue(x));
public static void EnqueueWarnsRange(IEnumerable<CompilationEventData> s) => s.ForEach(x => State.warnings.Enqueue(x));


public static void Info(string s) => MarkupLine($"[aqua]INFO[/]: {s}");
public static void Warn(string s) => MarkupLine($"[orange]WARN[/]: {s}");
public static void Error(string s) => MarkupLine($"[red]ERROR[/]: {s}");
public static void Error(Exception s) => WriteException(s);

public static void Info(string s, CompilationTarget t) => t.Logs.Info.Enqueue($"[aqua]INFO[/]: {s}");
public static void Warn(string s, CompilationTarget t) => t.Logs.Warn.Enqueue($"[orange]WARN[/]: {s}");
public static void Error(string s, CompilationTarget t) => t.Logs.Error.Enqueue($"[red]ERROR[/]: {s}");
Expand Down
3 changes: 1 addition & 2 deletions tools/compiler/compilation/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace vein.compilation;

using System.Security.Cryptography;
using System.Text;
using MoreLinq;

[ExcludeFromCodeCoverage]
public class Cache
Expand Down Expand Up @@ -59,7 +58,7 @@ public static void SaveAstAsset(CompilationTarget target)
var cacheFolder = target.Project.CacheDir.SubDirectory("ast");

if (!cacheFolder.Exists) cacheFolder.Create();
cacheFolder.EnumerateFiles("*.ast.json").Pipe(x => x.Delete()).Consume();
cacheFolder.EnumerateFiles("*.ast.json").ForEach(x => x.Delete());

foreach (var (key, val) in target.AST)
{
Expand Down
9 changes: 3 additions & 6 deletions tools/compiler/compilation/CompilationTask.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace vein.compilation;

using MoreLinq;
using vein;
using extensions;
using vein;
using pipes;
using project;
using stl;
Expand Down Expand Up @@ -131,8 +130,7 @@ await Parallel.ForEachAsync(collection, async (target, token) => {
.SelectMany(x => x.Artifacts)
.OfType<BinaryArtifact>()
.Select(x => target.Resolver.ResolveDep(x, list))
.Pipe(list.Add)
.Consume();
.ForEach(list.Add);

target.LoadedModules.AddRange(list);

Expand Down Expand Up @@ -255,8 +253,7 @@ private bool ProcessFiles(IReadOnlyCollection<FileInfo> files, IReadOnlyCollecti
.Pipe(GenerateStaticCtor)
.Pipe(ValidateInheritance)
.ToList()
.Pipe(x => x.clazz.Methods.OfExactType<MethodBuilder>().Pipe(PostgenerateBody).Consume())
.Consume();
.Pipe(x => x.clazz.Methods.OfExactType<MethodBuilder>().ForEach(PostgenerateBody));

Cache.SaveAstAsset(Target);
}
Expand Down
7 changes: 2 additions & 5 deletions tools/compiler/compilation/parts/classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ namespace vein.compilation;

using exceptions;
using extensions;
using MoreLinq;
using vein.reflection;
using static runtime.MethodFlags;

public partial class CompilationTask
Expand Down Expand Up @@ -47,14 +45,13 @@ public void LinkMetadata((ClassBuilder @class, MemberDeclarationSyntax member) x
owners
.Select(owner => FetchType(@class, owner, doc))
.Where(p => !@class.Parents.Contains(p))
.Pipe(p => @class.Parents.Add(p)).Consume();
.ForEach(p => @class.Parents.Add(p));
@class
.Parents
.ToArray() // copy emumerable
.SelectMany(x => x.Parents)
.Where(x => @class.Parents.Any(z => z.FullName == x.FullName))
.Pipe(v => @class.Parents.Remove(v))
.Consume();
.ForEach(v => @class.Parents.Remove(v));
}
private ClassFlags GenerateClassFlags(ClassDeclarationSyntax clazz)
{
Expand Down
4 changes: 1 addition & 3 deletions tools/compiler/compilation/parts/ctors.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace vein.compilation;

using MoreLinq;
using static runtime.VeinTypeCode;

public partial class CompilationTask
Expand Down Expand Up @@ -38,8 +37,7 @@ private void GenerateCtor(ClassBuilder @class, DocumentDeclaration doc, ClassDec

// emit calling based ctors
@class.Parents.Select(z => z.GetDefaultCtor()).Where(z => z != null)
.Pipe(z => gen.EmitThis().Emit(OpCodes.CALL, z))
.Consume();
.ForEach(z => gen.EmitThis().Emit(OpCodes.CALL, z));


var pregen = new List<(ExpressionSyntax exp, VeinField field)>();
Expand Down
1 change: 0 additions & 1 deletion tools/compiler/pipes/WriteOutputPipe.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace vein.pipes;

using fs;
using MoreLinq;

[ExcludeFromCodeCoverage]
public class WriteOutputPipe : CompilerPipeline
Expand Down

0 comments on commit c5e8bd6

Please sign in to comment.