/// <summary> /// Adds a TFunction to the function dictionary. /// </summary> /// <param name="interpreter">The interpreter that the method is being called from.</param> /// <param name="function">The TFunction to be added to the function dictionary.</param> /// <returns>Null if the operation was successful, otherwise a TException.</returns> public static TException AddFunction(Interpreter interpreter, TFunction function) { if (function == null) { return(new TException(interpreter, "Null TFunction given")); } // Reject the function if it's already in the standard library TFunction existingFunction = StdLibrary.GetFunction(function.Name); if (existingFunction != null) { return(new TException(interpreter, "Standard library function with name '" + function.Name + "' already exists")); } // Add it to the function dictionary, overwriting another function with the same name if neccessary existingFunction = GetFunction(function.Name, false); if (existingFunction == null) { functions.Add(function.Name, function); } else { existingFunction.HardCodedFunction = null; existingFunction.CustomFunction = ""; existingFunction.Block = null; existingFunction.CopyFrom(function); } return(null); }
/// <summary> /// Searches for a Toast function with the specified name. /// </summary> /// <param name="functionName">The name of the Toast function to seach for.</param> /// <param name="includeStdLibrary">Whether the Toast standard library should be searched.</param> /// <returns>A TFunction if the operation was successful, otherwise null.</returns> public static TFunction GetFunction(string functionName, bool includeStdLibrary = true) { TFunction returnValue; if (functions.TryGetValue(functionName, out returnValue)) { return(returnValue); } return(includeStdLibrary ? StdLibrary.GetFunction(functionName) : null); }
/// <summary> /// Initialises the function dictionaries and loads the Toast standard library functions. /// </summary> public static void Init() { if (functions != null) { return; } functions = new Dictionary <string, TFunction>(); StdLibrary.Init(); }
public Machine(StdLibrary stdlibrary) { _NextTypeId = (int)VariableType.UserType; _Log = new Log(); _Globals = new TableObject(); // InitialiseDefaultTypes(); // Bind standard library to machine StandardLibary = stdlibrary; stdlibrary.RegisterLibrary(this); }
private void Do(StringBuilder log, string libFileName) { Action <string> printFunc = (s) => { log.AppendLine(s); }; var stdLib = new StdLibrary(printFunc); var uut = new Machine(stdLib); var newLib = new Library(); newLib.LoadFromFile(uut, libFileName); uut.ExecuteLibrary(newLib); }