Skip to content

Commit

Permalink
translate assert expression & wrap main body with a try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Aug 23, 2024
1 parent f9afca3 commit 3fef7cc
Show file tree
Hide file tree
Showing 19 changed files with 237 additions and 35 deletions.
24 changes: 21 additions & 3 deletions lib/back/closure_translator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ let ff_is_equal_aux = C.VARIABLE "ff_is_equal_aux"

let ff_is_zero = C.VARIABLE "ff_is_zero"

let ff_assert = C.VARIABLE "ff_assert"

let ff_is_not_equal = C.VARIABLE "ff_is_not_equal"

let ff_get_mem = C.VARIABLE "ff_get_member"
Expand All @@ -73,8 +75,9 @@ let ff_match_constr = C.VARIABLE "ff_match_constr"
let ff_match_tuple = C.VARIABLE "ff_match_tuple"

let header = {|
#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

|}

Expand Down Expand Up @@ -332,6 +335,14 @@ and trans_expr ctx e =
let _e0_v, e0_stmts = trans_expr ctx e0 in
let e1_v, e1_stmts = trans_expr ctx e1 in
(e1_v, e0_stmts @ e1_stmts)
| EAssert e0 ->
let e0_v, e0_stmts = trans_expr ctx e0 in
let is_true_v = create_decl "is_true" ctx in
let assert_stmt =
make_assign (VARIABLE is_true_v)
(CALL (ff_assert, [ VARIABLE e0_v ]))
in
(is_true_v, e0_stmts @ [ assert_stmt ])
| EStruct _ -> ("todo", [])

and trans_const (c : S.constant) =
Expand Down Expand Up @@ -518,7 +529,14 @@ let translate (main, (fns : func list)) =
Printf.sprintf {|
int main()
{
%s(nullptr);
try
{
%s(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %%s", error.what());
}
}
|} main_name
in
Expand Down
1 change: 1 addition & 0 deletions lib/clos/closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type expr =
| EField of expr * string
| ECmp of T.cmp_op * expr * expr
| ESeq of expr * expr
| EAssert of expr

and pattern = L.pattern

Expand Down
4 changes: 3 additions & 1 deletion lib/clos/lift.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ let rec lift ?(hint = "temp") (e : L.expr) (vars : string list) :
| L.EField (e, name) ->
let e', fns = lift e vars ~hint in
(C.EField (e', name), fns)
| L.EAssert _ -> failwith "todo"
| L.EAssert e ->
let e', fns = lift e vars ~hint in
(C.EAssert e', fns)

and lift_letrec binds vars =
let xs = List.map fst binds in
Expand Down
2 changes: 2 additions & 0 deletions runtime/include/fun_rt_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@ ff_obj_t ff_is_not_equal(ff_obj_t x, ff_obj_t y);

bool ff_is_zero(ff_obj_t x);

ff_obj_t ff_assert(ff_obj_t x);

#endif
9 changes: 9 additions & 0 deletions runtime/src/fun_rt_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdio>
#include <cstring>
#include <stddef.h>
#include <stdexcept>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -200,3 +201,11 @@ bool ff_is_zero(ff_obj_t x) {
auto val = ff_get_int(x);
return val == 0;
}

ff_obj_t ff_assert(ff_obj_t x) {
auto val = ff_get_int(x);
if (val != 0) {
throw std::runtime_error("Assertion failed!");
}
return ff_make_int(0);
}
58 changes: 58 additions & 0 deletions tests/cram/test_dirs/assert.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


$ ff test_assert.fun -o test_assert.cpp

$ cat test_assert.cpp

#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

ff_obj_t main_1__fn(ff_fvs_t fvs_1)
{
ff_obj_t mod_12;
ff_obj_t x_11;
ff_obj_t is_true_10;
ff_obj_t temp_9;
ff_obj_t x_8;
ff_obj_t temp_7;
ff_obj_t app_res_6;
ff_obj_t x_5;
ff_obj_t is_true_4;
ff_obj_t temp_3;
ff_obj_t println_str_2;
println_str_2 = ff_builtin_println_str;
temp_3 = ff_make_int(1);
is_true_4 = ff_assert(temp_3);
x_5 = is_true_4;
temp_7 = ff_make_str("A true asserted!");
app_res_6 = ff_apply_generic(println_str_2, temp_7);
x_8 = app_res_6;
temp_9 = ff_make_int(0);
is_true_10 = ff_assert(temp_9);
x_11 = is_true_10;
mod_12 = ff_make_mod_obj(4, {"println_str", "x", "x", "x"},
{println_str_2, x_5, x_8, x_11});
return mod_12;
}


int main()
{
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ $FF test_assert.fun

$ ./test_assert.fun.out
Runtime error: Assertion failed!

7 changes: 7 additions & 0 deletions tests/cram/test_dirs/assert.t/test_assert.fun
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
external println_str : string -> unit = "ff_builtin_println_str"

let x = assert true

let x = println_str "A true asserted!"

let x = assert false
14 changes: 11 additions & 3 deletions tests/cram/test_dirs/equality.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

$ cat test_equality.fun.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

Expand Down Expand Up @@ -84,7 +85,14 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ ./test_equality.fun.out
Expand Down
28 changes: 22 additions & 6 deletions tests/cram/test_dirs/external.t/run.t
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

$ ff test_external.fun --stdout

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

Expand All @@ -18,16 +19,24 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}


$ $FF test_add_external.fun --stdout

$ cat test_add_external.fun.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

Expand Down Expand Up @@ -60,7 +69,14 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ ./test_add_external.fun.out
Expand Down
14 changes: 11 additions & 3 deletions tests/cram/test_dirs/hello.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

$ cat hello.fun.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

Expand All @@ -33,5 +34,12 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}
14 changes: 11 additions & 3 deletions tests/cram/test_dirs/interval_functor.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

$ cat test_interval.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);
ff_obj_t print_int_interval_14__fn(ff_fvs_t fvs_4, ff_obj_t interval_3);
Expand Down Expand Up @@ -555,7 +556,14 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ $FF test_interval.fun
Expand Down
14 changes: 11 additions & 3 deletions tests/cram/test_dirs/literal.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

$ cat test_literal.fun.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);

Expand Down Expand Up @@ -34,6 +35,13 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

14 changes: 11 additions & 3 deletions tests/cram/test_dirs/match.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

$ cat test_match.fun.cpp

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);
ff_obj_t f_2__fn(ff_fvs_t fvs_3, ff_obj_t x_2);
Expand Down Expand Up @@ -151,7 +152,14 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ ./test_match.fun.out
Expand Down
14 changes: 11 additions & 3 deletions tests/cram/test_dirs/simple.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
$ ff simple.fun
$ ff simple.fun --stdout

#include"fun_rt.hpp"
#include<stdio.h>
#include "fun_rt.hpp"
#include <stdio.h>
#include <stdexcept>

ff_obj_t main_1__fn(ff_fvs_t fvs_1);
ff_obj_t m_4__fn(ff_fvs_t fvs_3, ff_obj_t x_2);
Expand Down Expand Up @@ -76,7 +77,14 @@

int main()
{
main_1__fn(nullptr);
try
{
main_1__fn(nullptr);
}
catch (const std::runtime_error& error)
{
printf("Runtime error: %s", error.what());
}
}

$ ff simple.fun -o simple1.out --debug
Expand Down
Loading

0 comments on commit 3fef7cc

Please sign in to comment.