diff --git a/corpus/edigits_like.lang b/corpus/edigits_like.lang index 874dc4c..782fa02 100644 --- a/corpus/edigits_like.lang +++ b/corpus/edigits_like.lang @@ -6,7 +6,7 @@ EdigitsDraft :: std : BigInt; :: math; -exec.main = { +exec.main { %n := (?? os.args.size > 1 => (<> os.args[1]).scan !!=> 27); %k := binary_search 0; %p & %q := sum_terms 0 (k - 1); @@ -25,7 +25,7 @@ exec.main = { }; } -sum_terms 'a 'b : Int Int -> std.BigInt -> std.BigInt = { +sum_terms 'a 'b : Int Int -> std.BigInt -> std.BigInt { ?? 'b ==. 'a + 1 => return std.BigInt.of 1 & std.BigInt.of 'b; %mid := 'a + 'b /. 2; @@ -35,7 +35,7 @@ sum_terms 'a 'b : Int Int -> std.BigInt -> std.BigInt = { return p_left * q_right +. p_right &.. q_left * q_right; } -binary_search 'n : Int -> Int = { +binary_search 'n : Int -> Int { %a := 0; %b := 1; @ not (test_k 'n b) => { @@ -50,7 +50,7 @@ binary_search 'n : Int -> Int = { } -test_k 'n 'k : Int Int -> Bool = { +test_k 'n 'k : Int Int -> Bool { ?? k < 0 => return false; %ln_k_factorial := k * (math.log k - 1) +. 0.5 * math.log math.tau; diff --git a/corpus/test.lang b/corpus/test.lang index 87e65f3..484067f 100644 --- a/corpus/test.lang +++ b/corpus/test.lang @@ -11,7 +11,7 @@ Test :: module_3 : func1 func2 func3; :: module_namespace = module_4; -func = { +func { @ => { %x := scan; @@ -27,7 +27,7 @@ func = { }; ?? abracadabbra < abracadabra_abracadabra || some_long_name == another_long_name - &&. abracadabra-abracadabra < long_long_long_long_name => io.print x + &&. abracadabra_abracadabra < long_long_long_long_name => io.print x !! x < 0 => { x += 1; io.print y; @@ -55,7 +55,7 @@ fib 'n : @n Int -> Int = 'n =: 0 | 1 => 1 =: _ => fib ('n - 1) + fib 'n; -func_2 = { +func_2 { %variant := x; %val | %err := f x; @@ -112,15 +112,15 @@ func_2 = { : operator definition example ( - ) 'a 'b = 'a + neg 'b; -test.something = { +test.something { do_something a b c; } -exec.something = { +exec.something { do_something a b c; } -example.something = { +example.something { do_something a b c; } @@ -132,7 +132,7 @@ Fruit = @apple Unit | @banana Unit; : function that takes array reference argument -bubble_sort 'arr : <> Array['A] = { +bubble_sort 'arr : <> Array['A] { swap_occured := true; @ swap_occured => { swap_occured = false; @@ -141,7 +141,7 @@ bubble_sort 'arr : <> Array['A] = { } : bubble_sort with names instead of symbols -bubble_sort_2 'arr : ref Array['A] = { +bubble_sort_2 'arr : ref Array['A] { var swap_occured := true; for swap_occured do { swap_occured = false; @@ -166,17 +166,17 @@ bubble_sort_2 'arr : ref Array['A] = { .delete <> 'this 'key = do_something; // var methods -generic_type_name_expressions = { +generic_type_name_expressions { $x := TreeNode[Int Int].new; $y := std.Array[Int].new; } -pipes_example = { +pipes_example { expr |> func_1 a b |> func_2 c d |> print; // print (func_2 (func_1 expr a b) c d) print <| func_1 a b <| func_2 c d <| expr; // print (func_1 a b (func_2 c d expr)) } -test_ref_access_precendence = { +test_ref_access_precendence { %x := <> arr[123]; } @@ -212,7 +212,7 @@ result_example 'a! = 'a =: _? => print "value inside" =: _ => print "error inside"; : function, that returns result -parse_number : Unit! = { +parse_number : Unit! { %number_str := String.scan; %number! := Int.parse number_str; number.print; @@ -232,7 +232,7 @@ tuple_argument_test 'x : (A & B & C) = do_something; // ((A1 & A2) | B | C) same to Variant[Tuple[A1 A2] B C] variant_argument_test 'x : ( (A1 & A2) | B | C) = do_something; -literals_test = { +literals_test { %float_number_literal := 1.0f; %double_number_literal := 1.0; %int_literal := 1i; @@ -247,9 +247,16 @@ literals_test = { %null_literal := null; } -array_argument_test 'x : [[ Int ]] = do_something; +array_argument_test 'x : A[Int] = do_something; -functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float )) = do_something; +functional_arguments_test 'x 'y : F[Int -> Int] F[Float <- Float -> Float] = do_something; + +type_name_expression_test 'x : Int = A[Int].a.b.c x y z; +array_name_expression_test 'x : Int = [[(A a) b]].a.b.c x y z; +name_expression_test 'x : Int = abacaba.a.b.c x y z; +field_name_expression_test 'x : Int = (abacaba.a).b.c x y z; + +array_function_test 'x : Int [[ 'x (do_something 'x) (T 'x)]] -------------------------------------------------------------------------------- @@ -344,12 +351,8 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float )) (simple_name_identifier))) (operator_tail1) (operator_expression - (operator_expression - (name_expression - (simple_name_identifier)) - (operator) - (name_expression - (simple_name_identifier))) + (name_expression + (simple_name_identifier)) (operator) (name_expression (simple_name_identifier)))) @@ -1478,7 +1481,8 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float )) (function_definition (simple_name_identifier) (argument_name_identifier) - (array_type + (simple_type + (simple_type_identifier) (simple_type (simple_type_identifier))) (name_expression @@ -1488,13 +1492,15 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float )) (simple_name_identifier) (argument_name_identifier) (argument_name_identifier) - (function_type + (simple_type + (simple_type_identifier) (simple_type (simple_type_identifier)) (reference_type (simple_type (simple_type_identifier)))) - (function_type + (simple_type + (simple_type_identifier) (simple_type (simple_type_identifier)) (reference_type @@ -1505,4 +1511,75 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float )) (simple_type_identifier)))) (name_expression (simple_name_identifier))) + (empty_lines) + (function_definition + (simple_name_identifier) + (argument_name_identifier) + (simple_type + (simple_type_identifier)) + (name_expression + (simple_type + (simple_type_identifier) + (simple_type + (simple_type_identifier))) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier))) + (empty_lines) + (function_definition + (simple_name_identifier) + (argument_name_identifier) + (simple_type + (simple_type_identifier)) + (name_expression + (array + (constructor + (simple_type + (simple_type_identifier)) + (simple_name_identifier)) + (simple_name_identifier)) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier))) + (empty_lines) + (function_definition + (simple_name_identifier) + (argument_name_identifier) + (simple_type + (simple_type_identifier)) + (name_expression + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier))) + (empty_lines) + (function_definition + (simple_name_identifier) + (argument_name_identifier) + (simple_type + (simple_type_identifier)) + (name_expression + (name_expression + (simple_name_identifier)) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier) + (simple_name_identifier))) + (empty_lines) + (function_definition + (simple_name_identifier) + (argument_name_identifier) + (simple_type + (simple_type_identifier)) + (array + (argument_name_identifier) + (name_expression + (simple_name_identifier) + (argument_name_identifier)) + (constructor + (simple_type + (simple_type_identifier)) + (argument_name_identifier)))) (empty_lines)) diff --git a/grammar.g4 b/grammar.g4 index edc33ec..1b56c67 100644 --- a/grammar.g4 +++ b/grammar.g4 @@ -22,7 +22,7 @@ function_definition: (definietion_info)? (ANNOTATION_INFO)* (constraint ';')* ('.')? (SIMPLE_NAME_IDENTIFIER | ('(' OPERATOR ')')) ('?' | '!') (((ANNOTATION_IDENTIFIER)? (REFERENCE)? ARGUMENT_NAME_IDENTIFIER ('?' | '!')?)*) (':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? scoped_type)+)? - (('=' ( /*prec 2*/ block | (super_expression ';'))) | ';') + ((/*prec 2*/ array | block) | ('=' super_expression ';')) | ';') ; type_definition: (DEFINITION_INFO)? (ANNOTATION_INFO)* (REFERENCE)? @@ -125,12 +125,8 @@ variant_type: ('|')? annotated_type ('|' annotated_type)+ ; tuple_type: ('&')? annotated_type ('&' _annotated_type)+ ; -function_type: '((' (_annotated_type)+ '))'; - annotated_type: (ANNOTATION_IDENTIFIER)? scoped_type ; -array_type: '[[' type ']]' ; - reference_type: /* prec -1 */ REFERENCE scoped_type ; modified_type: scoped_type ('?' | '!') ; @@ -140,18 +136,14 @@ simple_type: type_identifier ('[' (scoped_type)+ ']')? ; type: simple_type | reference_type | modified_type - | array_type | variant_type | tuple_type - | function_type ; scoped_type: simple_type | reference_type | modified_type - | array_type | '(' (variant_type | tuple_type) ')' - | function_type ; name_identifier: (ARGUMENT_NAME_IDENTIFIER | SIMPLE_NAME_IDENTIFIER) ; diff --git a/grammar.js b/grammar.js index f661588..94a386b 100644 --- a/grammar.js +++ b/grammar.js @@ -55,7 +55,7 @@ module.exports = grammar({ ':', repeat1($._annotated_type), )), - choice(seq('=', choice(prec(2, choice($.block, $.array)), seq($._super_expression, ';'))), ';'), + choice(choice(prec(2, choice($.block, $.array)), seq('=', $._super_expression, ';')), ';'), ), // datatype or typeclass definition @@ -222,13 +222,12 @@ module.exports = grammar({ repeat1(seq('&', $._annotated_type)), ), - function_type: $ => seq('((', repeat1($._annotated_type), '))'), + // for function type Function[...] or F[...] is used // add annotations to in all types ?? _annotated_type: $ => seq(optional($.annotation_identifier), $._scoped_type), - // same to Array[...] - array_type: $ => seq('[[', $._type, ']]'), + // for array type Array[...] or A[...] is used reference_type: $ => prec(-1, seq($._reference, $._scoped_type)), @@ -244,23 +243,18 @@ module.exports = grammar({ $.simple_type, $.reference_type, $.modified_type, - $.array_type, $.variant_type, $.tuple_type, - $.function_type, ), _scoped_type: $ => choice( $.simple_type, $.reference_type, $.modified_type, - $.array_type, seq('(', choice( $.variant_type, $.tuple_type, - //$.function_type, ), ')'), - $.function_type, ), // --- comments