+ while success and index < len(tokens):
+ # "function" is actually the full function call if the next parse attempt doesn't succeed
+ # We can't give this a better name without a bunch of checks, however.
+ function = FurFunctionCallExpression(
+ metadata=metadata,
+ function=function,
+ arguments=arguments,
+ )
+
+ metadata = tokens[index].metadata
+
+ success, index, arguments = _parenthese_wrapped_parser(_comma_separated_expression_list_parser)(
+ index,
+ tokens,
+ )
+
+ return True, index, function
+
+def _if_expression_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].match == 'if':
+ index += 1
+ else:
+ return failure
+
+ success, index, condition_expression = _or_level_expression_parser(index, tokens)
+
+ if not success:
+ raise Exception('Expected condition after "if" on line {}'.format(tokens[index].line))
+
+ if tokens[index].match == 'do':
+ index += 1
+ else:
+ raise Exception('Expected "do" after "if" on line {}'.format(tokens[index].line))
+
+
+ success, index, if_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+ _, index, _ = consume_newlines(index, tokens)
+
+ if tokens[index].match == 'else':
+ index += 1
+ success, index, else_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+ _, index, _ = consume_newlines(index, tokens)
+ else:
+ else_statement_list = ()
+
+ if tokens[index].match == 'end':
+ index += 1
+ else:
+ raise Exception('Expected "end" after "if" on line {}'.format(tokens[index].line))
+
+ return (
+ True,
+ index,
+ FurIfExpression(
+ condition_expression=condition_expression,
+ if_statement_list=if_statement_list,
+ else_statement_list=else_statement_list,
+ ),
+ )
+
+_expression_parser = _or_parser(
+ _or_level_expression_parser,
+ _if_expression_parser, # This should always be at the top level
+)
+
+def _expression_statement_parser(index, tokens):
+ failure = (False, index, None)
+
+ success, index, expression = _expression_parser(index, tokens)
+
+ if not success:
+ return failure
+
+ return (True, index, FurExpressionStatement(expression=expression))
+
+BUILTINS = {'print', 'pow'}
+
+def _assignment_statement_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].type == 'symbol':
+ target = tokens[index].match
+ target_assignment_line = tokens[index].metadata.line
+
+ index += 1
+ else:
+ return failure
+
+
+ if tokens[index].type == 'assignment_operator':
+ if target in BUILTINS:
+ raise Exception(
+ 'Trying to assign to builtin "{}" on line {}'.format(target, target_assignment_line),
+ )
+ assignment_operator_index = index
+ else:
+ return failure
+
+ success, index, expression = _expression_parser(index + 1, tokens)
+
+ if not success:
+ raise Exception(
+ 'Expected expression after assignment operator on line {}'.format(
+ tokens[assignment_operator_index].line
+ )
+ )
+
+ return True, index, FurAssignmentStatement(target=target, expression=expression)
+
+def _function_definition_statement_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'def':
+ index += 1
+ else:
+ return failure
+
+ if tokens[index].type == 'symbol':
+ name = tokens[index].match
+ index += 1
+ else:
+ raise Exception('Expected function name, found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].type == 'open_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected "(", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ success, index, argument_name_list = _comma_separated_list_parser(_symbol_expression_parser)(
+ index,
+ tokens,
+ )
+
+ if tokens[index].type == 'close_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected ")", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].match == 'do':
+ index += 1
+ else:
+ return failure
+
+ success, index, statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+
+ _, index, _ = consume_newlines(index, tokens)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'end':
+ index += 1
+ else:
+ return failure
+
+ return True, index, FurFunctionDefinitionStatement(
+ name=name,
+ argument_name_list=tuple(an.symbol for an in argument_name_list),
+ statement_list=statement_list,
+ )
+
+def _statement_parser(index, tokens):
+ _, index, _ = consume_newlines(index, tokens)
+
+ if index == len(tokens):
+ return (False, index, None)
+
+ return _or_parser(
+ _assignment_statement_parser,
+ _expression_statement_parser,
+ _function_definition_statement_parser,
+ )(index, tokens)