Skip to content

Commit

Permalink
add specific diagnostics for i32 vs i64
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffIrwin committed Nov 9, 2024
1 parent 14b3709 commit 62ebe52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/errors.f90
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module syntran__errors_m

!===============================================================================

function err_bad_int(context, span, num) result(err)
function err_bad_i32(context, span, num) result(err)
type(text_context_t) :: context
type(text_span_t), intent(in) :: span
character(len = :), allocatable :: err
Expand All @@ -64,7 +64,22 @@ function err_bad_int(context, span, num) result(err)
//underline(context, span) &
//' bad integer'//color_reset

end function err_bad_int
end function err_bad_i32

!===============================================================================

function err_bad_i64(context, span, num) result(err)
type(text_context_t) :: context
type(text_span_t), intent(in) :: span
character(len = :), allocatable :: err

character(len = *), intent(in) :: num
err = err_prefix//'bad i64 integer `'//num &
//'` does not fit in 64 bits' &
//underline(context, span) &
//' bad integer'//color_reset

end function err_bad_i64

!===============================================================================

Expand Down
17 changes: 13 additions & 4 deletions src/lex.f90
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,20 @@ function lex(lexer) result(token)
suffix_end = lexer%pos

suffix = lexer%text(suffix_start: suffix_end-1)

! TODO: call lookup_type() on suffix? It would eliminate the magic
! strings like "i32" which are duplicated here, but we would still
! need a select/case here to block bad types (e.g. "f32")
select case (suffix)
case ("i32")
!print *, "i32"
type = i32_type
case ("i64")
type = i64_type
case default
! TODO: maybe hint in diag about which suffixes *are* allowed?
! Might want entirely different diag fns for hex vs dec instead
! of passing "hex" str arg
span = new_span(suffix_start, suffix_end - suffix_start)
call lexer%diagnostics%push(err_bad_type_suffix( &
lexer%context, span, suffix, "hex"))
Expand Down Expand Up @@ -231,6 +238,9 @@ function lex(lexer) result(token)
lexer%context, span, suffix, "decimal"))
end select

! TODO: throw new diag if float and i32 or i64? Currently, `4.0'i32`
! throws err_bad_i32() which isn't exactly the right message

end if

text = lexer%text(start: end_ - 1)
Expand All @@ -251,7 +261,6 @@ function lex(lexer) result(token)
else
token = new_token(bad_token, lexer%pos, text)
span = new_span(start, len(text))
! TODO: specific f32/f64 diags
call lexer%diagnostics%push(err_bad_f32( &
lexer%context, span, text))
end if
Expand Down Expand Up @@ -279,7 +288,7 @@ function lex(lexer) result(token)
token = new_token(bad_token, lexer%pos, text)
span = new_span(start, len(text))
! TODO: specific i32/i64 diags
call lexer%diagnostics%push(err_bad_int( &
call lexer%diagnostics%push(err_bad_i32( &
lexer%context, span, text))
end if

Expand All @@ -292,7 +301,7 @@ function lex(lexer) result(token)
else
token = new_token(bad_token, lexer%pos, text)
span = new_span(start, len(text))
call lexer%diagnostics%push(err_bad_int( &
call lexer%diagnostics%push(err_bad_i64( &
lexer%context, span, text))
end if

Expand Down Expand Up @@ -354,7 +363,7 @@ function lex(lexer) result(token)
else
token = new_token(bad_token, lexer%pos, text)
span = new_span(start, len(text))
call lexer%diagnostics%push(err_bad_int( &
call lexer%diagnostics%push(err_bad_i64( &
lexer%context, span, text))
end if

Expand Down
4 changes: 3 additions & 1 deletion src/tests/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ end subroutine unit_test_f64_mix
subroutine unit_test_literals(npass, nfail)

! Hex, octal, and binary literals, use of "_" as numeric separators,
! explicit type suffixes, ...
! explicit type "'" suffixes, ...

implicit none

Expand All @@ -1539,6 +1539,8 @@ subroutine unit_test_literals(npass, nfail)
[ &
eval_i32("0x0;") == 0, &
eval_i32("0x1;") == 1, &
eval_i32( "10;") == 5*2, &
eval_i32("0x10;") == 16, &
eval_i32("0x1'i32;") == 1, &
eval_i64("0x1'i64;") == 1, &
eval_i32("1'i32;") == 1, &
Expand Down

0 comments on commit 62ebe52

Please sign in to comment.