BadScript 2
Loading...
Searching...
No Matches
BadDatePrototype.cs
Go to the documentation of this file.
1using System.Globalization;
5
7
9{
10
11 private static Dictionary<string, BadObjectReference> s_StaticMembers = new Dictionary<string, BadObjectReference>
12 {
13 {
14 "Now", BadObjectReference.Make("Date.Now", p => BadDate.Now)
15 },
16 {
17 "UtcNow", BadObjectReference.Make("Date.UtcNow", p => BadDate.UtcNow)
18 },
19 {
20 "Parse", BadObjectReference.Make("Date.Parse", p => new BadInteropFunction("Parse", Parse, true, BadDate.Prototype))
21 }
22 };
23 private static BadObject Parse(BadExecutionContext ctx, BadObject[] args)
24 {
25 if (args.Length == 1 || (args.Length == 2 && args[1] == Null))
26 {
27 return DateTimeOffset.Parse(args[0].ToString());
28 }
29 if (args.Length == 2)
30 {
31 if(args[1] is IBadString str)
32 return DateTimeOffset.Parse(args[0].ToString(), CultureInfo.GetCultureInfo(str.Value));
33 }
34
35 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
36 }
37
39 {
40 if (args.Length == 0)
41 {
42 return BadDate.Now;
43 }
44 if(args.Length == 1)
45 {
46 if(args[0] is IBadNumber n)
47 {
48 return new BadDate(DateTimeOffset.FromUnixTimeMilliseconds((long)n.Value));
49 }
50 if(args[0] is BadString s)
51 {
52 if(DateTimeOffset.TryParse(s.Value, out DateTimeOffset dt))
53 {
54 return new BadDate(dt);
55 }
56 }
57 }
58 if(args.Length == 3)
59 {
60 if(args[0] is IBadNumber y && args[1] is IBadNumber m && args[2] is IBadNumber d)
61 {
62 return new BadDate(new DateTimeOffset((int)y.Value, (int)m.Value, (int)d.Value, 0, 0, 0, TimeSpan.Zero));
63 }
64 }
65 if(args.Length == 6)
66 {
67 if(args[0] is IBadNumber y && args[1] is IBadNumber m && args[2] is IBadNumber d && args[3] is IBadNumber h && args[4] is IBadNumber min && args[5] is IBadNumber s)
68 {
69 return new BadDate(new DateTimeOffset((int)y.Value, (int)m.Value, (int)d.Value, (int)h.Value, (int)min.Value, (int)s.Value, TimeSpan.Zero));
70 }
71 }
72 if(args.Length == 7)
73 {
74 if(args[0] is IBadNumber y && args[1] is IBadNumber m && args[2] is IBadNumber d && args[3] is IBadNumber h && args[4] is IBadNumber min && args[5] is IBadNumber s && args[6] is IBadNumber ms)
75 {
76 return new BadDate(new DateTimeOffset((int)y.Value, (int)m.Value, (int)d.Value, (int)h.Value, (int)min.Value, (int)s.Value, (int)ms.Value, TimeSpan.Zero));
77 }
78 }
79 if (args.Length == 8)
80 {
81 if(args[0] is IBadNumber y && args[1] is IBadNumber m && args[2] is IBadNumber d && args[3] is IBadNumber h && args[4] is IBadNumber min && args[5] is IBadNumber s && args[6] is IBadNumber ms && args[7] is IBadTime time)
82 {
83 return new BadDate(new DateTimeOffset((int)y.Value, (int)m.Value, (int)d.Value, (int)h.Value, (int)min.Value, (int)s.Value, (int)ms.Value, time.Value));
84 }
85 }
86
87 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
88 }
90 {
91 }
92}
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Interop Function taking an array of arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? delete=null)
Creates a new Reference Object.
static BadClassPrototype Prototype
Definition BadDate.cs:9
Implements a Native String.
Definition BadString.cs:9
static BadObject Parse(BadExecutionContext ctx, BadObject[] args)
static Dictionary< string, BadObjectReference > s_StaticMembers
static BadObject DateConstructor(BadExecutionContext ctx, BadObject[] args)
Implements the Interface for Native Numbers.
Definition IBadNumber.cs:7
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6