File tree Expand file tree Collapse file tree 4 files changed +32
-16
lines changed
HydraScript.Application.CodeGeneration/Visitors
HydraScript.Application.StaticAnalysis/Visitors
HydraScript.Domain.BackEnd/Impl/Instructions
HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Declarations/AfterTypesAreLoaded Expand file tree Collapse file tree 4 files changed +32
-16
lines changed Original file line number Diff line number Diff line change @@ -129,8 +129,14 @@ public AddressedInstructions Visit(FunctionDeclaration visitable)
129129 }
130130 } ;
131131
132- foreach ( var arg in visitable . Arguments )
133- result . Add ( new PopParameter ( arg . Name ) ) ;
132+ for ( var i = 0 ; i < visitable . Arguments . Count ; i ++ )
133+ {
134+ var arg = visitable . Arguments [ i ] ;
135+ if ( arg is DefaultValueArgument @default )
136+ result . Add ( new PopParameter ( arg . Name , @default . Info . Value ) ) ;
137+ else
138+ result . Add ( new PopParameter ( arg . Name ) ) ;
139+ }
134140
135141 result . AddRange ( visitable . Statements . Accept ( This ) ) ;
136142 if ( ! visitable . HasReturnStatement ( ) )
Original file line number Diff line number Diff line change @@ -69,12 +69,12 @@ public VisitUnit Visit(FunctionDeclaration visitable)
6969 {
7070 var indexOfFirstDefaultArgument = visitable . Arguments
7171 . Select ( ( x , i ) => new { Argument = x , Index = i } )
72- . FirstOrDefault ( pair => pair . Argument . Default ) ? . Index ?? - 1 ;
72+ . FirstOrDefault ( pair => pair . Argument is DefaultValueArgument ) ? . Index ?? - 1 ;
7373 if ( indexOfFirstDefaultArgument is not - 1 )
7474 {
7575 for ( var i = indexOfFirstDefaultArgument ; i < visitable . Arguments . Count ; i ++ )
7676 {
77- if ( ! visitable . Arguments [ i ] . Default )
77+ if ( visitable . Arguments [ i ] is not DefaultValueArgument )
7878 throw new NamedArgumentAfterDefaultValueArgument (
7979 visitable . Segment ,
8080 function : visitable . Name ,
Original file line number Diff line number Diff line change 11namespace HydraScript . Domain . BackEnd . Impl . Instructions ;
22
3- public class PopParameter ( string parameter ) : Instruction
3+ public class PopParameter ( string parameter , object ? defaultValue = null ) : Instruction
44{
55 public override IAddress Execute ( IExecuteParams executeParams )
66 {
7- var argument = executeParams . Arguments . Dequeue ( ) ;
8- executeParams . Frames . Peek ( ) [ parameter ] = argument ;
7+ var frame = executeParams . Frames . Peek ( ) ;
8+ if ( executeParams . Arguments . TryDequeue ( out var argument ) )
9+ frame [ parameter ] = argument ;
10+ else
11+ frame [ parameter ] = defaultValue ;
912 return Address . Next ;
1013 }
1114
1215 protected override string ToStringInternal ( ) =>
13- $ "PopParameter { parameter } ";
16+ defaultValue is null
17+ ? $ "PopParameter { parameter } "
18+ : $ "PopParameter { parameter } { defaultValue } ";
1419}
Original file line number Diff line number Diff line change @@ -7,8 +7,6 @@ public interface IFunctionArgument
77 public string Name { get ; }
88
99 public TypeValue TypeValue { get ; }
10-
11- public bool Default => false ;
1210}
1311
1412public record NamedArgument (
@@ -19,14 +17,21 @@ public override string ToString() =>
1917 $ "{ Name } : { TypeValue } ";
2018}
2119
22- public record DefaultValueArgument (
23- string Name ,
24- Literal Value ) : IFunctionArgument
20+ public record DefaultValueArgument : IFunctionArgument
2521{
26- public TypeValue TypeValue { get ; } = Value . Type ;
22+ public DefaultValueArgument ( string name , Literal literal )
23+ {
24+ Name = name ;
25+ TypeValue = literal . Type ;
26+ Info = literal . ToValueDto ( ) ;
27+ }
28+
29+ public string Name { get ; }
30+
31+ public TypeValue TypeValue { get ; }
2732
28- public bool Default => true ;
33+ public ValueDto Info { get ; }
2934
3035 public override string ToString ( ) =>
31- $ "{ Name } = { Value } ";
36+ $ "{ Name } = { Info . Label } ";
3237}
You can’t perform that action at this time.
0 commit comments