From 8619f67b9118be7bd99b66d39af81aa6a638d5b1 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 6 Aug 2023 17:14:18 -0600 Subject: [PATCH] Get rid of extra runtime type checks --- Interpreter.cs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/Interpreter.cs b/Interpreter.cs index a1ba45a..6edb57d 100644 --- a/Interpreter.cs +++ b/Interpreter.cs @@ -63,16 +63,6 @@ public class Env public class Interpreter : AST.IExprVisitor { - 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 { 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 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; }