Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/stancjs/stancjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ let stan2cpp model_name model_string flags =
"Semantic check failed but reported no errors. This should never \
happen."
in
if is_flag_set "version" then Result.Ok (Fmt.strf "%s" version, [])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to do something to keep the ability for phases of the compiler to return warnings rather than delete these for-now empty lists.

if is_flag_set "version" then Result.Ok (Fmt.strf "%s" version)
else
Result.bind ast
~f:
(Fn.compose semantic_err_to_string
Semantic_check.semantic_check_program)
|> Result.map ~f:(fun typed_ast ->
if is_flag_set "print-canonical" then
( Pretty_printing.pretty_print_typed_program
(Canonicalize.canonicalize_program typed_ast)
, [] )
Pretty_printing.pretty_print_typed_program
(Canonicalize.canonicalize_program typed_ast)
else if is_flag_set "auto-format" then
match ast with
| Result.Ok f -> (Pretty_printing.pretty_print_program f, [])
| _ -> ("Error: This should not happend!", [])
| Result.Ok f -> Pretty_printing.pretty_print_program f
| _ -> "Error: This should not happend!"
else
let mir = Ast_to_Mir.trans_prog model_name typed_ast in
let tx_mir = Transform_Mir.trans_prog mir in
Expand All @@ -72,21 +71,26 @@ let stan2cpp model_name model_string flags =
Pedantic_analysis.print_warn_uninitialized mir ;
if is_flag_set "warn-pedantic" then
Pedantic_analysis.print_warn_pedantic mir ;
(cpp, []) )
cpp )

let stanc_stdout = ref ""
let stanc_stderr = ref ""

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use a list or something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, a list would be constant time to append to instead of O(N)


let wrap_result = function
| Result.Ok (s, k) ->
| Result.Ok s ->
Js.Unsafe.obj
[| ("result", Js.Unsafe.inject (Js.string s))
; ( "warnings"
, Js.Unsafe.inject
(Js.array (List.to_array (List.map ~f:Js.string k))) ) |]
; ("warnings", Js.Unsafe.inject (Js.string !stanc_stderr)) |]
| Error e ->
Js.Unsafe.obj
[| ("errors", Js.Unsafe.inject (Array.map ~f:Js.string [|e|]))
; ("warnings", Js.Unsafe.inject Js.array_empty) |]
; ("warnings", Js.Unsafe.inject (Js.string !stanc_stderr)) |]

let stan2cpp_wrapped name code (flags : Js.string_array Js.t Js.opt) =
stanc_stdout := "" ;
stanc_stderr := "" ;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to reset every time because these are global variables. If we don't, we just accumulate warnings from all previous compilations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better pattern would be to have the components return warnings and errors rather than printing them. Could we do that instead or would that mess up the timing for a release or something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first plan, but I quickly realized that is a pretty big refactor. Particulary not sure on how to return from the parser for example.

Sys_js.set_channel_flusher stdout (fun s -> stanc_stdout := !stanc_stdout ^ s) ;
Sys_js.set_channel_flusher stderr (fun s -> stanc_stderr := !stanc_stderr ^ s) ;
stan2cpp (Js.to_string name) (Js.to_string code)
Js.(
Opt.map flags (fun a ->
Expand Down
13 changes: 12 additions & 1 deletion test/stancjs/pedantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@ model {
}
`
var pedantic_test = stanc.stanc("pedantic", pedantic_model, ["warn-pedantic"]);
if (pedantic_test.warnings) {
console.log(pedantic_test.warnings)
}

var pedantic_test = stanc.stanc("pedantic", pedantic_model);

if (pedantic_test.warnings) {
console.log(pedantic_test.warnings)
}
var warn_uninit_model = `
transformed data {
real tt;
tt = tt + 2;
}
`
var warn_uninit_test = stanc.stanc("uninit", warn_uninit_model, ["warn-uninitialized"]);
if (warn_uninit_test.warnings) {
console.log(warn_uninit_test.warnings)
}

var warn_uninit_test = stanc.stanc("uninit", warn_uninit_model);
if (warn_uninit_test.warnings) {
console.log(warn_uninit_test.warnings)
}
9 changes: 9 additions & 0 deletions test/stancjs/stancjs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ Warning:
Warning at 'string', line 7, column 17 to column 22:
Argument 10000 suggests there may be parameters that are not unit scale;
consider rescaling with a multiplier (see manual section 22.12).

Warning at 'string', line 4, column 9 to column 11:
The variable tt may not have been assigned a value before its use.

$ node standalone-functions.js

$ node version.js
%%NAME%% %%VERSION%%
%%NAME%% %%VERSION%%
%%NAME%% %%VERSION%%
$ node warnings.js

Warning: deprecated language construct used in 'string', line 4, column 4:

Comments beginning with # are deprecated. Please use // in place of # for line comments.


14 changes: 14 additions & 0 deletions test/stancjs/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var stanc = require('../../src/stancjs/stancjs.bc.js');
var utils = require("./utils/utils.js");

var deprecated_model = `
parameters {
real y;
# hash comment is deprecated
}
model {
y ~ normal(0,1);
}
`
var deprecated_test = stanc.stanc("deprecated", deprecated_model);
console.log(deprecated_test.warnings)