public override string CodeForm(TAC.Machine vm, int recursionLimit = -1) { if (recursionLimit == 0) { return("{...}"); } if (recursionLimit > 0 && recursionLimit < 3 && vm != null) { string shortName = vm.FindShortName(this); if (shortName != null) { return(shortName); } } var strs = new string[map.Count]; int i = 0; foreach (KeyValuePair <Value, Value> kv in map) { int nextRecurLimit = recursionLimit - 1; if (kv.Key == ValString.magicIsA) { nextRecurLimit = 1; } strs[i++] = string.Format("{0}: {1}", kv.Key.CodeForm(vm, nextRecurLimit), kv.Value == null ? "null" : kv.Value.CodeForm(vm, nextRecurLimit)); } return("{" + String.Join(", ", strs) + "}"); }
public override string CodeForm(TAC.Machine vm, int recursionLimit = -1) { if (recursionLimit == 0) { return("[...]"); } if (recursionLimit > 0 && recursionLimit < 3 && vm != null) { string shortName = vm.FindShortName(this); if (shortName != null) { return(shortName); } } var strs = new string[values.Count]; for (var i = 0; i < values.Count; i++) { if (values[i] == null) { strs[i] = "null"; } else { strs[i] = values[i].CodeForm(vm, recursionLimit - 1); } } return("[" + string.Join(", ", strs) + "]"); }
public override bool IsA(Value type, TAC.Machine vm) { // If the given type is the magic 'map' type, then we're definitely // one of those. Otherwise, we have to walk the __isa chain. if (type == vm.mapType) { return(true); } Value p = null; map.TryGetValue(ValString.magicIsA, out p); while (p != null) { if (p == type) { return(true); } if (!(p is ValMap)) { return(false); } ((ValMap)p).map.TryGetValue(ValString.magicIsA, out p); } return(false); }
/// <summary> /// Read Eval Print Loop. Run the given source until it either terminates, /// or hits the given time limit. When it terminates, if we have new /// implicit output, print that to the implicitOutput stream. /// </summary> /// <param name="sourceLine">Source line.</param> /// <param name="timeLimit">Time limit.</param> public void REPL(string sourceLine, double timeLimit = 60) { if (parser == null) { parser = new Parser(); } if (vm == null) { vm = parser.CreateVM(standardOutput); vm.interpreter = new WeakReference(this); } else if (vm.done && !parser.NeedMoreInput()) { // Since the machine and parser are both done, we don't really need the // previously-compiled code. So let's clear it out, as a memory optimization. vm.GetTopContext().ClearCodeAndTemps(); parser.PartialReset(); } if (sourceLine == "#DUMP") { vm.DumpTopContext(); return; } double startTime = vm.runTime; int startImpResultCount = vm.globalContext.implicitResultCounter; vm.storeImplicit = (implicitOutput != null); try { if (sourceLine != null) { parser.Parse(sourceLine, true); } if (!parser.NeedMoreInput()) { while (!vm.done && !vm.yielding) { if (vm.runTime - startTime > timeLimit) { return; // time's up for now! } vm.Step(); } if (implicitOutput != null && vm.globalContext.implicitResultCounter > startImpResultCount) { Value result = vm.globalContext.GetVar(ValVar.implicitResult.identifier); if (result != null) { implicitOutput.Invoke(result.ToString(vm)); } } } } catch (MiniscriptException mse) { ReportError(mse); // Attempt to recover from an error by jumping to the end of the code. vm.GetTopContext().JumpToEnd(); } }
public void Dispose() { if (vm != null) { vm.Dispose(); } vm = null; }
public override string ToString(TAC.Machine vm) { if (noInvoke) { return("@" + identifier); } return(identifier); }
/// <summary> /// Read Eval Print Loop. Run the given source until it either terminates, /// or hits the given time limit. When it terminates, if we have new /// implicit output, print that to the implicitOutput stream. /// </summary> /// <param name="sourceLine">Source line.</param> /// <param name="timeLimit">Time limit.</param> public void REPL(string sourceLine, double timeLimit = 60) { if (parser == null) { parser = new Parser(); } if (vm == null) { vm = parser.CreateVM(standardOutput); vm.interpreter = new WeakReference(this); } if (sourceLine == "#DUMP") { vm.DumpTopContext(); return; } double startTime = vm.runTime; int startImpResultCount = vm.globalContext.implicitResultCounter; vm.storeImplicit = (implicitOutput != null); try { if (sourceLine != null) { parser.Parse(sourceLine, true); } if (!parser.NeedMoreInput()) { while (!vm.done) { if (vm.runTime - startTime > timeLimit) { return; // time's up for now! } vm.Step(); } if (implicitOutput != null && vm.globalContext.implicitResultCounter > startImpResultCount) { Value result = vm.globalContext.GetVar(ValVar.implicitResult.identifier); if (result != null) { implicitOutput.Invoke(result.ToString(vm)); } } } } catch (MiniscriptException mse) { ReportError(mse); // Attempt to recover from an error by jumping to the end of the code. vm.GetTopContext().JumpToEnd(); } }
/// <summary> /// Compile our source code, if we haven't already done so, so that we are /// either ready to run, or generate compiler errors (reported via errorOutput). /// </summary> public void Compile() { if (vm != null) { return; // already compiled } if (parser == null) { parser = new Parser(); } try { parser.Parse(source); vm = parser.CreateVM(standardOutput); vm.interpreter = new WeakReference(this); } catch (MiniscriptException mse) { ReportError(mse); } }
public override string ToString(TAC.Machine vm) { // Convert to a string in the standard MiniScript way. if (value % 1.0 == 0.0) { // integer values as integers return(value.ToString("0", CultureInfo.InvariantCulture)); } else if (value > 1E10 || value < -1E10 || (value < 1E-6 && value > -1E-6)) { // very large/small numbers in exponential form return(value.ToString("E6", CultureInfo.InvariantCulture)); } else { // all others in decimal form, with 1-6 digits past the decimal point return(value.ToString("0.0#####", CultureInfo.InvariantCulture)); } }
public string ToString(TAC.Machine vm) { var s = new System.Text.StringBuilder(); s.Append("FUNCTION("); for (var i = 0; i < parameters.Count(); i++) { if (i > 0) { s.Append(", "); } s.Append(parameters[i].name); if (parameters[i].defaultValue != null) { s.Append("=" + parameters[i].defaultValue.CodeForm(vm)); } } s.Append(")"); return(s.ToString()); }
public override string ToString(TAC.Machine vm) { return(CodeForm(vm, 3)); }
public override bool IsA(Value type, TAC.Machine vm) { return(type == vm.listType); }
/// <summary> /// Get this value in the form of a MiniScript literal. /// </summary> /// <param name="recursionLimit">how deeply we can recurse, or -1 for no limit</param> /// <returns></returns> public virtual string CodeForm(TAC.Machine vm, int recursionLimit = -1) { return(ToString(vm)); }
public override string CodeForm(TAC.Machine vm, int recursionLimit = -1) { return("\"" + value.Replace("\"", "\"\"") + "\""); }
public override string ToString(TAC.Machine vm) { return(function.ToString(vm)); }
public override bool IsA(Value type, TAC.Machine vm) { return(false); }
/// <summary> /// Return whether this value is the given type (or some subclass thereof) /// in the context of the given virtual machine. /// </summary> public virtual bool IsA(Value type, TAC.Machine vm) { return(false); }
public override string ToString(TAC.Machine vm) { return(string.Format("{0}{1}[{2}]", noInvoke ? "@" : "", sequence, index)); }
public override string ToString(TAC.Machine vm) { return("_" + tempNum.ToString(CultureInfo.InvariantCulture)); }
public override bool IsA(Value type, TAC.Machine vm) { return(type == vm.functionType); }
public override string ToString(TAC.Machine vm) { return("_" + tempNum); }
/// <summary> /// Reset the interpreter with the given source code. /// </summary> /// <param name="source"></param> public void Reset(string source = "") { this.source = source; parser = null; vm = null; }
public abstract string ToString(TAC.Machine vm);
public override string ToString(TAC.Machine machine) { return("null"); }
public override string ToString(TAC.Machine vm) { return(value); }