From 1a237adf0e8c8547bbe456c0ab8f860a9f0ad168 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sat, 29 Jul 2023 12:06:22 +0300 Subject: [PATCH] or_out or_in references added --- corpus/test.langexp | 45 +++++++++++++++++++++++++++++++-------------- grammar.g4 | 18 +++++++++++++----- grammar.js | 2 +- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/corpus/test.langexp b/corpus/test.langexp index 5e082ac..8ccfd9b 100644 --- a/corpus/test.langexp +++ b/corpus/test.langexp @@ -6,10 +6,10 @@ Test :: module; // import module to current namespace -:: _ = module; // import module to current namespace and use functions inside without namespace +:: _ = module_2; // import module to current namespace and use functions inside without namespace -:: module : func1 func2 func3; -:: module_namespace = module; +:: module_3 : func1 func2 func3; +:: module_namespace = module_4; func = { @@ -44,7 +44,7 @@ sum 'a? 'b? = : example that shows that default annotations are argument names (without ') @a is integer @b also integer -sum 'a 'b = 'a + 'b; +sum_2 'a 'b = 'a + 'b; : this function can be used to calculate Fibonacci sequence elements : it is important is some algorithmic tasks @@ -141,7 +141,7 @@ bubble_sort 'arr : <> Array['A] = { } : bubble_sort with names instead of symbols -bubble_sort 'arr : ref Array['A] = { +bubble_sort_2 'arr : ref Array['A] = { var swap_occured := true; for swap_occured do { swap_occured = false; @@ -155,9 +155,7 @@ bubble_sort 'arr : ref Array['A] = { & @key Key & @value Value & @left ^TreeNode['Key 'Value] - & @right ^TreeNode['Key 'Value]; - -TreeNode { + & @right ^TreeNode['Key 'Value] { new = do_something; // static methods $insert 'key = do_something; // const methods @@ -190,7 +188,7 @@ print_two 'a 'b = print 'a, print 'b; swap 'a 'b : <> 'A <> 'A = %c := <- 'a, 'a := <- 'b, 'b := <- c; : previous example with automatic type deduction -swap <> 'a <> 'b = %c := <- 'a, 'a := <- 'b, 'b := <- c; +swap_2 <> 'a <> 'b = %c := <- 'a, 'a := <- 'b, 'b := <- c; : several outputs example scan_three : -> String -> String -> String = scan & scan & scan; @@ -221,6 +219,7 @@ parse_number : Unit! = { bring (); } +( & ) <-| 'a <-| 'b |-> 'x |-> 'y = (<-| 'a := |-> 'x) && (<-| 'b := |-> 'y); -------------------------------------------------------------------------------- @@ -877,10 +876,7 @@ parse_number : Unit! = { (type (argument_type_identifier)) (type - (argument_type_identifier)))))) - (empty_lines) - (type_definition - (simple_type_identifier) + (argument_type_identifier))))) (function_definition (simple_name_identifier) (name_expression @@ -1185,4 +1181,25 @@ parse_number : Unit! = { (name_expression (simple_name_identifier)) (return - (unit_literal))))) + (unit_literal)))) + (empty_lines) + (function_definition + (operator) + (argument_name_identifier) + (argument_name_identifier) + (argument_name_identifier) + (argument_name_identifier) + (operator_expression + (match + (reference_expression + (argument_name_identifier)) + (case + (reference_expression + (argument_name_identifier)))) + (operator) + (match + (reference_expression + (argument_name_identifier)) + (case + (reference_expression + (argument_name_identifier))))))) diff --git a/grammar.g4 b/grammar.g4 index 8ee8719..b556f9b 100644 --- a/grammar.g4 +++ b/grammar.g4 @@ -1,6 +1,6 @@ grammar lang ; -// not always most recent grammar ?? +// not always most recent grammar // not checked source_file: (statement)+ EOF ; @@ -25,8 +25,8 @@ type_definition: (DEFINITION_INFO)? (ANNOTATION_INFO)* ('^')? function_definition: (definietion_info)? (ANNOTATION_INFO)* (constraint ';')* (('%' | 'let') | ('$' | 'var'))? (SIMPLE_NAME_IDENTIFIER | ('(' OPERATOR ')')) - ((ANNOTATION_IDENTIFIER)? (('->' | 'in') | ('<-' | 'out') | ('<>' | 'ref')) ARGUMENT_NAME_IDENTIFIER ('?')?)* - (':' ((ANNOTATION_IDENTIFIER)? (('->' | 'in') | ('<-' | 'out') | ('<>' | 'ref')))? type)+)? + ((ANNOTATION_IDENTIFIER)? (REFERENCE)? ARGUMENT_NAME_IDENTIFIER ('?')?)* + (':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? type)+)? (('=' ( /*prec 2*/ block | (super_expression ';'))) | ';') ; @@ -67,7 +67,7 @@ array_access: scoped_expression '[' super_expression ']' ; tuple_access: scoped_expression, '.' NUMBER_LITERAL -reference_expression: /* prec -1 ?? */ (('->' | 'in') | ('<-' | 'out') | ('<>' | 'ref')) scoped_expression ; +reference_expression: /* prec -1 ?? */ REFERENCE scoped_expression ; suffix_expression: scoped_expression ('?' | '!') @@ -141,16 +141,24 @@ literal: FLOAT_NUMBER_LITERAL | BOOL_LITERAL | UNIT_LITERAL | NULL_LITERAL + ; DEFINITION_INFO : : ': ' ([^\n]*)+ ; ANNOTATION_INFO : ANNOTATION_IDENTIFIER [^\n]* ; -EMPTY_LINES '\n' ('\n')+ +EMPTY_LINES '\n' ('\n')+ ; LINE_COMMENT : '#!' [^\n]* -> skip ; LINE_COMMENT : '//' [^\n]* -> skip ; BLOCK_COMMENT : '\*' ([^*] |('\*' [^/]))* '*/' -> skip ; +REFERENCE: ('->' | 'out') + | ('<-' | 'in') + | ('<>' | 'ref') + | ('|->' | 'or_out') + | ('<-|' | 'or_in') + ; + PLACEHOLDER : '_' ; SIMPLE_NAME_IDENTIFIER : ([a-z_][a-z0-9_]* '.')* [a-z_][a-z0-9_]* ; diff --git a/grammar.js b/grammar.js index 7527d8a..b544dbd 100644 --- a/grammar.js +++ b/grammar.js @@ -255,7 +255,7 @@ module.exports = grammar({ _do: $ => choice('=>', 'do'), _var_let: $ => choice(choice('%', 'let'), choice('$', 'var')), _optional_result: $ => choice('?', '!'), - _reference: $ => choice(choice('->', 'out'), choice('<-', 'in'), choice('<>', 'ref')), + _reference: $ => choice(choice('->', 'out'), choice('<-', 'in'), choice('<>', 'ref'), choice('|->', 'or_out'), choice('<-|', 'or_in')), _name_identifier: $ => choice($.argument_name_identifier, $.simple_name_identifier), _type_identifier: $ => choice($.argument_type_identifier, $.simple_type_identifier),