Skip to content

Commit

Permalink
Config: Fix issue with boolean values in configuration
Browse files Browse the repository at this point in the history
Improve support for large integers and ranges
  • Loading branch information
Alasdair committed Jan 15, 2025
1 parent 35eedf7 commit 2d6d09b
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 24 deletions.
30 changes: 17 additions & 13 deletions lib/json/sail_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ sail_config_json sail_config_list_nth(const sail_config_json config, int64_t ind
return (sail_config_json)item;
}

bool sail_config_is_bool(const sail_config_json config)
{
return cJSON_IsBool((cJSON *)config);
}

bool sail_config_unwrap_bool(const sail_config_json config)
{
return cJSON_IsTrue((cJSON *)config);
}

bool sail_config_is_object(const sail_config_json config)
{
return cJSON_IsObject((cJSON *)config);
Expand All @@ -133,6 +143,11 @@ bool sail_config_is_string(const sail_config_json config)
return cJSON_IsString((cJSON *)config);
}

bool sail_config_is_int(const sail_config_json config)
{
return cJSON_IsNumber((cJSON *)config);
}

bool sail_config_is_array(const sail_config_json config)
{
return cJSON_IsArray((cJSON *)config);
Expand Down Expand Up @@ -169,17 +184,6 @@ bool sail_config_is_bits(const sail_config_json config)
return is_bool_array || is_bv_object;
}

bool sail_config_is_bool_array_with_size(const sail_config_json config, mach_int expected)
{
if (!sail_config_is_bool_array(config)) {
return false;
}

int len = cJSON_GetArraySize((cJSON *)config);

return (mach_int)len == expected;
}

void sail_config_unwrap_string(sail_string *str, const sail_config_json config)
{
sail_string conf_str = cJSON_GetStringValue((cJSON *)config);
Expand All @@ -191,8 +195,8 @@ void sail_config_unwrap_string(sail_string *str, const sail_config_json config)

void sail_config_unwrap_int(sail_int *n, const sail_config_json config)
{
char *str = cJSON_GetStringValue((cJSON *)config);
mpz_set_str(*n, str, 10);
cJSON *json = (cJSON *)config;
mpz_set_str(*n, json->valuestring, 10);
}

void sail_config_truncate(lbits *rop) {
Expand Down
13 changes: 7 additions & 6 deletions lib/json/sail_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ sail_config_json sail_config_object_key(const sail_config_json config, const sai
int64_t sail_config_list_length(const sail_config_json config);
sail_config_json sail_config_list_nth(const sail_config_json config, int64_t index);

bool sail_config_is_string(const sail_config_json config);

bool sail_config_is_array(const sail_config_json config);
bool sail_config_is_bool_array(const sail_config_json config);
bool sail_config_is_bool_array_with_size(const sail_config_json config, mach_int expected);
bool sail_config_is_bits(const sail_config_json config);
bool sail_config_is_bool(const sail_config_json config);
bool sail_config_is_bool_array(const sail_config_json config);
bool sail_config_is_int(const sail_config_json config);
bool sail_config_is_string(const sail_config_json config);

void sail_config_unwrap_string(sail_string *str, const sail_config_json config);
void sail_config_unwrap_int(sail_int *n, const sail_config_json config);
void sail_config_unwrap_bits(lbits *bv, const sail_config_json config);
bool sail_config_unwrap_bool(const sail_config_json config);
void sail_config_unwrap_int(sail_int *n, const sail_config_json config);
void sail_config_unwrap_string(sail_string *str, const sail_config_json config);

#ifdef __cplusplus
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ end = struct
Some (logic_to_schema nc_logic)
| _ -> None
)
| [], NC_aux (NC_true, _), Typ_aux (Typ_app (id, _), _) when string_of_id id = "atom_bool" ->
Some (`Assoc [("type", `String "boolean")])
| _, NC_aux (NC_true, _), Typ_aux (Typ_app (id, [A_aux (A_bool arg, _)]), _) when string_of_id id = "atom_bool"
-> (
match (kopts, arg) with
| [KOpt_aux (KOpt_kind (_, v), _)], NC_aux (NC_var v', _) when Kid.compare v v' = 0 ->
Some (`Assoc [("type", `String "boolean")])
| _ -> None
)
| _, _, Typ_aux (Typ_app (id, [A_aux (A_nexp arg, _)]), _) when string_of_id id = "bitvector" -> (
let schema_bool_array clauses =
[("type", `String "array"); ("items", `Assoc [("type", `String "boolean")])] @ clauses
Expand Down Expand Up @@ -498,6 +503,7 @@ let rec sail_exp_from_json ~at:l env typ =
else mk_lit_exp ~loc:l (L_string s)
| `Bool true -> mk_lit_exp ~loc:l L_true
| `Bool false -> mk_lit_exp ~loc:l L_false
| `Null -> mk_lit_exp ~loc:l L_unit
| `List jsons -> (
let base_typ = match destruct_exist typ with None -> typ | Some (_, _, typ) -> typ in
match base_typ with
Expand Down
6 changes: 4 additions & 2 deletions src/lib/jib_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,13 @@ module Make (C : CONFIG) = struct
| CT_string ->
config_extract CT_string json ~validate:("sail_config_is_string", []) ~extract:"sail_config_unwrap_string"
| CT_unit -> ([], (fun clexp -> icopy l clexp unit_cval), [])
| CT_lint -> config_extract CT_lint json ~validate:("sail_config_is_string", []) ~extract:"sail_config_unwrap_int"
| CT_lint -> config_extract CT_lint json ~validate:("sail_config_is_int", []) ~extract:"sail_config_unwrap_int"
| CT_fint _ -> config_extract CT_lint json ~validate:("sail_config_is_int", []) ~extract:"sail_config_unwrap_int"
| CT_lbits ->
config_extract CT_lbits json ~validate:("sail_config_is_bits", []) ~extract:"sail_config_unwrap_bits"
| CT_fbits n ->
| CT_fbits _ ->
config_extract CT_lbits json ~validate:("sail_config_is_bits", []) ~extract:"sail_config_unwrap_bits"
| CT_bool -> config_extract CT_bool json ~validate:("sail_config_is_bool", []) ~extract:"sail_config_unwrap_bool"
| CT_struct (_, fields) as struct_ctyp ->
let struct_name = ngensym () in
let fields_from_json =
Expand Down
1 change: 1 addition & 0 deletions test/c/config_bool.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
5 changes: 5 additions & 0 deletions test/c/config_bool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"some" : {
"boolean" : true
}
}
18 changes: 18 additions & 0 deletions test/c/config_bool.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
default Order dec

$include <prelude.sail>

$iftarget c
$c_in_main sail_config_set_file("config_bool.json");
$c_in_main_post sail_config_cleanup();
$else
$option --config ../c/config_bool.json
$endif

val main : unit -> unit

function main() = {
if config some.boolean then {
print_endline("Hello, World!")
}
}
4 changes: 4 additions & 0 deletions test/c/config_int.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x = 64
y = 0
z = 8392413984723472389546328576138756413875644375
w = 9999999999999999999999999999999999999999999999999999999
6 changes: 6 additions & 0 deletions test/c/config_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"c1" : 64,
"c2" : 0,
"c3" : 8392413984723472389546328576138756413875644375,
"c4" : 9999999999999999999999999999999999999999999999999999999
}
23 changes: 23 additions & 0 deletions test/c/config_int.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
default Order dec

$include <prelude.sail>

$iftarget c
$c_in_main sail_config_set_file("config_int.json");
$c_in_main_post sail_config_cleanup();
$else
$option --config ../c/config_int.json
$endif

val main : unit -> unit

function main() = {
let x : range(0, 64) = config c1;
print_int("x = ", x);
let y : range(0, 1) = config c2;
print_int("y = ", y);
let z : int = config c3;
print_int("z = ", z);
let w : {'n, 'n >= 0. int('n)} = config c4;
print_int("w = ", w);
}
2 changes: 1 addition & 1 deletion test/c/config_test.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hello": {
"world": "Hello, World!",
"number": "13438537428731902344561435823034520154709854735643",
"number": 13438537428731902344561435823034520154709854735643,
"bits" : [true, false, false, false, false]
}
}
1 change: 1 addition & 0 deletions test/c/config_unit.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok
3 changes: 3 additions & 0 deletions test/c/config_unit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"c1" : null
}
17 changes: 17 additions & 0 deletions test/c/config_unit.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default Order dec

$include <prelude.sail>

$iftarget c
$c_in_main sail_config_set_file("config_unit.json");
$c_in_main_post sail_config_cleanup();
$else
$option --config ../c/config_unit.json
$endif

val main : unit -> unit

function main() = {
let x : unit = config c1;
print_endline("ok")
}

0 comments on commit 2d6d09b

Please sign in to comment.