mirror of
https://codeberg.org/ProgramSnail/tree-sitter-lang.git
synced 2025-12-06 14:48:48 +00:00
block and array without = in function definition, Array and Function type shortcuts removed
This commit is contained in:
parent
154fbfb4d4
commit
cbd923d0f1
4 changed files with 109 additions and 46 deletions
|
|
@ -6,7 +6,7 @@ EdigitsDraft
|
||||||
:: std : BigInt;
|
:: std : BigInt;
|
||||||
:: math;
|
:: math;
|
||||||
|
|
||||||
exec.main = {
|
exec.main {
|
||||||
%n := (?? os.args.size > 1 => (<> os.args[1]).scan !!=> 27);
|
%n := (?? os.args.size > 1 => (<> os.args[1]).scan !!=> 27);
|
||||||
%k := binary_search 0;
|
%k := binary_search 0;
|
||||||
%p & %q := sum_terms 0 (k - 1);
|
%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;
|
?? 'b ==. 'a + 1 => return std.BigInt.of 1 & std.BigInt.of 'b;
|
||||||
|
|
||||||
%mid := 'a + 'b /. 2;
|
%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;
|
return p_left * q_right +. p_right &.. q_left * q_right;
|
||||||
}
|
}
|
||||||
|
|
||||||
binary_search 'n : Int -> Int = {
|
binary_search 'n : Int -> Int {
|
||||||
%a := 0;
|
%a := 0;
|
||||||
%b := 1;
|
%b := 1;
|
||||||
@ not (test_k 'n b) => {
|
@ 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;
|
?? k < 0 => return false;
|
||||||
|
|
||||||
%ln_k_factorial := k * (math.log k - 1) +. 0.5 * math.log math.tau;
|
%ln_k_factorial := k * (math.log k - 1) +. 0.5 * math.log math.tau;
|
||||||
|
|
|
||||||
121
corpus/test.lang
121
corpus/test.lang
|
|
@ -11,7 +11,7 @@ Test
|
||||||
:: module_3 : func1 func2 func3;
|
:: module_3 : func1 func2 func3;
|
||||||
:: module_namespace = module_4;
|
:: module_namespace = module_4;
|
||||||
|
|
||||||
func = {
|
func {
|
||||||
|
|
||||||
@ => {
|
@ => {
|
||||||
%x := scan;
|
%x := scan;
|
||||||
|
|
@ -27,7 +27,7 @@ func = {
|
||||||
};
|
};
|
||||||
|
|
||||||
?? abracadabbra < abracadabra_abracadabra || some_long_name == another_long_name
|
?? 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 < 0 => {
|
||||||
x += 1;
|
x += 1;
|
||||||
io.print y;
|
io.print y;
|
||||||
|
|
@ -55,7 +55,7 @@ fib 'n : @n Int -> Int =
|
||||||
'n =: 0 | 1 => 1
|
'n =: 0 | 1 => 1
|
||||||
=: _ => fib ('n - 1) + fib 'n;
|
=: _ => fib ('n - 1) + fib 'n;
|
||||||
|
|
||||||
func_2 = {
|
func_2 {
|
||||||
%variant := x;
|
%variant := x;
|
||||||
|
|
||||||
%val | %err := f x;
|
%val | %err := f x;
|
||||||
|
|
@ -112,15 +112,15 @@ func_2 = {
|
||||||
: operator definition example
|
: operator definition example
|
||||||
( - ) 'a 'b = 'a + neg 'b;
|
( - ) 'a 'b = 'a + neg 'b;
|
||||||
|
|
||||||
test.something = {
|
test.something {
|
||||||
do_something a b c;
|
do_something a b c;
|
||||||
}
|
}
|
||||||
|
|
||||||
exec.something = {
|
exec.something {
|
||||||
do_something a b c;
|
do_something a b c;
|
||||||
}
|
}
|
||||||
|
|
||||||
example.something = {
|
example.something {
|
||||||
do_something a b c;
|
do_something a b c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,7 +132,7 @@ Fruit = @apple Unit
|
||||||
| @banana Unit;
|
| @banana Unit;
|
||||||
|
|
||||||
: function that takes array reference argument
|
: function that takes array reference argument
|
||||||
bubble_sort 'arr : <> Array['A] = {
|
bubble_sort 'arr : <> Array['A] {
|
||||||
swap_occured := true;
|
swap_occured := true;
|
||||||
@ swap_occured => {
|
@ swap_occured => {
|
||||||
swap_occured = false;
|
swap_occured = false;
|
||||||
|
|
@ -141,7 +141,7 @@ bubble_sort 'arr : <> Array['A] = {
|
||||||
}
|
}
|
||||||
|
|
||||||
: bubble_sort with names instead of symbols
|
: 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;
|
var swap_occured := true;
|
||||||
for swap_occured do {
|
for swap_occured do {
|
||||||
swap_occured = false;
|
swap_occured = false;
|
||||||
|
|
@ -166,17 +166,17 @@ bubble_sort_2 'arr : ref Array['A] = {
|
||||||
.delete <> 'this 'key = do_something; // var methods
|
.delete <> 'this 'key = do_something; // var methods
|
||||||
|
|
||||||
|
|
||||||
generic_type_name_expressions = {
|
generic_type_name_expressions {
|
||||||
$x := TreeNode[Int Int].new;
|
$x := TreeNode[Int Int].new;
|
||||||
$y := std.Array[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)
|
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))
|
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];
|
%x := <> arr[123];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,7 +212,7 @@ result_example 'a! = 'a =: _? => print "value inside"
|
||||||
=: _ => print "error inside";
|
=: _ => print "error inside";
|
||||||
|
|
||||||
: function, that returns result
|
: function, that returns result
|
||||||
parse_number : Unit! = {
|
parse_number : Unit! {
|
||||||
%number_str := String.scan;
|
%number_str := String.scan;
|
||||||
%number! := Int.parse number_str;
|
%number! := Int.parse number_str;
|
||||||
number.print;
|
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]
|
// ((A1 & A2) | B | C) same to Variant[Tuple[A1 A2] B C]
|
||||||
variant_argument_test 'x : ( (A1 & A2) | B | C) = do_something;
|
variant_argument_test 'x : ( (A1 & A2) | B | C) = do_something;
|
||||||
|
|
||||||
literals_test = {
|
literals_test {
|
||||||
%float_number_literal := 1.0f;
|
%float_number_literal := 1.0f;
|
||||||
%double_number_literal := 1.0;
|
%double_number_literal := 1.0;
|
||||||
%int_literal := 1i;
|
%int_literal := 1i;
|
||||||
|
|
@ -247,9 +247,16 @@ literals_test = {
|
||||||
%null_literal := null;
|
%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)]]
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -343,14 +350,10 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float ))
|
||||||
(name_expression
|
(name_expression
|
||||||
(simple_name_identifier)))
|
(simple_name_identifier)))
|
||||||
(operator_tail1)
|
(operator_tail1)
|
||||||
(operator_expression
|
|
||||||
(operator_expression
|
(operator_expression
|
||||||
(name_expression
|
(name_expression
|
||||||
(simple_name_identifier))
|
(simple_name_identifier))
|
||||||
(operator)
|
(operator)
|
||||||
(name_expression
|
|
||||||
(simple_name_identifier)))
|
|
||||||
(operator)
|
|
||||||
(name_expression
|
(name_expression
|
||||||
(simple_name_identifier))))
|
(simple_name_identifier))))
|
||||||
(name_expression
|
(name_expression
|
||||||
|
|
@ -1478,7 +1481,8 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float ))
|
||||||
(function_definition
|
(function_definition
|
||||||
(simple_name_identifier)
|
(simple_name_identifier)
|
||||||
(argument_name_identifier)
|
(argument_name_identifier)
|
||||||
(array_type
|
(simple_type
|
||||||
|
(simple_type_identifier)
|
||||||
(simple_type
|
(simple_type
|
||||||
(simple_type_identifier)))
|
(simple_type_identifier)))
|
||||||
(name_expression
|
(name_expression
|
||||||
|
|
@ -1488,13 +1492,15 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float ))
|
||||||
(simple_name_identifier)
|
(simple_name_identifier)
|
||||||
(argument_name_identifier)
|
(argument_name_identifier)
|
||||||
(argument_name_identifier)
|
(argument_name_identifier)
|
||||||
(function_type
|
(simple_type
|
||||||
|
(simple_type_identifier)
|
||||||
(simple_type
|
(simple_type
|
||||||
(simple_type_identifier))
|
(simple_type_identifier))
|
||||||
(reference_type
|
(reference_type
|
||||||
(simple_type
|
(simple_type
|
||||||
(simple_type_identifier))))
|
(simple_type_identifier))))
|
||||||
(function_type
|
(simple_type
|
||||||
|
(simple_type_identifier)
|
||||||
(simple_type
|
(simple_type
|
||||||
(simple_type_identifier))
|
(simple_type_identifier))
|
||||||
(reference_type
|
(reference_type
|
||||||
|
|
@ -1505,4 +1511,75 @@ functional_arguments_test 'x 'y : (( Int -> Int )) (( Float <- Float -> Float ))
|
||||||
(simple_type_identifier))))
|
(simple_type_identifier))))
|
||||||
(name_expression
|
(name_expression
|
||||||
(simple_name_identifier)))
|
(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))
|
(empty_lines))
|
||||||
|
|
|
||||||
10
grammar.g4
10
grammar.g4
|
|
@ -22,7 +22,7 @@ function_definition: (definietion_info)? (ANNOTATION_INFO)* (constraint ';')*
|
||||||
('.')? (SIMPLE_NAME_IDENTIFIER | ('(' OPERATOR ')')) ('?' | '!')
|
('.')? (SIMPLE_NAME_IDENTIFIER | ('(' OPERATOR ')')) ('?' | '!')
|
||||||
(((ANNOTATION_IDENTIFIER)? (REFERENCE)? ARGUMENT_NAME_IDENTIFIER ('?' | '!')?)*)
|
(((ANNOTATION_IDENTIFIER)? (REFERENCE)? ARGUMENT_NAME_IDENTIFIER ('?' | '!')?)*)
|
||||||
(':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? scoped_type)+)?
|
(':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? scoped_type)+)?
|
||||||
(('=' ( /*prec 2*/ block | (super_expression ';'))) | ';')
|
((/*prec 2*/ array | block) | ('=' super_expression ';')) | ';')
|
||||||
;
|
;
|
||||||
|
|
||||||
type_definition: (DEFINITION_INFO)? (ANNOTATION_INFO)* (REFERENCE)?
|
type_definition: (DEFINITION_INFO)? (ANNOTATION_INFO)* (REFERENCE)?
|
||||||
|
|
@ -125,12 +125,8 @@ variant_type: ('|')? annotated_type ('|' annotated_type)+ ;
|
||||||
|
|
||||||
tuple_type: ('&')? annotated_type ('&' _annotated_type)+ ;
|
tuple_type: ('&')? annotated_type ('&' _annotated_type)+ ;
|
||||||
|
|
||||||
function_type: '((' (_annotated_type)+ '))';
|
|
||||||
|
|
||||||
annotated_type: (ANNOTATION_IDENTIFIER)? scoped_type ;
|
annotated_type: (ANNOTATION_IDENTIFIER)? scoped_type ;
|
||||||
|
|
||||||
array_type: '[[' type ']]' ;
|
|
||||||
|
|
||||||
reference_type: /* prec -1 */ REFERENCE scoped_type ;
|
reference_type: /* prec -1 */ REFERENCE scoped_type ;
|
||||||
|
|
||||||
modified_type: scoped_type ('?' | '!') ;
|
modified_type: scoped_type ('?' | '!') ;
|
||||||
|
|
@ -140,18 +136,14 @@ simple_type: type_identifier ('[' (scoped_type)+ ']')? ;
|
||||||
type: simple_type
|
type: simple_type
|
||||||
| reference_type
|
| reference_type
|
||||||
| modified_type
|
| modified_type
|
||||||
| array_type
|
|
||||||
| variant_type
|
| variant_type
|
||||||
| tuple_type
|
| tuple_type
|
||||||
| function_type
|
|
||||||
;
|
;
|
||||||
|
|
||||||
scoped_type: simple_type
|
scoped_type: simple_type
|
||||||
| reference_type
|
| reference_type
|
||||||
| modified_type
|
| modified_type
|
||||||
| array_type
|
|
||||||
| '(' (variant_type | tuple_type) ')'
|
| '(' (variant_type | tuple_type) ')'
|
||||||
| function_type
|
|
||||||
;
|
;
|
||||||
|
|
||||||
name_identifier: (ARGUMENT_NAME_IDENTIFIER | SIMPLE_NAME_IDENTIFIER) ;
|
name_identifier: (ARGUMENT_NAME_IDENTIFIER | SIMPLE_NAME_IDENTIFIER) ;
|
||||||
|
|
|
||||||
12
grammar.js
12
grammar.js
|
|
@ -55,7 +55,7 @@ module.exports = grammar({
|
||||||
':',
|
':',
|
||||||
repeat1($._annotated_type),
|
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
|
// datatype or typeclass definition
|
||||||
|
|
@ -222,13 +222,12 @@ module.exports = grammar({
|
||||||
repeat1(seq('&', $._annotated_type)),
|
repeat1(seq('&', $._annotated_type)),
|
||||||
),
|
),
|
||||||
|
|
||||||
function_type: $ => seq('((', repeat1($._annotated_type), '))'),
|
// for function type Function[...] or F[...] is used
|
||||||
|
|
||||||
// add annotations to in all types ??
|
// add annotations to in all types ??
|
||||||
_annotated_type: $ => seq(optional($.annotation_identifier), $._scoped_type),
|
_annotated_type: $ => seq(optional($.annotation_identifier), $._scoped_type),
|
||||||
|
|
||||||
// same to Array[...]
|
// for array type Array[...] or A[...] is used
|
||||||
array_type: $ => seq('[[', $._type, ']]'),
|
|
||||||
|
|
||||||
reference_type: $ => prec(-1, seq($._reference, $._scoped_type)),
|
reference_type: $ => prec(-1, seq($._reference, $._scoped_type)),
|
||||||
|
|
||||||
|
|
@ -244,23 +243,18 @@ module.exports = grammar({
|
||||||
$.simple_type,
|
$.simple_type,
|
||||||
$.reference_type,
|
$.reference_type,
|
||||||
$.modified_type,
|
$.modified_type,
|
||||||
$.array_type,
|
|
||||||
$.variant_type,
|
$.variant_type,
|
||||||
$.tuple_type,
|
$.tuple_type,
|
||||||
$.function_type,
|
|
||||||
),
|
),
|
||||||
|
|
||||||
_scoped_type: $ => choice(
|
_scoped_type: $ => choice(
|
||||||
$.simple_type,
|
$.simple_type,
|
||||||
$.reference_type,
|
$.reference_type,
|
||||||
$.modified_type,
|
$.modified_type,
|
||||||
$.array_type,
|
|
||||||
seq('(', choice(
|
seq('(', choice(
|
||||||
$.variant_type,
|
$.variant_type,
|
||||||
$.tuple_type,
|
$.tuple_type,
|
||||||
//$.function_type,
|
|
||||||
), ')'),
|
), ')'),
|
||||||
$.function_type,
|
|
||||||
),
|
),
|
||||||
|
|
||||||
// --- comments
|
// --- comments
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue