/// <summary> /// Parses a conditional statement. /// </summary> public ParseResult ParseConditional(List <Token> tokens) { var node = new ConditionalNode(); var scopeCheck = new ScopeCheck(); for (int i = 0; i < tokens.Count; i++) { var token = tokens[i]; scopeCheck.Check(token); if (token.Equals("?", TokenTypes.Punctuator) && scopeCheck.IsInScope) { SkipInfo skip = SkipFromTo("?", ":", tokens, i); var conditionNode = ParseClean(tokens.GetRange(0, i)); var successNode = ParseClean(tokens.GetRange(i + 1, skip.Delta - 1)); var failNode = ParseClean(tokens.GetRange(skip.End + 1, tokens.Count - (skip.End + 1))); node.Condition = conditionNode ?? throw new SkryptException("Syntax error, condition statement can't be empty"); node.Pass = successNode ?? throw new SkryptException("Syntax error, consequent statement can't be empty"); node.Fail = failNode ?? throw new SkryptException("Syntax error, alternative statement can't be empty"); return(new ParseResult { Node = node, Delta = tokens.Count }); } } _engine.ThrowError("Syntax error, conditional statement incomplete", tokens[0]); return(null); }
// Checks whether a list of tokens is a valid conditional statement. private bool IsConditional(List <Token> tokens) { var isConditional = false; var scopeCheck = new ScopeCheck(); for (int i = 0; i < tokens.Count; i++) { var token = tokens[i]; scopeCheck.Check(token); if (token.Equals("?", TokenTypes.Punctuator) && scopeCheck.IsInScope) { isConditional = true; SkipInfo skip = SkipFromTo("?", ":", tokens, i); } } return(isConditional); }