# encoding: utf-8 # frozen_string_literal: true # rubocop:disable Metrics/CyclomaticComplexity require "carbon/compiler/parser/statements/if" require "carbon/compiler/parser/statements/match" require "carbon/compiler/parser/statements/try" module Carbon module Compiler class Parser # Parses statements. module Statements include If include Match include Try protected def parse_statement case peek.type when :if then parse_statement_if when :match then parse_statement_match when :try then parse_statement_try when :for then parse_statement_for when :return then parse_statement_return when :while then parse_statement_while when :let then parse_statement_let when :do then parse_function_body else expr = parse_expression expect :";" expr end end def parse_statement_return expect :return expr = parse_expression unless peek? :";" expect :";" Node::Statement::Return.new([expr].compact) end def parse_statement_let expect :let name = parse_name expect :":" type = parse_type value = if peek? :"=" expect :"=" parse_expression end expect :";" Node::Statement::Let.new([name, type, value]) end def parse_statement_while expect :while expect :"(" condition = parse_expression expect :")" body = parse_statement Node::Statement::While.new([condition, body]) end def parse_statement_for expect :for expect :"(" initial = parse_expression expect :";" condition = parse_expression expect :";" increment = parse_expression expect :")" body = parse_statement Node::Statement::For.new([initial, condition, increment, body]) end end end end end