Skip to content

Commit b81288f

Browse files
author
runrevali
committed
[[ StdMlc ]] Add type conversion functions
1 parent 34d2013 commit b81288f

File tree

5 files changed

+200
-25
lines changed

5 files changed

+200
-25
lines changed

libscript/src/arithmetic.mlc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public foreign handler MCArithmeticEvalNotEqualToInteger(in Left as int, in Righ
6666
public foreign handler MCArithmeticEvalNotEqualToReal(in Left as double, in Right as double, out Value as bool) as undefined binds to "<builtin>"
6767
public foreign handler MCArithmeticEvalNotEqualToNumber(in Left as number, in Right as number, out Value as bool) as undefined binds to "<builtin>"
6868

69+
public foreign handler MCArithmeticEvalNumberFormattedAsString(in Target as number, out Value as string) as undefined binds to "<builtin>"
70+
public foreign handler MCArithmeticEvalStringParsedAsNumber(in Target as string, out Value as number) as undefined binds to "<builtin>"
71+
6972
--
7073

7174
/*
@@ -379,6 +382,10 @@ Left: An expression that evaluates to a number.
379382
Right: An expression that evaluates to a number.
380383

381384
Returns: True if <Left> is less than <Right>, and false otherwise.
385+
386+
Description:
387+
388+
Tags: Math
382389
*/
383390

384391
syntax NumberIsLessThanNumber is neutral binary operator with precedence 3
@@ -393,6 +400,10 @@ Left: An expression that evaluates to a number.
393400
Right: An expression that evaluates to a number.
394401

395402
Returns: True if <Left> is less than or equal to <Right>, and false otherwise.
403+
404+
Description:
405+
406+
Tags: Math
396407
*/
397408

398409

@@ -410,6 +421,10 @@ Left: An expression that evaluates to a number.
410421
Right: An expression that evaluates to a number.
411422

412423
Returns: True if <Left> is equal to <Right>, and false otherwise.
424+
425+
Description:
426+
427+
Tags: Math
413428
*/
414429

415430
syntax NumberIsEqualToNumber is neutral binary operator with precedence 4
@@ -418,16 +433,95 @@ begin
418433
MCArithmeticEvalEqualToNumber(Left, Right, output)
419434
end syntax
420435

436+
/*
437+
Summary: Determines if <Left> is <Right>.
438+
Left: An expression that evaluates to a number.
439+
Right: An expression that evaluates to a number.
440+
441+
Returns: True if <Left> is equal to <Right>, and false otherwise.
442+
443+
Description:
444+
Identical to the <NumberIsEqualToNumber> operator.
445+
446+
References: NumberIsEqualToNumber (operator)
447+
448+
Tags: Math
449+
*/
450+
421451
syntax NumberIsNumber is neutral binary operator with precedence 5
422452
<Left: Expression> "is" <Right: Expression>
423453
begin
424454
MCArithmeticEvalEqualToNumber(Left, Right, output)
425455
end syntax
426456

457+
/*
458+
Summary: Determines if <Left> is not <Right>.
459+
Left: An expression that evaluates to a number.
460+
Right: An expression that evaluates to a number.
461+
462+
Returns: True if <Left> is not equal to <Right>, and false otherwise.
463+
464+
Description:
465+
Returns the opposite of the <NumberIsNumber> operator.
466+
467+
References: NumberIsNumber (operator)
468+
469+
Tags: Math
470+
*/
471+
472+
427473
syntax NumberIsNotNumber is neutral binary operator with precedence 5
428474
<Left: Expression> "is not" <Right: Expression>
429475
begin
430476
MCArithmeticEvalNotEqualToNumber(Left, Right, output)
431477
end syntax
432478

479+
480+
/*
481+
Summary: Formats a numeric value as a string
482+
483+
Operand: An expression that evaluates to a number.
484+
485+
Returns: <Operand> formatted as a string.
486+
487+
Example:
488+
variable tString as string
489+
variable tNum as number
490+
put 5 into tNum
491+
put tNum formatted as string into tString -- tString contains "5"
492+
493+
Description:
494+
Use <NumberFormattedAsString> when you want to manipulate a numeric value as text.
495+
*/
496+
497+
syntax NumberFormattedAsString is postfix operator with precedence 1
498+
<Operand: Expression> "formatted" "as" "string"
499+
begin
500+
MCArithmeticEvalNumberFormattedAsString(Operand, output)
501+
end syntax
502+
503+
/*
504+
Summary: Parses a string as an number
505+
506+
Operand: An expression that evaluates to a string.
507+
508+
Returns: <Operand> parsed as a number.
509+
510+
Example:
511+
variable tString as string
512+
variable tNum as number
513+
put "5.6" into tString
514+
put tString parsed as number into tNum -- tNum contains 5.6
515+
516+
Description:
517+
Use <StringParsedAsNumber> when you want to interpret text numerically.
518+
519+
>*Note:* Any input which cannot be parsed as a number will cause an error to be thrown.
520+
*/
521+
syntax StringParsedAsNumber is postfix operator with precedence 1
522+
<Operand: Expression> "parsed" "as" "number"
523+
begin
524+
MCArithmeticEvalStringParsedAsNumber(Operand, output)
525+
end syntax
526+
433527
end module

libscript/src/logic.mlc

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ public foreign handler MCLogicEvalNot(in Operand as bool, out Value as bool) as
66
public foreign handler MCLogicEvalIsEqualTo(in Left as bool, in Right as bool, out Value as bool) as undefined binds to "<builtin>"
77
public foreign handler MCLogicEvalIsNotEqualTo(in Left as bool, in Right as bool, out Value as bool) as undefined binds to "<builtin>"
88

9+
public foreign handler MCLogicEvalBoolFormattedAsString(in Target as bool, out Value as string) as undefined binds to "<builtin>"
10+
public foreign handler MCLogicEvalStringParsedAsBool(in Target as string, out Value as bool) as undefined binds to "<builtin>"
11+
912
--
1013

1114
/*
@@ -69,8 +72,8 @@ end syntax
6972
/*
7073
Summary: Determines whether <Left> and <Right> are equal or not.
7174

72-
Left: An expression which evaluates to a bool.
73-
Right: An expression which evaluates to a bool.
75+
Left: An expression which evaluates to a boolean value.
76+
Right: An expression which evaluates to a boolean value.
7477

7578

7679
Returns: Returns true if <Left> evaluates to the same value as <Right>.
@@ -91,8 +94,8 @@ end syntax
9194
/*
9295
Summary: Determines whether <Left> and <Right> are equal or not.
9396

94-
Left: An expression which evaluates to a bool.
95-
Right: An expression which evaluates to a bool.
97+
Left: An expression which evaluates to a boolean value.
98+
Right: An expression which evaluates to a boolean value.
9699

97100

98101
Returns: Returns true if <Left> evaluates to the opposite boolean value as <Right>.
@@ -110,4 +113,51 @@ begin
110113
MCLogicEvalIsNotEqualTo(Left, Right, output)
111114
end syntax
112115

116+
/*
117+
Summary: Formats a boolean value as a string
118+
119+
Operand: An expression that evaluates to a boolean value.
120+
121+
Returns: "true" or "false"
122+
123+
Example:
124+
variable tString as string
125+
variable tBool as boolean
126+
put (false is not not true) is false into tBool
127+
put tBool formatted as string into tString -- tString contains "true"
128+
129+
Description:
130+
Use <BooleanFormattedAsString> when you want to manipulate a boolean value as text.
131+
*/
132+
133+
syntax BooleanFormattedAsString is postfix operator with precedence 1
134+
<Operand: Expression> "formatted" "as" "string"
135+
begin
136+
MCLogicEvalBoolFormattedAsString(Operand, output)
137+
end syntax
138+
139+
/*
140+
Summary: Parses a string as a boolean value
141+
142+
Operand: An expression that evaluates to a string.
143+
144+
Returns: true or false
145+
146+
Example:
147+
variable tString as string
148+
variable tBool as boolean
149+
put "false" into tString
150+
put tString parsed as boolean into tBool -- tBool contains false
151+
152+
Description:
153+
Use <StringParsedAsBoolean> when you want to determine if a string contains "true" or "false"
154+
155+
>*Note:* Only the strings "true" and "false" will parse to give a boolean value. Any other input will cause an error to be thrown.
156+
*/
157+
syntax StringParsedAsBoolean is postfix operator with precedence 1
158+
<Operand: Expression> "parsed" "as" "boolean"
159+
begin
160+
MCLogicEvalStringParsedAsBool(Operand, output)
161+
end syntax
162+
113163
end module

libscript/src/module-arithmetic.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,22 @@ extern "C" MC_DLLEXPORT void MCArithmeticEvalNotEqualToNumber(MCNumberRef p_left
446446
{
447447
r_output = MCNumberFetchAsReal(p_left) != MCNumberFetchAsReal(p_right);
448448
}
449+
450+
////////////////////////////////////////////////////////////////////////////////////////////////////
451+
452+
extern "C" MC_DLLEXPORT void MCArithmeticEvalNumberFormattedAsString(MCNumberRef p_operand, MCStringRef& r_output)
453+
{
454+
MCStringFormat(r_output, "%f", MCNumberFetchAsReal(p_operand));
455+
}
456+
457+
extern "C" MC_DLLEXPORT void MCArithmeticEvalStringParsedAsNumber(MCStringRef p_operand, MCNumberRef& r_output)
458+
{
459+
double t_converted;
460+
if (!MCTypeConvertStringToReal(p_operand, t_converted))
461+
return;
462+
463+
if (!MCNumberCreateWithReal(t_converted, r_output))
464+
return;
465+
}
466+
467+

libscript/src/module-logic.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ extern "C" MC_DLLEXPORT void MCLogicEvalIsEqualTo(bool p_left, bool p_right, boo
2929
extern "C" MC_DLLEXPORT void MCLogicEvalIsNotEqualTo(bool p_left, bool p_right, bool& r_result)
3030
{
3131
r_result = (p_left != p_right);
32+
}
33+
34+
extern "C" MC_DLLEXPORT void MCLogicEvalBooleanFormattedAsString(bool p_operand, MCStringRef& r_output)
35+
{
36+
r_output = MCValueRetain(p_operand ? kMCTrueString : kMCFalseString);
37+
}
38+
39+
extern "C" MC_DLLEXPORT void MCLogicEvalStringParsedAsBoolean(MCStringRef p_operand, bool& r_output)
40+
{
41+
if (MCStringIsEqualTo(p_operand, kMCTrueString, kMCStringOptionCompareCaseless))
42+
r_output = true;
43+
else if (MCStringIsEqualTo(p_operand, kMCFalseString, kMCStringOptionCompareCaseless))
44+
r_output = false;
45+
else
46+
// Throw
47+
return;
3248
}

libscript/src/type.mlc

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
/*
2-
This module specifies the syntax for the basic type operations available in modular LiveCode
3-
*/
4-
51
module com.livecode.type
62

7-
public foreign handler EvalBoolAsString(in Target as bool, out Value as string) as undefined binds to "<builtin>"
8-
public foreign handler EvalRealAsString(in Target as real, out Value as string) as undefined binds to "<builtin>"
9-
public foreign handler EvalIntAsString(in Target as int, out Value as string) as undefined binds to "<builtin>"
10-
public foreign handler EvalIntAsReal(in Target as int, out Value as real) as undefined binds to "<builtin>"
11-
public foreign handler EvalStringAsBool(in Target as string, out Value as bool) as undefined binds to "<builtin>"
12-
public foreign handler EvalStringAsReal(in Target as string, out Value as real) as undefined binds to "<builtin>"
13-
public foreign handler EvalStringAsInt(in Target as string, out Value as int) as undefined binds to "<builtin>"
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>"
146

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+
*/
1511
public foreign handler MCTypeEvalIsEmpty(in Target as any, out Value as bool) as undefined binds to "<builtin>"
1612
public foreign handler MCTypeEvalIsNotEmpty(in Target as any, out Value as bool) as undefined binds to "<builtin>"
1713
public foreign handler MCTypeEvalIsDefined(in Target as any, out Value as bool) as undefined binds to "<builtin>"
@@ -30,7 +26,7 @@ Example: put true as string into tVar // tVar contains the string "true"
3026

3127
/*
3228
syntax AsString is postfix operator with precedence 1
33-
<Target: Expression> "as" "string"
29+
<Target: Expression> "formatted" "as" "string"
3430
begin
3531
EvalBoolAsString(Target, output)
3632
EvalIntAsString(Target, output)
@@ -96,27 +92,27 @@ Target: Any expression
9692
output: Returns true if the given expression <Target> evaluates to the empty value of that type, and false otherwise.
9793

9894
*/
99-
95+
/*
10096
syntax IsEmpty is postfix operator with precedence 1
10197
<Target: Expression> "is" "empty"
10298
begin
10399
MCTypeEvalIsEmpty(Target, output)
104100
end syntax
105-
101+
*/
106102
/*
107103
Summary: Determines whether <Target> is empty or not.
108104

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

112108
*/
113-
109+
/*
114110
syntax IsNotEmpty is postfix operator with precedence 1
115111
<Target: Expression> "is not" "empty"
116112
begin
117113
MCTypeEvalIsNotEmpty(Target, output)
118114
end syntax
119-
115+
*/
120116
--
121117

122118
/*
@@ -126,27 +122,27 @@ Target: Any expression
126122
output: Returns true if the given expression <Target> is defined, and false if not.
127123

128124
*/
129-
125+
/*
130126
syntax IsDefined is postfix operator with precedence 1
131127
<Target: Expression> "is" "defined"
132128
begin
133129
MCTypeEvalIsDefined(Target, output)
134130
end syntax
135-
131+
*/
136132
/*
137133
Summary: Determines whether <Target> is defined or not.
138134

139135
Target: Any expression
140136
output: Returns false if the given expression <Target> is defined, and true if not.
141137

142138
*/
143-
139+
/*
144140
syntax IsNotDefined is postfix operator with precedence 1
145141
<Target: Expression> "is not" "defined"
146142
begin
147143
MCTypeEvalIsNotDefined(Target, output)
148144
end syntax
149-
145+
*/
150146
--
151147

152-
end module
148+
end module

0 commit comments

Comments
 (0)