Example #1
0
        // Note: The main reason there is no OpenReadText is there is no easy way to query for EOF.
        // It might be nice to not have to read large text files all at once, ie. via File::ReadLines,
        // but OTOH if you are reading files that are THAT big Pebble may not be best tool for
        // the job. If there is enough demand for it can add it later.

        public static void Register(Engine engine)
        {
            //@ class Stream
            //   Note that it is intentional that there is no OpenReadText method. It's easier to use
            //   the File library for reading text.
            TypeDef_Class ourType  = TypeFactory.GetTypeDef_Class("Stream", null, false);
            ClassDef      classDef = engine.defaultContext.CreateClass("Stream", ourType, null, null, false);

            classDef.childAllocator = () => {
                return(new PebbleStreamHelper());
            };
            classDef.Initialize();

            //@ bool Close()
            //   Closes the stream.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.Close());
                };

                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("Close", newValue.valType, newValue, false);
            }

            //@ bool IsReading()
            //   Return true iff the stream is open in read mode.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.IsReading());
                };

                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("IsReading", newValue.valType, newValue, false);
            }

            //@ bool IsWriting()
            //   Returns true iff the stream is open in write mode.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.IsWriting());
                };

                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("IsWriting", newValue.valType, newValue, false);
            }

            //@ bool IsOpen()
            //   Returs true iff the stream is open in either read or write mode.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.IsOpen());
                };

                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                }, eval, false);
                classDef.AddMemberLiteral("IsOpen", newValue.valType, newValue, false);
            }

            //@ bool OpenReadBinary(string filepath)
            //   Opens the specified file for reading in binary mode.
            //   Returns false if there is an error.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    string path = args[0] as string;

                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.OpenFileRead(context, path));
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                    IntrinsicTypeDefs.STRING
                }, eval, true);
                classDef.AddMemberLiteral("OpenReadBinary", newValue.valType, newValue, false);
            }

            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    string path = args[0] as string;

                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.OpenFileWrite(context, path, false));
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                    IntrinsicTypeDefs.STRING
                }, eval, true);
                classDef.AddMemberLiteral("OpenWriteBinary", newValue.valType, newValue, false);
            }

            //@ bool OpenWriteText(string filepath)
            //   Opens the specified file for writing in text mode.
            //   Returns false if there is an error.
            {
                FunctionValue_Host.EvaluateDelegate eval = (context, args, thisScope) => {
                    string path = args[0] as string;

                    PebbleStreamHelper helper = thisScope as PebbleStreamHelper;
                    return(helper.OpenFileWrite(context, path, true));
                };
                FunctionValue newValue = new FunctionValue_Host(IntrinsicTypeDefs.BOOL, new ArgList {
                    IntrinsicTypeDefs.STRING
                }, eval, true);
                classDef.AddMemberLiteral("OpenWriteText", newValue.valType, newValue, false);
            }

            classDef.FinalizeClass(engine.defaultContext);

            UnitTests.testFuncDelegates.Add("StreamLib", RunTests);
        }
Example #2
0
        /*
         * public override string ToString() {
         *      string argString = "ARGS";
         *      return (_isConst ? "const " : "") + "<" + retType + "(" + argString + ")>";
         * }
         */

        public override ITypeDef Resolve(ExecContext context, ref bool error)
        {
            ITypeDef retValType = retType.Resolve(context, ref error);

            if (null == retValType)
            {
                context.engine.LogCompileError(ParseErrorType.TypeNotFound, "Type '" + retType + "' not found.");
                error = true;
                return(null);
            }

            if (null != argHasDefaults && argHasDefaults.Count < argTypes.Count)
            {
                context.engine.LogCompileError(ParseErrorType.DefaultArgGap, "An argument with a default value is followed by an argument without one.");
                error = true;
                return(null);
            }

            int minArgs = argTypes.Count;

            if (null != argHasDefaults)
            {
                int firstDefault = -1;
                for (int ii = 0; ii < argHasDefaults.Count; ++ii)
                {
                    if (argHasDefaults[ii])
                    {
                        if (firstDefault < 0)
                        {
                            firstDefault = ii;
                            minArgs      = ii;
                        }
                    }
                    else if (firstDefault >= 0)
                    {
                        context.engine.LogCompileError(ParseErrorType.DefaultArgGap, "An argument with a default value is followed by an argument without one.");
                        error = true;
                        return(null);
                    }
                }
            }

            List <ITypeDef> argValTypes = new List <ITypeDef>();

            for (int ii = 0; ii < argTypes.Count; ++ii)
            {
                ITypeDef argValType = argTypes[ii].Resolve(context, ref error);
                if (null == argValType)
                {
                    context.engine.LogCompileError(ParseErrorType.TypeNotFound, "Type '" + argTypes[ii] + "' not found.");
                    error = true;
                    return(null);
                }

                argValTypes.Add(argValType);
            }

            TypeDef_Class classType = null;

            if (null != className)
            {
                ClassDef classDef = context.GetClass(className);
                if (null == classDef)
                {
                    Pb.Assert(false, "Internal error: error resolving class type.");
                }
                classType = classDef.typeDef;
            }

            var ret = TypeFactory.GetTypeDef_Function(retValType, argValTypes, minArgs, varArgs, classType, _isConst, false);

            return(ret);
        }