/// <summary> /// Check if given token <paramref name="tkn"/> is a <see cref="MatrisBase{object}"/> and update it's value if needed /// </summary> /// <param name="tkn">Token to check</param> /// <param name="matDict">Matrix dictionary to reference to</param> /// <param name="mode">Compiler mode</param> /// <param name="assertNonNull">Wheter to assert token's value to be non-null after checks and updates</param> /// <returns>True if given token holds a <see cref="MatrisBase{object}"/></returns> public static bool CheckMatrixAndUpdateVal(Token tkn, Dictionary <string, MatrisBase <dynamic> > matDict, CompilerDictionaryMode mode = CompilerDictionaryMode.Matrix, bool assertNonNull = false) { switch (tkn.tknType) { case TokenType.MATRIS: if (matDict.ContainsKey(tkn.name)) { Validations.CheckModeAndMatrixReference(mode, (dynamic)matDict[tkn.name]); tkn.val = matDict[tkn.name]; } else if (!(tkn.val is MatrisBase <object>)) { throw new Exception(CompilerMessage.NOT_SAVED_MATRIX(tkn.name)); } else { Validations.CheckModeAndMatrixReference(mode, tkn.val); } if (assertNonNull) { AssertNotNull(tkn); } return(true); default: if (assertNonNull) { AssertNotNull(tkn); } return(false); } }
/// <summary> /// Assign <paramref name="RHS"/> matrix or scalar to item named <paramref name="LHS"/> /// </summary> /// <param name="LHS">Left hand side, name will be picked from this</param> /// <param name="RHS">Right hand side, value will be picked from this</param> /// <param name="matDict">Matrix dictionary to refer to if needed</param> public static void OPAssignment(Token LHS, Token RHS, Dictionary <string, MatrisBase <dynamic> > matDict, CompilerDictionaryMode mode = CompilerDictionaryMode.Matrix) { if (LHS.tknType != TokenType.MATRIS) // LHS should just be a valid name for a matrix { throw new Exception(CompilerMessage.EQ_FORMAT); } else { switch (RHS.tknType) { case TokenType.NUMBER: // RHS is scalar { RHS.val = new MatrisBase <dynamic>(1, 1, RHS.val); break; } case TokenType.MATRIS: // RHS is possibly a matrix { if (matDict.ContainsKey(RHS.name)) { Validations.CheckModeAndMatrixReference(mode, (dynamic)matDict[RHS.name]); RHS.val = (dynamic)matDict[RHS.name]; } else if (RHS.val is MatrisBase <object> ) { Validations.CheckModeAndMatrixReference(mode, RHS.val); } else { throw new Exception(CompilerMessage.NOT_SAVED_MATRIX(RHS.name)); } break; } default: { if (!(RHS.val is MatrisBase <object>)) // If RHS is not even a matrix, throw { throw new Exception(CompilerMessage.EQ_FAILED); } Validations.CheckModeAndMatrixReference(mode, RHS.val); break; } } // Update the matrix table accordingly if (matDict.ContainsKey(LHS.name)) { Validations.CheckModeAndMatrixReference(mode, matDict[LHS.name]); /* // THIS PART UPDATES EXISTING MATRIX'S VALUES IN PLACE * if (matDict[LHS.name] is Dataframe && !(RHS.val is Dataframe)) * { * matDict[LHS.name] = new Dataframe(((MatrisBase<object>)RHS.val).GetValues()); * } * else if (RHS.val is Dataframe dataframe && !(matDict[LHS.name] is Dataframe)) * { * matDict[LHS.name] = new MatrisBase<object>(dataframe.GetValues()); * } * else * { * matDict[LHS.name] = RHS.val; * } */ matDict[LHS.name] = RHS.val; } else if (Validations.ValidMatrixName(LHS.name)) { int dfcount = 0; foreach (string name in matDict.Keys) { if (matDict[name] is Dataframe) { dfcount++; } } if (RHS.val is Dataframe) { if (dfcount < (int)DataframeLimits.forDataframeCount) { Validations.CheckModeAndMatrixReference(mode, RHS.val); matDict.Add(LHS.name, RHS.val); } else { throw new Exception(CompilerMessage.DF_LIMIT); } } else if (matDict.Count - dfcount < (int)MatrisLimits.forMatrisCount) { Validations.CheckModeAndMatrixReference(mode, RHS.val); AssertNotNull(RHS); matDict.Add(LHS.name, RHS.val); } else { throw new Exception(CompilerMessage.MAT_LIMIT); } } else // LHS was invalid { if (string.IsNullOrEmpty(LHS.name)) { throw new Exception(CompilerMessage.EQ_FORMAT); } else { throw new Exception(CompilerMessage.MAT_NAME_INVALID); } } } }