Skip to content

Commit

Permalink
print compiler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMagzuz authored and Bliztle committed Apr 10, 2024
1 parent f4c218b commit b0be2cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion SocietalConstructionTool/Runtime/Trace/JsonFileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public void OnExit()
file.Write(Encoding.UTF8.GetBytes(_builder.ToString()));
}

public void OnTick(IRuntimeContext context) => _builder.Append(JsonSerializer.Serialize(context.AgentHandler.Agents));
public void OnTick(IRuntimeContext context) => _builder.Append(JsonSerializer.Serialize(context.AgentHandler.Agents) + '\n');
}
}
34 changes: 23 additions & 11 deletions SocietalConstructionTool/SctRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class SctRunner
* <param name="filename">The path of the SCT source file</param>
* <returns>The resulting C# source, or null if compilation failed</returns>
*/
public static string? CompileSct(string filename)
public static (string? outputText, IEnumerable<CompilerError> errors) CompileSct(string filename)
{
// TODO: Add error handling
string input = File.ReadAllText(filename);
Expand Down Expand Up @@ -56,13 +56,24 @@ public static class SctRunner

if (errors.Count > 0)
{
string errorsText = string.Join('\n', (IEnumerable<CompilerError>)errors);
// TODO: Better error handling
throw new ArgumentException($"Could not compile SCT file.\nCompilation errors:\n{errorsText}");
return (null, errors);
}

// TODO: It's a little evil that we hold the entire generated source text in memory, but it is what it is
return translator.Root?.NormalizeWhitespace().ToFullString();
if (translator.Root is null)
{
throw new InvalidOperationException("Translation failed");
}


// HACK: We do something bad in our translator somewhere that means that we produce a syntax tree that is not valid,
// but its string representation is.
// Take for example the expression `foo.bar`.
// The correct way to represent this would (roughly) be: `(member_access (id "foo") (id "bar"))`
// However, writing it as `(id "foo.bar")` produces the same string, but causes problems when C#
// tries to traverse the tree. As far as I can tell, the error we get isn't very telling, so for now we just deal with it.
var outputText = translator.Root.NormalizeWhitespace().ToFullString();

return (outputText, []);
}

/**
Expand Down Expand Up @@ -135,17 +146,18 @@ public static void CompileAndRun(string[] filenames, IOutputLogger logger)
{
// TODO: Actually concatenate the files. Isak is working on this.
var filename = filenames[0];
var outputSource = CompileSct(filename);
if (outputSource is null)
var (outputText, errors) = CompileSct(filename);

if (errors.Any() || outputText is null)
{
// TODO: Error reporting
Console.Error.WriteLine("Compiling SCT failed");
Console.Error.WriteLine("Compilation failed:");
Console.Error.WriteLine(string.Join('\n', errors));
return;
}

// Write the compiled C# into memory
MemoryStream memoryStream = new();
var result = Emit(outputSource, memoryStream);
var result = Emit(outputText, memoryStream);

if (!result.Success)
{
Expand Down

0 comments on commit b0be2cc

Please sign in to comment.