/// <summary> /// Builds ASymbol. /// </summary> /// <param name="shape">The shape of the AType.</param> /// <param name="symbolLengths">The lengths of the ASymbols contained by the AType.</param> /// <param name="info">The class containing the informations for importing.</param> /// <returns></returns> private AType BuildSymbolArray(List <int> shape, IEnumerable <int> symbolLengths, ImportInfo info) { AType result = Utils.ANull(); if (shape.Count == 0) { StringBuilder toSymbol = new StringBuilder(); int length = symbolLengths.First(); for (int i = 0; i < length; i++) { toSymbol.Append((char)info.Data[info.DataIndex]); info.DataIndex++; } result = ASymbol.Create(toSymbol.ToString()); } else { List <int> nextShape = (shape.Count > 1) ? shape.GetRange(1, shape.Count - 1) : new List <int>(); int subDimensionLength = nextShape.Product(); for (int i = 0; i < shape[0]; i++) { IEnumerable <int> nextSymbolLengths = symbolLengths.Skip(i * subDimensionLength).Take(subDimensionLength); result.Add(BuildSymbolArray(nextShape, nextSymbolLengths, info)); // advance the bytestream further. } } return(result); }
public void SelectiveAssignment() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create("ctx"), Utils.ANull(), AInteger.Create(0), ABox.Create(AArray.Create(ATypes.ASymbol, ASymbol.Create("Scalar"), ASymbol.Create("Vector"), ASymbol.Create("Matrix"))), Helpers.BuildString("static") } ); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("$cx ctx", scope); this.engine.Execute("b := (`scalar `vector `matrix; (3.14; 'abcdef'; iota 3 2))", scope); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f)}", scope); this.engine.Execute("_scb{`b;(cbf;'static')}", scope); this.engine.Execute("(1 0 /b) := <`Scalar `Vector `Matrix", scope); Assert.AreEqual(expected.CompareInfos(scope.GetVariable <AType>(".result")), InfoResult.OK); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); }
public void SymToInt() { AType expected = AInteger.Create(ASymbol.Create("valami").GetHashCode()); AType result = this.engine.Execute <AType>("`int bwor `valami"); Assert.AreEqual(expected, result); }
public Aplus(Scope dlrglobals, LexerMode parsemode) { this.sysvars = new SystemVariables(); this.dependencies = new DependencyManager(); this.callbackManager = new CallbackManager(); this.dlrglobals = dlrglobals; this.globals = new DYN.ExpandoObject(); this.sysvars["mode"] = ASymbol.Create(parsemode.ToString().ToLower()); this.mmfmanager = new MemoryMappedFileManager(); this.systemFunctions = Function.SystemFunction.DiscoverSystemFunctions(); if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("APATH", EnvironmentVariableTarget.User))) { string paths = String.Join(";", ".", "./Runtime/Context/"); Environment.SetEnvironmentVariable("APATH", paths, EnvironmentVariableTarget.User); } // TODO: Move this to app.config? this.autoloadContexts = new string[] { "sys" }; this.contextLoader = new ContextLoader(this); }
public void PickChooseExample() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create("ctx"), ASymbol.Create("matrix"), Helpers.BuildStrand(new AType[] { AInteger.Create(0), AInteger.Create(1) }), AInteger.Create(22), Helpers.BuildString("static") } ); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("$cx ctx", scope); this.engine.Execute("b := (`scalar `vector `matrix; (3.14; 'abcdef'; iota 3 2))", scope); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f)}", scope); this.engine.Execute("_scb{`b;(cbf;'static')}", scope); this.engine.Execute("((1;0)#`matrix pick b) := 22", scope); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); }
public void ExpandIntegerVector2NestedMixedArray1() { ScriptScope scope = this.engine.CreateScope(); AType f = this.engine.Execute <AType>("f := <{+}", scope); AType expected = AArray.Create( ATypes.ASymbol, AArray.Create( ATypes.ASymbol, ASymbol.Create("a"), ABox.Create(AInteger.Create(4)) ), AArray.Create( ATypes.ASymbol, ASymbol.Create(""), ASymbol.Create("") ), AArray.Create( ATypes.AFunc, f, ASymbol.Create("b") ), AArray.Create( ATypes.ASymbol, ASymbol.Create(""), ASymbol.Create("") ) ); AType result = this.engine.Execute <AType>("1 0 1 0 \\ 2 2 rho `a , (<4) , f , `b", scope); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void PartitionIntegerList2MixedNestedArray() { ScriptScope scope = this.engine.CreateScope(); AType f = this.engine.Execute <AType>("f := <{+}", scope); AType expected = AArray.Create( ATypes.ABox, ABox.Create( AArray.Create( ATypes.ASymbol, ASymbol.Create("a"), ABox.Create(AInteger.Create(4)) ) ), ABox.Create( AArray.Create( ATypes.AFunc, f ) ), ABox.Create(Utils.ANull()) ); AType result = this.engine.Execute <AType>("2 2 1 bag `a , (< 4) , f", scope); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); Assert.AreEqual(expected, result); }
public void AppendAssignPresetCallback() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create(""), Utils.ANull(), ABox.Create(AArray.Create(ATypes.AInteger, AInteger.Create(2), AInteger.Create(3))), AArray.Create(ATypes.AInteger, AInteger.Create(3), AInteger.Create(4)), Helpers.BuildString("static") } ); AType expectedValue = AArray.Create(ATypes.AInteger, AInteger.Create(1), AInteger.Create(2), AInteger.Create(3), AInteger.Create(4)); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("b := 1 2", scope); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f); := b}", scope); this.engine.Execute("_spcb{`b;(cbf;'static')}", scope); this.engine.Execute("b[,] := 3 4", scope); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); Assert.AreEqual(expectedValue, scope.GetVariable <AType>(".b")); }
public void AlsfExample() { AType titles = AArray.Create(ATypes.ASymbol, ASymbol.Create("a"), ASymbol.Create("b"), ASymbol.Create("c") ); AType content = AArray.Create(ATypes.ABox, ABox.Create(AInteger.Create(1)), ABox.Create(AInteger.Create(2)), ABox.Create(AArray.Create( ATypes.AInteger, AInteger.Create(3), AInteger.Create(4), AInteger.Create(5) ) ) ); AType expected = AArray.Create(ATypes.ABox, ABox.Create(titles), ABox.Create(content)); AType result = this.engine.Execute <AType>(" _alsf{(`a;1;`b;2;`c;3 4 5)}"); Assert.AreEqual(expected, result); }
public void TransposeAxisIntegerList2NestedMixedArray() { ScriptScope scope = this.engine.CreateScope(); AType f = this.engine.Execute <AType>("f := <{+}", scope); AType expected = AArray.Create( ATypes.ASymbol, AArray.Create( ATypes.ASymbol, ASymbol.Create("a"), f, ABox.Create(AInteger.Create(2)) ), AArray.Create( ATypes.ABox, ABox.Create(AInteger.Create(4)), ASymbol.Create("b"), f ) ); AType result = this.engine.Execute <AType>("1 0 flip 3 2 rho `a , (<4) , f , `b , (<2) , f", scope); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); Assert.AreEqual(expected, result); }
public void IdentitySymbolUni() { AType expected = ASymbol.Create("abc"); AType result = this.engineUni.Execute <AType>("+ `abc"); Assert.AreEqual(expected, result); }
private void CallbackBySocketException(AipcConnection connection, SocketException exception, bool isRead) { int handle = connection.ConnectionAttributes.HandleNumber; switch (exception.SocketErrorCode) { case SocketError.TimedOut: connection.MakeCallback("timeout", ASymbol.Create("Timeout")); break; case SocketError.Interrupted: connection.MakeCallback("interrupt", ASymbol.Create("Interrupted")); break; case SocketError.NoBufferSpaceAvailable: connection.MakeCallback(isRead ? "buffread" : "buffwrite", ASymbol.Create(String.Concat(isRead ? "Read " : "Write ", "buffer is full"))); this.Close(connection.ConnectionAttributes.HandleNumber); break; case SocketError.NotInitialized: case SocketError.NotConnected: this.Close(connection.ConnectionAttributes.HandleNumber); break; case SocketError.ConnectionReset: default: connection.MakeCallback("reset", ASymbol.Create("unknown State")); this.Close(connection.ConnectionAttributes.HandleNumber); break; } }
public void ValueInContextPresetExample() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create("ctx"), Utils.ANull(), Utils.ANull(), AInteger.Create(3), Helpers.BuildString("static") } ); AType expectedValue = AInteger.Create(3); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("$cx ctx"); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f); := b}", scope); this.engine.Execute("_spcb{`b;(cbf;'static')}", scope); this.engine.Execute("(`ctx ref `b) := 3", scope); Assert.AreEqual(expectedValue, scope.GetVariable <AType>("ctx.b")); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); }
public void DependencyPresetCallback() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create("ctx"), Utils.ANull(), Utils.ANull(), AInteger.Create(8), Helpers.BuildString("static") } ); AType expectedValue = AInteger.Create(8); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("$cx ctx"); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f); := b}", scope); this.engine.Execute("_spcb{`b;(cbf;'static')}", scope); this.engine.Execute("a := 3", scope); this.engine.Execute("b:2*a", scope); this.engine.Execute("a := 4", scope); this.engine.Execute("b", scope); Assert.AreEqual(expectedValue, scope.GetVariable <AType>("ctx.b")); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); }
public void SimpleIndexAssignCallback() { AType expected = Helpers.BuildStrand( new AType[] { ASymbol.Create("b"), ASymbol.Create(""), Utils.ANull(), AInteger.Create(1), AInteger.Create(200), Helpers.BuildString("static") } ); ScriptScope scope = this.engine.CreateScope(); this.engine.Execute("b := 10 20 30 40", scope); this.engine.Execute("cbf{a;b;c;d;e;f}:{.result := (a;b;c;d;e;f)}", scope); this.engine.Execute("_scb{`b;(cbf;'static')}", scope); this.engine.Execute("b[1] := 200", scope); AType result = scope.GetVariable <AType>(".result"); Assert.AreEqual(expected.CompareInfos(result), InfoResult.OK); Assert.AreEqual(expected, scope.GetVariable <AType>(".result")); }
public void ExpandIntegerList2NestedMixedMatrix2() { ScriptScope scope = this.engine.CreateScope(); AType f = this.engine.Execute <AType>("f := <{+}", scope); AType expected = AArray.Create( ATypes.ASymbol, AArray.Create( ATypes.ASymbol, ASymbol.Create("a"), ASymbol.Create(""), ABox.Create(AInteger.Create(4)) ), AArray.Create( ATypes.AFunc, f, ABox.Create(Utils.ANull()), ASymbol.Create("b") ), AArray.Create( ATypes.ABox, ABox.Create(AInteger.Create(2)), ABox.Create(Utils.ANull()), f ) ); AType result = this.engine.Execute <AType>("1 0 1 \\@ 1 1 rtack 3 2 rho `a , (<4) , f , `b , (<2) , f", scope); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void RestructureIntegerList2NestedArray() { ScriptScope scope = this.engine.CreateScope(); AType f = this.engine.Execute <AType>("f := {+}", scope); AType expected = AArray.Create( ATypes.ASymbol, AArray.Create( ATypes.ASymbol, ASymbol.Create("a"), ASymbol.Create("b") ), AArray.Create( ATypes.ABox, ABox.Create(AInteger.Create(3)), ABox.Create(AInteger.Create(4)) ), AArray.Create( ATypes.AFunc, ABox.Create(f), ABox.Create(AInteger.Create(6)) ) ); AType result = this.engine.Execute <AType>("2 ! (`a`b , (3;4)) , (f;6)", scope); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
/// <summary> /// Tester method which returns different typed results. /// </summary> /// <param name="env"><see cref="Aplus"/> environemnt.</param> /// <param name="number">Input number.</param> /// <remarks> /// The resulting type of the value is determined by the input number's value. /// If the value is: /// <list type="bullet"> /// <item><description>0: returns 1.1 as a AFloat.</description></item> /// <item><description>1: returns 0 as a AInteger.</description></item> /// <item><description>2: returns `a as a ASymbol.</description></item> /// <item><description>3: returns 'a' as a AFChar.</description></item> /// <item><description>otherwise: returns empty array.</description></item> /// </list> /// </remarks> /// <returns>Different typed ATypes.</returns> public static AType MonadicTypeAlternate(Aplus env, AType number) { AType result; switch (number.asInteger) { case 0: result = AFloat.Create(1.1); break; case 1: result = AInteger.Create(0); break; case 2: result = ASymbol.Create("a"); break; case 3: result = AChar.Create('a'); break; default: result = Utils.ANull(); break; } return(result); }
/// <summary> /// Convert Character constant array to symbol array. /// </summary> /// <param name="argument"></param> /// <returns></returns> private AType Compute(AType argument) { //If argument is character constant or character constant vector then we convert it symbol, //and cut blanks from end. if (argument.Rank <= 1) { return(ASymbol.Create(argument.ToString().TrimEnd())); } else { AType result = AArray.Create(ATypes.ASymbol); foreach (AType item in argument) { result.AddWithNoUpdate(Compute(item)); } result.Length = argument.Length; result.Shape = new List <int>(); result.Shape.AddRange(argument.Shape.GetRange(0, argument.Shape.Count - 1)); result.Rank = argument.Rank - 1; return(result); } }
/// <summary> /// Separate symbol constant by definiton. /// </summary> /// <param name="argument"></param> /// <returns></returns> private AType SeparateSymbol(AType argument) { AType result = AArray.Create(ATypes.ASymbol); string symbol = argument.asString; int index = symbol.LastIndexOf('.'); if (index != -1) { result.AddWithNoUpdate(ASymbol.Create(symbol.Substring(0, index))); result.AddWithNoUpdate(ASymbol.Create(symbol.Substring(index + 1))); } else { result.AddWithNoUpdate(ASymbol.Create("")); result.AddWithNoUpdate(argument.Clone()); } result.Length = 2; result.Shape = new List <int>() { 2 }; result.Rank = 1; return(result); }
public void LaminateBoxArray2SymbolConstant() { AType expected = AArray.Create( ATypes.ABox, Helpers.BuildStrand( new AType[] { AInteger.Create(1), AInteger.Create(4), AInteger.Create(3) } ), AArray.Create( ATypes.ASymbol, ASymbol.Create("b"), ASymbol.Create("b"), ASymbol.Create("b") ) ); AType result = this.engine.Execute <AType>("(3;4;1) ~ `b"); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void CharacterType() { AType expected = ASymbol.Create("char"); AType result = this.engine.Execute <AType>("?'hello'"); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void FloatType() { AType expected = ASymbol.Create("float"); AType result = this.engine.Execute <AType>("?1.1"); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void IntegerTypeUni() { AType expected = ASymbol.Create("int"); AType result = this.engineUni.Execute <AType>("|1"); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public void NullType() { AType expected = ASymbol.Create("null"); AType result = this.engine.Execute <AType>("?()"); Assert.AreEqual(expected, result); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); }
public override DLR.Expression Generate(AplusScope scope) { if (this.list.Count == 1) { return(this.list.First.Value.Generate(scope)); } AType items = null; if (this.type == ConstantType.Integer) { bool convertToFloat = false; items = AArray.Create(ATypes.AInteger); foreach (Constant item in this.list) { AType value = item.AsNumericAType; items.Add(value); if (value.Type == ATypes.AFloat) { convertToFloat = true; } } // Convert integer items in previous array to float if (convertToFloat) { items.ConvertToFloat(); } } // Treat the Infinite constants same as floats else if (this.type == ConstantType.Double || this.type == ConstantType.PositiveInfinity || this.type == ConstantType.NegativeInfinity) { items = AArray.Create(ATypes.AFloat); foreach (Constant item in this.list) { items.Add(AFloat.Create(item.AsFloat)); } } else if (this.type == ConstantType.Symbol) { items = AArray.Create(ATypes.ASymbol); foreach (Constant item in this.list) { items.Add(ASymbol.Create(item.AsString)); } } else { throw new Error.Parse(String.Format("Unknow ConstantType({0}) in current context", this.type)); } DLR.Expression result = DLR.Expression.Constant(items, typeof(AType)); // Example: .Constant<AplusCore.Types.AArray`1[AplusCore.Types.AInteger]>(1 2) return(result); }
public void TransposeSymbolConstant() { AType expected = ASymbol.Create("test"); AType result = this.engine.Execute <AType>("flip `test"); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); Assert.AreEqual(expected, result); }
public void ChooseInteger2SymbolConstantListUni() { AType expected = ASymbol.Create("c"); AType result = this.engineUni.Execute <AType>("2 # `a`b`c`d`e"); Assert.AreEqual(InfoResult.OK, result.CompareInfos(expected)); Assert.AreEqual(expected, result); }
public AState ApplySymbol(ASymbol key) { if (transition.ContainsKey(key)) { return(transition[key]); } return(this); }
/// <summary> /// Creates a result for the SyncRead/SyncWrite if it succeed /// </summary> protected AType SyncFillOk(AType message, bool isRead) { return(AArray.Create( ATypes.ABox, ABox.Create(ASymbol.Create("OK")), ABox.Create(message), ABox.Create(isRead ? Utils.ANull() : AipcAttributes.GetWriteQueue()) )); }