Skip to content

Commit 754ae6e

Browse files
author
runrevali
committed
[[ LiveCode Builder ]] Add is (not) defined and is (not) empty
1 parent 71bb3e6 commit 754ae6e

5 files changed

Lines changed: 34 additions & 133 deletions

File tree

libscript/src/module-type.cpp

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,20 @@
22
#include <foundation.h>
33
#include <foundation-auto.h>
44

5-
void MCTypeEvalBoolAsString(bool p_target, MCStringRef& r_output)
5+
static bool MCTypeValueIsEmpty(MCValueRef p_value)
66
{
7-
r_output = MCValueRetain(p_target ? kMCTrueString : kMCFalseString);
8-
}
9-
10-
void MCTypeEvalRealAsString(double p_target, MCStringRef& r_output)
11-
{
12-
MCStringFormat(r_output, "%f", p_target);
13-
}
14-
15-
void MCTypeEvalIntAsString(integer_t p_target, MCStringRef& r_output)
16-
{
17-
MCStringFormat(r_output, "%d", p_target);
18-
}
19-
20-
void MCTypeEvalStringAsBool(MCStringRef p_target, bool& r_output)
21-
{
22-
if (!MCTypeConvertStringToBool(p_target, r_output))
23-
return;
24-
}
25-
26-
void MCTypeEvalStringAsReal(MCStringRef p_target, real64_t& r_output)
27-
{
28-
if (!MCTypeConvertStringToReal(p_target, r_output))
29-
return;
30-
}
31-
32-
void MCTypeEvalStringAsInt(MCStringRef p_target, integer_t& r_output)
33-
{
34-
if (!MCTypeConvertStringToLongInteger(p_target, r_output))
35-
return;
36-
}
37-
38-
void MCTypeEvalIntAsReal(integer_t p_target, real64_t r_output)
39-
{
40-
r_output = p_target;
7+
return p_value != kMCNull &&
8+
(p_value == kMCEmptyName ||
9+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeArray && MCArrayIsEmpty((MCArrayRef)p_value)) ||
10+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeString && MCStringIsEmpty((MCStringRef)p_value)) ||
11+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeName && MCNameIsEmpty((MCNameRef)p_value)) ||
12+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeData && MCDataIsEmpty((MCDataRef)p_value)) ||
13+
(MCValueGetTypeCode(p_value) == kMCValueTypeCodeList && MCListIsEmpty((MCListRef)p_value)));
4114
}
4215

4316
extern "C" MC_DLLEXPORT void MCTypeEvalIsEmpty(MCValueRef p_target, bool& r_output)
4417
{
45-
//r_output = p_target != kMCNull && MCValueIsEmpty(p_target);
18+
r_output = MCTypeValueIsEmpty(p_target);
4619
}
4720

4821
extern "C" MC_DLLEXPORT void MCTypeEvalIsNotEmpty(MCValueRef p_target, bool& r_output)
@@ -53,12 +26,12 @@ extern "C" MC_DLLEXPORT void MCTypeEvalIsNotEmpty(MCValueRef p_target, bool& r_o
5326
r_output = !t_empty;
5427
}
5528

56-
extern "C" MC_DLLEXPORT void MCTypeEvalIsDefined(MCValueRef p_target, bool& r_output)
29+
extern "C" MC_DLLEXPORT void MCTypeEvalIsDefined(MCValueRef *p_target, bool& r_output)
5730
{
58-
r_output = p_target == kMCNull;
31+
r_output = p_target != nil;
5932
}
6033

61-
extern "C" MC_DLLEXPORT void MCTypeEvalIsNotDefined(MCValueRef p_target, bool p_is_not, bool& r_output)
34+
extern "C" MC_DLLEXPORT void MCTypeEvalIsNotDefined(MCValueRef *p_target, bool& r_output)
6235
{
6336
bool t_defined;
6437
MCTypeEvalIsDefined(p_target, t_defined);

libscript/src/type.mlc

Lines changed: 10 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,9 @@
11
module com.livecode.type
22

3-
/*
4-
public foreign handler MCTypeEvalBoolFormattedAsString(in Target as bool, out Value as string) as undefined binds to "<builtin>"
5-
public foreign handler MCTypeEvalStringParsedAsBool(in Target as string, out Value as bool) as undefined binds to "<builtin>"
6-
7-
public foreign handler MCTypeEvalNumberFormattedAsString(in Target as number, out Value as string) as undefined binds to "<builtin>"
8-
public foreign handler MCTypeEvalStringParsedAsReal(in Target as string, out Value as number) as undefined binds to "<builtin>"
9-
public foreign handler MCTypeEvalStringParsedAsInt(in Target as string, out Value as number) as undefined binds to "<builtin>"
10-
*/
113
public foreign handler MCTypeEvalIsEmpty(in Target as any, out Value as bool) as undefined binds to "<builtin>"
124
public foreign handler MCTypeEvalIsNotEmpty(in Target as any, out Value as bool) as undefined binds to "<builtin>"
13-
public foreign handler MCTypeEvalIsDefined(in Target as any, out Value as bool) as undefined binds to "<builtin>"
14-
public foreign handler MCTypeEvalIsNotDefined(in Target as any, out Value as bool) as undefined binds to "<builtin>"
15-
16-
--
17-
18-
/*
19-
Summary: Converts <Target> to a string.
20-
Target: An expression that evaluates to a bool, real, or integer.
21-
output: The converted value if there is a valid conversion from the type of <Target> to a string.
22-
Bools, ints and reals always have valid conversions to strings.
23-
24-
Example: put true as string into tVar // tVar contains the string "true"
25-
*/
26-
27-
/*
28-
syntax AsString is postfix operator with precedence 1
29-
<Target: Expression> "formatted" "as" "string"
30-
begin
31-
EvalBoolAsString(Target, output)
32-
EvalIntAsString(Target, output)
33-
EvalRealAsString(Target, output)
34-
end syntax
35-
*/
36-
37-
/*
38-
Summary: Converts <Target> to a real.
39-
40-
Target: An expression that evaluates to an integer, or a string.
41-
output: The converted value if there is a valid conversion from the type of <Target> to a real.
42-
An int always has a valid conversion to a real.
43-
A string has a valid conversions to a real if it consists of digits, and optionally a leading minus sign, a decimal point, and an "E" or "e" for exponential notation.
44-
45-
Example: put "8.0" as real into tVar // tVar contains the real number 8.0
46-
*/
47-
/*
48-
syntax AsReal is postfix operator with precedence 1
49-
<Target: Expression> "as" "real"
50-
begin
51-
EvalIntAsReal(Target, output)
52-
EvalStringAsReal(Target, output)
53-
end syntax
54-
*/
55-
/*
56-
Summary: Converts <Target> to an integer.
57-
58-
Target: An expression that evaluates to a string.
59-
output: The converted value if there is a valid conversion the string <Target> to an int.
60-
A string has a valid conversion to an int if it consists of digits, and optionally a leading minus sign, and if the resulting integer is within the valid range for a 32-bit integer.
61-
Example: put "8" as integer into tVar // tVar contains the integer 8
62-
*/
63-
/*
64-
syntax AsInt is postfix operator with precedence 1
65-
<Target: Expression> "as" "int"
66-
begin
67-
EvalStringAsInt(Target, output)
68-
end syntax
69-
*/
70-
71-
/*
72-
Summary: Converts <Target> to a bool, if there is a valid conversion.
73-
Target: An expression that evaluates to a string.
74-
output: The converted value if there is a valid conversion the string <Target> to an bool.
75-
A string has a valid conversion to a bool if it evaluates to the string "true" or the string "false"
76-
77-
Example: put "true" as bool into tVar // tVar contains the boolean value true
78-
*/
79-
/*
80-
syntax AsBool is postfix operator with precedence 1
81-
<Target: Expression> "as" "bool"
82-
begin
83-
EvalStringAsBool(Target, output)
84-
end syntax
85-
*/
86-
--
5+
public foreign handler MCTypeEvalIsDefined(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
6+
public foreign handler MCTypeEvalIsNotDefined(in Target as optional any, out Value as bool) as undefined binds to "<builtin>"
877

888
/*
899
Summary: Determines whether <Target> is empty or not.
@@ -92,27 +12,27 @@ Target: Any expression
9212
output: Returns true if the given expression <Target> evaluates to the empty value of that type, and false otherwise.
9313

9414
*/
95-
/*
15+
9616
syntax IsEmpty is postfix operator with precedence 1
9717
<Target: Expression> "is" "empty"
9818
begin
9919
MCTypeEvalIsEmpty(Target, output)
10020
end syntax
101-
*/
21+
10222
/*
10323
Summary: Determines whether <Target> is empty or not.
10424

10525
Target: Any expression
10626
output: Returns false if the given expression <Target> evaluates to the empty value of that type, and true otherwise.
10727

10828
*/
109-
/*
29+
11030
syntax IsNotEmpty is postfix operator with precedence 1
11131
<Target: Expression> "is not" "empty"
11232
begin
11333
MCTypeEvalIsNotEmpty(Target, output)
11434
end syntax
115-
*/
35+
11636
--
11737

11838
/*
@@ -122,27 +42,27 @@ Target: Any expression
12242
output: Returns true if the given expression <Target> is defined, and false if not.
12343

12444
*/
125-
/*
45+
12646
syntax IsDefined is postfix operator with precedence 1
12747
<Target: Expression> "is" "defined"
12848
begin
12949
MCTypeEvalIsDefined(Target, output)
13050
end syntax
131-
*/
51+
13252
/*
13353
Summary: Determines whether <Target> is defined or not.
13454

13555
Target: Any expression
13656
output: Returns false if the given expression <Target> is defined, and true if not.
13757

13858
*/
139-
/*
59+
14060
syntax IsNotDefined is postfix operator with precedence 1
14161
<Target: Expression> "is not" "defined"
14262
begin
14363
MCTypeEvalIsNotDefined(Target, output)
14464
end syntax
145-
*/
65+
14666
--
14767

14868
end module

toolchain/lc-compile/lc-compile.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@
858858
);
859859
runOnlyForDeploymentPostprocessing = 0;
860860
shellPath = /bin/sh;
861-
shellScript = "cd src\necho \"${BUILT_PRODUCTS_DIR}/modules\"\nmkdir -p \"_G_\"\nmkdir -p \"${BUILT_PRODUCTS_DIR}/modules\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/canvas.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/engine.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/widget.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -bootstrap -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" -template \"${SRCROOT}/src/grammar.g\" -output \"${SRCROOT}/src/grammar_full.g\" \"${SRCROOT}/../../libscript/src/string.mlc\" \"${SRCROOT}/../../libscript/src/binary.mlc\" \"${SRCROOT}/../../libscript/src/arithmetic.mlc\" \"${SRCROOT}/../../libscript/src/bitwise.mlc\" \"${SRCROOT}/../../libscript/src/byte.mlc\" \"${SRCROOT}/../../libscript/src/char.mlc\" \"${SRCROOT}/../../libscript/src/list.mlc\" \"${SRCROOT}/../../libscript/src/type-convert.mlc\" \"${SRCROOT}/../../libscript/src/logic.mlc\" \"${SRCROOT}/../../libscript/src/sort.mlc\" \"${SRCROOT}/../../libscript/src/array.mlc\" \"${SRCROOT}/../../libscript/src/math.mlc\" \"${SRCROOT}/../../libscript/src/math-foundation.mlc\" \"${SRCROOT}/../../engine/src/canvas.mlc\" \"${SRCROOT}/../../engine/src/engine.mlc\" \"${SRCROOT}/../../engine/src/widget.mlc\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
861+
shellScript = "cd src\necho \"${BUILT_PRODUCTS_DIR}/modules\"\nmkdir -p \"_G_\"\nmkdir -p \"${BUILT_PRODUCTS_DIR}/modules\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/canvas.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/engine.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" \"${SRCROOT}/../../engine/src/widget.mlc\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -bootstrap -modulepath \"${BUILT_PRODUCTS_DIR}/modules\" -template \"${SRCROOT}/src/grammar.g\" -output \"${SRCROOT}/src/grammar_full.g\" \"${SRCROOT}/../../libscript/src/string.mlc\" \"${SRCROOT}/../../libscript/src/binary.mlc\" \"${SRCROOT}/../../libscript/src/arithmetic.mlc\" \"${SRCROOT}/../../libscript/src/bitwise.mlc\" \"${SRCROOT}/../../libscript/src/byte.mlc\" \"${SRCROOT}/../../libscript/src/char.mlc\" \"${SRCROOT}/../../libscript/src/list.mlc\" \"${SRCROOT}/../../libscript/src/type-convert.mlc\" \"${SRCROOT}/../../libscript/src/logic.mlc\" \"${SRCROOT}/../../libscript/src/sort.mlc\" \"${SRCROOT}/../../libscript/src/array.mlc\" \"${SRCROOT}/../../libscript/src/math.mlc\" \"${SRCROOT}/../../libscript/src/math-foundation.mlc\" \"${SRCROOT}/../../engine/src/canvas.mlc\" \"${SRCROOT}/../../engine/src/engine.mlc\" \"${SRCROOT}/../../engine/src/widget.mlc\" \"${SRCROOT}/../../libscript/src/type.mlc\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
862862
showEnvVarsInLog = 0;
863863
};
864864
/* End PBXShellScriptBuildPhase section */

toolchain/lc-compile/src/module-helper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ extern void (*MCMathEvalRealToPowerOfReal)();
7979
extern void (*MCMathFoundationExecRoundRealToNearest)();
8080
extern void (*MCSortExecSortListAscendingText)();
8181
extern void (*MCStringEvalConcatenate)();
82+
extern void (*MCTypeEvalIsEmpty)();
8283
extern void (*MCTypeConvertExecSplitStringByDelimiter)();
8384

8485
// Pull in a reference to all of the module-*.cpp objects too
@@ -96,6 +97,7 @@ void *g_builtin_ptrs[] =
9697
&MCMathFoundationExecRoundRealToNearest,
9798
&MCSortExecSortListAscendingText,
9899
&MCStringEvalConcatenate,
100+
&MCTypeEvalIsEmpty,
99101
&MCTypeConvertExecSplitStringByDelimiter
100102
};
101103

toolchain/lc-compile/test.mlc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public handler test()
1414
-- structural
1515
testRepeat(tResults)
1616
testLogicStructural(tResults)
17-
17+
18+
--com.livecode.type
19+
testType(tResults)
20+
1821
--com.livecode.array
1922
testArray(tResults)
2023

@@ -125,6 +128,9 @@ end handler
125128

126129
public handler testType(inout xResults as list)
127130

131+
variable tVar
132+
testLog("Type", "IsNotDefined", tVar is not defined, xResults)
133+
128134
end handler
129135

130136
public handler testMathFoundation(inout xResults as list)
@@ -639,8 +645,8 @@ public handler testList(inout xResults as list)
639645
testLog("List", "Is", tSubList is ["abcd", 1], xResults)
640646
testLog("List", "IsNot", tSubList is not ["Abcd", 1], xResults)
641647

642-
testLog("List", "IsIn", "abcd" is in tSubList, xResults)
643-
testLog("List", "IsNotIn", not tSubList is in tTestList, xResults)
648+
--testLog("List", "IsIn", "abcd" is in tSubList, xResults)
649+
--testLog("List", "IsNotIn", not tSubList is in tTestList, xResults)
644650

645651
variable tString as string
646652
pop front of tSubList into tString

0 commit comments

Comments
 (0)