Get rid of extra runtime type checks

This commit is contained in:
Brandon Dyck 2023-08-06 17:14:18 -06:00
parent 93215cd294
commit 8619f67b91

View File

@ -63,16 +63,6 @@ public class Env
public class Interpreter : AST.IExprVisitor<Env, object>
{
private static void checkTypesEqual(object a, object b)
{
var aType = a.GetType();
var bType = b.GetType();
if (aType != bType)
{
throw new Exception("Type mismatch: {aType} != {bType}.");
}
}
private class PatternMismatchException : Exception
{
public readonly Token Start;
@ -203,7 +193,6 @@ public class Interpreter : AST.IExprVisitor<Env, object>
{
throw new ArgumentException($"no such field: {name}");
}
checkTypesEqual(value, values.Peek());
return new Record { Fields = Fields.SetItem(name, values!.Pop().Push(value)) };
}
@ -470,13 +459,7 @@ public class Interpreter : AST.IExprVisitor<Env, object>
List l = List.Empty;
foreach (var itemExpr in expr.Elements)
{
var item = evaluate(env, itemExpr);
if (!l.IsEmpty)
{
try { checkTypesEqual(l[0], item); }
catch { throw new RuntimeError(itemExpr.Start, "List items must all have same type."); }
}
l = l.Add(item);
l = l.Add(evaluate(env, itemExpr));
}
return l;
}