InternalFunction
using CLanguage.Compiler;
using CLanguage.Parser;
using CLanguage.Syntax;
using CLanguage.Types;
using System;
using System.Runtime.CompilerServices;
namespace CLanguage.Interpreter
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class InternalFunction : BaseFunction
{
public InternalFunctionAction Action { get; set; }
public InternalFunction(string name, string nameContext, CFunctionType functionType)
{
base.Name = name;
base.NameContext = nameContext;
base.FunctionType = functionType;
Action = delegate {
};
}
public InternalFunction(MachineInfo machineInfo, string prototype, [System.Runtime.CompilerServices.Nullable(2)] InternalFunctionAction action = null)
{
Report report = new Report(null);
TranslationUnit translationUnit = new CParser().ParseTranslationUnit("_internal.h", prototype + ";", (string _, bool __) => null, report);
CCompiler cCompiler = new CCompiler(machineInfo, report);
cCompiler.Add(translationUnit);
cCompiler.Compile();
if (translationUnit.Functions.Count == 0)
throw new Exception("Failed to parse function prototype: " + prototype);
CompiledFunction compiledFunction = translationUnit.Functions[0];
base.Name = compiledFunction.Name;
base.NameContext = compiledFunction.NameContext;
base.FunctionType = compiledFunction.FunctionType;
if (action != null)
Action = action;
else
Action = delegate {
};
}
public override void Step(CInterpreter state, ExecutionFrame frame)
{
Action?.Invoke(state);
if (state.YieldedValue == 0)
state.Return();
}
}
}