Skip to content

Commit

Permalink
add more functions to string library
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 18, 2023
1 parent cc972d6 commit 8cbb6fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
12 changes: 11 additions & 1 deletion FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public partial class EvmFrontEnd
{ "v|version", "display compiler and VM version information.", (v) => _displayVersionAndQuit = v != null },
{ "I|include-dir=", "add include directory to the list of search paths.", (I) => _includeHandler.AddIncludeSearchPath(I) },
{ "g|gen-docs", "generate documentation for all detected native modules.", (g) => _generateModuleDocsAndQuit = g != null },
{ "d|disasm", "disassemble the compiled script.", (d) => _disassembleCompiledScript = d != null }
{ "d|disasm", "disassemble the compiled script.", (d) => _disassembleCompiledScript = d != null },
{ "o|optimize", "optimize generated code.", (o) => _optimizeCode = o != null }
};

private static bool _displayHelpAndQuit;
private static bool _displayVersionAndQuit;
private static bool _generateModuleDocsAndQuit;
private static bool _disassembleCompiledScript;
private static bool _optimizeCode;

public async Task Run(string[] args)
{
Expand All @@ -59,6 +61,11 @@ public async Task Run(string[] args)
Terminate();
}

if (_optimizeCode)
{
_compiler.OptimizeCodeGeneration = true;
}

if (!extra.Any())
{
Terminate(
Expand Down Expand Up @@ -305,6 +312,9 @@ private void CrashHandler(Fiber fiber, Exception exception)
scriptTop = top.As<ScriptStackFrame>();
}

var dd = scriptTop.Chunk.DebugDatabase;

sb.AppendLine($"{dd.DefinedInFile}:{dd.GetLineForIP((int)scriptTop.PreviousOpCodeIP)}: {exception.Message}");
sb.AppendLine($"Runtime error in fiber {fiberIndex}, function {scriptTop.Chunk.Name} (def. in {scriptTop.Chunk.DebugDatabase.DefinedInFile}:{scriptTop.Chunk.DebugDatabase.DefinedOnLine}): {exception.Message}");
sb.AppendLine();
sb.AppendLine("Stack trace:");
Expand Down
22 changes: 21 additions & 1 deletion VirtualMachine/Ceres.Runtime/Modules/StringModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ private static DynamicValue ToCharacter(Fiber _, params DynamicValue[] args)

return ((char)charCode).ToString();
}

[RuntimeModuleFunction("code")]
private static DynamicValue ToCharacterCode(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(1)
.ExpectCharAt(0, out var character);

return (ushort)character;
}

[RuntimeModuleFunction("replace")]
private static DynamicValue Replace(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(3)
.ExpectStringAt(0, out var str)
.ExpectStringAt(1, out var toReplace)
.ExpectStringAt(2, out var with);

return str.Replace(toReplace, with);
}

[RuntimeModuleFunction("bytes")]
[EvilDocFunction(
Expand Down Expand Up @@ -140,7 +160,7 @@ private static DynamicValue Join(Fiber _, params DynamicValue[] args)
return string.Join(separator, args.Skip(1).Select(x => x.ConvertToString().String!));
}

[RuntimeModuleFunction("rep")]
[RuntimeModuleFunction("repeat")]
[EvilDocFunction(
"Repeats the provided String `count` times.",
Returns = "A String containing `str` repeated `count` times.",
Expand Down
2 changes: 1 addition & 1 deletion VirtualMachine/Ceres/ExecutionEngine/Concurrency/Fiber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public string StackTrace(bool skipNativeFrames)

if (ssf.Chunk.DebugDatabase.DefinedOnLine > 0)
{
sb.Append($": line {ssf.Chunk.DebugDatabase.GetLineForIP((int)ssf.PreviousOpCodeIP)}");
sb.Append($": line {ssf.Chunk.DebugDatabase.GetLineForIP((int)ssf.PreviousOpCodeIP)} (IP: {ssf.PreviousOpCodeIP:X8})");
}

sb.AppendLine();
Expand Down

0 comments on commit 8cbb6fe

Please sign in to comment.