From 62ebe528eefa8937c8500026971ce1e1bd1c7bcf Mon Sep 17 00:00:00 2001 From: Jeff Irwin Date: Sat, 9 Nov 2024 16:27:38 -0500 Subject: [PATCH] add specific diagnostics for i32 vs i64 --- src/errors.f90 | 19 +++++++++++++++++-- src/lex.f90 | 17 +++++++++++++---- src/tests/test.f90 | 4 +++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/errors.f90 b/src/errors.f90 index 980fc76..5bf9e90 100644 --- a/src/errors.f90 +++ b/src/errors.f90 @@ -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 @@ -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 !=============================================================================== diff --git a/src/lex.f90 b/src/lex.f90 index 9b36501..416505c 100644 --- a/src/lex.f90 +++ b/src/lex.f90 @@ -102,6 +102,10 @@ 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" @@ -109,6 +113,9 @@ function lex(lexer) result(token) 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")) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/tests/test.f90 b/src/tests/test.f90 index 3392891..1de0ff2 100644 --- a/src/tests/test.f90 +++ b/src/tests/test.f90 @@ -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 @@ -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, &