Skip to content

Commit

Permalink
fix: The Python compiler emits reserved names for datatypes (dafny-la…
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomadge authored Dec 9, 2023
1 parent 4faa561 commit 5f30d1b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
62 changes: 14 additions & 48 deletions Source/DafnyCore/Compilers/Python/Compiler-python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ string FormatDefaultTypeParameterValue(TopLevelDecl tp) {
protected override string True { get => "True"; }
protected override string False { get => "False"; }
protected override string Conj { get => "and"; }
private static readonly IEnumerable<string> Keywords = new HashSet<string> { "False", "None", "True", "and", "as"
, "assert", "async", "await", "break", "class", "continue", "def", "del", "enum", "elif", "else", "except"
, "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass"
, "raise", "return", "try", "while", "with", "yield" };
protected override void EmitHeader(Program program, ConcreteSyntaxTree wr) {
wr.WriteLine($"# Dafny program {program.Name} compiled into Python");
if (Options.IncludeRuntime) {
Expand Down Expand Up @@ -122,53 +126,15 @@ private void EmitImports(string moduleName, ConcreteSyntaxTree wr) {
protected override string GetHelperModuleName() => DafnyRuntimeModule;

private static string MangleName(string name) {
switch (name) {
case "False":
case "None":
case "True":
case "and":
case "as":
case "assert":
case "async":
case "await":
case "break":
case "class":
case "continue":
case "def":
case "del":
case "enum":
case "elif":
case "else":
case "except":
case "finally":
case "for":
case "from":
case "global":
case "if":
case "import":
case "in":
case "is":
case "lambda":
case "nonlocal":
case "not":
case "or":
case "pass":
case "raise":
case "return":
case "try":
case "while":
case "with":
case "yield":
name = $"{name}_";
break;
default:
while (name.StartsWith("_")) {
name = $"{name[1..]}_";
}
if (name.Length > 0 && char.IsDigit(name[0])) {
name = $"d_{name}";
}
break;
if (Keywords.Contains(name)) {
name = $"{name}_";
} else {
while (name.StartsWith("_")) {
name = $"{name[1..]}_";
}
if (name.Length > 0 && char.IsDigit(name[0])) {
name = $"d_{name}";
}
}
return name;
}
Expand Down Expand Up @@ -262,7 +228,7 @@ protected override IClassWriter DeclareDatatype(DatatypeDecl dt, ConcreteSyntaxT
return null;
}

var DtT = dt.GetCompileName(Options);
var DtT = IdProtect(dt.GetCompileName(Options));

var baseClasses = dt.ParentTypeInformation.UniqueParentTraits().Any()
? $"({dt.ParentTypeInformation.UniqueParentTraits().Comma(trait => TypeName(trait, wr, dt.tok))})"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// RUN: %testDafnyForEachCompiler "%s"

datatype None = None
Empty file.
1 change: 1 addition & 0 deletions docs/dev/news/4843.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Python compiler emits reserved names for datatypes

0 comments on commit 5f30d1b

Please sign in to comment.