public override P5Scalar Assign(Runtime runtime, IP5Any other) { var ob = other.AsScalar(runtime).Body; var obr = ob as P5Reference; var obb = ob as P5TypeglobBody; if (obb != null) body = globBody = obb; else if (obr != null) { var referred = obr.Referred; var code = referred as P5Code; var scalar = referred as P5Scalar; var array = referred as P5Array; var hash = referred as P5Hash; if (code != null) globBody.Code = code; else if (scalar != null) globBody.Scalar = scalar; else if (array != null) globBody.Array = array; else if (hash != null) globBody.Hash = hash; } else { throw new System.NotImplementedException("Assign either glob or reference"); } return this; }
public override void Set(Runtime runtime, IP5Any other) { if (length.HasValue) value.SpliceSubstring(runtime, offset, length.Value, other); else value.SpliceSubstring(runtime, offset, other); }
public static P5Scalar IsFile(Runtime runtime, IP5Any path) { var str = path.AsString(runtime); if (System.IO.File.Exists(str)) return new P5Scalar(runtime, true); if (System.IO.Directory.Exists(str)) return new P5Scalar(runtime, false); return new P5Scalar(runtime); }
public static IP5Any ArrayReplace(Runtime runtime, IP5Array array, IP5Any offset, IP5Any count, params IP5Any[] values) { int start = offset.AsInteger(runtime); int length = count.AsInteger(runtime); int max = array.GetCount(runtime); if (start < 0) start = max + start; if (length < 0) length = max + length - start; return array.Replace(runtime, start, length, values); }
public override void Set(Runtime runtime, IP5Any other) { var str = value.AsString(runtime); var intval = other.AsInteger(runtime); int byte_offset = (offset * bits) / 8; int bit_offset = (offset * bits) % 8; int mask = ((1 << bits) - 1); var t = new System.Text.StringBuilder(str.Length); foreach (char c in str) t.Append(c); while (byte_offset >= t.Length) t.Append((char)0); int changed = (t[byte_offset] & ~(mask << bit_offset)) | ((intval & mask) << bit_offset); t[byte_offset] = (char)changed; value.SetString(runtime, t.ToString()); }
public static IP5Any ArraySplice(Runtime runtime, IP5Array array, IP5Any offset, IP5Any count) { int start, length, max = array.GetCount(runtime); if (offset == null) start = 0; else start = offset.AsInteger(runtime); if (start < 0) start = max + start; if (count == null) length = max - start; else length = count.AsInteger(runtime); if (length < 0) length = max + length - start; return array.Splice(runtime, start, length); }
public override void Set(Runtime runtime, IP5Any other) { var scalar = other.AsScalar(runtime); if (!scalar.IsDefined(runtime)) { Value.SetPos(runtime, -1, true); return; } int pos = scalar.AsInteger(runtime); int length = Value.Length(runtime); if (pos < 0 && -pos >= length) pos = 0; else if (pos < 0) pos = length + pos; else if (pos > length) pos = length; Value.SetPos(runtime, pos, true); }
public P5Scalar SpliceSubstring(Runtime runtime, int start, IP5Any replace) { var sn = ForceString(runtime); AdjustOffsets(sn.stringValue, ref start); // TODO handle the various corner cases for start/end var part = new P5Scalar(runtime, sn.stringValue.Substring(start, sn.stringValue.Length - start)); sn.stringValue = sn.stringValue.Substring(0, start) + replace.AsString(runtime); return part; }
public P5Scalar Repeat(Runtime runtime, IP5Any c) { int count = c.AsInteger(runtime); string val = AsString(runtime); var str = new System.Text.StringBuilder(); for (int i = 0; i < count; ++i) str.Append(val); return new P5Scalar(runtime, str.ToString()); }
public P5Scalar ConcatAssign(Runtime runtime, IP5Any other) { P5StringNumber sn = body as P5StringNumber; if (sn == null) body = sn = new P5StringNumber(runtime, body.AsString(runtime)); else sn.flags = P5StringNumber.HasString; sn.stringValue = sn.stringValue + other.AsScalar(runtime).AsString(runtime); sn.pos = -1; return this; }
public virtual P5Scalar Assign(Runtime runtime, IP5Any other) { body = body.Assign(runtime, other.AsScalar(runtime).body); return this; }
public virtual void Set(Runtime runtime, IP5Any other) { throw new System.Exception("Ouch!"); }
public static IP5Any Return(Runtime runtime, Opcode.ContextValues cxt, IP5Any value) { if (cxt == Opcode.ContextValues.SCALAR) return value.AsScalar(runtime); if (cxt == Opcode.ContextValues.LIST) return value as P5Array ?? new P5List(runtime, value); return P5List.EmptyList; }
public void RestoreElement(Runtime runtime, int index, IP5Any value) { throw new System.NotImplementedException(); }
public int Write(Runtime runtime, IP5Any scalar, int offset, int length) { // TODO use offset/length output.Write(scalar.AsString(runtime)); return 1; }
public static P5Scalar Ord(Runtime runtime, IP5Any value) { var s = value.AsString(runtime); return new P5Scalar(runtime, s.Length > 0 ? (int)s[0] : 0); }
public static P5Scalar SubtractScalarsAssign(Runtime runtime, P5Scalar left, IP5Any right) { return SubtractScalars(runtime, left, left, right); }
public static P5Scalar SubtractScalars(Runtime runtime, P5Scalar res, IP5Any left, IP5Any right) { // TODO handle integer addition and integer -> float promotion res.SetFloat(runtime, left.AsFloat(runtime) - right.AsFloat(runtime)); return res; }
public static P5List SplitSpaces(Runtime runtime, IP5Any value) { var str = value.AsString(runtime); var res = new P5List(runtime); int start = 0, curr = 0; for ( ; curr < str.Length; ) { for ( ; curr < str.Length && char.IsWhiteSpace(str[curr]); ++curr) ; start = curr; if (start == str.Length) break; for ( ; curr < str.Length && !char.IsWhiteSpace(str[curr]); ++curr) ; res.Push(runtime, new P5Scalar(runtime, str.Substring(start, curr - start))); } return res; }
public static P5Scalar RightShiftScalarsAssign(Runtime runtime, P5Scalar left, IP5Any right) { return RightShiftScalars(runtime, left, left, right); }
public static P5Scalar RightShiftScalars(Runtime runtime, P5Scalar res, IP5Any left, IP5Any right) { res.SetInteger(runtime, left.AsInteger(runtime) >> right.AsInteger(runtime)); return res; }
public IP5Any GetItemOrUndef(Runtime runtime, IP5Any index, bool create) { int idx = GetItemIndex(runtime, index.AsInteger(runtime), false); if (create) return new P5NetArrayItem(array, type, idx); else return NetGlue.WrapValue(array[idx]); }
public P5List Replace(Runtime runtime, int start, int length, IP5Any[] values) { // TODO duplicated var spliced = new List<IP5Any>(); foreach (var i in values) { var a = i as P5Array; var h = i as P5Hash; IEnumerator<IP5Any> enumerator = null; if (h != null) enumerator = ((P5Hash)h.Clone(runtime, 1)).GetEnumerator(runtime); else if (a != null) enumerator = ((P5Array)a.Clone(runtime, 1)).GetEnumerator(runtime); if (enumerator != null) while (enumerator.MoveNext()) spliced.Add(enumerator.Current); else spliced.Add(i.Clone(runtime, 0)); } var res = new List<IP5Any>(); // TODO _very_ inefficient, but IList does not have the // right interface for (int i = 0; i < length; ++i) { // TODO only in list context res.Add(NetGlue.WrapValue(array[start])); array.RemoveAt(start); } for (int i = 0; i < spliced.Count; ++i) array.Insert(start + i, NetGlue.UnwrapValue(runtime, spliced[i], type)); return new P5List(runtime, res); }
public override P5Scalar Assign(Runtime runtime, IP5Any other) { (body as P5ActiveScalarBody).Set(runtime, other); return this; }
public override void Set(Runtime runtime, IP5Any other) { array[index] = NetGlue.UnwrapValue(runtime, other, type); }
public static P5Scalar QuoteMeta(Runtime runtime, IP5Any value) { var t = new System.Text.StringBuilder(); foreach (char c in value.AsString(runtime)) { if (!char.IsLetterOrDigit(c) && c != '_') t.Append('\\'); t.Append(c); } return new P5Scalar(runtime, t.ToString()); }
public IP5Any MatchGlobal(Runtime runtime, IP5Any value, int flags, Opcode.ContextValues cxt, ref RxResult oldState) { return P5Regex.MatchGlobalHelper(this, runtime, value, flags, cxt, ref oldState); }
public static P5Scalar Uppercase(Runtime runtime, IP5Any value) { var s = value.AsString(runtime).ToUpper(); return new P5Scalar(runtime, s); }
public P5List(Runtime runtime, IP5Any value) : base(runtime, new IP5Any[] { value }) { }
public static P5Scalar Bless(Runtime runtime, P5Scalar reference, IP5Any pack) { var pack_str = pack.AsString(runtime); var stash = runtime.SymbolTable.GetPackage(runtime, pack_str, true); reference.BlessReference(runtime, stash); return reference; }