From 55ecd2ff710377c48cbc620be02db32cfcdc29c6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 20 Jul 2023 11:44:00 +0100 Subject: Add explicit locals --- .../shedlang/compiler/backends/llvm/NameAllocator.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/backends') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java b/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java index d8ca3fc2..7a1aba01 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/NameAllocator.java @@ -21,12 +21,7 @@ public record NameAllocator(PSet existingNames) { } public Result allocateFixedName(TPrefix prefix, String name) { - var fullName = prefix + "__" + name; - - if (existingNames.contains(fullName)) { - throw new RuntimeException("name is already allocated: " + fullName); - } - + var fullName = allocateFixedName(existingNames::contains, prefix, name); var newAllocator = new NameAllocator(existingNames.plus(fullName)); return new Result<>(fullName, newAllocator); } @@ -52,6 +47,16 @@ public record NameAllocator(PSet existingNames) { } } + public static String allocateFixedName(Predicate isUsed, TPrefix prefix, String name) { + var fullName = prefix + "__" + name; + + if (isUsed.test(fullName)) { + throw new RuntimeException("name is already allocated: " + fullName); + } + + return fullName; + } + public record Result(String name, NameAllocator allocator) { } } -- cgit v1.2.3