Skip to content

Commit 2ad48ef

Browse files
committed
Type punning for Sphax
1 parent ae71ca7 commit 2ad48ef

File tree

3 files changed

+138
-22
lines changed

3 files changed

+138
-22
lines changed

Actions.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,9 @@ void Extension::GotoRoot()
7070
current = root;
7171
}
7272

73-
#include <sstream>
7473
void Extension::DebugWindow()
7574
{
76-
#ifdef UNICODE
77-
std::wostringstream oss;
78-
#else
79-
std::ostringstream oss;
80-
#endif
75+
std::basic_ostringstream<TCHAR> oss;
8176
oss << _T("IsString: ") << std::boolalpha << IsString() << std::endl;
8277
oss << _T("IsInteger: ") << std::boolalpha << IsInteger() << std::endl;
8378
oss << _T("IsDouble: ") << std::boolalpha << IsDouble() << std::endl;

Conditions.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,17 @@ bool Extension::IsNull()
3535
}
3636
bool Extension::IsTrue()
3737
{
38-
return IsBoolean() && current->u.boolean;
38+
if(IsBoolean())
39+
{
40+
return current->u.boolean ? true : false;
41+
}
42+
else if(IsInteger())
43+
{
44+
return current->u.integer ? true : false;
45+
}
46+
else if(IsString())
47+
{
48+
return GetString() == std::basic_string<TCHAR>(_T("true"));
49+
}
50+
return false;
3951
}

Expressions.cpp

Lines changed: 124 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,140 @@
11
#include "Common.h"
22

3-
43
TCHAR const *Extension::GetError()
54
{
65
return Runtime.CopyString(error.c_str());
76
}
87
TCHAR const *Extension::GetString()
98
{
10-
if(!IsString()) return _T("");
11-
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
12-
TCHAR *c = Runtime.CopyString(t);
13-
Edif::FreeString(t);
14-
return c;
9+
if(IsString())
10+
{
11+
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
12+
TCHAR *c = Runtime.CopyString(t);
13+
Edif::FreeString(t);
14+
return c;
15+
}
16+
else if(IsInteger())
17+
{
18+
std::basic_ostringstream<TCHAR> oss;
19+
oss << GetLong();
20+
return Runtime.CopyString(oss.str().c_str());
21+
}
22+
else if(IsDouble())
23+
{
24+
std::basic_ostringstream<TCHAR> oss;
25+
oss << GetDouble();
26+
return Runtime.CopyString(oss.str().c_str());
27+
}
28+
else if(IsBoolean())
29+
{
30+
if(GetBoolNum())
31+
{
32+
return _T("true");
33+
}
34+
else
35+
{
36+
return _T("false");
37+
}
38+
}
39+
else if(IsNull())
40+
{
41+
return _T("null");
42+
}
43+
return _T("");
1544
}
1645
int Extension::GetInteger()
1746
{
18-
return IsInteger() ? static_cast<int>(current->u.integer) : 0;
47+
if(IsInteger())
48+
{
49+
return static_cast<int>(current->u.integer);
50+
}
51+
else if(IsDouble())
52+
{
53+
return static_cast<int>(current->u.dbl);
54+
}
55+
else if(IsString())
56+
{
57+
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
58+
std::basic_istringstream<TCHAR> iss (t);
59+
Edif::FreeString(t);
60+
int x = 0;
61+
iss >> x;
62+
return x;
63+
}
64+
return 0;
1965
}
2066
TCHAR const *Extension::GetLong()
2167
{
2268
if(IsInteger())
2369
{
24-
std::basic_ostringstream<TCHAR> ss;
25-
ss << current->u.integer;
26-
return Runtime.CopyString(ss.str().c_str());
70+
std::basic_ostringstream<TCHAR> oss;
71+
oss << current->u.integer;
72+
return Runtime.CopyString(oss.str().c_str());
73+
}
74+
else if(IsDouble())
75+
{
76+
std::basic_ostringstream<TCHAR> oss;
77+
oss << static_cast<long long>(current->u.dbl);
78+
return Runtime.CopyString(oss.str().c_str());
79+
}
80+
else if(IsString())
81+
{
82+
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
83+
std::basic_istringstream<TCHAR> iss (t);
84+
Edif::FreeString(t);
85+
long long x = 0;
86+
iss >> x;
87+
std::basic_ostringstream<TCHAR> oss;
88+
oss << x;
89+
return Runtime.CopyString(oss.str().c_str());
2790
}
2891
return _T("0");
2992
}
3093
float Extension::GetFloat()
3194
{
32-
return IsDouble() ? static_cast<float>(current->u.dbl) : 0.0f;
95+
if(IsDouble())
96+
{
97+
return static_cast<float>(current->u.dbl);
98+
}
99+
else if(IsInteger())
100+
{
101+
return static_cast<float>(current->u.integer);
102+
}
103+
else if(IsString())
104+
{
105+
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
106+
std::basic_istringstream<TCHAR> iss (t);
107+
Edif::FreeString(t);
108+
float x = 0;
109+
iss >> x;
110+
return x;
111+
}
112+
return 0.0f;
33113
}
34114
TCHAR const *Extension::GetDouble()
35115
{
36116
if(IsDouble())
37117
{
38-
std::basic_ostringstream<TCHAR> ss;
39-
ss << std::setprecision(20) << current->u.dbl;
40-
return Runtime.CopyString(ss.str().c_str());
118+
std::basic_ostringstream<TCHAR> oss;
119+
oss << std::setprecision(20) << current->u.dbl;
120+
return Runtime.CopyString(oss.str().c_str());
121+
}
122+
else if(IsInteger())
123+
{
124+
std::basic_ostringstream<TCHAR> oss;
125+
oss << static_cast<double>(current->u.integer);
126+
return Runtime.CopyString(oss.str().c_str());
127+
}
128+
else if(IsString())
129+
{
130+
TCHAR *t = Edif::ConvertString(current->u.string.ptr);
131+
std::basic_istringstream<TCHAR> iss (t);
132+
Edif::FreeString(t);
133+
double x = 0;
134+
iss >> x;
135+
std::basic_ostringstream<TCHAR> oss;
136+
oss << x;
137+
return Runtime.CopyString(oss.str().c_str());
41138
}
42139
return _T("0.0");
43140
}
@@ -55,5 +152,17 @@ unsigned Extension::GetNumValues()
55152
}
56153
unsigned Extension::GetBoolNum()
57154
{
58-
return IsBoolean() ? (current->u.boolean ? 1 : 0) : 0;
155+
if(IsBoolean())
156+
{
157+
return current->u.boolean ? 1 : 0;
158+
}
159+
else if(IsInteger())
160+
{
161+
return current->u.integer ? 1 : 0;
162+
}
163+
else if(IsString())
164+
{
165+
return GetString() == std::basic_string<TCHAR>(_T("true")) ? 1 : 0;
166+
}
167+
return 0;
59168
}

0 commit comments

Comments
 (0)