Skip to content

Commit

Permalink
add parse_f64, min, and max fns for f64
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffIrwin committed Sep 27, 2024
1 parent f1271d4 commit 2049345
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
51 changes: 48 additions & 3 deletions src/core.f90
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ subroutine declare_intrinsic_fns(fns)
i64_sca_fn, parse_i64_fn, i32_sca_fn, exit_fn, any_fn, all_fn, count_fn, &
min_i64_fn, max_i64_fn, i32_arr_fn, i64_arr_fn, sum_i32_fn, &
sum_f32_fn, sum_i64_fn, parse_f32_fn, min_f32_fn, max_f32_fn, &
char_fn, sum_f64_fn
char_fn, sum_f64_fn, parse_f64_fn, min_f64_fn, max_f64_fn

! Increment index for each fn and then set num_fns
id_index = 0
Expand Down Expand Up @@ -260,8 +260,6 @@ subroutine declare_intrinsic_fns(fns)
! same type. For example, min(1, 2) and min(1.1, 2.1) are allowed, but
! min(1, 2.1) does not compile. I think that's a reasonable restriction

! TODO: min_f64_fn, max_f64_fn

min_i32_fn%type%type = i32_type
allocate(min_i32_fn%params(2))
allocate(min_i32_fn%param_names%v(2))
Expand Down Expand Up @@ -315,6 +313,23 @@ subroutine declare_intrinsic_fns(fns)

!********

min_f64_fn%type%type = f64_type
allocate(min_f64_fn%params(2))
allocate(min_f64_fn%param_names%v(2))

min_f64_fn%params(1)%type = f64_type
min_f64_fn%param_names%v(1)%s = "a0"

min_f64_fn%params(2)%type = f64_type
min_f64_fn%param_names%v(2)%s = "a1"

min_f64_fn%variadic_min = 0
min_f64_fn%variadic_type = f64_type

call fns%insert("0min_f64", min_f64_fn, id_index)

!********

max_i32_fn%type%type = i32_type
allocate(max_i32_fn%params(2))
allocate(max_i32_fn%param_names%v(2))
Expand Down Expand Up @@ -368,6 +383,23 @@ subroutine declare_intrinsic_fns(fns)

!********

max_f64_fn%type%type = f64_type
allocate(max_f64_fn%params(2))
allocate(max_f64_fn%param_names%v(2))

max_f64_fn%params(1)%type = f64_type
max_f64_fn%param_names%v(1)%s = "a0"

max_f64_fn%params(2)%type = f64_type
max_f64_fn%param_names%v(2)%s = "a1"

max_f64_fn%variadic_min = 0
max_f64_fn%variadic_type = f64_type

call fns%insert("0max_f64", max_f64_fn, id_index)

!********

! TODO: update docs to use println() instead of old holyc implicit prints

println_fn%type%type = void_type ! TODO?
Expand Down Expand Up @@ -441,6 +473,16 @@ subroutine declare_intrinsic_fns(fns)

!********

parse_f64_fn%type%type = f64_type
allocate(parse_f64_fn%params(1))
allocate(parse_f64_fn%param_names%v(1))
parse_f64_fn%params(1)%type = str_type
parse_f64_fn%param_names%v(1)%s = "str"

call fns%insert("parse_f64", parse_f64_fn, id_index)

!********

char_fn%type%type = str_type
allocate(char_fn%params(1))
allocate(char_fn%param_names%v(1))
Expand Down Expand Up @@ -739,15 +781,18 @@ subroutine declare_intrinsic_fns(fns)
min_i32_fn , &
min_i64_fn , &
min_f32_fn , &
min_f64_fn , &
max_i32_fn , &
max_i64_fn , &
max_f32_fn , &
max_f64_fn , &
println_fn , &
str_fn , &
len_fn , &
parse_i32_fn, &
parse_i64_fn, &
parse_f32_fn, &
parse_f64_fn, &
char_fn , &
i32_sca_fn , &
i32_arr_fn , &
Expand Down
33 changes: 33 additions & 0 deletions src/eval.f90
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,17 @@ recursive subroutine eval_fn_call(node, state, res)
end do
state%returned = .true.

case ("0min_f64")

call syntax_eval(node%args(1), state, arg)
res%sca%f64 = arg%sca%f64

do i = 2, size(node%args)
call syntax_eval(node%args(i), state, arg)
res%sca%f64 = min(res%sca%f64, arg%sca%f64)
end do
state%returned = .true.

case ("0max_i32")

call syntax_eval(node%args(1), state, arg)
Expand Down Expand Up @@ -806,6 +817,17 @@ recursive subroutine eval_fn_call(node, state, res)
end do
state%returned = .true.

case ("0max_f64")

call syntax_eval(node%args(1), state, arg)
res%sca%f64 = arg%sca%f64

do i = 2, size(node%args)
call syntax_eval(node%args(i), state, arg)
res%sca%f64 = max(res%sca%f64, arg%sca%f64)
end do
state%returned = .true.

case ("println")

! TODO: if struct, pass a struct_t as opt arg to to_str(), which
Expand Down Expand Up @@ -877,6 +899,17 @@ recursive subroutine eval_fn_call(node, state, res)
end if
state%returned = .true.

case ("parse_f64")

call syntax_eval(node%args(1), state, arg)
read(arg%sca%str%s, *, iostat = io) res%sca%f64
if (io /= 0) then
write(*,*) err_rt_prefix//" cannot parse_f64() for argument `"// &
arg%sca%str%s//"`"//color_reset
call internal_error()
end if
state%returned = .true.

case ("char")

! The `i32()` intrinsic uses iachar(), so this should use achar(), not
Expand Down
8 changes: 7 additions & 1 deletion src/parse_fn.f90
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ recursive module function parse_fn_call(parser) result(fn_call)

! Resolve special overloaded intrinsic fns
!
! TODO: split out to a routine if possible
!
! TODO: split out to a routine if possible
!
select case (identifier%text)
case ("min")

Expand All @@ -89,6 +91,8 @@ recursive module function parse_fn_call(parser) result(fn_call)
fn_call%identifier%text = "0min_i64"
case (f32_type)
fn_call%identifier%text = "0min_f32"
case (f64_type)
fn_call%identifier%text = "0min_f64"
case default
fn_call%identifier%text = "0min_i32"
end select
Expand All @@ -103,6 +107,8 @@ recursive module function parse_fn_call(parser) result(fn_call)
fn_call%identifier%text = "0max_i64"
case (f32_type)
fn_call%identifier%text = "0max_f32"
case (f64_type)
fn_call%identifier%text = "0max_f64"
case default
fn_call%identifier%text = "0max_i32"
end select
Expand Down
2 changes: 2 additions & 0 deletions src/tests/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ subroutine unit_test_intr_fns(npass, nfail)
abs(eval_f32('max(2.0f, 3.0f);') - 3.0) < tol, &
abs(eval_f32('max(4.0f, 3.0f, 5.0f);') - 5.0) < tol, &
abs(eval_f32('max(4.0f, 3.0f, -5.0f);') - 4.0) < tol, &
abs(eval_f64('min(3.0, 2.0);') - 2.0) < tol, &
abs(eval_f64('min(2.0, 3.0);') - 2.0) < tol, &
eval_i32('max(3, 2);') == 3, &
eval_i32('max(2, 2);') == 2, &
eval_i32('max(2, 3, 4);') == 4, &
Expand Down

0 comments on commit 2049345

Please sign in to comment.