private LayeObject Callback__require(LayeState state, LayeObject ths, params LayeObject[] args) { // Vars we need: LayeKit thisKit = null; string originalRequirePath = null; // Try to get these: if (args.Length < 2) throw new ArgumentException("args"); // TODO throw an exception in the state. thisKit = args[0] as LayeKit; originalRequirePath = (args[1] as LayeString)?.value; // Check if we succeeded: if (thisKit == null || originalRequirePath == null) throw new ArgumentException("kit || requirePath"); // TODO throw an exception in the state. // Get the name of the kit: // Load up the kit, or error if no kit can be found: LayeKit kit; switch (originalRequirePath) { case "std": kit = state.std; break; default: // Determine the actual path to require: var requirePath = originalRequirePath.Replace('.', '\\') + ".laye"; var newRequirePath = Path.Combine(thisKit.fileLocation, requirePath); if (File.Exists(newRequirePath)) { // Compile the kit and run it: try { Compile(newRequirePath, null, out kit); } catch (CompilerException e) { e.log.Print(); throw e; } kit.Run(state); } else { state.RaiseException("Failed to load kit '{0}'.", originalRequirePath); return NULL; } break; } // Load that kit into this kits global state: thisKit.Use(state, kit, originalRequirePath); // This is void. return NULL; }
internal KitStd(LayeState state) { // std types LayeTypeDef[] types = { LayeBool.TYPE, LayeClosure.TYPE, LayeFloat.TYPE, LayeGenerator.TYPE, LayeInt.TYPE, LayeKit.TYPE, LayeList.TYPE, LayeObject.TYPE, LayeString.TYPE, LayeSymbol.TYPE, LayeTuple.TYPE, LayeTypeDef.TYPE }; foreach (var type in types) this[state, type.name] = type; this[state, "print"] = (LayeCallback)Callback__print; this[state, "println"] = (LayeCallback)Callback__println; this[state, "require"] = (LayeCallback)Callback__require; }
private LayeObject Callback__println(LayeState state, LayeObject ths, params LayeObject[] args) { var builder = new StringBuilder(); for (var i = 0; i < args.Length; i++) { if (i > 0) builder.Append(" "); var value = args[i]; builder.Append(value.ToString(state)); } stdout.WriteLine(builder.ToString()); return NULL; }
/// <summary> /// Attempts to resume this generator. /// All this does is check that the state is valid (not already running /// or dead) and raises exceptions if in an invalid state. /// The state is set to <c>State.RUNNING</c> on success. /// Returns false if exceptions were thrown, true otherwise. /// </summary> /// <param name="state"></param> /// <returns></returns> protected bool TryResume(LayeState state) { if (this.state == State.RUNNING) { state.RaiseException("Attempt to resume a running generator."); return false; } if (this.state == State.DEAD) { state.RaiseException("Attempt to resume a dead generator."); return false; } this.state = State.RUNNING; return true; }
public abstract override LayeObject InvokeAsMethod(LayeState state, LayeObject ths, params LayeObject[] args);
public override LayeObject Invoke(LayeState state, params LayeObject[] args) { return InvokeAsMethod(state, null, args); }
/// <summary> /// Returns <c>true</c> if this generator resumed, even if the function returned, false if the resume failed. /// If result is <c>null</c>, either resuming failed or the generator died this cycle. /// </summary> /// <param name="state"></param> /// <param name="result"></param> /// <returns></returns> public abstract bool Resume(LayeState state, out LayeObject result);
public override LayeObject InvokeAsMethod(LayeState state, LayeObject ths, params LayeObject[] args) { var frame = state.stack.NewFrame(closure, ths, args); return new LayeClosureGenerator(frame, closure, ths); }
public override bool Resume(LayeState state, out LayeObject result) { if (!TryResume(state)) { result = null; return false; } frame.yielded = false; state.Execute(frame, closure, ths); if (frame.yielded) { Suspend(); result = frame.HasValue() ? frame.Pop() : Laye.NULL; return true; } Kill(); result = null; return true; }
public abstract void Store(LayeState state, LayeObject value);