Skip to content

Commit

Permalink
Fix I37 Int64 limit
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Oct 8, 2023
1 parent 8033484 commit c5112ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
18 changes: 9 additions & 9 deletions source/Compiler.re
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ let getFieldName = json => {
| `Int(_i) => "int"
| `Null => "null"
| `String(_identifier) => "string"
/* Those 3 are added by Yojson.Safe */
| `Variant(_) => "variant"
| `Tuple(_) => "list"
| `Intlit(_) => "int"
};
};

Expand Down Expand Up @@ -234,8 +238,7 @@ let index = (value: int, json: Json.t) => {
};
};

let rec compile =
(expression: expression, json): result(list(Json.t), string) => {
let rec compile = (expression, json): result(list(Json.t), string) => {
switch (expression) {
| Identity => Results.return(json)
| Empty => empty
Expand Down Expand Up @@ -290,13 +293,10 @@ and operation = (leftR, rightR, op, json) => {
}
and map = (expr: expression, json: Json.t) => {
switch (json) {
| `List(list) =>
List.length(list) > 0
? {
Results.collect(List.map(item => compile(expr, item), list))
|> Result.map(x => [`List(x)]);
}
: Error(makeEmptyListError("map"))
| `List(list) when List.length(list) > 0 =>
Results.collect(List.map(item => compile(expr, item), list))
|> Result.map(x => [`List(x)])
| `List(_list) => Error(makeEmptyListError("map"))
| _ => Error(makeError("map", json))
};
};
36 changes: 24 additions & 12 deletions source/Json.re
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
open Printf;
open Console;

include Yojson.Basic;
include Yojson.Basic.Util;
include Yojson.Safe;
include Yojson.Safe.Util;

let (pp_print_string, pp_print_bool, pp_print_list, fprintf, asprintf) =
Format.(pp_print_string, pp_print_bool, pp_print_list, fprintf, asprintf);

let quotes = str => "\"" ++ str ++ "\"";

let parseString = str =>
try(Ok(Yojson.Basic.from_string(str))) {
try(Ok(Yojson.Safe.from_string(str))) {
| e =>
Error(Printexc.to_string(e) ++ " There was an error reading the string")
};

let parseFile = file =>
try(Ok(Yojson.Basic.from_file(file))) {
try(Ok(Yojson.Safe.from_file(file))) {
| e =>
Error(Printexc.to_string(e) ++ " There was an error reading the file")
};

let parseChannel = channel =>
try(Ok(Yojson.Basic.from_channel(channel))) {
try(Ok(Yojson.Safe.from_channel(channel))) {
| e =>
Error(
Printexc.to_string(e)
++ " There was an error reading from standard input",
)
};

let string = str => {
let encode = str => {
let buf = Buffer.create(String.length(str) * 5 / 4);
for (i in 0 to String.length(str) - 1) {
switch (str.[i]) {
Expand Down Expand Up @@ -64,8 +64,13 @@ module Color = {
| `Float(f) =>
Easy_format.Atom(f |> string_of_float |> Chalk.green, Easy_format.atom)
| `String(s) =>
Easy_format.Atom(string(s) |> quotes |> Chalk.green, Easy_format.atom)
Easy_format.Atom(encode(s) |> quotes |> Chalk.green, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s |> Chalk.green, Easy_format.atom)
| `Variant(s, _opt) =>
Easy_format.Atom(encode(s) |> Chalk.green, Easy_format.atom)
| `Tuple([])
| `List([]) => Easy_format.Atom("[]", Easy_format.atom)
| `Tuple(l)
| `List(l) =>
Easy_format.List(("[", ",", "]", array), List.map(format, l))
| `Assoc([]) => Easy_format.Atom("{}", Easy_format.atom)
Expand All @@ -74,7 +79,7 @@ module Color = {
}
and format_field = ((name, json)) => {
let s =
sprintf("%s:", string(name) |> quotes |> Chalk.blue |> Chalk.bold);
sprintf("%s:", encode(name) |> quotes |> Chalk.blue |> Chalk.bold);

Easy_format.Label(
(Easy_format.Atom(s, Easy_format.atom), Easy_format.label),
Expand All @@ -89,17 +94,20 @@ module Summarize = {
| `Null => Easy_format.Atom("null", Easy_format.atom)
| `Bool(b) => Easy_format.Atom(string_of_bool(b), Easy_format.atom)
| `Int(i) => Easy_format.Atom(i |> string_of_int, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> string_of_float, Easy_format.atom)
| `String(s) => Easy_format.Atom(string(s) |> quotes, Easy_format.atom)
| `String(s) => Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
| `List([]) => Easy_format.Atom("[]", Easy_format.atom)
| `Variant(s, _opt) => Easy_format.Atom(s, Easy_format.atom)
| `Tuple(l)
| `List(l) =>
Easy_format.List(("[", ",", "]", array), List.map(format, l))
| `Assoc([]) => Easy_format.Atom("{}", Easy_format.atom)
| `Assoc(l) =>
Easy_format.List(("{", ",", "}", record), List.map(format_field, l))
}
and format_field = ((name, _json)) => {
let s = sprintf("%s:", string(name) |> quotes);
let s = sprintf("%s:", encode(name) |> quotes);

Easy_format.Label(
(Easy_format.Atom(s, Easy_format.atom), Easy_format.label),
Expand All @@ -114,17 +122,21 @@ module NoColor = {
| `Null => Easy_format.Atom("null", Easy_format.atom)
| `Bool(b) => Easy_format.Atom(string_of_bool(b), Easy_format.atom)
| `Int(i) => Easy_format.Atom(i |> string_of_int, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> string_of_float, Easy_format.atom)
| `String(s) => Easy_format.Atom(string(s) |> quotes, Easy_format.atom)
| `String(s) => Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
| `Variant(s, _opt) =>
Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
| `List([]) => Easy_format.Atom("[]", Easy_format.atom)
| `Tuple(l)
| `List(l) =>
Easy_format.List(("[", ",", "]", array), List.map(format, l))
| `Assoc([]) => Easy_format.Atom("{}", Easy_format.atom)
| `Assoc(l) =>
Easy_format.List(("{", ",", "}", record), List.map(format_field, l))
}
and format_field = ((name, json)) => {
let s = sprintf("%s:", string(name) |> quotes);
let s = sprintf("%s:", encode(name) |> quotes);

Easy_format.Label(
(Easy_format.Atom(s, Easy_format.atom), Easy_format.label),
Expand Down
2 changes: 1 addition & 1 deletion test/Runtime_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ let tests = [
),
case(
{| { "overflow": 12345678999999999999 } |},
`Assoc([("overflow", `String("12345678999999999999"))]),
`Assoc([("overflow", `Intlit("12345678999999999999"))]),
),
];

0 comments on commit c5112ec

Please sign in to comment.