Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.RightStatementOperation`1"/> class.
        /// </summary>
        /// <param name="reader">Reader.</param>
        public RightStatementOperation(FileIOManager reader)
        {
            // Parse operator
            this.operatorSignature = new OperatorSignature(reader);

            // Parse left hand side
            string nextLine = reader.ReadNextContentLineAndTrim();

            if (CommonHelperMethods.StringStartsWith(nextLine, RightStatement.Name))
            {
                this.leftHandSide = new RightStatement(reader);
            }
            else
            {
                CommonHelperMethods.ThrowStatementParseException(
                    nextLine,
                    reader,
                    RightStatement.Name);
            }

            // Parse right hand side
            nextLine = reader.ReadNextContentLineAndTrim();
            if (CommonHelperMethods.StringStartsWith(nextLine, RightStatement.Name))
            {
                this.rightHandSide = new RightStatement(reader);
            }
            else
            {
                CommonHelperMethods.ThrowStatementParseException(
                    nextLine,
                    reader,
                    RightStatement.Name);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluates the operator.
        /// </summary>
        /// <returns>The operator.</returns>
        /// <param name="signature">Signature.</param>
        /// <param name="lhs">Lhs.</param>
        /// <param name="rhs">Rhs.</param>
        new public static GeneticBool EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs)
        {
            GeneticObject.ValidateOperatorCall(signature, typeof(GeneticBool), lhs, rhs);

            switch (signature.Value)
            {
            case "<":
                return(GeneticBool.OperatorLessThan((GeneticInt)lhs, (GeneticInt)rhs));

            case ">":
                return(GeneticBool.OperatorGreaterThan((GeneticInt)lhs, (GeneticInt)rhs));

            case "==":
                return(GeneticBool.OperatorEquals(lhs, rhs));

            case "!=":
                return(GeneticBool.OperatorNotEquals(lhs, rhs));

            case "&&":
                return(GeneticBool.OperatorAnd((GeneticBool)lhs, (GeneticBool)rhs));

            case "||":
                return(GeneticBool.OperatorAnd((GeneticBool)lhs, (GeneticBool)rhs));
            }

            throw new InvalidOperationException(string.Format(
                                                    "Did not recognize operator with signature {0} with return type {1}",
                                                    signature.Value,
                                                    signature.ReturnType));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.RightStatementOperation"/> class.
        /// </summary>
        /// <param name="returnType">Return type.</param>
        public RightStatementOperation(OperatorSignature signature, int depthIn)
        {
            this.depth = depthIn;

            this.operatorSignature = signature;
            this.leftHandSide      = new RightStatement(signature.LhsType, depthIn + 1);
            this.rightHandSide     = new RightStatement(signature.RhsType, depthIn + 1);
        }
Exemplo n.º 4
0
        private void AddOperator(MethodInfo methodInfo, SprakOperatorAttribute meta)
        {
            BuiltInFunction function = new BuiltInFunction(methodInfo, this);

            if (!Operator.TryParse(out Operator op, name: meta.OperatorName))
            {
                string message = $"{MethodName(methodInfo)} is declared to be an unrecognized operator: \"{meta.OperatorName}\"";
                throw new ArgumentException(message);
            }

            int paramCount = function.Parameters.Count;

            bool binary        = meta.InputsHint == InputSides.Both;
            int  requiredCount = binary ? 2 : 1;

            if (paramCount != requiredCount)
            {
                string desc = binary ? "binary" : "unary";

                string message = $"{MethodName(methodInfo)} was declared to be the {desc} operator \"{meta.OperatorName}\", " +
                                 $"but has {function.Parameters.Count} Sprak arguments";

                throw new ArgumentException(message);
            }

            InputSides inputs = meta.InputsHint;

            SprakType left  = null;
            SprakType right = null;

            switch (inputs)
            {
            case InputSides.Both:
                left  = function.Parameters[0];
                right = function.Parameters[1];
                break;

            case InputSides.Left:
                left = function.Parameters[0];
                break;

            case InputSides.Right:
                right = function.Parameters[0];
                break;
            }

            OperatorTypeSignature typeSignature
                = new OperatorTypeSignature(left, right, inputs);

            OperatorSignature signature = new OperatorSignature(op.Name, typeSignature);

            _operators.Add(signature, function);
        }
Exemplo n.º 5
0
 protected static void ValidateOperatorCall(
     OperatorSignature signature,
     Type expectedReturnType,
     GeneticObject lhs,
     GeneticObject rhs)
 {
     if (signature.ReturnType != expectedReturnType)
     {
         throw new InvalidOperationException(string.Format(
                                                 "Expecting type {0} got type {1}",
                                                 expectedReturnType,
                                                 signature.ReturnType));
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Evaluates the operator.
        /// </summary>
        /// <returns>The operator.</returns>
        /// <param name="signature">Signature.</param>
        /// <param name="lhs">Lhs.</param>
        /// <param name="rhs">Rhs.</param>
        public static GeneticObject EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs)
        {
            if (signature.ReturnType == typeof(GeneticInt))
            {
                return(GeneticInt.EvaluateOperator(signature, lhs, rhs));
            }

            if (signature.ReturnType == typeof(GeneticBool))
            {
                return(GeneticBool.EvaluateOperator(signature, lhs, rhs));
            }

            throw new InvalidOperationException(string.Format(
                                                    "{0} is missing implementation support for an operator {1}",
                                                    signature.ReturnType,
                                                    signature.Value));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Evaluates the operator.
        /// </summary>
        /// <returns>The operator.</returns>
        /// <param name="signature">Signature.</param>
        /// <param name="lhs">Lhs.</param>
        /// <param name="rhs">Rhs.</param>
        new public static GeneticInt EvaluateOperator(OperatorSignature signature, GeneticObject lhs, GeneticObject rhs)
        {
            GeneticObject.ValidateOperatorCall(signature, typeof(GeneticInt), lhs, rhs);

            switch (signature.Value)
            {
            case "+":
                return(GeneticInt.OperatorPlus((GeneticInt)lhs, (GeneticInt)rhs));

            case "-":
                return(GeneticInt.OperatorMinus((GeneticInt)lhs, (GeneticInt)rhs));

            case "*":
                return(GeneticInt.OperatorMultiply((GeneticInt)lhs, (GeneticInt)rhs));

            case "/":
                return(GeneticInt.OperatorDivide((GeneticInt)lhs, (GeneticInt)rhs));
            }

            throw new InvalidOperationException(string.Format(
                                                    "Did not recognize operator with signature {0} with return type {1}",
                                                    signature.Value,
                                                    signature.ReturnType));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Will possibly mutate this section of logic.
        /// </summary>
        public void PossiblyMutate()
        {
            if (GeneticLogicRoot.RollMutateDice())
            {
                OperatorSignature signature;
                if (RegistrationManager.TrySelectOperatorAtRandom(this.ReturnType, out signature))
                {
                    this.operatorSignature = signature;
                    this.leftHandSide      = new RightStatement(signature.LhsType);
                    this.rightHandSide     = new RightStatement(signature.RhsType);
                    return;
                }
            }

            if (GeneticLogicRoot.RollMutateDice())
            {
                this.rightHandSide = new RightStatement(this.operatorSignature.RhsType);
                return;
            }

            if (GeneticLogicRoot.RollMutateDice())
            {
                this.leftHandSide = new RightStatement(this.operatorSignature.LhsType);
                return;
            }

            if (this.rightHandSide != null)
            {
                this.rightHandSide.PossiblyMutate();
            }

            if (this.leftHandSide != null)
            {
                this.leftHandSide.PossiblyMutate();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.RightStatementOperation`1"/> class.
        /// </summary>
        /// <param name="reader">Reader.</param>
        public RightStatementOperation(FileIOManager reader, int depthIn)
        {
            this.depth = depthIn;

            // Parse operator
            this.operatorSignature = new OperatorSignature(reader);

            // Parse left hand side
            byte nextByte = reader.ReadByte();

            if (nextByte == StatementTypeEnum.RightStatement)
            {
                this.leftHandSide = new RightStatement(reader, depthIn + 1);
            }
            else
            {
                CommonHelperMethods.ThrowStatementParseException(
                    nextByte,
                    reader,
                    StatementTypeEnum.RightStatement);
            }

            // Parse right hand side
            nextByte = reader.ReadByte();
            if (nextByte == StatementTypeEnum.RightStatement)
            {
                this.rightHandSide = new RightStatement(reader, depthIn + 1);
            }
            else
            {
                CommonHelperMethods.ThrowStatementParseException(
                    nextByte,
                    reader,
                    StatementTypeEnum.RightStatement);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.RightStatementOperation"/> class.
 /// </summary>
 /// <param name="returnType">Return type.</param>
 public RightStatementOperation(OperatorSignature signature)
 {
     this.operatorSignature = signature;
     this.leftHandSide      = new RightStatement(signature.LhsType);
     this.rightHandSide     = new RightStatement(signature.RhsType);
 }
Exemplo n.º 11
0
 public OperatorOpBinding(Func <Op> builder, OperatorSignature signature) : base(builder)
 {
     Signature = signature;
 }