private StringBuilder AddTokens(TokenList tokens, StringBuilder builder, int c_prev_int, int c_peek_int) { StringBuilder ret; string val = builder.ToString(); if (val.Length > 0) { ret = new StringBuilder(); } else { ret = builder; } double as_num; if (IsNumber(val, out as_num)) { Token token = new Token(TokenType.Number, as_num); if (tokens[tokens.Count - 1].Type == TokenType.Minus) { token.Val = -((double)token.Val); tokens[tokens.Count - 1] = token; } else { tokens.Add(token); } } else if (val.Length > 1) { Token token; string var_name, str_val; if (IsVariable(val, out var_name)) { token = new Token(TokenType.Variable, var_name); } else if (IsString(val, out str_val)) { token = new Token(TokenType.String, str_val); } else { token = new Token(TokenType.Word, val); } tokens.Add(token); } else if (val.Length == 1) { tokens.Add(TokenForChar(c_prev_int, c_peek_int, val[0])); } return(ret); }
private StringBuilder AddTokens(TokenList tokens, StringBuilder builder, int c_prev_int, int c_peek_int, char c) { StringBuilder ret = AddTokens(tokens, builder, c_prev_int, c_peek_int); if (!(IsWhitespace(c) || IsSpecial(c))) { tokens.Add(TokenForChar(c_prev_int, c_peek_int, c)); } return(ret); }
private void ParseForwards(TokenList partial, IEnumerator iterator, int parens) { while (iterator.MoveNext()) { Token token = (Token)iterator.Current; switch (token.Type) { case TokenType.OpenBracket: partial.Add(new Token(TokenType.PlaceholderElement, ParseList(iterator))); break; case TokenType.OpenParens: TokenList inner = new TokenList(); ParseForwards(inner, iterator, parens + 1); partial.Add(new Token(TokenType.PlaceholderGroup, inner)); break; case TokenType.CloseParens: return; case TokenType.Word: if (String.Compare((string)token.Val, "to", true) == 0) { // FIXME: Usual case is in void context // token = ParseTo (iterator); ParseTo(iterator); } else { partial.Add(token); } break; default: partial.Add(token); break; } } }
private TokenList ParseBrackets(IEnumerator iterator) { TokenList list = new TokenList(); while (iterator.MoveNext()) { Token token = (Token)iterator.Current; if (token.Type == TokenType.CloseBracket) { break; } else { list.Add(token); } } return(list); }
private TokenList ParseBrackets (IEnumerator iterator) { TokenList list = new TokenList (); while (iterator.MoveNext ()) { Token token = (Token) iterator.Current; if (token.Type == TokenType.CloseBracket) break; else list.Add (token); } return list; }
private TokenList ParseInfix (TokenList partial) { TokenList output = new TokenList (); Stack stack = new Stack (); bool last_was_operand = false; int length = partial.Count; for (int i = length - 1; i >= 0; i--) { Token token = partial[i]; if (token.Type == TokenType.Word) { ExtendList (output, stack); output.Add (token); stack.Clear (); } else if (token.Type == TokenType.Infix) { if (stack.Count == 0) { stack.Push (token); } else { Token prev = (Token) stack.Pop (); if (Weight (prev) <= Weight (token)) { stack.Push (token); stack.Push (prev); } else { output.Add (prev); stack.Push (token); } } last_was_operand = false; } else { if (token.Type == TokenType.PlaceholderGroup) { token.Val = ParseInfix ((TokenList) token.Val); } if (i > 0) { Token peek = partial[i - 1]; if (peek.Type == TokenType.Minus) { output.Add (token); output.Add (peek); last_was_operand = true; i--; continue; } } if (last_was_operand) { ExtendList (output, stack); stack.Clear (); } output.Add (token); last_was_operand = true; } } ExtendList (output, stack); output.Reverse (); return output; }
private void ParseForwards (TokenList partial, IEnumerator iterator, int parens) { while (iterator.MoveNext ()) { Token token = (Token) iterator.Current; switch (token.Type) { case TokenType.OpenBracket: partial.Add (new Token (TokenType.PlaceholderElement, ParseList (iterator))); break; case TokenType.OpenParens: TokenList inner = new TokenList (); ParseForwards (inner, iterator, parens + 1); partial.Add (new Token (TokenType.PlaceholderGroup, inner)); break; case TokenType.CloseParens: return; case TokenType.Word: if (String.Compare ((string) token.Val, "to", true) == 0) { // FIXME: Usual case is in void context // token = ParseTo (iterator); ParseTo (iterator); } else { partial.Add (token); } break; default: partial.Add (token); break; } } }
private Token ParseTo (IEnumerator iterator) { ParseToState state = ParseToState.FunctionName; Token name = new Token (TokenType.Word, null); TokenList args = new TokenList (); TokenList def = new TokenList (); bool reading = true; bool prev_newline = false; while (reading && iterator.MoveNext ()) { Token token = (Token) iterator.Current; if (state != ParseToState.Content && token.Type == TokenType.Newline) { state = ParseToState.Content; prev_newline = true; continue; } switch (state) { case ParseToState.FunctionName: name = token; state = AdvanceState (state); break; case ParseToState.Argument: case ParseToState.ArgumentWithValue: case ParseToState.ArgumentCollector: if (token.Type == TokenType.OpenBracket) { TokenList arg = ParseBrackets (iterator); if (state == ParseToState.Argument || (state == ParseToState.ArgumentWithValue && arg.Count == 1)) { state = AdvanceState (state); } args.Add (new Token (TokenType.PlaceholderElement, new Element (ElementType.List, arg))); } else { args.Add (token); } break; case ParseToState.Content: if (prev_newline && token.Type == TokenType.Word && String.Compare ((string) token.Val, "end", true) == 0) { reading = false; } else { def.Add (token); prev_newline = (token.Type == TokenType.Newline); } break; } } TokenList func = new TokenList (); func.Add (name); func.Add (new Token (TokenType.PlaceholderGroup, args)); func.Add (new Token (TokenType.PlaceholderGroup, def)); Function func_obj = CreateFunction (func); funcs.AddMessage (func_obj); if (to_parse == null) to_parse = new ArrayList (); to_parse.Add (func_obj); return new Token (TokenType.PlaceholderElement, new Element (ElementType.Function, func_obj)); }
private TokenList ParseInfix(TokenList partial) { TokenList output = new TokenList(); Stack stack = new Stack(); bool last_was_operand = false; int length = partial.Count; for (int i = length - 1; i >= 0; i--) { Token token = partial[i]; if (token.Type == TokenType.Word) { ExtendList(output, stack); output.Add(token); stack.Clear(); } else if (token.Type == TokenType.Infix) { if (stack.Count == 0) { stack.Push(token); } else { Token prev = (Token)stack.Pop(); if (Weight(prev) <= Weight(token)) { stack.Push(token); stack.Push(prev); } else { output.Add(prev); stack.Push(token); } } last_was_operand = false; } else { if (token.Type == TokenType.PlaceholderGroup) { token.Val = ParseInfix((TokenList)token.Val); } if (i > 0) { Token peek = partial[i - 1]; if (peek.Type == TokenType.Minus) { output.Add(token); output.Add(peek); last_was_operand = true; i--; continue; } } if (last_was_operand) { ExtendList(output, stack); stack.Clear(); } output.Add(token); last_was_operand = true; } } ExtendList(output, stack); output.Reverse(); return(output); }
private Token ParseTo(IEnumerator iterator) { ParseToState state = ParseToState.FunctionName; Token name = new Token(TokenType.Word, null); TokenList args = new TokenList(); TokenList def = new TokenList(); bool reading = true; bool prev_newline = false; while (reading && iterator.MoveNext()) { Token token = (Token)iterator.Current; if (state != ParseToState.Content && token.Type == TokenType.Newline) { state = ParseToState.Content; prev_newline = true; continue; } switch (state) { case ParseToState.FunctionName: name = token; state = AdvanceState(state); break; case ParseToState.Argument: case ParseToState.ArgumentWithValue: case ParseToState.ArgumentCollector: if (token.Type == TokenType.OpenBracket) { TokenList arg = ParseBrackets(iterator); if (state == ParseToState.Argument || (state == ParseToState.ArgumentWithValue && arg.Count == 1)) { state = AdvanceState(state); } args.Add(new Token(TokenType.PlaceholderElement, new Element(ElementType.List, arg))); } else { args.Add(token); } break; case ParseToState.Content: if (prev_newline && token.Type == TokenType.Word && String.Compare((string)token.Val, "end", true) == 0) { reading = false; } else { def.Add(token); prev_newline = (token.Type == TokenType.Newline); } break; } } TokenList func = new TokenList(); func.Add(name); func.Add(new Token(TokenType.PlaceholderGroup, args)); func.Add(new Token(TokenType.PlaceholderGroup, def)); Function func_obj = CreateFunction(func); funcs.AddMessage(func_obj); if (to_parse == null) { to_parse = new ArrayList(); } to_parse.Add(func_obj); return(new Token(TokenType.PlaceholderElement, new Element(ElementType.Function, func_obj))); }
private StringBuilder AddTokens (TokenList tokens, StringBuilder builder, int c_prev_int, int c_peek_int, char c) { StringBuilder ret = AddTokens (tokens, builder, c_prev_int, c_peek_int); if (!(IsWhitespace (c) || IsSpecial (c))) tokens.Add (TokenForChar (c_prev_int, c_peek_int, c)); return ret; }
private StringBuilder AddTokens (TokenList tokens, StringBuilder builder, int c_prev_int, int c_peek_int) { StringBuilder ret; string val = builder.ToString (); if (val.Length > 0) { ret = new StringBuilder (); } else { ret = builder; } double as_num; if (IsNumber (val, out as_num)) { Token token = new Token (TokenType.Number, as_num); if (tokens[tokens.Count - 1].Type == TokenType.Minus) { token.Val = -((double) token.Val); tokens[tokens.Count - 1] = token; } else { tokens.Add (token); } } else if (val.Length > 1) { Token token; string var_name, str_val; if (IsVariable (val, out var_name)) token = new Token (TokenType.Variable, var_name); else if (IsString (val, out str_val)) token = new Token (TokenType.String, str_val); else token = new Token (TokenType.Word, val); tokens.Add (token); } else if (val.Length == 1) { tokens.Add (TokenForChar (c_prev_int, c_peek_int, val[0])); } return ret; }