public static P5Scalar BitAnd(Runtime runtime, P5Scalar res, P5Scalar a, P5Scalar b) { if (a.IsString(runtime) && b.IsString(runtime)) { string sa = a.AsString(runtime), sb = b.AsString(runtime); System.Text.StringBuilder t; if (sa.Length > sb.Length) { t = new System.Text.StringBuilder(sa); for (int i = 0; i < sb.Length; ++i) t[i] &= sb[i]; } else { t = new System.Text.StringBuilder(sb); for (int i = 0; i < sa.Length; ++i) t[i] &= sa[i]; } res.SetString(runtime, t.ToString()); } else { // TODO take into account signed/unsigned? res.SetInteger(runtime, a.AsInteger(runtime) & b.AsInteger(runtime)); } return res; }
public P5VecBody(Runtime runtime, P5Scalar _value, int _offset, int _bits) { value = _value; offset = _offset; bits = _bits; }
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; }
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 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 static P5Scalar Close(Runtime runtime, P5Scalar arg) { var handle = arg.DereferenceHandle(runtime); bool res = handle.Close(runtime); return new P5Scalar(runtime, res); }
internal DynamicModuleGenerator(Runtime _runtime) { runtime = _runtime; subroutines = new Dictionary<Subroutine, P5Code>(); regexes = new Dictionary<Subroutine, IP5Regex>(); main_pad = new P5ScratchPad(); }
public bool Close(Runtime runtime) { bool ok = true; if (input != null) try { input.Close(); } catch (IOException) { ok = false; } if (output != null) try { output.Close(); } catch (IOException) { ok = false; } return ok; }
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 override P5Scalar Get(Runtime runtime) { if (length.HasValue) return value.Substring(runtime, offset, length.Value); else return value.Substring(runtime, offset); }
// TODO handle stacked options public static void ParseCommandLine(Runtime runtime, string[] args, out string[] argv) { for (int i = 0; i < args.Length; ++i) { string arg = args[i]; switch (arg) { // standard Perl flags case "-c": runtime.CompileOnly = true; break; // code generation options case "-Znative-regex": runtime.NativeRegex = true; break; case "-Zignore-bytecode": runtime.IgnoreBytecode = true; break; default: argv = new string[args.Length - i]; for (int j = i; j < args.Length; ++j) argv[j - i] = args[j]; return; } } argv = null; return; }
public virtual int AssignArray(Runtime runtime, IP5Value other) { // FIXME multiple dispatch P5Array a = other as P5Array; P5Hash h = other as P5Hash; iterator = null; if (a != null) { AssignIterator(runtime, a.GetEnumerator(runtime)); return a.GetCount(runtime); } else if (h != null) { hash.Clear(); foreach (var e in h.hash) hash[e.Key] = e.Value.Clone(runtime, 0); return h.GetCount(runtime) * 2; } return 0; }
public DynamicGenerator(Runtime _runtime, Runtime _parser_runtime, P5Scalar _intermediate, P5Scalar _transform) : this(_runtime) { parser_runtime = _parser_runtime; intermediate = _intermediate; transform = _transform; }
public P5Handle(Runtime _runtime, TextReader _input, TextWriter _output) { input = _input; output = _output; if (input != null) read_buffer = new char[BUFFER_SIZE]; }
public override P5Scalar Get(Runtime runtime) { if ( runtime.LastMatch.StringCaptures != null && Index < runtime.LastMatch.StringCaptures.Length && runtime.LastMatch.StringCaptures[Index] != null) return new P5Scalar(runtime, runtime.LastMatch.StringCaptures[Index]); else return new P5Scalar(runtime); }
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 bool Readline(Runtime runtime, out P5Scalar result) { System.Text.StringBuilder builder = null; for (;;) { if (rdbuf_start < rdbuf_end) { int newline = System.Array.IndexOf(read_buffer, '\n', rdbuf_start, rdbuf_end - rdbuf_start); if (newline < 0 && rdbuf_end != BUFFER_SIZE) newline = rdbuf_end - 1; if (newline >= 0) { if (builder != null) { builder.Append(read_buffer, rdbuf_start, newline + 1 - rdbuf_start); result = new P5Scalar(runtime, builder.ToString()); } else result = new P5Scalar(runtime, new string(read_buffer, rdbuf_start, newline + 1 - rdbuf_start)); rdbuf_start = newline + 1; return true; } if (builder == null) builder = new System.Text.StringBuilder(2 * BUFFER_SIZE); builder.Append(read_buffer, rdbuf_start, rdbuf_end - rdbuf_start); } rdbuf_start = 0; rdbuf_end = input.Read(read_buffer, 0, BUFFER_SIZE); if (rdbuf_start == rdbuf_end) { if (builder != null) { result = new P5Scalar(runtime, builder.ToString()); return true; } else { result = new P5Scalar(runtime); return false; } } } }
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 DoFile(Runtime runtime, Opcode.ContextValues context, P5Scalar file) { var file_s = file.AsString(runtime); var ret = LoadFile(runtime, context, file_s); if (ret == null) return new P5Scalar(runtime); return ret; }
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 static IP5Any LoadFile(Runtime runtime, Opcode.ContextValues context, string file) { IP5Any ret = null; foreach (var loader in runtime.ModuleLoaders) { ret = loader.TryLoad(runtime, context, file); if (ret != null) return ret; } return ret; }
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 override P5Scalar Get(Runtime runtime) { var str = value.AsString(runtime); int byte_offset = (offset * bits) / 8; int bit_offset = (offset * bits) % 8; int mask = ((1 << bits) - 1); int intval; if (byte_offset < str.Length) intval = (str[byte_offset] >> bit_offset) & mask; else intval = 0; return new P5Scalar(runtime, intval); }
public virtual IP5Any AssignIterator(Runtime runtime, IEnumerator<IP5Any> e) { hash.Clear(); while (e.MoveNext()) { P5Scalar k = e.Current.AsScalar(runtime); e.MoveNext(); IP5Any v = e.Current; hash[k.KeyString(runtime)] = v.Clone(runtime, 0); } iterator = null; return this; }
public static void Main(string[] args) { // use the invariant locale as the default System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; var runtime = new Runtime(); string[] argv; ParseCommandLine(runtime, args, out argv); try { if (argv[0].EndsWith(".pb")) { var cu = Serializer.ReadCompilationUnit(runtime, argv[0]); P5Code main = new DynamicGenerator(runtime).GenerateAndLoad(cu); if (!runtime.CompileOnly) main.CallMain(runtime); } else { var parser = runtime.parser; parser.Run(runtime, argv); } } catch (System.Reflection.TargetInvocationException te) { var e = te.InnerException as P5Exception; if (e == null) { System.Console.WriteLine(); System.Console.WriteLine(te.InnerException.ToString()); } else System.Console.WriteLine(e.AsString(runtime)); } catch (P5Exception e) { System.Console.WriteLine(e.AsString(runtime)); } }
public static P5Scalar BitNot(Runtime runtime, P5Scalar value) { if (value.IsString(runtime)) { string svalue = value.AsString(runtime); var t = new System.Text.StringBuilder(svalue);; for (int i = 0; i < svalue.Length; ++i) t[i] = (char)(~t[i] & 0xff); // only ASCII for now return new P5Scalar(runtime, t.ToString()); } else { // TODO take into account signed/unsigned? return new P5Scalar(runtime, ~value.AsInteger(runtime)); } }
public void AddOperation(Runtime runtime, string key, IP5Any value) { switch (key) { case "+": AddOperation(runtime, OverloadOperation.ADD, value); break; case "+=": AddOperation(runtime, OverloadOperation.ADD_ASSIGN, value); break; case "-": AddOperation(runtime, OverloadOperation.SUBTRACT, value); break; case "-=": AddOperation(runtime, OverloadOperation.SUBTRACT_ASSIGN, value); break; case "*": AddOperation(runtime, OverloadOperation.MULTIPLY, value); break; case "*=": AddOperation(runtime, OverloadOperation.MULTIPLY_ASSIGN, value); break; case "/": AddOperation(runtime, OverloadOperation.DIVIDE, value); break; case "/=": AddOperation(runtime, OverloadOperation.DIVIDE_ASSIGN, value); break; case "<<": AddOperation(runtime, OverloadOperation.SHIFT_LEFT, value); break; case "<<=": AddOperation(runtime, OverloadOperation.SHIFT_LEFT_ASSIGN, value); break; case ">>": AddOperation(runtime, OverloadOperation.SHIFT_RIGHT, value); break; case ">>=": AddOperation(runtime, OverloadOperation.SHIFT_RIGHT_ASSIGN, value); break; } }
public static IP5Any RequireFile(Runtime runtime, Opcode.ContextValues context, P5Scalar file) { if (file.IsInteger(runtime) || file.IsFloat(runtime)) { var value = file.AsFloat(runtime); var version = runtime.SymbolTable.GetScalar(runtime, "]", false); var version_f = version.AsFloat(runtime); if (version_f >= value) return new P5Scalar(runtime, true); var msg = string.Format("Perl {0:F} required--this is only {1:F} stopped.", value, version_f); throw new P5Exception(runtime, msg); } var file_s = file.AsString(runtime); var inc = runtime.SymbolTable.GetHash(runtime, "INC", true); if (inc.ExistsKey(runtime, file_s)) return new P5Scalar(runtime, 1); var ret = LoadFile(runtime, context, file_s); if (ret == null) { var message = new System.Text.StringBuilder(); var inc_a = runtime.SymbolTable.GetArray(runtime, "INC", true); message.Append(string.Format("Can't locate {0:S} in @INC (@INC contains:", file_s)); foreach (var dir in inc_a) { message.Append(" "); message.Append(dir.AsString(runtime)); } message.Append(")"); throw new P5Exception(runtime, message.ToString()); } return ret; }
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 DynamicGenerator(Runtime _runtime, Runtime _parser_runtime) : this(_runtime) { parser_runtime = _parser_runtime; // load Language::P::Intermediate frontend Builtins.RequireFile(parser_runtime, Opcode.ContextValues.VOID, new P5Scalar(parser_runtime, "Language/P/Intermediate/Generator.pm")); // set the opcode factory var assembly_i = parser_runtime.SymbolTable.GetGlob(parser_runtime, "Language::P::Assembly::i", true); assembly_i.Code = new P5NativeCode("Language::P::Assembly::i", new P5Code.Sub(Opcode.WrapCreate)); // wrap Language::P::Intermediate support classes NetGlue.Extend(parser_runtime, "Language::P::Intermediate::Code", "org.mbarbon.p.runtime.Subroutine", "new", false); NetGlue.Extend(parser_runtime, "Language::P::Intermediate::BasicBlock", "org.mbarbon.p.runtime.BasicBlock", "new_from_label", false); NetGlue.Extend(parser_runtime, "Language::P::Intermediate::Scope", "org.mbarbon.p.runtime.Scope", "new", false); NetGlue.Extend(parser_runtime, "Language::P::Intermediate::LexicalState", "org.mbarbon.p.runtime.LexicalState", "new", false); NetGlue.Extend(parser_runtime, "Language::P::Intermediate::LexicalInfo", "org.mbarbon.p.runtime.LexicalInfo", "new", false); intermediate = CreateIRGenerator(); // load Language::P::Transform Builtins.RequireFile(parser_runtime, Opcode.ContextValues.VOID, new P5Scalar(parser_runtime, "Language/P/Intermediate/Transform.pm")); transform = CreateTransform(); }