1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using SER . ArgumentSystem . Arguments ;
4+ using SER . ArgumentSystem . BaseArguments ;
5+ using SER . FlagSystem ;
6+ using SER . FlagSystem . Flags ;
7+ using SER . Helpers . Exceptions ;
8+ using SER . Helpers . Extensions ;
9+ using SER . MethodSystem . BaseMethods ;
10+ using SER . MethodSystem . MethodDescriptors ;
11+ using SER . ValueSystem ;
12+ using SER . VariableSystem . Bases ;
13+
14+ namespace SER . MethodSystem . Methods . ScriptMethods ;
15+
16+ public class RunFuncMethod : SynchronousMethod , ICanError
17+ {
18+ public override string Description => "Runs a function script with arguments." ;
19+
20+ public string [ ] ErrorReasons =>
21+ [
22+ "Provided script is not a function" ,
23+ "Provided arguments are incompatible with the function"
24+ ] ;
25+
26+ public override Argument [ ] ExpectedArguments =>
27+ [
28+ new CreatedScriptArgument ( "script to run" ) ,
29+ new AnyValueArgument ( "values to pass" )
30+ {
31+ ConsumesRemainingValues = true ,
32+ DefaultValue = new ( new List < Value > ( ) , "none" ) ,
33+ Description = "The values that will be passed to the function. " +
34+ "These values must be compatible with the variables the function is expecting."
35+ }
36+ ] ;
37+
38+ public override void Execute ( )
39+ {
40+ var scriptToRun = Args . GetCreatedScript ( "script to run" ) ;
41+ var valuesToPass = Args . GetRemainingArguments < Value , AnyValueArgument > ( "values to pass" ) ;
42+
43+ if ( ScriptFlagHandler . GetScriptFlags ( scriptToRun . Name ) . FirstOrDefault ( f => f . GetType ( ) == typeof ( FunctionFlag ) )
44+ is not FunctionFlag functionFlag )
45+ {
46+ throw new ScriptRuntimeError ( $ "Script '{ scriptToRun . Name } ' is not a function.") ;
47+ }
48+
49+ if ( valuesToPass . Length != functionFlag . ExpectedVarTokens . Count )
50+ {
51+ throw new ScriptRuntimeError (
52+ $ "Function expects { functionFlag . ExpectedVarTokens . Count } arguments, but { valuesToPass . Length } were provided."
53+ ) ;
54+ }
55+
56+ var zippedTuples = valuesToPass
57+ . Zip ( functionFlag . ExpectedVarTokens , ( v , t ) => ( value : v , varToken : t ) )
58+ . ToArray ( ) ;
59+
60+ for ( var i = 0 ; i < zippedTuples . Length ; i ++ )
61+ {
62+ if ( zippedTuples [ i ] is not { varToken : { } varToken , value : { } value } )
63+ {
64+ throw new AndrzejFuckedUpException ( ) ;
65+ }
66+
67+ if ( ! varToken . ValueType . IsInstanceOfType ( value ) )
68+ {
69+ throw new ScriptRuntimeError (
70+ $ "Function '{ scriptToRun . Name } ' expects argument { i + 1 } to be of type " +
71+ $ "{ varToken . ValueType . FriendlyTypeName ( ) } , but { value . GetType ( ) . FriendlyTypeName ( ) } " +
72+ $ "was provided."
73+ ) ;
74+ }
75+
76+ var variable = Variable . CreateVariable ( varToken . Name , value ) ;
77+ if ( variable . GetType ( ) != varToken . VariableType )
78+ {
79+ throw new AndrzejFuckedUpException ( ) ;
80+ }
81+
82+ scriptToRun . AddVariable ( variable ) ;
83+ }
84+
85+ scriptToRun . Run ( ) ;
86+ }
87+ }
0 commit comments