public ScalarValue Function(StringValue octstr) { var sum = 0; var hex = new Stack <Int32>(); var weight = 1; for (var i = 1; i <= octstr.Length; i++) { var chr = octstr[i]; if (!ParseEngine.IsWhiteSpace(chr) && !ParseEngine.IsNewLine(chr)) { if (chr >= '0' && chr <= '7') { hex.Push((Int32)(chr - '0')); } else { throw new YAMPRuntimeException("oct2dec can only interpret octal strings."); } } } while (hex.Count != 0) { var el = hex.Pop(); sum += weight * el; weight *= 8; } return(new ScalarValue(sum)); }
public ScalarValue Function(StringValue binarystr) { var sum = 0; var binary = new Stack <bool>(); var weight = 1; for (var i = 1; i <= binarystr.Length; i++) { var chr = binarystr[i]; if (ParseEngine.IsWhiteSpace(chr)) { continue; } else if (ParseEngine.IsNewLine(chr)) { continue; } else if (chr == '0') { binary.Push(false); } else if (chr == '1') { binary.Push(true); } else { throw new YAMPRuntimeException("bin2dec can only interpret binary strings."); } } while (binary.Count != 0) { var el = binary.Pop(); if (el) { sum += weight; } weight *= 2; } return(new ScalarValue(sum)); }
public ScalarValue Function(StringValue hexstr) { var sum = 0; var hex = new Stack <int>(); var weight = 1; for (var i = 1; i <= hexstr.Length; i++) { var chr = hexstr[i]; if (ParseEngine.IsWhiteSpace(chr)) { continue; } else if (ParseEngine.IsNewLine(chr)) { continue; } else if (chr >= '0' && chr <= '9') { hex.Push((int)(chr - '0')); } else if (chr >= 'A' && chr <= 'F') { hex.Push((int)(chr - 'A') + 10); } else if (chr >= 'a' && chr <= 'f') { hex.Push((int)(chr - 'a') + 10); } else { throw new YAMPRuntimeException("hex2dec can only interpret hexadecimal strings."); } } while (hex.Count != 0) { var el = hex.Pop(); sum += weight * el; weight *= 16; } return(new ScalarValue(sum)); }
public override Operator Create(ParseEngine engine) { var start = engine.Pointer; //Arguments need to be attached directly. if (start == 0 || ParseEngine.IsWhiteSpace(engine.Characters[start - 1]) || ParseEngine.IsNewLine(engine.Characters[start - 1])) { return(null); } var ao = new ArgsOperator(); ao.Query = engine.Query; ao.StartLine = engine.CurrentLine; ao.StartColumn = engine.CurrentColumn; ao._content = Elements.Instance.FindExpression <BracketExpression>().Scan(engine); ao.Length = engine.Pointer - start; return(ao); }
/// <summary> /// Scans the current parse engine for a matrix expression. /// </summary> /// <param name="engine">The parse engine to use.</param> /// <returns>The found expression or NULL.</returns> public override Expression Scan(ParseEngine engine) { var column = engine.CurrentColumn; var line = engine.CurrentLine; var chars = engine.Characters; var start = engine.Pointer; if (chars[start] == '[') { engine.Advance(); var terminated = false; var statement = new Statement(); bool ws = false, nl = false; while (engine.Pointer < chars.Length && engine.IsParsing) { if (ParseEngine.IsWhiteSpace(chars[engine.Pointer])) { ws = true; engine.Advance(); } else if (ParseEngine.IsNewLine(chars[engine.Pointer])) { nl = true; engine.Advance(); } else if (chars[engine.Pointer] == ']') { terminated = true; engine.Advance(); break; } else if (chars[engine.Pointer] == ',') { ws = false; nl = false; statement.Push(engine, new ColumnOperator(engine)); engine.Advance(); } else if (chars[engine.Pointer] == ';') { ws = false; nl = false; statement.Push(engine, new RowOperator(engine)); engine.Advance(); } else if (engine.Pointer < chars.Length - 1 && ParseEngine.IsComment(chars[engine.Pointer], chars[engine.Pointer + 1])) { if (ParseEngine.IsLineComment(chars[engine.Pointer], chars[engine.Pointer + 1])) { engine.AdvanceToNextLine(); } else { engine.AdvanceTo("*/"); } } else { engine.ParseBlock(statement, nl ? (Operator) new RowOperator(engine) : (ws ? new ColumnOperator(engine) : null)); ws = false; nl = false; } } if (!terminated) { var err = new YAMPMatrixNotClosedError(line, column); engine.AddError(err); } var container = statement.Finalize(engine).Container; return(new MatrixExpression(line, column, engine.Pointer - start, engine.Query, container ?? new ContainerExpression())); } return(null); }
public override Operator Create(ParseEngine engine) { var start = engine.Pointer; //Arguments need to be attached directly. if (start != 0 && !ParseEngine.IsWhiteSpace(engine.Characters[start - 1]) && !ParseEngine.IsNewLine(engine.Characters[start - 1])) { var query = engine.Query; var line = engine.CurrentLine; var column = engine.CurrentColumn; var content = engine.Elements.FindExpression <BracketExpression>().Scan(engine); var length = engine.Pointer - start; return(new ArgsOperator(content) { Query = query, StartLine = line, StartColumn = column, Length = length }); } return(null); }