1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-21 01:18:02 +02:00
Beef/IDEHelper/Tests/src/Comptime.bf

134 lines
2.8 KiB
Beef
Raw Normal View History

2021-01-11 09:41:43 -08:00
using System;
using System.Diagnostics;
using System.Reflection;
2021-01-15 14:28:21 -08:00
using System.Collections;
2021-01-11 09:41:43 -08:00
namespace Tests
{
class Comptime
{
[AttributeUsage(.All)]
struct IFaceAAttribute : Attribute, IComptimeTypeApply
{
String mMemberName;
int32 mInitVal;
public int32 InitVal
{
set mut
{
mInitVal = value;
}
}
public this(String memberName)
{
mMemberName = memberName;
mInitVal = 0;
}
[Comptime]
public void ApplyToType(Type type)
{
Compiler.EmitTypeBody(type, scope $"""
2021-01-11 09:41:43 -08:00
public int32 m{mMemberName} = {mInitVal};
public int32 GetVal{mMemberName}() => mC;
""");
}
}
[AttributeUsage(.Method)]
struct LogAttribute : Attribute, IComptimeMethodApply
{
public static String gLog = new .() ~ delete _;
2021-01-11 09:41:43 -08:00
[Comptime]
public void ApplyToMethod(ComptimeMethodInfo method)
{
String emit = scope $"LogAttribute.gLog.AppendF($\"Called {method}";
for (var fieldIdx < method.ParamCount)
emit.AppendF($" {{ {method.GetParamName(fieldIdx)} }}");
2021-01-16 12:35:51 -08:00
emit.Append("\\n\");");
Compiler.EmitMethodEntry(method, emit);
2021-01-16 12:35:51 -08:00
if (var genericType = method.ReturnType as SpecializedGenericType)
{
if ((genericType.UnspecializedType == typeof(Result<>)) || (genericType.UnspecializedType == typeof(Result<,>)))
{
Compiler.EmitMethodExit(method, """
if (@return case .Err)
LogAttribute.gLog.AppendF($"Error: {@return}");
""");
}
}
2021-01-11 09:41:43 -08:00
}
}
[IFaceA("C", InitVal=345)]
class ClassA
{
public int mA = 123;
[OnCompile(.TypeInit), Comptime]
public static void Generate()
{
Compiler.EmitTypeBody(typeof(Self), """
2021-01-11 09:41:43 -08:00
public int32 mB = 234;
public int32 GetValB() => mB;
""");
}
}
2021-01-16 12:35:51 -08:00
enum MethodAErr
{
2021-01-16 12:35:51 -08:00
ErrorA,
ErrorB
}
2021-01-16 12:35:51 -08:00
[Log]
static Result<int, MethodAErr> MethodA(int a, int b)
{
return .Err(.ErrorB);
}
2021-01-16 06:26:55 -08:00
static Type GetBiggerType(Type t)
2021-01-15 14:28:21 -08:00
{
2021-01-16 06:26:55 -08:00
switch (t)
2021-01-15 14:28:21 -08:00
{
2021-01-16 06:26:55 -08:00
case typeof(int8): return typeof(int16);
case typeof(int16): return typeof(int32);
case typeof(int32): return typeof(int64);
case typeof(float): return typeof(double);
2021-01-15 14:28:21 -08:00
}
2021-01-16 06:26:55 -08:00
return null;
2021-01-15 14:28:21 -08:00
}
2021-01-16 06:26:55 -08:00
static TTo GetBigger<TFrom, TTo>(TFrom val) where TTo : comptype(GetBiggerType(typeof(TFrom))), operator explicit TFrom
2021-01-15 14:28:21 -08:00
{
2021-01-16 06:26:55 -08:00
return (.)val;
2021-01-15 14:28:21 -08:00
}
2021-01-11 09:41:43 -08:00
[Test]
public static void TestBasics()
{
ClassA ca = scope .();
Test.Assert(ca.mA == 123);
Test.Assert(ca.mB == 234);
Test.Assert(ca.GetValB() == 234);
Test.Assert(ca.mC == 345);
Test.Assert(ca.GetValC() == 345);
Compiler.Mixin("int val = 99;");
Test.Assert(val == 99);
2021-01-16 12:35:51 -08:00
MethodA(34, 45).IgnoreError();
Debug.Assert(LogAttribute.gLog == "Called Tests.Comptime.MethodA(int a, int b) 34 45\nError: Err(ErrorB)");
2021-01-15 14:28:21 -08:00
2021-01-16 06:26:55 -08:00
var v0 = GetBigger((int8)123);
Test.Assert(v0.GetType() == typeof(int16));
2021-01-11 09:41:43 -08:00
}
}
}