BadScript 2
Loading...
Searching...
No Matches
BadTimePrototype.cs
Go to the documentation of this file.
1using System.Globalization;
5
7
9{
10 private static Dictionary<string, BadObjectReference> s_StaticMembers = new Dictionary<string, BadObjectReference>
11 {
12 {
13 "Zero", BadObjectReference.Make("Time.Zero", p => BadTime.Zero)
14 },
15 {
16 "Parse", BadObjectReference.Make("Time.Parse", p => new BadInteropFunction("Parse", Parse, true, BadDate.Prototype))
17 }
18 };
19 private static BadObject Parse(BadExecutionContext ctx, BadObject[] args)
20 {
21 if (args.Length == 1 || (args.Length == 2 && args[1] == Null))
22 {
23 return TimeSpan.Parse(args[0].ToString());
24 }
25 if (args.Length == 2)
26 {
27 if(args[1] is IBadString str)
28 return TimeSpan.Parse(args[0].ToString(), CultureInfo.GetCultureInfo(str.Value));
29 }
30
31 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
32 }
33
35 {
36 if (args.Length == 0)
37 {
38 return BadTime.Zero;
39 }
40 if(args.Length == 1)
41 {
42 if(args[0] is IBadNumber n)
43 {
44 return new BadTime(TimeSpan.FromMilliseconds((double)n.Value));
45 }
46 if(args[0] is BadString s)
47 {
48 if(TimeSpan.TryParse(s.Value, out TimeSpan dt))
49 {
50 return new BadTime(dt);
51 }
52 }
53 }
54 if(args.Length == 3)
55 {
56 if(args[0] is IBadNumber h && args[1] is IBadNumber m && args[2] is IBadNumber s)
57 {
58 return new BadTime(new TimeSpan((int)h.Value, (int)m.Value, (int)s.Value));
59 }
60 }
61 if(args.Length == 4)
62 {
63 if(args[0] is IBadNumber h && args[1] is IBadNumber m && args[2] is IBadNumber s && args[3] is IBadNumber ms)
64 {
65 return new BadTime(new TimeSpan((int)h.Value, (int)m.Value, (int)s.Value, (int)ms.Value));
66 }
67 }
68 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
69 }
71 {
72 }
73}
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 Dictionary< string, BadObjectReference > s_StaticMembers
static BadObject TimeConstructor(BadExecutionContext ctx, BadObject[] args)
static BadObject Parse(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