11use std:: { marker:: PhantomData , rc:: Rc } ;
22
33use crate :: type_infer:: TypeChecker ;
4- use crate :: types:: { Directive , Misc } ;
4+ use crate :: types:: { ArgumentType , Directive , IgnoreArgument , Misc , SelfReference } ;
55use crate :: {
66 arena:: Arena ,
7- symbols:: SymbolType ,
87 t_ast:: TypedAst ,
98 types:: {
10- ArrayIndexAccessExpression , ArrayLiteral , AssertStatement , AssignStatement , AstNode ,
11- BinaryExpression , Block , BlockType , BoolLiteral , BreakStatement , ConstantDefinition ,
12- Definition , EnumDefinition , Expression , ExternalFunctionDefinition , FunctionCallExpression ,
13- FunctionDefinition , FunctionType , GenericType , Identifier , IfStatement , Literal , Location ,
14- LoopStatement , MemberAccessExpression , NumberLiteral , OperatorKind , Parameter ,
15- ParenthesizedExpression , PrefixUnaryExpression , QualifiedName , ReturnStatement , SimpleType ,
16- SourceFile , SpecDefinition , Statement , StringLiteral , StructDefinition , StructField , Type ,
17- TypeArray , TypeDefinition , TypeDefinitionStatement , TypeQualifiedName , UnaryOperatorKind ,
18- UnitLiteral , UseDirective , UzumakiExpression , VariableDefinitionStatement ,
9+ Argument , ArrayIndexAccessExpression , ArrayLiteral , AssertStatement , AssignStatement ,
10+ AstNode , BinaryExpression , Block , BlockType , BoolLiteral , BreakStatement ,
11+ ConstantDefinition , Definition , EnumDefinition , Expression , ExternalFunctionDefinition ,
12+ FunctionCallExpression , FunctionDefinition , FunctionType , GenericType , Identifier ,
13+ IfStatement , Literal , Location , LoopStatement , MemberAccessExpression , NumberLiteral ,
14+ OperatorKind , ParenthesizedExpression , PrefixUnaryExpression , QualifiedName ,
15+ ReturnStatement , SimpleType , SourceFile , SpecDefinition , Statement , StringLiteral ,
16+ StructDefinition , StructField , Type , TypeArray , TypeDefinition , TypeDefinitionStatement ,
17+ TypeQualifiedName , UnaryOperatorKind , UnitLiteral , UseDirective , UzumakiExpression ,
18+ VariableDefinitionStatement ,
1919 } ,
2020} ;
2121use tree_sitter:: Node ;
@@ -34,7 +34,6 @@ impl BuilderComplete for CompleteState {}
3434pub struct Builder < ' a , S > {
3535 arena : Arena ,
3636 source_code : Vec < ( Node < ' a > , & ' a [ u8 ] ) > ,
37- types : Vec < SymbolType > ,
3837 t_ast : Option < TypedAst > ,
3938 _state : PhantomData < S > ,
4039}
@@ -51,7 +50,6 @@ impl<'a> Builder<'a, InitState> {
5150 Self {
5251 arena : Arena :: default ( ) ,
5352 source_code : Vec :: new ( ) ,
54- types : Vec :: new ( ) ,
5553 t_ast : None ,
5654 _state : PhantomData ,
5755 }
@@ -116,7 +114,6 @@ impl<'a> Builder<'a, InitState> {
116114 Ok ( Builder {
117115 arena : Arena :: default ( ) ,
118116 source_code : Vec :: new ( ) ,
119- types : Vec :: new ( ) ,
120117 t_ast : Some ( t_ast) ,
121118 _state : PhantomData ,
122119 } )
@@ -154,11 +151,7 @@ impl<'a> Builder<'a, InitState> {
154151 cursor = node. walk ( ) ;
155152 let founded_imported_types = node
156153 . children_by_field_name ( "imported_type" , & mut cursor)
157- . map ( |imported_type| {
158- let t = self . build_identifier ( id, & imported_type, code) ;
159- self . types . push ( SymbolType :: Global ( t. name . clone ( ) ) ) ;
160- t
161- } ) ;
154+ . map ( |imported_type| self . build_identifier ( id, & imported_type, code) ) ;
162155 let founded_imported_types: Vec < Rc < Identifier > > = founded_imported_types. collect ( ) ;
163156 if !founded_imported_types. is_empty ( ) {
164157 imported_types = Some ( founded_imported_types) ;
@@ -221,7 +214,6 @@ impl<'a> Builder<'a, InitState> {
221214 variants = founded_variants;
222215 }
223216
224- self . types . push ( SymbolType :: Inner ( name. name . clone ( ) ) ) ;
225217 let node = Rc :: new ( EnumDefinition :: new ( id, name, variants, location) ) ;
226218 self . arena . add_node (
227219 AstNode :: Definition ( Definition :: Enum ( node. clone ( ) ) ) ,
@@ -344,8 +336,8 @@ impl<'a> Builder<'a, InitState> {
344336 let mut cursor = argument_list_node. walk ( ) ;
345337 let founded_arguments = argument_list_node
346338 . children_by_field_name ( "argument" , & mut cursor)
347- . map ( |segment| self . build_argument ( id, & segment, code) ) ;
348- let founded_arguments: Vec < Rc < Parameter > > = founded_arguments. collect ( ) ;
339+ . map ( |segment| self . build_argument_type ( id, & segment, code) ) ;
340+ let founded_arguments: Vec < ArgumentType > = founded_arguments. collect ( ) ;
349341 if !founded_arguments. is_empty ( ) {
350342 arguments = Some ( founded_arguments) ;
351343 }
@@ -400,8 +392,8 @@ impl<'a> Builder<'a, InitState> {
400392
401393 let founded_arguments = node
402394 . children_by_field_name ( "argument" , & mut cursor)
403- . map ( |segment| self . build_identifier ( id, & segment, code) ) ;
404- let founded_arguments: Vec < Rc < Identifier > > = founded_arguments. collect ( ) ;
395+ . map ( |segment| self . build_argument_type ( id, & segment, code) ) ;
396+ let founded_arguments: Vec < ArgumentType > = founded_arguments. collect ( ) ;
405397 if !founded_arguments. is_empty ( ) {
406398 arguments = Some ( founded_arguments) ;
407399 }
@@ -438,16 +430,77 @@ impl<'a> Builder<'a, InitState> {
438430 node
439431 }
440432
441- fn build_argument ( & mut self , parent_id : u32 , node : & Node , code : & [ u8 ] ) -> Rc < Parameter > {
433+ fn build_argument_type ( & mut self , parent_id : u32 , node : & Node , code : & [ u8 ] ) -> ArgumentType {
434+ match node. kind ( ) {
435+ "argument_declaration" => {
436+ let argument = self . build_argument ( parent_id, node, code) ;
437+ ArgumentType :: Argument ( argument)
438+ }
439+ "self_reference" => {
440+ let self_reference = self . build_self_reference ( parent_id, node, code) ;
441+ ArgumentType :: SelfReference ( self_reference)
442+ }
443+ "ignore_argument" => {
444+ let ignore_argument = self . build_ignore_argument ( parent_id, node, code) ;
445+ ArgumentType :: IgnoreArgument ( ignore_argument)
446+ }
447+ _ => ArgumentType :: Type ( self . build_type ( parent_id, node, code) ) ,
448+ }
449+ }
450+
451+ fn build_argument ( & mut self , parent_id : u32 , node : & Node , code : & [ u8 ] ) -> Rc < Argument > {
442452 let id = Self :: get_node_id ( ) ;
443453 let location = Self :: get_location ( node, code) ;
444454 let name_node = node. child_by_field_name ( "name" ) . unwrap ( ) ;
445455 let type_node = node. child_by_field_name ( "type" ) . unwrap ( ) ;
446456 let ty = self . build_type ( id, & type_node, code) ;
457+ let is_mut = node
458+ . child_by_field_name ( "mut" )
459+ . map ( |n| n. kind ( ) == "true" )
460+ . unwrap_or ( false ) ;
447461 let name = self . build_identifier ( id, & name_node, code) ;
448- let node = Rc :: new ( Parameter :: new ( id, location, name, ty) ) ;
449- self . arena
450- . add_node ( AstNode :: Misc ( Misc :: Parameter ( node. clone ( ) ) ) , parent_id) ;
462+ let node = Rc :: new ( Argument :: new ( id, location, name, is_mut, ty) ) ;
463+ self . arena . add_node (
464+ AstNode :: ArgumentType ( ArgumentType :: Argument ( node. clone ( ) ) ) ,
465+ parent_id,
466+ ) ;
467+ node
468+ }
469+
470+ fn build_self_reference (
471+ & mut self ,
472+ parent_id : u32 ,
473+ node : & Node ,
474+ code : & [ u8 ] ,
475+ ) -> Rc < SelfReference > {
476+ let id = Self :: get_node_id ( ) ;
477+ let location = Self :: get_location ( node, code) ;
478+ let is_mut = node
479+ . child_by_field_name ( "mut" )
480+ . map ( |n| n. kind ( ) == "true" )
481+ . unwrap_or ( false ) ;
482+ let node = Rc :: new ( SelfReference :: new ( id, location, is_mut) ) ;
483+ self . arena . add_node (
484+ AstNode :: ArgumentType ( ArgumentType :: SelfReference ( node. clone ( ) ) ) ,
485+ parent_id,
486+ ) ;
487+ node
488+ }
489+
490+ fn build_ignore_argument (
491+ & mut self ,
492+ parent_id : u32 ,
493+ node : & Node ,
494+ code : & [ u8 ] ,
495+ ) -> Rc < IgnoreArgument > {
496+ let id = Self :: get_node_id ( ) ;
497+ let location = Self :: get_location ( node, code) ;
498+ let ty = self . build_type ( id, & node. child_by_field_name ( "type" ) . unwrap ( ) , code) ;
499+ let node = Rc :: new ( IgnoreArgument :: new ( id, location, ty) ) ;
500+ self . arena . add_node (
501+ AstNode :: ArgumentType ( ArgumentType :: IgnoreArgument ( node. clone ( ) ) ) ,
502+ parent_id,
503+ ) ;
451504 node
452505 }
453506
@@ -1054,7 +1107,6 @@ impl<'a> Builder<'a, InitState> {
10541107 "type_fn" => Type :: Function ( self . build_function_type ( parent_id, node, code) ) ,
10551108 "identifier" => {
10561109 let name = self . build_identifier ( parent_id, node, code) ;
1057- self . types . push ( SymbolType :: Global ( name. name . clone ( ) ) ) ;
10581110 Type :: Custom ( name)
10591111 }
10601112 _ => {
@@ -1084,7 +1136,6 @@ impl<'a> Builder<'a, InitState> {
10841136 let id = Self :: get_node_id ( ) ;
10851137 let location = Self :: get_location ( node, code) ;
10861138 let name = node. utf8_text ( code) . unwrap ( ) . to_string ( ) ;
1087- self . types . push ( SymbolType :: Global ( name. clone ( ) ) ) ;
10881139 let node = Rc :: new ( SimpleType :: new ( id, location, name) ) ;
10891140 self . arena . add_node (
10901141 AstNode :: Expression ( Expression :: Type ( Type :: Simple ( node. clone ( ) ) ) ) ,
0 commit comments