Example #1
0
        public void GetMeta()
        {
            // Floor
            _scope.AddNativeMethod(new FuncMethodBuilder()
            {
                Name          = "Floor",
                Documentation = "Rounds the provided number down to the nearest integer.",
                ReturnType    = this,
                Action        = (actionSet, methodCall) => Element.RoundToInt(actionSet.CurrentObject, Rounding.Down)
            }.GetMethod());
            // Ceil
            _scope.AddNativeMethod(new FuncMethodBuilder()
            {
                Name          = "Ceil",
                Documentation = "Return the ceiling of the provided number.",
                ReturnType    = this,
                Action        = (actionSet, methodCall) => Element.RoundToInt(actionSet.CurrentObject, Rounding.Up)
            }.GetMethod());
            // Round
            _scope.AddNativeMethod(new FuncMethodBuilder()
            {
                Name          = "Round",
                Documentation = "Returns the provided number rounded to the nearest integer.",
                ReturnType    = this,
                Action        = (actionSet, methodCall) => Element.RoundToInt(actionSet.CurrentObject, Rounding.Nearest)
            }.GetMethod());
            // Absolute value
            _scope.AddNativeMethod(new FuncMethodBuilder()
            {
                Name          = "Abs",
                Documentation = "Returns the absolute value of the provided number. Also known as the value's distance to 0.",
                ReturnType    = this,
                Action        = (actionSet, methodCall) => Element.Abs(actionSet.CurrentObject)
            }.GetMethod());

            Operations.AddTypeOperation(new TypeOperation[] {
                new TypeOperation(TypeOperator.Add, this, this),                                  // Number + number
                new TypeOperation(TypeOperator.Subtract, this, this),                             // Number - number
                new TypeOperation(TypeOperator.Multiply, this, this),                             // Number * number
                new TypeOperation(TypeOperator.Divide, this, this),                               // Number / number
                new TypeOperation(TypeOperator.Modulo, this, this),                               // Number % number
                new TypeOperation(TypeOperator.Pow, this, this),
                new TypeOperation(TypeOperator.Multiply, _supplier.Vector(), _supplier.Vector()), // Number * vector
                new TypeOperation(TypeOperator.LessThan, this, _supplier.Boolean()),              // Number < number
                new TypeOperation(TypeOperator.LessThanOrEqual, this, _supplier.Boolean()),       // Number <= number
                new TypeOperation(TypeOperator.GreaterThanOrEqual, this, _supplier.Boolean()),    // Number >= number
                new TypeOperation(TypeOperator.GreaterThan, this, _supplier.Boolean()),           // Number > number
            });
            Operations.AddTypeOperation(AssignmentOperation.GetNumericOperations(this));
        }
Example #2
0
        void ResolveElements()
        {
            Operations.AddTypeOperation(new TypeOperation[] {
                new TypeOperation(_deltinScript.Types, TypeOperator.Equal, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.NotEqual, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.GreaterThan, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.GreaterThanOrEqual, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.LessThan, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.LessThanOrEqual, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.And, this),
                new TypeOperation(_deltinScript.Types, TypeOperator.Or, this),

                new TypeOperation(TypeOperator.Add, this, this),
                new TypeOperation(TypeOperator.Divide, this, this),
                new TypeOperation(TypeOperator.Modulo, this, this),
                new TypeOperation(TypeOperator.Multiply, this, this),
                new TypeOperation(TypeOperator.Pow, this, this),
                new TypeOperation(TypeOperator.Subtract, this, this)
            });
            Operations.AddTypeOperation(AssignmentOperation.GetNumericOperations(this));
        }
        public SetVariableAction(ParseInfo parseInfo, Scope scope, Assignment assignmentContext)
        {
            // Get the variable expression.
            IExpression variableExpression = parseInfo.GetExpression(scope, assignmentContext.VariableExpression);

            // Extract the variable data.
            _variableResolve = new VariableResolve(parseInfo, new VariableResolveOptions()
            {
                ShouldBeSettable = true
            }, variableExpression, assignmentContext.VariableExpression.Range);

            // Get the value.
            _value = parseInfo.SetExpectType(_variableResolve.SetVariable?.Type()).GetExpression(scope, assignmentContext.Value);

            // Get the operation.
            Token              assignmentToken = assignmentContext.AssignmentToken;
            CodeType           variableType = variableExpression.Type(), valueType = _value.Type();
            AssignmentOperator op = AssignmentOperation.OperatorFromTokenType(assignmentToken.TokenType);

            _operation = variableType.Operations.GetOperation(op, valueType);

            // No operators exist for the variable and value pair.
            if (_operation == null)
            {
                // If the variable type is any, use default operation.
                if (assignmentToken.TokenType == TokenType.Equal && variableType.Operations.DefaultAssignment && TypeComparison.IsAny(parseInfo.Types, variableType))
                {
                    _operation = new AssignmentOperation(op, parseInfo.Types.Any());
                }
                // Otherwise, add an error.
                else
                {
                    parseInfo.Script.Diagnostics.Error("Operator '" + assignmentToken.Text + "' cannot be applied to the types '" + variableType.GetNameOrAny() + "' and '" + valueType.GetNameOrAny() + "'.", assignmentToken.Range);
                }
            }
        }