Skip to content

Commit

Permalink
fix error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 24, 2023
1 parent bf40013 commit d061999
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace EVIL.Grammar.Parsing
{
public partial class Parser
{
private IndexerExpression Indexing(Expression indexable)
private IndexerExpression IndexerExpression(Expression indexable)
{
int line, col;
Expression indexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EVIL.Grammar.Parsing
{
public partial class Parser
{
private InvocationExpression FunctionCall(Expression callee)
private InvocationExpression InvocationExpression(Expression callee)
{
var (line, col) = (_lexer.State.Line, _lexer.State.Column);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ private Expression PostfixExpression()

if (token.Type == TokenType.LParenthesis)
{
node = FunctionCall(node);
node = InvocationExpression(node);
}
else if (token.Type == TokenType.LBracket || token.Type == TokenType.Dot)
{
node = Indexing(node);
node = IndexerExpression(node);
}
else if (token.Type == TokenType.DoubleColon)
{
Expand Down
2 changes: 1 addition & 1 deletion Core/EVIL.Grammar/Parsing/Statements/Parser.EachLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private EachStatement EachLoop()
if (CurrentToken == Token.Val)
{
throw new ParserException(
"Each-loop variables must be `rw'.",
"Each-loop variables must be 'rw'.",
(_lexer.State.Line, _lexer.State.Column)
);
}
Expand Down
2 changes: 2 additions & 0 deletions FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public async Task Run(string[] args)
RegisterAllModules();
SetStandardGlobals(scriptPath);

_runtime.RegisterBuiltInFunctions();

_vm.Scheduler.SetDefaultCrashHandler(CrashHandler);
_vm.MainFiber.SetCrashHandler(CrashHandler);
_vm.Start();
Expand Down
7 changes: 7 additions & 0 deletions VirtualMachine/Ceres.Runtime/Ceres.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
<ItemGroup>
<ProjectReference Include="..\Ceres\Ceres.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="ScriptBuiltins\dofile.vil" />
<EmbeddedResource Include="ScriptBuiltins\dofile.vil">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
39 changes: 38 additions & 1 deletion VirtualMachine/Ceres.Runtime/EvilRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Ceres.ExecutionEngine;
using Ceres.ExecutionEngine.Collections;
using Ceres.ExecutionEngine.Diagnostics;
using Ceres.ExecutionEngine.TypeSystem;
using Ceres.Runtime.Extensions;
using Ceres.Runtime.Modules;
using Ceres.TranslationEngine;

namespace Ceres.Runtime
{
public sealed class EvilRuntime
{
private readonly CeresVM _vm;
private readonly Compiler _compiler;

private Table Global => _vm.Global;

public EvilRuntime(CeresVM vm)
{
_vm = vm;
_compiler = new Compiler();
}

public List<Chunk> RegisterBuiltInFunctions()
{
var scriptNames = Assembly
.GetExecutingAssembly()
.GetManifestResourceNames()
.Where(x => x.StartsWith("Ceres.Runtime.ScriptBuiltins"));

var list = new List<Chunk>();

foreach (var scriptName in scriptNames)
{
using var stream = Assembly
.GetExecutingAssembly()
.GetManifestResourceStream(scriptName)!;

using var streamReader = new StreamReader(stream);

var text = streamReader.ReadToEnd();
var root = _compiler.Compile(text, $"builtin::{scriptName}");

foreach (var (name, id) in root.NamedSubChunkLookup)
{
list.Add(root.SubChunks[id]);
_vm.Global.Set(name, root.SubChunks[id]);
}
}

return list;
}

public List<RuntimeModule> RegisterBuiltInModules()
{
var modules = new List<RuntimeModule>();

modules.Add(RegisterModule<ArrayModule>(out _));
modules.Add(RegisterModule<ConvertModule>(out _));
modules.Add(RegisterModule<CoreModule>(out _));
Expand Down
14 changes: 14 additions & 0 deletions VirtualMachine/Ceres.Runtime/ScriptBuiltins/dofile.vil
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn dofile(path) {
if (!fs.file.exists(path)) -> core.error(
"Couldn't find a file at '" + @path + "'."
);

val source = fs.file.get_text(path);
val result = evil.compile(source, path);

if (!result || !result.success) -> core.error(
"Failed to compile the file '" + @path + "': " + @result.message
);

ret result.chunk();
}
17 changes: 10 additions & 7 deletions VirtualMachine/Ceres/TranslationEngine/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using EVIL.Grammar.AST.Base;
using EVIL.Grammar.AST.Constants;
using EVIL.Grammar.AST.Miscellaneous;
using EVIL.Grammar.AST.Statements;
using EVIL.Grammar.AST.Statements.TopLevel;
using EVIL.Grammar.Parsing;
using EVIL.Grammar.Traversal;
using EVIL.Lexical;
Expand Down Expand Up @@ -110,10 +112,10 @@ public Chunk Compile(ProgramNode programNode, string fileName = "")
CurrentFileName = fileName;
_closedScopes.Clear();
_chunks.Clear();

return InRootChunkDo(() => Visit(programNode));
}

private Chunk InRootChunkDo(Action action)
{
_rootChunk = new Chunk("!_root_chunk");
Expand Down Expand Up @@ -194,13 +196,14 @@ public override void Visit(AstNode node)
node = expression.Reduce();
}

base.Visit(node);

if (_chunks.Any())
if (_blockDescent > 0 || _chunks.Count == 1)
{
var location = LocationStack.Pop();
Chunk.DebugDatabase.AddDebugRecord(location.Line, Chunk.CodeGenerator.LastOpCodeIP);
Chunk.DebugDatabase.AddDebugRecord(Line, Chunk.CodeGenerator.LastOpCodeIP);
}

base.Visit(node);

Chunk.DebugDatabase.AddDebugRecord(Line, Chunk.CodeGenerator.LastOpCodeIP);
}

public void RegisterAttributeProcessor(string attributeName, AttributeProcessor processor)
Expand Down

0 comments on commit d061999

Please sign in to comment.