InternalFunction
using CLanguage.Parser;
using CLanguage.Syntax;
using System;
namespace CLanguage.Interpreter
{
public class InternalFunction : BaseFunction
{
public InternalFunctionAction Action { get; set; }
public InternalFunction(MachineInfo machineInfo, string prototype, InternalFunctionAction action = null)
{
Report report = new Report(new Report.TextWriterPrinter(Console.Out));
CParser cParser = new CParser();
Preprocessor preprocessor = new Preprocessor(report);
preprocessor.AddCode("<Internal>", prototype + ";");
TranslationUnit translationUnit = cParser.ParseTranslationUnit(new Lexer(preprocessor));
Compiler compiler = new Compiler(machineInfo, report);
compiler.Add(translationUnit);
compiler.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.FunctionType = compiledFunction.FunctionType;
Action = action;
}
public override string ToString()
{
return base.Name;
}
public override void Step(CInterpreter state)
{
if (Action != null)
Action(state);
state.Return();
}
}
}