Skip to content

Commit

Permalink
- Added ErrorMetadataAttribute which tags certain Yolol emulator me…
Browse files Browse the repository at this point in the history
…thods with information about when they will throw errors

 - Tagged division and string decrementing with new attribute
 - Ordered state printing in cmdline emulator interface
  • Loading branch information
martindevans committed Oct 12, 2020
1 parent 0bf4763 commit da37857
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 15 deletions.
34 changes: 34 additions & 0 deletions Yolol/Execution/Attributes/ErrorMetadataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace Yolol.Execution.Attributes
{
/// <summary>
/// Tags an operator overload with methods that check if it might/will throw
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class ErrorMetadataAttribute
: Attribute
{
public bool IsInfallible { get; }
public string? WillThrow { get; }

/// <summary>
/// </summary>
/// <param name="isInfallible">If true, indicates that this method can never throw a runtime error</param>
public ErrorMetadataAttribute(bool isInfallible)
{
IsInfallible = isInfallible;
WillThrow = null;
}

/// <summary>
///
/// </summary>
/// <param name="willThrow">The name of a method which checks if the operation (given value(s)) will throw.</param>
public ErrorMetadataAttribute(string willThrow)
{
IsInfallible = false;
WillThrow = willThrow;
}
}
}
23 changes: 22 additions & 1 deletion Yolol/Execution/Number.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using Yolol.Execution.Attributes;

namespace Yolol.Execution
{
Expand Down Expand Up @@ -186,6 +187,24 @@ public static explicit operator float(Number n)
}


internal static bool WillDivThrow(Number _, Value r)
{
if (r.Type == Type.String)
return true;
return r.Number == 0;
}

internal static bool WillDivThrow(Number _, Number r)
{
return r == 0;
}

internal static bool WillDivThrow(Number _, bool r)
{
return !r;
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Number operator /(Number l, Number r)
{
if (r._value == 0)
Expand All @@ -194,11 +213,12 @@ public static explicit operator float(Number n)
return new Number((l._value * Scale) / r._value);
}

public static StaticError operator /(Number l, YString r)
public static StaticError operator /(Number _, YString __)
{
return new StaticError("Attempted to divide by a string");
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Value operator /(Number l, Value r)
{
if (r.Type == Type.String)
Expand All @@ -207,6 +227,7 @@ public static explicit operator float(Number n)
return l / r.Number;
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Number operator /(Number l, bool r)
{
return l / (Number)r;
Expand Down
28 changes: 28 additions & 0 deletions Yolol/Execution/Value.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Yolol.Execution.Attributes;
using Yolol.Grammar.AST.Expressions;

namespace Yolol.Execution
Expand Down Expand Up @@ -417,6 +418,31 @@ public override int GetHashCode()
}


internal static bool WillDivThrow(Value l, Value r)
{
if (l.Type == Type.Number && r.Type == Type.Number)
return Number.WillDivThrow(l.Number, r.Number);
else
return true;
}

internal static bool WillDivThrow(Value l, Number r)
{
if (l.Type == Type.Number)
return Number.WillDivThrow(l.Number, r);
else
return true;
}

internal static bool WillDivThrow(Value l, bool r)
{
if (l.Type == Type.Number)
return Number.WillDivThrow(l.Number, (Number)r);
else
return true;
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Number operator /(Value left, Value right)
{
if (left.Type == Type.Number && right.Type == Type.Number)
Expand All @@ -430,6 +456,7 @@ public override int GetHashCode()
throw new ExecutionException("Attempted to divide a string");
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Number operator /(Value left, Number right)
{
if (left.Type == Type.Number)
Expand All @@ -438,6 +465,7 @@ public override int GetHashCode()
throw new ExecutionException("Attempted to divide a string");
}

[ErrorMetadata(nameof(WillDivThrow))]
public static Number operator /(Value left, bool right)
{
if (left.Type == Type.Number)
Expand Down
31 changes: 19 additions & 12 deletions Yolol/Execution/YString.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using Yolol.Execution.Attributes;

namespace Yolol.Execution
{
Expand Down Expand Up @@ -264,69 +265,75 @@ private static int CompareStringSpans(in YString left, in YString right)
}


public static StaticError operator *(YString l, YString r)
public static StaticError operator *(YString _, YString __)
{
throw new ExecutionException("Attempted to multiply a string");
}

public static StaticError operator *(YString l, Number r)
public static StaticError operator *(YString _, Number __)
{
throw new ExecutionException("Attempted to multiply a string");
}

public static StaticError operator *(YString l, Value r)
public static StaticError operator *(YString _, Value __)
{
throw new ExecutionException("Attempted to multiply a string");
}

public static StaticError operator *(YString l, bool r)
public static StaticError operator *(YString _, bool __)
{
throw new ExecutionException("Attempted to multiply a string");
}


public static StaticError operator /(YString l, YString r)
public static StaticError operator /(YString _, YString __)
{
throw new ExecutionException("Attempted to divide a string");
}

public static StaticError operator /(YString l, Number r)
public static StaticError operator /(YString _, Number __)
{
throw new ExecutionException("Attempted to divide a string");
}

public static StaticError operator /(YString l, Value r)
public static StaticError operator /(YString _, Value __)
{
throw new ExecutionException("Attempted to divide a string");
}

public static StaticError operator /(YString l, bool r)
public static StaticError operator /(YString _, bool __)
{
throw new ExecutionException("Attempted to divide a string");
}


public static StaticError operator %(YString l, YString r)
public static StaticError operator %(YString _, YString __)
{
throw new ExecutionException("Attempted to mod a string");
}

public static StaticError operator %(YString l, Number r)
public static StaticError operator %(YString _, Number __)
{
throw new ExecutionException("Attempted to mod a string");
}

public static StaticError operator %(YString l, Value r)
public static StaticError operator %(YString _, Value __)
{
throw new ExecutionException("Attempted to mod a string");
}

public static StaticError operator %(YString l, bool r)
public static StaticError operator %(YString _, bool __)
{
throw new ExecutionException("Attempted to mod a string");
}


internal static bool WillDecThrow(YString str)
{
return str.Length == 0;
}

[ErrorMetadata(nameof(WillDecThrow))]
public static YString operator --(YString value)
{
if (value.Length == 0)
Expand Down
2 changes: 1 addition & 1 deletion Yolol/Yolol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/martindevans/Yolol</PackageProjectUrl>
<RepositoryUrl>https://github.com/martindevans/Yolol</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>9.7.0</Version>
<Version>9.8.0</Version>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
2 changes: 1 addition & 1 deletion YololEmulator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static void Run(Options options)
// Print machine state
Console.Title = $"Elapsed Game Time: {TimeSpan.FromMilliseconds(200 * lines).TotalSeconds.ToString(CultureInfo.CurrentCulture)}s";
Console.WriteLine("State:");
foreach (var (key, value) in st)
foreach (var (key, value) in st.OrderBy(a => a.Key))
{
if (value.Value.Type == Yolol.Execution.Type.String)
Console.WriteLine($" | {key} = \"{value}\"");
Expand Down

0 comments on commit da37857

Please sign in to comment.