public P5Code ParseFile(Runtime runtime, string program, bool is_main) { var parser = SafeInstance(runtime); P5Array arglist_parse_file = new P5Array(parser_runtime, parser, new P5Scalar(parser_runtime, program), new P5Scalar(parser_runtime, 3)); IP5Any res; try { res = arglist_parse_file.CallMethod(parser_runtime, Opcode.ContextValues.SCALAR, "parse_file"); } catch (System.Reflection.TargetInvocationException te) { var e = te.InnerException as P5Exception; if (e == null) throw te; else throw FixupException(e); } catch (P5Exception e) { throw FixupException(e); } return NetGlue.UnwrapValue(res, typeof(P5Code)) as P5Code; }
public Parser(Runtime runtime) { parser_runtime = new Runtime(); parser_runtime.NativeRegex = true; // find compiled code var parser_assembly = System.Reflection.Assembly.Load("Language.P.Net.Parser"); parser_runtime.ModuleLoaders.Insert(0, new AssemblyModuleLoader(parser_assembly)); // load Language::P frontend Builtins.RequireFile(parser_runtime, Opcode.ContextValues.VOID, new P5Scalar(parser_runtime, "Language/P.pm")); // create generator generator = new DynamicGenerator(runtime, parser_runtime); // instantiate parser P5Array arglist_parser = new P5Array(parser_runtime, new P5Scalar(parser_runtime, "Language::P::Parser"), GetInit(runtime)); parser_template = arglist_parser.CallMethod(parser_runtime, Opcode.ContextValues.SCALAR, "new") as P5Scalar; }
public static P5Scalar Open(Runtime runtime, P5Array args) { if (args.GetCount(runtime) == 3) return Open3Args(runtime, args.GetItem(runtime, 0) as P5Scalar, args.GetItem(runtime, 1).AsString(runtime), args.GetItem(runtime, 2) as P5Scalar); throw new System.Exception("Unhandled arg count in open"); }
public static void AddOverload(Runtime runtime, string pack_name, P5Array args) { var overloads = new Overloads(); var pack = runtime.SymbolTable.GetPackage(runtime, pack_name, true); for (int i = 0; i < args.GetCount(runtime); i += 2) { string key = args.GetItem(runtime, i).AsString(runtime); var value = args.GetItem(runtime, i + 1); overloads.AddOperation(runtime, key, value); } pack.SetOverloads(overloads); }
public static P5Scalar Unlink(Runtime runtime, P5Array files) { int count = 0; foreach (var file in files) { try { System.IO.File.Delete(file.AsString(runtime)); count += 1; } catch (System.IO.IOException) { // ignored } } return new P5Scalar(runtime, count); }
public override P5List Slice(Runtime runtime, P5Array keys) { if (!flattened) Flatten(runtime); return base.Slice(runtime, keys); }
public virtual IP5ScalarBody Assign(Runtime runtime, IP5ScalarBody other) { var osb = other as P5TypeglobBody; if (osb == null) return other.CloneBody(runtime); scalar = osb.scalar; array = osb.array; hash = osb.hash; handle = osb.handle; code = osb.code; return this; }
public P5List Slice(Runtime runtime, P5Array keys, bool create) { var res = new P5List(runtime, (List<IP5Any>) null); var list = new List<IP5Any>(); foreach (var key in keys) { list.Add(GetItemOrUndef(runtime, key, create)); } res.SetArray(list); return res; }
public IP5Any CallMethod(Runtime runtime, Opcode.ContextValues context, string method, P5Array args) { if (!IsDefined(runtime)) throw new P5Exception(runtime, string.Format("Can't call method \"{0:S}\" on an undefined value", method)); P5Exception error; var pmethod = FindMethod(runtime, method, out error); var wrapper = NetWrapper(runtime); if (pmethod == null && wrapper != null) return wrapper.CallMethod(runtime, context, method, args); if (pmethod == null) throw error; return pmethod.Call(runtime, context, args); }
public static IP5Any Warn(Runtime runtime, P5Array args) { // TODO handle empty argument list when $@ is set and when it is not var message = new System.Text.StringBuilder(); for (var it = args.GetEnumerator(runtime); it.MoveNext(); ) message.Append(it.Current.AsString(runtime)); if (message.Length > 0 && message[message.Length - 1] != '\n') message.Append(string.Format(" at {0:S} line {1:D}.\n", runtime.File, runtime.Line)); var stderr = runtime.SymbolTable.GetGlob(runtime, "STDERR", true); stderr.Handle.Write(runtime, message.ToString()); return new P5Scalar(runtime, 1); }
public static P5Scalar JoinList(Runtime runtime, P5Array array) { var iter = array.GetEnumerator(); var res = new System.Text.StringBuilder(); bool first = true; iter.MoveNext(); var sep = iter.Current.AsString(runtime); while (iter.MoveNext()) { if (!first) res.Append(sep); first = false; res.Append(iter.Current.AsString(runtime)); } return new P5Scalar(runtime, res.ToString()); }
private static IP5Any WrapCallStatic(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var type = Glue.UnwrapValue<System.Type>(runtime, args.GetItem(runtime, 0)); var name = args.GetItem(runtime, 1).AsString(runtime); int count = args.GetCount(runtime); var arg = new P5Scalar[count - 2]; for (int i = 2; i < count; ++i) arg[i - 2] = args.GetItem(runtime, i) as P5Scalar; return Glue.CallStaticMethod(runtime, type, name, arg); }
private static IP5Any WrapCallMethod(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var obj = args.GetItem(runtime, 0) as P5Scalar; var name = args.GetItem(runtime, 1).AsString(runtime); int count = args.GetCount(runtime); var arg = new P5Scalar[count - 2]; for (int i = 2; i < count; ++i) arg[i - 2] = args.GetItem(runtime, i) as P5Scalar; return Glue.CallMethod(runtime, obj, name, arg); }
private static IP5Any WrapAddOverload(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var pack = args.GetItem(runtime, 0) as P5Scalar; var opref = args.GetItem(runtime, 1) as P5Scalar; var ops = opref.DereferenceArray(runtime) as P5Array; Builtins.AddOverload(runtime, pack.AsString(runtime), ops); return null; }
public P5Code ParseString(Runtime runtime, string program, string file, int line) { var parser = SafeInstance(runtime); var reader = new P5Handle( parser_runtime, new System.IO.StringReader(program), null); P5Array arglist_parse_stream = new P5Array(parser_runtime, parser, new P5Scalar(parser_runtime, reader), new P5Scalar(parser_runtime, file), new P5Scalar(parser_runtime, 3), new P5Scalar(parser_runtime)); IP5Any res; try { res = arglist_parse_stream.CallMethod(parser_runtime, Opcode.ContextValues.SCALAR, "parse_stream"); } catch (System.Reflection.TargetInvocationException te) { var e = te.InnerException as P5Exception; if (e == null) throw te; else throw FixupException(e); } catch (P5Exception e) { throw FixupException(e); } return NetGlue.UnwrapValue(res, typeof(P5Code)) as P5Code; }
private IP5Any SafeInstance(Runtime runtime) { P5Array arglist_safe_instance = new P5Array(parser_runtime, parser_template); var parser = arglist_safe_instance.CallMethod(parser_runtime, Opcode.ContextValues.SCALAR, "safe_instance"); return parser; }
public static P5Exception Die(Runtime runtime, P5Array args) { int argc = args.GetCount(runtime); if (argc == 1) { var s = args.GetItem(runtime, 0) as P5Scalar; if (s.IsReference(runtime)) return new P5Exception(runtime, s); } string message; if (argc == 0) { var exc = runtime.SymbolTable.GetStashScalar(runtime, "@", true); if (exc.IsDefined(runtime)) message = exc.AsString(runtime) + "\t...propagated"; else message = "Died"; } else { var t = new System.Text.StringBuilder(); foreach (var e in args) t.Append(e.AsString(runtime)); message = t.ToString(); } return new P5Exception(runtime, message); }
private static IP5Any WrapCompileAssembly(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var asm_path = args.GetItem(runtime, 0).AsString(runtime); var generator = new Generator(runtime, asm_path); for (int i = 1; i < args.GetCount(runtime); ++i) { var arg = args.GetItem(runtime, i).AsString(runtime); string path, file; path = Builtins.SearchFile(runtime, arg); if (path != null) file = arg; else { file = arg.Replace("::", "/") + ".pm"; path = Builtins.SearchFile(runtime, file); } if (path == null) throw new System.Exception(string.Format("File not found for '{0:S}'", arg)); var cu = Serializer.ReadCompilationUnit(runtime, path); cu.FileName = file; generator.Generate(cu); } generator.Assembly.Save(new System.IO.FileInfo(asm_path + ".dll").Name); return new P5Scalar(runtime); }
public static IP5Any Reverse(Runtime runtime, Opcode.ContextValues context, P5Array args) { if (context == Opcode.ContextValues.LIST) return args.Reversed(runtime); int count = args.GetCount(runtime); char[] value; if (count == 0) value = runtime.SymbolTable.GetStashScalar(runtime, "_", true).AsString(runtime).ToCharArray(); else if (count == 1) value = args.GetItem(runtime, 0).AsString(runtime).ToCharArray(); else { var t = new System.Text.StringBuilder(); foreach (var i in args) t.Append(i.AsString(runtime)); value = t.ToString().ToCharArray(); } // TODO does not handle UCS-4 System.Array.Reverse(value); return new P5Scalar(runtime, new string(value)); }
private static IP5Any WrapCreate(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var cls = args.GetItem(runtime, 0) as P5Scalar; int count = args.GetCount(runtime); var arg = new P5Scalar[count - 1]; for (int i = 1; i < count; ++i) arg[i - 1] = args.GetItem(runtime, i) as P5Scalar; return Glue.CallConstructor(runtime, cls, arg); }
public IP5Any CallMethod(Runtime runtime, Opcode.ContextValues context, string method, P5Array args) { int count = args.GetCount(runtime); var arg = new P5Scalar[count - 1]; for (int i = 1; i < count; ++i) arg[i - 1] = args.GetItem(runtime, i) as P5Scalar; var type = obj as System.Type; if (type != null) return NetGlue.CallStaticMethod(runtime, type, method, arg); else return NetGlue.CallMethod(runtime, obj, method, arg); }
private static IP5Any WrapExtend(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var pack = args.GetItem(runtime, 0); var cls = args.GetItem(runtime, 1); return Glue.Extend(runtime, pack.AsString(runtime), cls.AsString(runtime), "new", true); }
public P5Scalar PushList(Runtime runtime, P5Array items) { foreach (var item in items) array.Add(NetGlue.UnwrapValue(runtime, item, type)); return new P5Scalar(runtime, array.Count); }
private static IP5Any WrapGetClass(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var name = args.GetItem(runtime, 0); return Glue.GetClass(runtime, name.AsString(runtime)); }
public P5Scalar UnshiftList(Runtime runtime, P5Array items) { int i = 0; foreach (var item in items) array.Insert(i++, NetGlue.UnwrapValue(runtime, item, type)); return new P5Scalar(runtime, array.Count); }
private static IP5Any WrapIsa(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var value = args.GetItem(runtime, 0) as P5Scalar; var parent = args.GetItem(runtime, 1); bool is_derived = Builtins.IsDerivedFrom(runtime, value, parent); return new P5Scalar(runtime, is_derived); }
public override IP5Any Clone(Runtime runtime, int depth) { P5Array clone = new P5Array(runtime, array.Count); if (depth > 0) { foreach (var i in array) { var enumerable = i as IP5Enumerable; if (enumerable != null) clone.PushFlatten(runtime, i); else clone.Push(runtime, i.Clone(runtime, depth - 1)); } } else foreach (var i in array) clone.PushFlatten(runtime, i); return clone; }
private static IP5Any WrapSetProperty(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var obj = args.GetItem(runtime, 0) as P5Scalar; var name = args.GetItem(runtime, 1); var value = args.GetItem(runtime, 2); Glue.SetProperty(runtime, obj, name.AsString(runtime), value); return new P5Scalar(runtime); }
public virtual P5List Slice(Runtime runtime, P5Array keys) { var res = new P5List(runtime); var list = new List<IP5Any>(keys.GetCount(runtime)); bool found = false; foreach (var key in keys) { int i = key.AsInteger(runtime); if (i < array.Count) found = true; list.Add(GetItemOrUndef(runtime, key, false)); } if (found) res.SetArray(list); return res; }
private static IP5Any WrapSpecializeType(Runtime runtime, Opcode.ContextValues context, P5ScratchPad pad, P5Array args) { var type = Glue.UnwrapValue<System.Type>(runtime, args.GetItem(runtime, 0)); var tyargs = new System.Type[args.GetCount(runtime) - 1]; for (int i = args.GetCount(runtime) - 1; i > 0; --i) tyargs[i - 1] = Glue.UnwrapValue<System.Type>(runtime, args.GetItem(runtime, i)); return new P5Scalar(new P5NetWrapper(type.MakeGenericType(tyargs))); }