Exemplo n.º 1
0
 void onValueChange(ValueInt v1, int v2)
 {
     if (_onValueChange != null)
     {
         _onValueChange(v1, v2);
     }
 }
Exemplo n.º 2
0
        public override IdpeMessage Parse(bool onlyConstraints)
        {
            try
            {
                if (base.Value.ToUpper() != "NULL")
                {
                    this._ParseResult = new IdpeMessage(IdpeMessageCodes.IDPE_SUCCESS);
                    if (ValueInt == null)
                    {
                        TryExtractingSpecificType();
                        if (ValueInt == null)  //still null, then throw
                        {
                            this._ParseResult         = new IdpeMessage(IdpeMessageCodes.IDPE_INT_TYPE_DATA_VALIDATION_FAILED);
                            this._ParseResult.Message = string.Format(this._ParseResult.Message, PrintRowColPosition(), Type, base.Value);
                            return(this._ParseResult);
                        }
                    }

                    Value = ValueInt.ToString();
                    CheckConstraints();
                }
            }
            catch (Exception ex)
            {
                ExtensionMethods.TraceError(ex.ToString());
                this._ParseResult         = new IdpeMessage(IdpeMessageCodes.IDPE_INT_TYPE_DATA_VALIDATION_FAILED);
                this._ParseResult.Message = string.Format(this._ParseResult.Message, PrintRowColPosition(), Type, base.Value);
            }
            _IsParsed = true;
            return(this._ParseResult);
        }
Exemplo n.º 3
0
Arquivo: Int.cs Projeto: takuto-h/rhea
 public static void Add(Arguments args, VM vm, SourceInfo info)
 {
     Check(args, 2, info);
     ValueInt integer1 = (ValueInt)args[0];
     ValueInt integer2 = (ValueInt)args[1];
     ValueInt newInteger = new ValueInt(
         integer1.Value + integer2.Value
     );
     vm.Push(newInteger);
 }
Exemplo n.º 4
0
        private void readValueArray(BinaryReader br)
        {
            if (ValueLength == 0)
            {
                return;
            }

            Value[] Values = new Value[ArrayCount];

            for (int i = 0; i < ArrayCount; i++)
            {
                switch (Type)
                {
                case ShaderParameterType.@int:
                    Values[i] = new ValueInt(br);
                    break;

                case ShaderParameterType.@float:
                    Values[i] = new ValueFloat(br);
                    break;

                case ShaderParameterType.float2:
                    Values[i] = new ValueFloat2(br);
                    break;

                case ShaderParameterType.float3:
                    Values[i] = new ValueFloat3(br);
                    break;

                case ShaderParameterType.float4:
                    Values[i] = new ValueFloat4(br);
                    break;

                case ShaderParameterType.@bool:
                    Values[i] = new ValueBool(br);
                    break;

                case ShaderParameterType.float4x3:
                    Values[i] = new ValueFloat4x3(br);
                    break;

                case ShaderParameterType.float4x4:
                    Values[i] = new ValueFloat4x4(br);
                    break;

                case ShaderParameterType.sampler:
                    int len = ValueLength / ArrayCount;
                    Values[i] = new ValueSamplerState(br, len);
                    break;
                }
            }
            Value = new ValueList(Values);
        }
Exemplo n.º 5
0
        private void readValue(BinaryReader br)
        {
            if (ValueLength == 0)
            {
                return;
            }

            switch (Type)
            {
            case ShaderParameterType.@int:
                Value = new ValueInt(br);
                break;

            case ShaderParameterType.@float:
                Value = new ValueFloat(br);
                break;

            case ShaderParameterType.float2:
                Value = new ValueFloat2(br);
                break;

            case ShaderParameterType.float3:
                Value = new ValueFloat3(br);
                break;

            case ShaderParameterType.float4:
                Value = new ValueFloat4(br);
                break;

            case ShaderParameterType.@bool:
                Value = new ValueBool(br);
                break;

            case ShaderParameterType.float4x3:
                Value = new ValueFloat4x3(br);
                break;

            case ShaderParameterType.float4x4:
                Value = new ValueFloat4x4(br);
                break;

            case ShaderParameterType.sampler:
                Value = new ValueSamplerState(br, ValueLength);
                break;
            }
        }
Exemplo n.º 6
0
        public string GetDisplayableValue()
        {
            if (ValueInt != null)
            {
                return(ValueInt.ToString());
            }

            if (ValueFloat != null)
            {
                return(ValueFloat.ToString());
            }

            if (ValueCol != null)
            {
                return(ValueCol.ToString());
            }

            return("#unimplemented value");
        }
Exemplo n.º 7
0
        public bool IsValueDefault()
        {
            if (ValueInt != null)
            {
                return(ValueInt.Equals(ValueIntDefault));
            }

            if (ValueFloat != null)
            {
                return(ValueFloat.Equals(ValueFloatDefault));
            }

            if (ValueCol != null)
            {
                return(ValueCol.Equals(ValueColDefault));
            }

            return(false);
        }
    public int CompareTo(TestClass other)
    {
        if (ReferenceEquals(this, other))
        {
            return(0);
        }
        if (ReferenceEquals(null, other))
        {
            return(1);
        }

        var valueIntComparison = ValueInt.CompareTo(other.ValueInt);

        if (valueIntComparison != 0)
        {
            return(valueIntComparison);
        }

        return(ValueChar.CompareTo(other.ValueChar));
    }
Exemplo n.º 9
0
        public void TestNumber()
        {
            Value pattern = PatternData.Single("a", ValueType.Number);
            Value match = null, leftover = null;

            {	// single int type - match
                Value input = new ValueInt(42);
                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(42, match.AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// single float type - match
                Value input = new ValueFloat(3.5);
                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(3.5, match.AsFloat);
                Assert.AreEqual(null, leftover);
            }

            {	// single string type - not a match
                Value input = new ValueString("hello");
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }
        }
Exemplo n.º 10
0
        public void TestMapFromArrayPattern()
        {
            List<Value> list = new List<Value>();
            list.Add(PatternData.Single("a", ValueType.Int));
            list.Add(PatternData.Single("b", new ValueInt(17)));
            ValueArray pattern = new ValueArray(list);
            Value match = null, leftover = null;

            {	// get a & b
                Map map = new Map();
                map["a"] = new ValueInt(42);
                map["b"] = new ValueInt(31);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(42, match.AsMap["a"].AsInt);
                Assert.AreEqual(31, match.AsMap["b"].AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// fill in default for b
                Map map = new Map();
                map["a"] = new ValueInt(42);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(42, match.AsMap["a"].AsInt);
                Assert.AreEqual(17, match.AsMap["b"].AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// leftover
                Map map = new Map();
                map["b"] = new ValueInt(31);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(1, match.AsMap.Count);
                Assert.AreEqual(31, match.AsMap["b"].AsInt);
                Assert.AreEqual(1, leftover.AsMap.Count);
                Assert.AreEqual("a", leftover.AsMap["a"].AsString);
            }

            {	// get a & b, ignore c because bShortPat=true
                Map map = new Map();
                map["a"] = new ValueInt(42);
                map["b"] = new ValueInt(31);
                map["c"] = new ValueInt(31);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, true/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(42, match.AsMap["a"].AsInt);
                Assert.AreEqual(31, match.AsMap["b"].AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// missing key
                List<Value> list2 = new List<Value>();
                list2.Add(PatternData.Single("a", ValueType.Int));
                list2.Add(PatternData.Rest("other"));
                ValueArray pattern2 = new ValueArray(list2);

                Map map = new Map();
                map["b"] = new ValueInt(31);
                map["c"] = new ValueInt(31);
                ValueMap input = new ValueMap(map);
                Assert.IsFalse(PatternChecker.Do(input, pattern2, true/*bShortPat*/, out match, out leftover));

                // but if "a" is present, it matches
                map["a"] = new ValueInt(42);
                Assert.IsTrue(PatternChecker.Do(input, pattern2, true/*bShortPat*/, out match, out leftover));
            }
        }
Exemplo n.º 11
0
        public void TestMapDefault()
        {
            Map map = new Map();
            map["first"] = PatternData.Single("first", new ValueInt(156));	// default
            map["second"] = PatternData.Single("second");
            ValueMap pattern = new ValueMap(map);
            Value match = null, leftover = null;

            {	// input is missing key w/ default - full match
                map = new Map();
                map["second"] = new ValueInt(3);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(156, match.AsMap["first"].AsInt);
                Assert.AreEqual(3, match.AsMap["second"].AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// input is missing key w/out default - match w/ leftover
                map = new Map();
                map["first"] = new ValueInt(5);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(1, match.AsMap.Count);
                Assert.AreEqual(5, match.AsMap["first"].AsInt);
                Assert.AreEqual(1, leftover.AsMap.Count);
                Assert.AreEqual("second", leftover.AsMap["second"].AsString);
            }
        }
Exemplo n.º 12
0
        public void TestMap()
        {
            Map map = new Map();
            map["do?"] = PatternData.Single("do?", ValueType.Bool);
            map["body"] = PatternData.Body();
            ValueMap pattern = new ValueMap(map);
            Value match = null, leftover = null;

            {	// single object - not a match
                Value input = PatternData.Single("a");
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// input is a map w/ fewer keys - match w/ leftover
                map = new Map();
                map["do?"] = new ValueBool(true);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(1, match.AsMap.Count);
                Assert.AreEqual(true, match.AsMap["do?"].AsBool);
                Assert.AreEqual(1, leftover.AsMap.Count);
                Assert.AreEqual("l3.body", leftover.AsMap["body"].AsString);
            }

            {	// map w/ same keys - match
                map = new Map();
                map["do?"] = new ValueBool(true);
                map["body"] = PatternData.Body();
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(true, match.AsMap["do?"].AsBool);
                Assert.AreEqual("l3.body", match.AsMap["body"].AsString);
                Assert.AreEqual(null, leftover);
            }

            {	// map w/ extra keys - not a match
                map = new Map();
                map["do?"] = new ValueBool(true);
                map["body"] = PatternData.Body();
                map["something"] = new ValueInt(42);
                ValueMap input = new ValueMap(map);

                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }
        }
Exemplo n.º 13
0
        public void TestBool()
        {
            Value pattern = PatternData.Single("a", ValueType.Bool);
            Value match = null, leftover = null;

            {	// single bool type - match
                Value input = new ValueBool(true);
                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.IsTrue(match.AsBool);
                Assert.AreEqual(null, leftover);
            }

            {	// single int type - not a match
                Value input = new ValueInt(42);
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }
        }
Exemplo n.º 14
0
        public void TestArray()
        {
            List<Value> list = new List<Value>();
            list.Add(PatternData.Single("a", ValueType.Int));
            list.Add(PatternData.Single("b", ValueType.String));
            ValueArray pattern = new ValueArray(list);
            Value match = null, leftover = null;

            {	// single object - not a match
                Value input = new ValueInt(37);
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// array with one item - match with leftover
                List<Value> one = new List<Value>();
                one.Add(new ValueInt(5));
                ValueArray input = new ValueArray(one);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(1, match.AsArray.Count);
                Assert.AreEqual(5, match.AsArray[0].AsInt);
                Assert.AreEqual(1, leftover.AsArray.Count);
                Assert.AreEqual("b", leftover.AsArray[0].AsString);
            }

            {	// array with two items of proper type - match
                List<Value> two = new List<Value>();
                two.Add(new ValueInt(5));
                two.Add(new ValueString("hello"));
                ValueArray input = new ValueArray(two);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsArray.Count);
                Assert.AreEqual(5, match.AsArray[0].AsInt);
                Assert.AreEqual("hello", match.AsArray[1].AsString);
                Assert.AreEqual(null, leftover);
            }

            {	// array with two items of wrong type - not a match
                List<Value> two = new List<Value>();
                two.Add(new ValueInt(5));
                two.Add(new ValueFloat(2.718));
                ValueArray input = new ValueArray(two);

                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// array with three items - not a match
                List<Value> three = new List<Value>();
                three.Add(new ValueInt(5));
                three.Add(new ValueString("hello"));
                three.Add(new ValueString("goodbye"));
                ValueArray input = new ValueArray(three);

                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// array with three items & smaller pattern is allowed
                List<Value> three = new List<Value>();
                three.Add(new ValueInt(5));
                three.Add(new ValueString("hello"));
                three.Add(new ValueString("goodbye"));
                ValueArray input = new ValueArray(three);

                Assert.IsTrue(PatternChecker.Do(input, pattern, true/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsArray.Count);
                Assert.AreEqual(5, match.AsArray[0].AsInt);
                Assert.AreEqual("hello", match.AsArray[1].AsString);
                Assert.AreEqual(null, leftover);
            }
        }
Exemplo n.º 15
0
        public void TestArrayOneOf()
        {
            // passed value must be a member of this list
            List<Value> options = new List<Value>();
            options.Add(new ValueInt(5));
            options.Add(new ValueInt(6));
            ValueArray oneOf = new ValueArray(options);

            List<Value> list = new List<Value>();
            list.Add(PatternData.Single("a", oneOf));
            ValueArray pattern = new ValueArray(list);
            Value match = null, leftover = null;

            {	// single object that's not a match
                Value input = new ValueInt(4);
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// array with one item that matches one of the options
                List<Value> one = new List<Value>();
                one.Add(new ValueInt(6));
                ValueArray input = new ValueArray(one);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(1, match.AsArray.Count);
                Assert.AreEqual(6, match.AsArray[0].AsInt);
                Assert.AreEqual(null, leftover);
            }
        }
Exemplo n.º 16
0
 /// <summary>Tack on the line number where the error occurred</summary>
 internal Loki3Exception AddLineNumber(int line)
 {
     if (!m_map.ContainsKey(keyLineNumber))
         m_map[keyLineNumber] = new ValueInt(line);
     return this;
 }
Exemplo n.º 17
0
        public void TestGetTypeFunction()
        {
            IScope scope = CreateValueScope();

            {	// built-in type w/out metadata
                scope.SetValue("a", new ValueInt(42));
                Value value = TestSupport.ToValue("l3.getType { :value a }", scope);
                Assert.AreEqual("int", value.AsString);
                value = TestSupport.ToValue("l3.getType { :value a :builtin? true }", scope);
                Assert.AreEqual("int", value.AsString);
                value = TestSupport.ToValue("l3.getType { :key :a }", scope);
                Assert.AreEqual("int", value.AsString);
            }

            {	// metadata type
                Value b = new ValueInt(42);
                b.MetaType = "myType";
                scope.SetValue("b", b);

                Value value = TestSupport.ToValue("l3.getType { :value b }", scope);
                Assert.AreEqual("myType", value.AsString);
                value = TestSupport.ToValue("l3.getType { :value b :builtin? true }", scope);
                Assert.AreEqual("int", value.AsString);
            }
        }
Exemplo n.º 18
0
        public void TestGetValue()
        {
            IScope scope = CreateValueScope();

            scope.SetValue("aString", new ValueString("testing"));

            Map map = new Map();
            map["one"] = new ValueInt(1);
            map["two"] = new ValueInt(2);
            ValueMap vMap = new ValueMap(map);
            scope.SetValue("aMap", vMap);

            List<Value> array = new List<Value>();
            array.Add(new ValueInt(2));
            array.Add(new ValueInt(4));
            scope.SetValue("anArray", new ValueArray(array));

            {
                Value value = TestSupport.ToValue("l3.getValue { :object aString :key 2 }", scope);
                Assert.AreEqual("s", value.AsString);
            }

            {
                Value value = TestSupport.ToValue("l3.getValue { :object aMap :key :two }", scope);
                Assert.AreEqual(2, value.AsInt);
            }

            {
                Value value = TestSupport.ToValue("l3.getValue { :object anArray :key 1 }", scope);
                Assert.AreEqual(4, value.AsInt);
            }

            {	// providing a default when the key isn't present
                Value value = TestSupport.ToValue("l3.getValue { :object aMap :key :notThere :default 42 }", scope);
                Assert.AreEqual(42, value.AsInt);
                // default ignored if key is present
                value = TestSupport.ToValue("l3.getValue { :object aMap :key :one :default 42 }", scope);
                Assert.AreEqual(1, value.AsInt);
            }

            // asking for a key that doesn't exist returns nil...
            {
                Value value = TestSupport.ToValue("l3.getValue { :object aMap :key :notThere }", scope);
                Assert.IsTrue(value.IsNil);
            }

            // ...unless the collection has a default
            {
                vMap.WritableMetadata[PatternData.keyDefault] = new ValueInt(42);
                Value value = TestSupport.ToValue("l3.getValue { :object aMap :key :notThere }", scope);
                Assert.AreEqual(42, value.AsInt);
            }
        }
Exemplo n.º 19
0
 public override string ToPromela()
 {
     return(Attribute.ToPromela() + " = " + (Value == null ? ValueInt.ToString() : Value.ToPromela()) + ";");
 }
Exemplo n.º 20
0
        public void TestRestOfMap()
        {
            Map map = new Map();
            map["a"] = PatternData.Single("a", ValueType.Int);
            map["other"] = PatternData.Rest("other");
            ValueMap pattern = new ValueMap(map);
            Value match = null, leftover = null;

            {	// input is a map w/ fewer keys - match w/ leftover
                map = new Map();
                map["a"] = new ValueInt(14);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(14, match.AsMap["a"].AsInt);
                Map rest = match.AsMap["other"].AsMap;
                Assert.AreEqual(0, rest.Count);				// 'rest' is present but empty
                Assert.AreEqual(null, leftover);
            }

            {	// input contains extra keys that will get stuffed in 'other'
                map = new Map();
                map["a"] = new ValueInt(14);
                map["b"] = new ValueInt(3);
                map["c"] = new ValueInt(15);
                ValueMap input = new ValueMap(map);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsMap.Count);
                Assert.AreEqual(14, match.AsMap["a"].AsInt);
                Map rest = match.AsMap["other"].AsMap;
                Assert.AreEqual(2, rest.Count);
                Assert.AreEqual(3, rest["b"].AsInt);
                Assert.AreEqual(15, rest["c"].AsInt);
                Assert.AreEqual(null, leftover);
            }
        }
Exemplo n.º 21
0
        public static bool TryCreateValue(string typeString, string valueString, out Value value)
        {
            value = null;

            var isNull = valueString.Equals("NULL") || valueString.Equals("null") || valueString.Equals("");

            switch (typeString)
            {
            case "integer":
                if (isNull)
                {
                    value = new ValueInt(null);
                    return(true);
                }
                if (!long.TryParse(valueString, out var intResult))
                {
                    return(false);
                }
                value = new ValueInt(intResult);
                return(true);

            case "string":
                if (isNull)
                {
                    value = new ValueString(null);
                    return(true);
                }
                value = new ValueString(valueString.Trim('\"'));
                return(true);

            case "time":
                if (isNull)
                {
                    value = new ValueTime(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueTime(valueString);
                return(true);

            case "double":
                if (isNull)
                {
                    value = new ValueDouble(null);
                    return(true);
                }
                if (!double.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out var doubleResult))
                {
                    return(false);
                }
                value = new ValueDouble(doubleResult);
                return(true);

            case "boolean":
                if (isNull)
                {
                    value = new ValueBoolean(null);
                    return(true);
                }
                if (!bool.TryParse(valueString, out var booleanResult))
                {
                    return(false);
                }
                value = new ValueBoolean(booleanResult);
                return(true);

            case "duration":
                if (isNull)
                {
                    value = new ValueDuration(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueDuration(valueString);
                return(true);

            case "contact":
                if (isNull)
                {
                    value = new ValueContact(null, null);
                    return(true);
                }

                var split = valueString.Split(' ');
                if (split.Length != 2 && split.Length != 3)
                {
                    return(false);
                }
                if (!IPAddress.TryParse(split[1], out var address))
                {
                    return(false);
                }
                var port = 555;
                if (split.Length == 3 && !int.TryParse(split[2], out port))
                {
                    return(false);
                }
                if (!split[0].StartsWith("/"))
                {
                    split[0] = '/' + split[0];
                }
                value = new ValueContact(new PathName(split[0]), address, port);
                return(true);
            }

            var splitByOf = typeString.Split(" of ", 2);

            if (splitByOf.Length != 2)
            {
                return(false);
            }
            var splitBySpace = splitByOf[1].Split(' ', 2);

            if (splitBySpace.Length != 2)
            {
                return(false);
            }

            if (!int.TryParse(splitBySpace[0], out var count))
            {
                return(false);
            }

            // based on StackOverflow answer: https://stackoverflow.com/a/3147901
            var collectionStrings = Regex.Split(valueString.Trim('{', '}', '[', ']'), ",(?=(?:[^']*'[^']*')*[^']*$)")
                                    .Select(s => s.Trim(' ')).ToList();

            if (collectionStrings.Count != count)
            {
                if (count != 0 && (collectionStrings.Count != 1 || !string.IsNullOrEmpty(collectionStrings[0])))
                {
                    return(false);
                }
                collectionStrings = new List <string>(count);
            }

            var elements          = new List <Value>(count);
            var elementTypeString = splitBySpace[1];

            foreach (var element in collectionStrings)
            {
                if (!TryCreateValue(elementTypeString, element, out var elementVal))
                {
                    return(false);
                }
                elements.Add(elementVal);
            }

            if (!TryGetPrimitiveAttributeType(elementTypeString, out var elementType))
            {
                return(false);
            }

            switch (splitByOf[0])
            {
            case "set":
                value = new ValueSet(elements.ToHashSet(), elementType);
                return(true);

            case "list":
                value = new ValueList(elements, elementType);
                return(true);
            }

            return(false);
        }
Exemplo n.º 22
0
        public void TestSetValue()
        {
            IScope scope = CreateValueScope();

            {
                Value value = TestSupport.ToValue("l3.setValue { :key :a :value 5 }", scope);
                Assert.AreEqual(5, value.AsInt);

                Value fetch = scope.GetValue(new Token("a"));
                Assert.AreEqual(5, fetch.AsInt);
            }

            {	// change a
                Value value = TestSupport.ToValue("l3.setValue { :key :a :value 7.5 }", scope);
                Assert.AreEqual(7.5, value.AsFloat);

                Value fetch = scope.GetValue(new Token("a"));
                Assert.AreEqual(7.5, fetch.AsFloat);
            }

            {	// set an array
                Value value = TestSupport.ToValue("l3.setValue { :key :key :value [ a 2 false ] }", scope);
                List<Value> array = value.AsArray;
                Assert.AreEqual(3, array.Count);
                Assert.AreEqual(7.5, array[0].AsFloat);
                Assert.AreEqual(2, array[1].AsInt);
                Assert.AreEqual(false, array[2].AsBool);

                Value fetch = scope.GetValue(new Token("key"));
                List<Value> fetchArray = fetch.AsArray;
                Assert.AreEqual(3, fetchArray.Count);
                Assert.AreEqual(7.5, fetchArray[0].AsFloat);
                Assert.AreEqual(2, fetchArray[1].AsInt);
                Assert.AreEqual(false, fetchArray[2].AsBool);
            }

            // create a nested scope
            IScope nested = new ScopeChain(scope);

            {	// explicitly say create-if-needed
                TestSupport.ToValue("l3.setValue { :key :created :value 3 :create? true }", nested);
                Value fetch = nested.GetValue(new Token("created"));
                Assert.AreEqual(3, fetch.AsInt);
            }

            {	// asking to reuse non-existent var throws
                bool bThrew = false;
                try
                {
                    Value value = TestSupport.ToValue("l3.setValue { :key :doesnt-exist :value 3 :create? false }", nested);
                }
                catch (Loki3Exception e)
                {
                    bThrew = true;
                    Assert.AreEqual("doesnt-exist", e.Errors[Loki3Exception.keyBadToken].AsString);
                }
                Assert.IsTrue(bThrew);
            }

            {	// set var on parent scope & reuse on nested scope
                TestSupport.ToValue("l3.setValue { :key :on-parent :value 5 :create? true }", scope);
                Assert.AreEqual(5, scope.AsMap["on-parent"].AsInt);
                TestSupport.ToValue("l3.setValue { :key :on-parent :value 4 :create? false }", nested);
                Assert.AreEqual(4, scope.AsMap["on-parent"].AsInt);
                Assert.AreNotEqual(null, nested.Exists("on-parent"));
                Assert.IsFalse(nested.AsMap.ContainsKey("on-parent"));
            }

            {	// set a value on a map
                Map map = new Map();
                map["one"] = new ValueInt(1);
                map["two"] = new ValueInt(2);
                ValueMap valueMap = new ValueMap(map);
                scope.SetValue("mymap", valueMap);

                Value value = TestSupport.ToValue("l3.setValue { :key :one :value 3 :map mymap }", scope);
                Assert.AreEqual(3, value.AsInt);
                Assert.AreEqual(3, map["one"].AsInt);
            }

            {	// pattern matching: extract values out of an array
                Value value = TestSupport.ToValue("l3.setValue { :key [ :first :second :third ] :value [ 11 22 33 ] :create? true }", scope);
                Assert.AreEqual(11, scope.AsMap["first"].AsInt);
                Assert.AreEqual(22, scope.AsMap["second"].AsInt);
                Assert.AreEqual(33, scope.AsMap["third"].AsInt);
            }

            {	// special case: initialize every token in array to nil
                Value value = TestSupport.ToValue("l3.setValue { :key [ :first :second :third ] :value nil :create? true }", scope);
                Assert.IsTrue(scope.AsMap["first"].IsNil);
                Assert.IsTrue(scope.AsMap["second"].IsNil);
                Assert.IsTrue(scope.AsMap["third"].IsNil);
            }

            {	// if initOnly?, don't overwrite existing values
                Value value = TestSupport.ToValue("l3.setValue { :key [ :aa :bb ] :value [ 2 3 ] :create? true :initOnly? true }", scope);
                Assert.AreEqual(2, scope.AsMap["aa"].AsInt);
                Assert.AreEqual(3, scope.AsMap["bb"].AsInt);
                value = TestSupport.ToValue("l3.setValue { :key [ :aa :bb ] :value [ 4 5 ] :create? true :initOnly? true }", scope);
                Assert.AreEqual(2, scope.AsMap["aa"].AsInt);
                Assert.AreEqual(3, scope.AsMap["bb"].AsInt);
                value = TestSupport.ToValue("l3.setValue { :key [ :bb :cc ] :value nil :create? true :initOnly? true }", scope);
                Assert.AreEqual(3, scope.AsMap["bb"].AsInt);
                Assert.IsTrue(scope.AsMap["cc"].IsNil);
            }

            {	// if returnSuccess?, return value tells whether a value was set
                Value value = TestSupport.ToValue("l3.setValue { :key [ :x :y ] :value [ 2 3 ] :returnSuccess? true }", scope);
                Assert.IsTrue(value.AsBool);
                value = TestSupport.ToValue("l3.setValue { :key [ :x :y :z ] :value [ 2 3 ] :returnSuccess? true }", scope);
                Assert.IsFalse(value.AsBool);
            }
        }
Exemplo n.º 23
0
 private void Init(string end, DelimiterType type, ValueFunction function)
 {
     Map meta = WritableMetadata;
     meta[keyDelimEnd] = new ValueString(end);
     meta[keyDelimType] = new ValueInt((int)type);
     if (function != null)
         meta[keyDelimFunction] = function;
 }
Exemplo n.º 24
0
        public void TestArrayDefault()
        {
            List<Value> list = new List<Value>();
            list.Add(PatternData.Single("a", ValueType.Int));
            list.Add(PatternData.Single("b", new ValueInt(17)));
            ValueArray pattern = new ValueArray(list);
            Value match = null, leftover = null;

            {	// single object - not a match
                Value input = new ValueInt(37);
                Assert.IsFalse(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
            }

            {	// array with one item - match with default added
                List<Value> one = new List<Value>();
                one.Add(new ValueInt(5));
                ValueArray input = new ValueArray(one);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsArray.Count);
                Assert.AreEqual(5, match.AsArray[0].AsInt);
                Assert.AreEqual(17, match.AsArray[1].AsInt);
                Assert.AreEqual(null, leftover);
            }

            {	// array with two items of proper type - match
                List<Value> two = new List<Value>();
                two.Add(new ValueInt(5));
                two.Add(new ValueString("hello"));
                ValueArray input = new ValueArray(two);

                Assert.IsTrue(PatternChecker.Do(input, pattern, false/*bShortPat*/, out match, out leftover));
                Assert.AreEqual(2, match.AsArray.Count);
                Assert.AreEqual(5, match.AsArray[0].AsInt);
                Assert.AreEqual("hello", match.AsArray[1].AsString);
                Assert.AreEqual(null, leftover);
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// Define info on the parameters and order the function will get run in
 /// </summary>
 /// <param name="previousPattern">if func consumes previous value, this describes the pattern</param>
 /// <param name="nextPattern">if func consumes next value, this describes the pattern</param>
 /// <param name="order">evaluation precedence of function within a list</param>
 protected void Init(Value previousPattern, Value nextPattern, Order order)
 {
     Map meta = WritableMetadata;
     if (previousPattern != null && !previousPattern.IsNil)
         meta[keyPreviousPattern] = previousPattern;
     if (nextPattern != null && !nextPattern.IsNil)
         meta[keyNextPattern] = nextPattern;
     meta[keyOrder] = new ValueInt((int)order);
 }
Exemplo n.º 26
0
 public int GetInt()
 {
     ValueInt v = PropValue as ValueInt; return(v != null ? v.Int : 0);
 }
Exemplo n.º 27
0
 /// <summary>
 /// Make a parameter that's an array of two values,
 /// each of which could be an int or float
 /// </summary>
 private DelimiterNode MakePair(double a, double b, bool bInt1, bool bInt2)
 {
     List<Value> list = new List<Value>();
     Value value1, value2;
     if (bInt1)
         value1 = new ValueInt((int)a);
     else
         value1 = new ValueFloat(a);
     list.Add(value1);
     if (bInt2)
         value2 = new ValueInt((int)b);
     else
         value2 = new ValueFloat(b);
     list.Add(value2);
     return new DelimiterNodeValue(new ValueArray(list));
 }