123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958 |
- module Tree = Qsp_syntax.Tree
- module Ast = Tree.Ast
- module Check = Qsp_syntax.Check
- module S = Qsp_syntax.S
- module T = Qsp_syntax.T
- let location_id, e1 = Check.build (module Tree)
- module Parser = Check.Make (struct
- let t = [| e1 |]
- end)
- let _position = (Lexing.dummy_pos, Lexing.dummy_pos)
- type 'a location = 'a * 'a Ast.statement list [@@deriving eq, show]
- let get_location :
- (S.pos location, Qsp_syntax.Report.t) result -> S.pos location = function
- | Ok e -> e
- | Error e ->
- let msg = Format.asprintf "%a" Qsp_syntax.Report.pp e in
- raise (Failure msg)
- (** Run the parser with the given expression and return the result *)
- let parse : string -> (S.pos location, Qsp_syntax.Report.t) result =
- fun content ->
- let lexing =
- Sedlexing.Latin1.from_string content |> Qparser.Lexbuf.from_lexbuf
- in
- let context = Parser.initialize () in
- Qparser.Analyzer.parse (module Parser) lexing context
- |> Result.map (fun v ->
- (* Uncatched excteptions here, but we are in the tests…
- If it’s fail here I have an error in the code. *)
- Array.get v.Qparser.Analyzer.content 0
- |> Check.get location_id |> Option.get)
- let location : S.pos location Alcotest.testable =
- let equal = equal_location (fun _ _ -> true) in
- let pp =
- pp_location (fun formater _ -> Format.fprintf formater "_position")
- in
- Alcotest.testable pp equal
- let test_empty_location () =
- let expected = (_position, [])
- and actual = get_location @@ parse {|# Location
- ------- |}
- and msg = "Empty location" in
- Alcotest.(check' location ~msg ~expected ~actual)
- let test_location_without_space () =
- let expected = (_position, [])
- and actual = get_location @@ parse {|#Location
- ------- |}
- and msg = "Empty location" in
- Alcotest.(check' location ~msg ~expected ~actual)
- let test_location_without_database () =
- let expected = (_position, [])
- and actual = get_location @@ parse {|# $Location
- ------- |}
- and msg = "Location without database" in
- let () = Alcotest.(check' location ~msg ~expected ~actual) in
- let actual = get_location @@ parse {|# !Location
- ------- |} in
- let () = Alcotest.(check' location ~msg ~expected ~actual) in
- let actual = get_location @@ parse {|# ^Location
- ------- |} in
- Alcotest.(check' location ~msg ~expected ~actual)
- let _test_instruction : string -> S.pos Ast.statement list -> unit =
- fun literal expected ->
- let expected = (_position, expected)
- and _location = Printf.sprintf {|# Location
- %s
- ------- |} literal in
- let actual = get_location @@ parse _location and msg = literal in
- Alcotest.(check' location ~msg ~expected ~actual)
- let test_numeric_expression () =
- _test_instruction "123" [ Expression (Integer (_position, "123")) ]
- let test_negative_numeric_expression () =
- _test_instruction "-123"
- [
- Tree.Ast.Expression
- (Tree.Ast.Op (_position, T.Neg, Tree.Ast.Integer (_position, "123")));
- ]
- let test_negative_numeric_expression2 () =
- let index = None in
- let var = { Ast.pos = _position; name = "CURTIMESUN"; index } in
- _test_instruction "-(780-CurTimeSun)"
- Ast.
- [
- Expression
- (Op
- ( _position,
- Neg,
- BinaryOp (_position, Minus, Integer (_position, "780"), Ident var)
- ));
- ]
- let test_str_variable () =
- let index = None in
- let var = { Ast.pos = _position; name = "$VALUE"; index } in
- _test_instruction "$value" [ Expression (Ident var) ]
- let test_variable () =
- let index = None in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction "value" [ Expression (Ident var) ]
- let test_indexed_variable () =
- let index = Some Ast.(Integer (_position, "1")) in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction "value[1]" [ Expression (Ident var) ]
- let test_let_literal () =
- let index = None in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction "let value = '123'"
- Ast.
- [
- Declaration (_position, var, Eq', Literal (_position, [ T.Text "123" ]));
- ]
- let test_set_array_append () =
- let var = { Ast.pos = _position; name = "$VALUE"; index = None } in
- _test_instruction "set $value[] = ''"
- Ast.
- [ Declaration (_position, var, Eq', Literal (_position, [ T.Text "" ])) ]
- let test_direct_assignation () =
- let index = None in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction "value = '123'"
- Ast.
- [
- Declaration (_position, var, Eq', Literal (_position, [ T.Text "123" ]));
- ]
- let test_command_assignation () =
- let index = None in
- let st_1 = { Ast.pos = _position; name = "ST_1"; index } in
- _test_instruction "st_1 = input 'Enter the amount'"
- Ast.
- [
- Declaration
- ( _position,
- st_1,
- Eq',
- Function
- ( _position,
- Input,
- [ Literal (_position, [ T.Text "Enter the amount" ]) ] ) );
- ]
- let test_assignation2 () =
- let index = None in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction "set value += 123"
- Ast.[ Declaration (_position, var, Inc, Integer (_position, "123")) ]
- let test_multilie_literal () =
- let index = None in
- let var = { Ast.pos = _position; name = "VALUE"; index } in
- _test_instruction {|
- value = {
- $a = '123'
- }|}
- Ast.
- [
- Declaration
- (_position, var, Eq', Literal (_position, [ T.Text "\n$a = '123'\n" ]));
- ]
- let test_nested_literal () =
- _test_instruction
- {|
- value = {
- {
- }
- }|}
- [
- Ast.Declaration
- ( _position,
- { Ast.pos = _position; name = "VALUE"; index = None },
- Qsp_syntax.T.Eq',
- Ast.Literal (_position, [ T.Text "\n\n {\n\n }\n" ]) );
- ]
- let test_concat_literal () =
- _test_instruction {|
- '123'
- +'456'
- |}
- [
- Ast.Expression (Ast.Literal (_position, [ T.Text "123" ]));
- Ast.Expression
- (Ast.Op
- ( _position,
- Qsp_syntax.T.Add,
- Ast.Literal (_position, [ T.Text "456" ]) ));
- ]
- let test_literal () =
- _test_instruction "'123'"
- [ Expression (Literal (_position, [ T.Text "123" ])) ]
- let test_qutoted_literal () =
- _test_instruction "'12''3'"
- [ Expression (Literal (_position, [ T.Text "12'3" ])) ]
- let test_multiline1 () =
- let content = {|
- apples = 5
- pears = 10
- |} in
- let index = None in
- let apples = { Ast.pos = _position; name = "APPLES"; index }
- and pears = { Ast.pos = _position; name = "PEARS"; index }
- and value_5 = Ast.Integer (_position, "5")
- and value_10 = Ast.Integer (_position, "10") in
- _test_instruction content
- Ast.
- [
- Declaration (_position, apples, Eq', value_5);
- Declaration (_position, pears, Eq', value_10);
- ]
- let test_multiline2 () =
- let content = "apples = 5 & pears = 10" in
- let index = None in
- let apples = { Ast.pos = _position; name = "APPLES"; index }
- and pears = { Ast.pos = _position; name = "PEARS"; index }
- and value_5 = Ast.Integer (_position, "5")
- and value_10 = Ast.Integer (_position, "10") in
- _test_instruction content
- [
- Declaration (_position, apples, Eq', value_5);
- Declaration (_position, pears, Eq', value_10);
- ]
- let test_equality () =
- let content = "apples = 5 = pears" in
- let index = None in
- let apples = { Ast.pos = _position; name = "APPLES"; index }
- and pears = { Ast.pos = _position; name = "PEARS"; index }
- and value_5 = Ast.Integer (_position, "5") in
- _test_instruction content
- [
- Declaration
- (_position, apples, Eq', BinaryOp (_position, Eq, value_5, Ident pears));
- ]
- let test_plus () =
- let content = {|
- apples = 5 + pears
- |} in
- let index = None in
- let apples = { Ast.pos = _position; name = "APPLES"; index }
- and pears = { Ast.pos = _position; name = "PEARS"; index }
- and value_5 = Ast.Integer (_position, "5") in
- _test_instruction content
- [
- Declaration
- ( _position,
- apples,
- Eq',
- BinaryOp (_position, Plus, value_5, Ident pears) );
- ]
- let test_plus_litt () =
- let content = {|
- 'five'+ pears
- |} in
- let index = None in
- let pears = { Ast.pos = _position; name = "PEARS"; index } in
- _test_instruction content
- [
- Ast.(
- Expression
- (BinaryOp
- ( _position,
- Plus,
- Literal (_position, [ T.Text "five" ]),
- Ident pears )));
- ]
- let test_concat () =
- let content = {|
- $firstName + ' ' + $lastName
- |} in
- _test_instruction content
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- Plus,
- Tree.Ast.BinaryOp
- ( _position,
- Plus,
- Tree.Ast.Ident
- {
- Tree.Ast.pos = _position;
- name = "$FIRSTNAME";
- index = None;
- },
- Tree.Ast.Literal (_position, [ T.Text " " ]) ),
- Tree.Ast.Ident
- { Tree.Ast.pos = _position; name = "$LASTNAME"; index = None } ));
- ]
- let test_mod () =
- _test_instruction {|2 mod 1|}
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- T.Mod,
- Tree.Ast.Integer (_position, "2"),
- Tree.Ast.Integer (_position, "1") ));
- ]
- let test_comment () = _test_instruction "! Comment" [ Comment _position ]
- let test_comment2 () =
- let index = None in
- let a = { Ast.pos = _position; name = "A"; index }
- and value_0 = Ast.Integer (_position, "0") in
- _test_instruction "a = 0 &! Comment"
- Ast.[ Declaration (_position, a, Eq', value_0); Comment _position ]
- let test_comment3 () = _test_instruction {|!!1234
- |} [ Comment _position ]
- (** The exclamation mark here is an operation and not a comment *)
- let test_comment4 () =
- let index = None in
- let a = { Ast.pos = _position; name = "A"; index }
- and value_0 = Ast.Integer (_position, "0") in
- _test_instruction "a = rand(0, 1) ! 0"
- [
- Ast.(
- Declaration
- ( _position,
- a,
- Eq',
- BinaryOp
- ( _position,
- Neq,
- Function
- ( _position,
- Rand,
- [ Integer (_position, "0"); Integer (_position, "1") ] ),
- value_0 ) ));
- ]
- let test_comment5 () =
- _test_instruction "a = rand() &! Comment"
- [
- Ast.Declaration
- ( _position,
- { Ast.pos = _position; name = "A"; index = None },
- Qsp_syntax.T.Eq',
- Ast.Function (_position, Rand, []) );
- Ast.Comment _position;
- ]
- let test_comment6 () =
- _test_instruction
- "gs 'stat' &!! It should be here, because some of the strigs have to be \
- initialized"
- [
- Ast.Call
- ( _position,
- Qsp_syntax.T.Gosub,
- [ Ast.Literal (_position, [ T.Text "stat" ]) ] );
- Ast.Comment _position;
- ]
- let test_long_comment () =
- _test_instruction
- {| !'this part of the comment is inside
- single quotes' but "this is still part
- of the same comment because sometimes
- life is unfair." Oh yeah, {curly brackets
- also count}. This is still the same comment. |}
- [ Comment _position ]
- (** This test ensure that the unary operator is applied to the whole expression
- *)
- let test_precedence () =
- let index = None in
- let x = Ast.Ident { Ast.pos = _position; name = "X"; index }
- and y = Ast.Ident { Ast.pos = _position; name = "Y"; index } in
- _test_instruction "no x = y"
- Ast.[ Expression (Op (_position, No, BinaryOp (_position, Eq, x, y))) ]
- (** This test ensure that a ! is not considered as a comment in an
- expression *)
- let test_precedence2 () =
- let index = None in
- let x = { Ast.pos = _position; name = "X"; index }
- and y = Ast.Ident { Ast.pos = _position; name = "Y"; index } in
- _test_instruction "x = y ! 0"
- Ast.
- [
- Declaration
- ( _position,
- x,
- Eq',
- BinaryOp (_position, Neq, y, Integer (_position, "0")) );
- ]
- let test_if () =
- let index = Some Ast.(Integer (_position, "0")) in
- let args = Ast.(Ident { pos = _position; name = "$ARGS"; index })
- and expr1 = Ast.(Literal (_position, [ T.Text "blockA" ]))
- and expr2 =
- Ast.(Expression (Literal (_position, [ T.Text "You are in block A" ])))
- in
- _test_instruction {| if $ARGS[0] = 'blockA':
- 'You are in block A'
- end |}
- Ast.
- [
- If
- {
- loc = _position;
- then_ = (_position, BinaryOp (_position, Eq, args, expr1), [ expr2 ]);
- elifs = [];
- else_ = [];
- };
- ]
- let test_if_chained () =
- let value_0 = Ast.Integer (_position, "0") in
- _test_instruction {| if 0:
- 0
- end &! -- |}
- Ast.
- [
- If
- {
- loc = _position;
- then_ = (_position, value_0, [ Expression value_0 ]);
- elifs = [];
- else_ = [];
- };
- Comment _position;
- ]
- let test_if_equality () =
- _test_instruction {|
- if 0 = 0:
- end &! --
- |}
- [
- Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Ast.BinaryOp
- ( _position,
- Qsp_syntax.T.Eq,
- Ast.Integer (_position, "0"),
- Ast.Integer (_position, "0") ),
- [] );
- elifs = [];
- else_ = [];
- };
- Ast.Comment _position;
- ]
- let test_if_inline () =
- let value_0 = Ast.Integer (_position, "0") in
- _test_instruction "if 0: 0 else 0"
- Ast.
- [
- If
- {
- loc = _position;
- then_ = (_position, value_0, [ Expression value_0 ]);
- elifs = [];
- else_ = [ Expression value_0 ];
- };
- ]
- let test_if_inline_comment () =
- let value_0 = Ast.Integer (_position, "0") in
- _test_instruction "if 0: 0 else 0 &! comment "
- Ast.
- [
- If
- {
- loc = _position;
- then_ = (_position, value_0, [ Expression value_0 ]);
- elifs = [];
- else_ = [ Expression value_0 ];
- };
- Comment _position;
- ]
- let test_if_inline_comment2 () =
- _test_instruction "if 0: 1 & !! Comment"
- [
- Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Ast.Integer (_position, "0"),
- [
- Ast.Expression (Ast.Integer (_position, "1"));
- Ast.Comment _position;
- ] );
- elifs = [];
- else_ = [];
- };
- ]
- let test_if_inline_act () =
- _test_instruction "if 1 and hour >= 8: minut += 1 & act 'go': gt 'go'"
- [
- Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Ast.BinaryOp
- ( _position,
- Qsp_syntax.T.And,
- Ast.Integer (_position, "1"),
- Ast.BinaryOp
- ( _position,
- Qsp_syntax.T.Gte,
- Ast.Ident
- { Ast.pos = _position; name = "HOUR"; index = None },
- Ast.Integer (_position, "8") ) ),
- [
- Ast.Declaration
- ( _position,
- { Ast.pos = _position; name = "MINUT"; index = None },
- Qsp_syntax.T.Inc,
- Ast.Integer (_position, "1") );
- Ast.Act
- {
- loc = _position;
- label = Ast.Literal (_position, [ T.Text "go" ]);
- statements =
- [
- Ast.Call
- ( _position,
- Qsp_syntax.T.Goto,
- [ Ast.Literal (_position, [ T.Text "go" ]) ] );
- ];
- };
- ] );
- elifs = [];
- else_ = [];
- };
- ]
- let test_if_multiline () =
- _test_instruction {|if 1 _
- and _hour >= 8: 1|}
- [
- Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Ast.BinaryOp
- ( _position,
- Qsp_syntax.T.And,
- Ast.Integer (_position, "1"),
- Ast.BinaryOp
- ( _position,
- Qsp_syntax.T.Gte,
- Ast.Ident
- { Ast.pos = _position; name = "_HOUR"; index = None },
- Ast.Integer (_position, "8") ) ),
- [ Tree.Ast.Expression (Tree.Ast.Integer (_position, "1")) ] );
- elifs = [];
- else_ = [];
- };
- ]
- let test_if_inline_act2 () =
- _test_instruction "if 1: act 'go': gt 'go' &! comment "
- [
- Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Ast.Integer (_position, "1"),
- [
- Ast.Act
- {
- loc = _position;
- label = Ast.Literal (_position, [ T.Text "go" ]);
- statements =
- [
- Ast.Call
- ( _position,
- Qsp_syntax.T.Goto,
- [ Ast.Literal (_position, [ T.Text "go" ]) ] );
- Ast.Comment _position;
- ];
- };
- ] );
- elifs = [];
- else_ = [];
- };
- ]
- let test_precedence3 () =
- let index = Some Ast.(Integer (_position, "0")) in
- let args = Ast.(Ident { pos = _position; name = "$ARGS"; index })
- and expr1 = Ast.(Literal (_position, [ T.Text "blockA" ]))
- and expr2 =
- Ast.(Expression (Literal (_position, [ T.Text "You are in block A" ])))
- and expr3 = Ast.(Expression (Integer (_position, "0"))) in
- _test_instruction {| if $ARGS[0] = 'blockA': 'You are in block A' & 0|}
- Ast.
- [
- If
- {
- loc = _position;
- then_ =
- ( _position,
- BinaryOp (_position, Eq, args, expr1),
- [ expr2; expr3 ] );
- elifs = [];
- else_ = [];
- };
- ]
- let test_gs () =
- _test_instruction "gs '123'"
- [
- Ast.(
- Call
- ( _position,
- Qsp_syntax.T.Gosub,
- [ Literal (_position, [ T.Text "123" ]) ] ));
- ]
- let test_gt () =
- _test_instruction "gt $curloc"
- [
- Ast.Call
- ( _position,
- Qsp_syntax.T.Goto,
- [ Ast.Ident { Ast.pos = _position; name = "$CURLOC"; index = None } ]
- );
- ]
- let test_nl () =
- _test_instruction "*NL 'It'"
- [
- Ast.Call
- ( _position,
- Qsp_syntax.T.Nl',
- [ Ast.Literal (_position, [ T.Text "It" ]) ] );
- ]
- let test_function () =
- _test_instruction "iif(123, 1, 0)"
- [
- Ast.(
- Expression
- (Function
- ( _position,
- Iif,
- [
- Integer (_position, "123");
- Integer (_position, "1");
- Integer (_position, "0");
- ] )));
- ]
- (** Include a space before the parameters *)
- let test_function2 () =
- _test_instruction "rand (0, 1)"
- [
- Ast.(
- Expression
- (Function
- ( _position,
- Rand,
- [ Integer (_position, "0"); Integer (_position, "1") ] )));
- ]
- (** The RND function does not have parens after the name *)
- let test_rnd () =
- _test_instruction "rnd"
- [ Tree.Ast.Expression (Tree.Ast.Function (_position, T.Rnd, [])) ]
- let test_precedence4 () =
- _test_instruction "trim()" Ast.[ Expression (Function (_position, Trim, [])) ]
- (** This should not be a keyword without arguments, followed by an expression *)
- let test_precedence5 () =
- _test_instruction "clear()" Ast.[ Call (_position, Qsp_syntax.T.Clear, []) ]
- let test_precedence6 () =
- _test_instruction "(1 = 0 and 2 ! 3)"
- [
- Ast.Expression
- (Ast.BinaryOp
- ( _position,
- And,
- Ast.BinaryOp
- ( _position,
- Eq,
- Ast.Integer (_position, "1"),
- Ast.Integer (_position, "0") ),
- Ast.BinaryOp
- ( _position,
- Neq,
- Ast.Integer (_position, "2"),
- Ast.Integer (_position, "3") ) ));
- ]
- (** An identifier cannot start by a number *0 is a product and not an
- identifier *)
- let test_operator () =
- let index = None in
- let a = { Ast.pos = _position; name = "A"; index }
- and value_0 = Ast.Integer (_position, "0") in
- _test_instruction "a *0"
- Ast.[ Expression (BinaryOp (_position, Product, Ident a, value_0)) ]
- let test_operator2 () =
- let value_0 = Ast.Integer (_position, "0") in
- _test_instruction "0 *rand()"
- Ast.
- [
- Expression
- (BinaryOp (_position, Product, value_0, Function (_position, Rand, [])));
- ]
- let test_dyneval () =
- _test_instruction "dyneval ''"
- [
- Ast.Expression
- (Ast.Function
- (_position, Dyneval, [ Ast.Literal (_position, [ T.Text "" ]) ]));
- ]
- (** The parens after input are considered as arguments for the function, not a
- following expression.
- This expression is a boolean.
- *)
- let test_input () =
- _test_instruction "( input('') = '' )"
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- Eq,
- Tree.Ast.Function
- ( _position,
- Input,
- [ Tree.Ast.Literal (_position, [ T.Text "" ]) ] ),
- Tree.Ast.Literal (_position, [ T.Text "" ]) ));
- ]
- let test_mutiple_inline_ifs () =
- _test_instruction "if 1 > 0: 1 else if 1 < 0: 0"
- [
- Tree.Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Tree.Ast.BinaryOp
- ( _position,
- Gt,
- Tree.Ast.Integer (_position, "1"),
- Tree.Ast.Integer (_position, "0") ),
- [ Tree.Ast.Expression (Tree.Ast.Integer (_position, "1")) ] );
- elifs = [];
- else_ =
- [
- Tree.Ast.If
- {
- loc = _position;
- then_ =
- ( _position,
- Tree.Ast.BinaryOp
- ( _position,
- Lt,
- Tree.Ast.Integer (_position, "1"),
- Tree.Ast.Integer (_position, "0") ),
- [
- Tree.Ast.Expression (Tree.Ast.Integer (_position, "0"));
- ] );
- elifs = [];
- else_ = [];
- };
- ];
- };
- ]
- (** The boolean comparaison has greater precedence than arithmetic operator *)
- let test_precedence7 () =
- _test_instruction "(1 + 1 = '')"
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- Eq,
- Tree.Ast.BinaryOp
- ( _position,
- Plus,
- Tree.Ast.Integer (_position, "1"),
- Tree.Ast.Integer (_position, "1") ),
- Tree.Ast.Literal (_position, [ T.Text "" ]) ));
- ]
- (** The OR operator has greater precedence than boolean comparaison *)
- let test_precedence8 () =
- _test_instruction "(0 = 1 or 0 = 1)"
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- Or,
- Tree.Ast.BinaryOp
- ( _position,
- Eq,
- Tree.Ast.Integer (_position, "0"),
- Tree.Ast.Integer (_position, "1") ),
- Tree.Ast.BinaryOp
- ( _position,
- Eq,
- Tree.Ast.Integer (_position, "0"),
- Tree.Ast.Integer (_position, "1") ) ));
- ]
- (** Test showing the - should be considered as an operator and cannot be
- aggregated inside the integer value. *)
- let minus_operator () =
- _test_instruction {|day-7|}
- [
- Tree.Ast.Expression
- (Tree.Ast.BinaryOp
- ( _position,
- T.Minus,
- Tree.Ast.Ident
- { Tree.Ast.pos = _position; name = "DAY"; index = None },
- Tree.Ast.Integer (_position, "7") ));
- ]
- (** STATTXT was considered as a function and raised an error in the syntax *)
- let test_stattxt () =
- _test_instruction "$value = $stattxt"
- [
- Tree.Ast.Declaration
- ( _position,
- { Tree.Ast.pos = _position; name = "$VALUE"; index = None },
- T.Eq',
- Tree.Ast.Ident
- { Tree.Ast.pos = _position; name = "$STATTXT"; index = None } );
- ]
- let test_for_end () = _test_instruction {|for a = 1 to 10:
- end|} []
- let test =
- ( "Syntax",
- [
- Alcotest.test_case "Location" `Quick test_empty_location;
- Alcotest.test_case "Location" `Quick test_location_without_space;
- Alcotest.test_case "Location" `Quick test_location_without_database;
- Alcotest.test_case " Numeric expression" `Quick test_numeric_expression;
- Alcotest.test_case "-Numeric expression" `Quick
- test_negative_numeric_expression;
- Alcotest.test_case "-Numeric expression2" `Quick
- test_negative_numeric_expression2;
- Alcotest.test_case "Minus op" `Quick minus_operator;
- Alcotest.test_case "$Variable expression" `Quick test_str_variable;
- Alcotest.test_case " Variable expression" `Quick test_variable;
- Alcotest.test_case "Indexed Variable expression" `Quick
- test_indexed_variable;
- Alcotest.test_case "Let instruction" `Quick test_let_literal;
- Alcotest.test_case "Set array_append" `Quick test_set_array_append;
- Alcotest.test_case "Variable_assignation" `Quick test_direct_assignation;
- Alcotest.test_case "Command assignation" `Quick test_command_assignation;
- Alcotest.test_case "Variable_assignation2" `Quick test_assignation2;
- Alcotest.test_case "Literal" `Quick test_literal;
- Alcotest.test_case "Literal2" `Quick test_qutoted_literal;
- Alcotest.test_case "Literal3" `Quick test_multilie_literal;
- Alcotest.test_case "Concat Literal" `Quick test_concat_literal;
- Alcotest.test_case "Nested Literal" `Quick test_nested_literal;
- Alcotest.test_case "Multiline1" `Quick test_multiline1;
- Alcotest.test_case "Multiline2" `Quick test_multiline2;
- Alcotest.test_case "Equality" `Quick test_equality;
- Alcotest.test_case "Plus" `Quick test_plus;
- Alcotest.test_case "Plus_litt" `Quick test_plus_litt;
- Alcotest.test_case "PlusChained" `Quick test_concat;
- Alcotest.test_case "Mod operator" `Quick test_mod;
- Alcotest.test_case "Comment" `Quick test_comment;
- Alcotest.test_case "Comment2" `Quick test_comment2;
- Alcotest.test_case "Comment3" `Quick test_comment3;
- Alcotest.test_case "Comment4" `Quick test_comment4;
- Alcotest.test_case "Comment5" `Quick test_comment5;
- Alcotest.test_case "Comment6" `Quick test_comment6;
- Alcotest.test_case "Multiline Comment" `Quick test_long_comment;
- Alcotest.test_case "If" `Quick test_if;
- Alcotest.test_case "If_chained" `Quick test_if_chained;
- Alcotest.test_case "If_equality" `Quick test_if_equality;
- Alcotest.test_case "If inline" `Quick test_if_inline;
- Alcotest.test_case "If inline &!" `Quick test_if_inline_comment;
- Alcotest.test_case "If inline & !!" `Quick test_if_inline_comment2;
- Alcotest.test_case "If : act" `Quick test_if_inline_act;
- Alcotest.test_case "If _ and " `Quick test_if_multiline;
- Alcotest.test_case "If : act: &!" `Quick test_if_inline_act2;
- Alcotest.test_case "Precedence1" `Quick test_precedence;
- Alcotest.test_case "Precedence2" `Quick test_precedence2;
- Alcotest.test_case "Precedence3" `Quick test_precedence3;
- Alcotest.test_case "Call gs" `Quick test_gs;
- Alcotest.test_case "Call gt" `Quick test_gt;
- Alcotest.test_case "Call nl" `Quick test_nl;
- Alcotest.test_case "Function iif" `Quick test_function;
- Alcotest.test_case "Function rand" `Quick test_function2;
- Alcotest.test_case "Function rnd" `Quick test_rnd;
- Alcotest.test_case "Precedence4" `Quick test_precedence4;
- Alcotest.test_case "Precedence5" `Quick test_precedence5;
- Alcotest.test_case "Precedence6" `Quick test_precedence6;
- Alcotest.test_case "Operator" `Quick test_operator;
- Alcotest.test_case "Operator2" `Quick test_operator2;
- Alcotest.test_case "Dyneval" `Quick test_dyneval;
- Alcotest.test_case "Input" `Quick test_input;
- Alcotest.test_case "inline if else if" `Quick test_mutiple_inline_ifs;
- Alcotest.test_case "Precedence7" `Quick test_precedence7;
- Alcotest.test_case "Precedence8" `Quick test_precedence8;
- Alcotest.test_case "stattxt" `Quick test_stattxt;
- Alcotest.test_case "for ... end" `Quick test_for_end;
- ] )
|