subroutineparse_primaryrecursiveprivate
recursive subroutine parse_primary(c, s, id)
A primary: a literal, a name, or something in brackets.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| c | type(fypp_t) | inout | the state | |
| s | type(scan_t) | inout | the expression | |
| id | integer | out | the result |
Calls
- new_expr
- next_token
- add
- take
- parse_fstring
- intern
- at_word
- parse_test
- parse_comprehension
- parse_items
- expect
- fail
Source
Show the code
recursive subroutine parse_primary(c, s, id)
type(fypp_t), intent(inout) :: c !! the state
type(scan_t), intent(inout) :: s !! the expression
integer, intent(out) :: id !! the result
type(buffer_t) :: b
character(len=:), allocatable :: text
integer :: item, name
id = 0
select case (s%kind)
case (T_INT)
id = new_expr(c, E_CONST)
c%exprs(id)%value%kind = V_INT
c%exprs(id)%value%number = s%number
call next_token(c, s)
case (T_STR)
do while (s%kind == T_STR)
call add(b, s%word)
call next_token(c, s)
end do
id = new_expr(c, E_CONST)
c%exprs(id)%value%kind = V_STR
call take(b, text)
call move_alloc(text, c%exprs(id)%value%text)
case (T_FSTR)
text = s%word
call next_token(c, s)
call parse_fstring(c, text, id)
case (T_NAME)
select case (s%word)
case ('True', 'False')
id = new_expr(c, E_CONST)
c%exprs(id)%value%kind = V_BOOL
c%exprs(id)%value%number = merge(1, 0, s%word == 'True')
case ('None')
id = new_expr(c, E_CONST)
case default
call intern(c, s%word, name)
id = new_expr(c, E_NAME)
c%exprs(id)%name = name
end select
call next_token(c, s)
case (T_OP)
if (at_word(s, '(')) then
call next_token(c, s)
if (at_word(s, ')')) then
id = new_expr(c, E_TUPLE)
allocate (c%exprs(id)%args(0))
else
call parse_test(c, s, item)
if (at_word(s, 'for')) then
call parse_comprehension(c, s, item, id)
else if (at_word(s, ',')) then
id = new_expr(c, E_TUPLE)
call parse_items(c, s, id, item, ')')
else
id = item
end if
end if
call expect(c, s, ')')
else if (at_word(s, '[')) then
call next_token(c, s)
if (at_word(s, ']')) then
id = new_expr(c, E_LIST)
allocate (c%exprs(id)%args(0))
else
call parse_test(c, s, item)
if (at_word(s, 'for')) then
call parse_comprehension(c, s, item, id)
else
id = new_expr(c, E_LIST)
call parse_items(c, s, id, item, ']')
end if
end if
call expect(c, s, ']')
else
call fail(c, 'not supported in Fypp expressions: '//s%text)
end if
case default
call fail(c, 'an expression ends too soon: '//s%text)
end select
end subroutine parse_primary