Esempio n. 1
0
        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;
        }
Esempio n. 2
0
        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;
        }
Esempio n. 3
0
        public void SetHandle(Runtime runtime, P5Handle handle)
        {
            var reference = body as P5Reference;

            if (reference != null)
            {
                reference.Referred = handle;
                return;
            }

            if (IsDefined(runtime))
                throw new System.NotImplementedException("No SetHandle for defined scalars");

            body = new P5Reference(runtime, handle);
        }
Esempio n. 4
0
        public static P5Scalar Print(Runtime runtime, P5Handle handle, P5Array args)
        {
            // wrong but works well enough for now
            for (int i = 0, m = args.GetCount(runtime); i < m; ++i)
            {
                handle.Write(runtime, args.GetItem(runtime, i), 0, -1);
            }

            return new P5Scalar(runtime, 1);
        }
Esempio n. 5
0
        public static P5Scalar Open3Args(Runtime runtime, P5Scalar target, string open_mode, P5Scalar value)
        {
            FileMode mode;
            FileAccess access;
            bool is_read = false, is_write = false, truncate = false;

            switch (open_mode)
            {
            case ">":
                mode = FileMode.OpenOrCreate;
                access = FileAccess.Write;
                is_write = truncate = true;
                break;
            case "<":
                mode = FileMode.Open;
                access = FileAccess.Read;
                is_read = true;
                break;
            case ">>":
                mode = FileMode.Append;
                access = FileAccess.Write;
                is_write = true;
                break;
            case "+>":
                mode = FileMode.OpenOrCreate;
                access = FileAccess.ReadWrite;
                is_read = is_write = truncate = true;
                break;
            case "+<":
                mode = FileMode.OpenOrCreate;
                access = FileAccess.ReadWrite;
                is_read = is_write = true;
                break;
            default:
                throw new P5Exception(runtime, string.Format("Unknown open() mode '{0}'", open_mode));
            }

            FileStream filestream;

            try
            {
                filestream = new FileStream(value.AsString(runtime), mode, access);
                if (truncate)
                    filestream.SetLength(0);
            }
            catch (IOException)
            {
                // TODO set $!
                return new P5Scalar(runtime, false);
            }

            // TODO handle encoding
            var handle = new P5Handle(
                runtime,
                is_read ? new StreamReader(filestream) : null,
                is_write ? new StreamWriter(filestream) : null);

            target.SetHandle(runtime, handle);

            return new P5Scalar(runtime, true);
        }
Esempio n. 6
0
        public static IP5Any Readline(Runtime runtime, P5Handle handle,
                                      Opcode.ContextValues cxt)
        {
            if (cxt == Opcode.ContextValues.LIST)
            {
                P5Scalar line;
                var lines = new List<IP5Any>();

                while (handle.Readline(runtime, out line))
                    lines.Add(line);

                return new P5List(runtime, lines);
            }
            else
            {
                P5Scalar line;
                handle.Readline(runtime, out line);

                return line;
            }
        }