Skip to content

Commit

Permalink
fix transfomer validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Apr 14, 2024
1 parent 5c26c61 commit 9f92178
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
43 changes: 10 additions & 33 deletions lib/pinc_backend/Tag.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,20 @@ let find_path path value =
;;

module Utils = struct
let apply_transformer
~eval_expression
~state
~(transformer : Ast.expression option)
(value : Value.value) =
let aux (transformer : Ast.expression) =
let arguments = [ value ] in
let apply_transformer ~eval_expression ~state ~transformer value =
let aux transformer =
let maybe_fn = transformer |> eval_expression ~state |> State.get_output in
match maybe_fn.value_desc with
| Function { parameters; state = _; exec = _ }
when List.compare_lengths parameters arguments > 0 ->
let arguments_len = List.length arguments in
let missing =
parameters
|> List.filteri (fun index _ -> index > arguments_len - 1)
|> List.map (fun item -> "`" ^ item ^ "`")
|> String.concat ", "
in
Pinc_Diagnostics.error
transformer.expression_loc
("This transformer was provided too few arguments. The following parameters \
are missing: "
^ missing)
| Function { parameters; state = _; exec = _ }
when List.compare_lengths parameters arguments < 0 ->
| Function { parameters = [ value_param ]; state = fn_state; exec } ->
let arguments = StringMap.singleton value_param value in
state |> State.add_output ~output:(exec ~arguments ~state:fn_state ())
| Function { parameters; state = _; exec = _ } ->
Pinc_Diagnostics.error
transformer.expression_loc
("This transformer only accepts "
^ string_of_int (List.length parameters)
^ " arguments, but was provided "
^ string_of_int (List.length arguments)
^ " here.")
| Function { parameters; state = fn_state; exec } ->
let arguments =
List.combine parameters arguments |> List.to_seq |> StringMap.of_seq
in
state |> State.add_output ~output:(exec ~arguments ~state:fn_state ())
(Printf.sprintf
"A transformer has to accept exactly one argument (the tag value).\n\
Here it was provided %i."
(List.length parameters))
| _ ->
Pinc_Diagnostics.error
transformer.expression_loc
Expand Down
26 changes: 25 additions & 1 deletion test/interpreter_error/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$ NO_COLOR="1" print . UseNonLibrary

ERROR in file ./data.pi:2:7-19
ERROR in file ./use_non_library.pi:2:7-19

1 │ component UseNonLibrary {
2 │ use NotALibrary;
Expand All @@ -17,3 +17,27 @@
Attempted to use a non library definition.
Expected to see a Library at the right hand side of the `use` statement.
[1]

$ NO_COLOR="1" print . BadTransformer_Arity

ERROR in file ./tag_transformer_arity.pi:6:25-64

5 │ component BadTransformer_Arity_Child {
6 │ let text = #String :: fn (a, b) -> Base.String.uppercase(a);;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 │

A transformer has to accept exactly one argument (the tag value).
Here it was provided 2.
[1]
$ NO_COLOR="1" print . BadTransformer_Typ

ERROR in file ./tag_transformer_typ.pi:6:25-42

5 │ component BadTransformer_Typ_Child {
6 │ let text = #String :: "not a function";
│ ^^^^^^^^^^^^^^^^^
7 │

Trying to assign a non function value to a transformer.
[1]
9 changes: 9 additions & 0 deletions test/interpreter_error/tag_transformer_arity.pi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component BadTransformer_Arity {
<BadTransformer_Arity_Child text="Test" />
}

component BadTransformer_Arity_Child {
let text = #String :: fn (a, b) -> Base.String.uppercase(a);;

text
}
9 changes: 9 additions & 0 deletions test/interpreter_error/tag_transformer_typ.pi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component BadTransformer_Typ {
<BadTransformer_Typ_Child text="Test" />
}

component BadTransformer_Typ_Child {
let text = #String :: "not a function";

text
}
File renamed without changes.

0 comments on commit 9f92178

Please sign in to comment.