summaryrefslogtreecommitdiffstats
path: root/DTScopedVars.c
blob: 0cde670460e562d80f5795f13cb36014452f3efb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#strict 2
// TODO: ScopedVars with temporary target (like a reference to a function local var)

static const ScopedVar_Global = 1;
static const ScopedVar_Local = 2;
static const ScopedVar_ArrayIndex = 3;

global func GlobalVar(name) { return [ScopedVar_Global, name]; }
global func LocalVar(name, object target) { return [ScopedVar_Local, name, target || this]; }
global func ArrayVar(index, array scopedVar) { return [ScopedVar_ArrayIndex, scopedVar, index]; }

global func &ScopedVar(array variable)
{
	if(!variable)
	{
		return 0;
	}
	if(variable[0] == ScopedVar_Global)
	{
		if(GetType(variable[1]) == C4V_String)
		{
			return GlobalN(variable[1]);
		}
		else
		{
			return Global(variable[1]);
		}
	}
	else if(variable[0] == ScopedVar_Local)
	{
		if(GetType(variable[1]) == C4V_String)
		{
			return LocalN(variable[1], variable[2]);
		}
		else
		{
			return Local(variable[1], variable[2]);
		}
	}
	else if(variable[0] == ScopedVar_ArrayIndex)
	{
		var index = variable[2];
		if(GetType(index) != C4V_Int && GetType(index) != C4V_Any)
		{
			index = ScopedVar(index);
		}
		return ScopedVar(variable[1])[index];
	}
	else
	{
		if(this)
		{
			return ScopedVar(this->~CustomScopedVar(variable));
		}
		else if(GetID())
		{
			return ScopedVar(GetID()->~CustomScopedVar(variable));
		}
		else
		{
			return ScopedVar(CustomScopedVar(variable));
		}
	}
}

global func CustomScopedVar() { return _inherited(...); } // this allows "overloading" this function even if the "overloading" function is loaded before

global func CheckScopedVar(array variable)
{
	if(!variable)
	{
		return false;
	}

	if(GetType(variable[0]) == C4V_Int)
	{
		if(variable[0] == ScopedVar_Global || variable[0] == ScopedVar_Local)
		{
			if(!variable[1] || GetType(variable[1]) == C4V_Int || (GetType(variable[1]) == C4V_String && variable[1] != ""))
			{
				return true;
			}
		}

		if(variable[0] == ScopedVar_ArrayIndex)
		{
			if(CheckScopedVar(variable[1]) && !variable[2] || (GetType(variable[2]) == C4V_Int && variable[2] >= 0))
			{
				return true;
			}
		}
	}

	if(this)
	{
		return this->~CheckCustomScopedVar(variable);
	}
	else if(GetID())
	{
		return GetID()->~CheckCustomScopedVar(variable);
	}
	else
	{
		return CheckCustomScopedVar(variable);
	}
}

global func CheckCustomScopedVar() { return _inherited(...); }