Skip to content

Commit

Permalink
idiot-proof the design
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 24, 2023
1 parent a2747e6 commit bf40013
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ private FnStatement FnStatement()
}

var (line, col) = Match(Token.Fn);
if (_functionDescent > 0)
{
throw new ParserException(
"Named function definitions may only appear outside of other functions.",
(line, col)
);
}

var functionIdentifier = Identifier();
var parameterList = ParameterList();

Expand Down
117 changes: 0 additions & 117 deletions VirtualMachine/Ceres/ExecutionEngine/Diagnostics/Script.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public override void Visit(FnStatement fnStatement)
if (wasExistingReplaced)
{
Log.EmitWarning(
$"Redefining chunk '{replacedChunk.Name}' previously defined in {replacedChunk.DebugDatabase.DefinedInFile} on line {replacedChunk.DebugDatabase.DefinedOnLine}.",
$"Named function '{replacedChunk.Name}' defined on line {fnStatement.Line} is " +
$"re-defining a previously defined function of the same name in {replacedChunk.DebugDatabase.DefinedInFile} on line {fnStatement.Line}.",
CurrentFileName,
EvilMessageCode.FnStatementRedefinedExistingChunk,
fnStatement.Line,
Expand Down
10 changes: 1 addition & 9 deletions VirtualMachine/Ceres/TranslationEngine/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public partial class Compiler : AstVisitor

private readonly Stack<Chunk> _chunks = new();
private Dictionary<string, List<AttributeProcessor>> _attributeProcessors = new();
private List<IncludeProcessor> _includeProcessors = new();

private int _blockDescent;
private readonly Stack<Loop> _loopDescent = new();
Expand Down Expand Up @@ -71,6 +70,7 @@ public Compiler(bool optimizeCodeGeneration = false)

public Chunk Compile(string source, string fileName = "")
{
CurrentFileName = fileName;
var parser = new Parser();

try
Expand Down Expand Up @@ -214,14 +214,6 @@ public void RegisterAttributeProcessor(string attributeName, AttributeProcessor
list.Add(processor);
}

public void RegisterIncludeProcessor(IncludeProcessor processor)
{
if (!_includeProcessors.Contains(processor))
{
_includeProcessors.Add(processor);
}
}

private DynamicValue ExtractConstantValueFrom(AstNode valueNode)
{
if (valueNode is BooleanConstant boolConst)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@ public override string ToString()
sb.Append($"EV{MessageCode:D4}");
}

sb.Append($" :: {Severity} :: {Message}");

if (FileName != null)
{
sb.Append($" :: {FileName}");
}

sb.Append($" :: {Severity}");
if (Line > 0)
{
sb.Append($" (line {Line}");
sb.Append($" :: line {Line}");

if (Column > 0)
{
sb.Append($", column {Column}");
}

sb.Append(")");
}

if (!string.IsNullOrEmpty(FileName))
{
sb.Append($" :: {FileName}");
}

sb.Append($" :: {Message}");
return sb.ToString();
}
}
Expand Down
12 changes: 0 additions & 12 deletions VirtualMachine/Ceres/TranslationEngine/IncludeProcessor.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<None Update="tests\018_closures.vil">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="tests\019_include.vil">
<None Update="tests\019_require.vil.disabled">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="tests\lib\test.vil">
Expand Down
90 changes: 16 additions & 74 deletions VirtualMachine/Tests/Ceres.LanguageTests/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TestRunner
private TextWriter TextOut { get; }
private EvilRuntime Runtime { get; }

private Dictionary<string, Script> TestScripts { get; } = new();
private Dictionary<string, Chunk> TestRoots { get; } = new();
private Dictionary<string, List<(Chunk TestChunk, string FailureReason)>> FailureLog { get; } = new();

public TestRunner(string testDirectory, CeresVM vm, TextWriter? textOut = null)
Expand All @@ -44,7 +44,6 @@ private void CompileTests()
.ToList();

var compiler = new Compiler();
compiler.RegisterIncludeProcessor(IncludeProcessor);
compiler.RegisterAttributeProcessor("approximate", AttributeProcessors.ApproximateAttribute);
compiler.RegisterAttributeProcessor("disasm", AttributeProcessors.DisasmAttribute);

Expand All @@ -54,8 +53,8 @@ private void CompileTests()
try
{
TextOut.Write($"Compiling test '{path}'...");
var script = compiler.Compile(source, Path.GetFullPath(path));
TestScripts.Add(path, script);
var rootChunk = compiler.Compile(source, Path.GetFullPath(path));
TestRoots.Add(path, rootChunk);
TextOut.WriteLine(" [ PASS ]");

if (compiler.Log.HasAnyMessages)
Expand All @@ -74,71 +73,28 @@ private void CompileTests()
TextOut.WriteLine();
}

private IEnumerable<Chunk> IncludeProcessor(Compiler compiler, Script script, string path, out bool isRedundantInclude)
{
isRedundantInclude = false;

var fullPathToInclude = Path.Combine(
Path.GetDirectoryName(compiler.CurrentFileName)!,
path
);

if (!File.Exists(fullPathToInclude))
{
throw new FileNotFoundException("Cannot find the included file.", fullPathToInclude);
}

if (fullPathToInclude == compiler.CurrentFileName)
{
throw new InvalidOperationException($"Recursive include detected in '{compiler.CurrentFileName}.");
}

if (_includeCache.TryGetValue(fullPathToInclude, out var chunks))
{
isRedundantInclude = true;
return chunks;
}

try
{
var includeCompiler = new Compiler();
includeCompiler.RegisterIncludeProcessor(IncludeProcessor);

var includedScript = includeCompiler.Compile(File.ReadAllText(fullPathToInclude), fullPathToInclude);


chunks = includedScript.Chunks;
_includeCache.Add(fullPathToInclude, chunks);
return chunks;
}
catch (CompilerException ce)
{
throw new TestBuildPhaseException(
$"Failed to compile the included file '{path}':\n{ce.Log}", ce
);
}
}

public async Task RunTests()
{
foreach (var testdesc in TestScripts)
foreach (var testRoot in TestRoots)
{
var passed = 0;
var failed = 0;
var ignored = 0;

var path = testdesc.Key;
var path = testRoot.Key;
TextOut.WriteLine($"--- [TEST '{path}'] ---");

var testScript = testdesc.Value;
var testRootChunk = testRoot.Value;
var testChunks = new List<Chunk>();
foreach (var (name, id) in testRootChunk.NamedSubChunkLookup)
{
var chunk = testRootChunk.SubChunks[id];
if (chunk.HasAttribute("test"))
{
testChunks.Add(chunk);
}

var onInitChunks = testScript.Chunks.Where(
x => x.HasAttribute("on_init")
);

var testChunks = testScript.Chunks.Where(
x => x.HasAttribute("test")
).ToList();
}

if (testChunks.Count == 0)
{
Expand All @@ -155,21 +111,7 @@ public async Task RunTests()
Runtime.RegisterBuiltInModules();
Runtime.RegisterModule<AssertModule>(out _);

var nonTestChunks = testScript.Chunks.Where(
x => !x.HasAttribute("test")
&& !x.HasAttribute("on_init")
).ToList();

foreach (var chunk in nonTestChunks)
{
VM.Global.Set(chunk.Name, chunk);
}

foreach (var onInitChunk in onInitChunks)
{
TextOut.WriteLine($"Running test set-up function '{onInitChunk.Name}'...");
VM.MainFiber.Schedule(onInitChunk);
}
VM.MainFiber.Schedule(testRootChunk);
VM.MainFiber.BlockUntilFinished();

for (var i = 0; i < testChunks.Count; i++)
Expand Down
Loading

0 comments on commit bf40013

Please sign in to comment.