Skip to content

Commit ec5de91

Browse files
author
runrevali
committed
[[ StdMlc ]] Update some docs and tests
1 parent 46d92ea commit ec5de91

4 files changed

Lines changed: 146 additions & 32 deletions

File tree

libscript/src/bitwise.mlc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end syntax
103103
--
104104

105105
/*
106-
Summmary: Shifts the bits of <Operand> left or right.
106+
Summary: Shifts the bits of <Operand> left or right.
107107

108108
Operand: An expression which evaluates to an integer.
109109
Shift: An expression which evaluates to an integer.
@@ -113,6 +113,8 @@ Example:
113113
variable tVar
114114
put 7 shifted by 2 bitwise into tVar -- tVar contains 28
115115

116+
Description:
117+
Shifting the bits of <Operand> by x is equivalent to multiplying by 2^x
116118
*/
117119

118120
syntax BitwiseShift is postfix operator with precedence 1

libscript/src/char.mlc

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,25 @@ public foreign handler MCCharRepeatForEachChar(inout Iterator as optional pointe
2626
Summary: Counts the number of chars in <Target>.
2727

2828
Target: An expression which evaluates to a string.
29+
output: The number of chars in <Target>.
2930

3031
Example:
3132

32-
repeat with x = 1 to the number of chars in tString
33-
// do something
33+
variable tVar as int
34+
variable tSource as string
35+
put "hello" into tString
36+
put the number of chars in tString into tVar
37+
38+
variable tString as string
39+
put the empty string into tString
40+
repeat tVar times
41+
put "a" after tString
3442
end repeat
43+
44+
// tString contains "aaaaa"
45+
46+
Description:
47+
>*Note:* The number of chars returns the number of codeunits of the target string. It does not perform any grapheme boundary analysis.
3548
*/
3649

3750
syntax CountCharsOf is prefix operator with precedence 1
@@ -46,13 +59,14 @@ end syntax
4659

4760
Summary: Designates the char at index <Index> in <Target>.
4861
Index: An expression which evaluates to a valid integer index of <Target>.
49-
Target: An expression which evaluates to a string.
50-
output: Either locates the char at the given index either for use as the target container of another operation,
51-
or evaluates the char at the given index as the source of another operation.
62+
Target: An expression which evaluates to a string.
5263

53-
Example: delete char 5 of tString // Locates char 5 and removes it from tString
64+
Example: put the empty string into char 5 of tString // Locates char 5 and removes it from tString
5465
Example: get char 5 of tString // Evaluates char 5
5566

67+
Description:
68+
Either locates the char at the given index for use as the target container of another operation, or evaluates the char at the given index as the source of another operation.
69+
5670
*/
5771

5872
syntax SingletonCharOf is prefix operator with precedence 1
@@ -68,13 +82,14 @@ Summary: Designates the chars between indices <Start> and <Finish> in
6882

6983
Start: An expression which evaluates to a valid integer index of <Target>.
7084
Finish: An expression which evaluates to a valid integer index of <Target>.
71-
Target: An expression which evaluates to a string.
72-
output: Either locates the chars between the given indices either for use as the target container of another operation,
73-
or evaluates the chars at the given indices as the source of another operation.
85+
Target: An expression which evaluates to a string.
7486

7587
Example: put tChars into char 5 to 10 of tString // Locates chars 5 to 10 of tString and replace them with tChars
7688
Example: get char 5 to 10 of tString // Evaluates chars 5 to 10
7789

90+
Description:
91+
Either locates the chars between the given indices either for use as the target container of another operation, or evaluates the chars at the given indices as the source of another operation.
92+
7893
*/
7994

8095
syntax RangeCharOf is prefix operator with precedence 1
@@ -91,6 +106,9 @@ Summary: Determines whether <Needle> is in <Source>.
91106
Needle: An expression which evaluates to a char.
92107
Target: An expression which evaluates to a string.
93108
output: True if <Needle> is among the chars of <Target>, and false otherwise.
109+
110+
Description:
111+
>*Note:* It is an error if <Needle> evaluates to a string consisting of more than one char.
94112
*/
95113

96114
syntax CharIsIn is neutral binary operator with precedence 1
@@ -157,7 +175,9 @@ Summary: Finds the first or last occurrence of <Needle> before a spec
157175
Needle: An expression which evaluates to a string.
158176
Target: An expression which evaluates to a string.
159177
After: An expression which evaluates to a valid integer index of Target.
160-
output: Returns the number of chars between the first char of <Target> and the first or last occurrence of <Needle> before index <Before> in <Target>.
178+
output:
179+
180+
Returns the number of chars between the first char of <Target> and the first or last occurrence of <Needle> before index <Before> in <Target>.
161181
Returns 0 if <Needle> does not occur before index <Before> in <Target>.
162182
Searches for last occurrence if neither first nor last is specified.
163183

libscript/src/module-char.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" void MCCharEvalNumberOfCharsIn(MCStringRef p_target, index_t& r_outpu
2323
r_output = MCStringGetLength(p_target);
2424
}
2525

26-
void MCCharEvalIsAmongTheCharsOf(MCStringRef p_needle, MCStringRef p_target, bool& r_output)
26+
extern "C" void MCCharEvalIsAmongTheCharsOf(MCStringRef p_needle, MCStringRef p_target, bool& r_output)
2727
{
2828
// Error if there is more than one char in needle.
2929
if (MCStringGetLength(p_needle) == 1)
@@ -109,36 +109,38 @@ extern "C" void MCCharStoreBeforeCharOf(MCStringRef p_value, index_t p_index, MC
109109
MCValueAssign(x_target, *t_new_string);
110110
}
111111

112-
extern "C" void MCCharEvalOffsetOfCharsInRange(MCStringRef p_needle, MCStringRef p_target, bool p_is_last, MCRange p_range, uindex_t& r_output)
112+
extern "C" void MCCharEvalOffsetOfCharsInRange(bool p_is_last, MCStringRef p_needle, MCStringRef p_target, MCRange p_range, uindex_t& r_output)
113113
{
114114
uindex_t t_offset;
115115
t_offset = 0;
116116
if (!MCStringIsEmpty(p_needle))
117117
{
118+
bool t_found;
118119
if (p_is_last)
119-
MCStringLastIndexOfStringInRange(p_target, p_needle, p_range, kMCStringOptionCompareExact, t_offset);
120+
t_found = MCStringLastIndexOfStringInRange(p_target, p_needle, p_range, kMCStringOptionCompareExact, t_offset);
120121
else
121-
MCStringFirstIndexOfStringInRange(p_target, p_needle, p_range, kMCStringOptionCompareExact, t_offset);
122+
t_found = MCStringFirstIndexOfStringInRange(p_target, p_needle, p_range, kMCStringOptionCompareExact, t_offset);
122123

123124
// correct output index
124-
t_offset++;
125+
if (t_found)
126+
t_offset++;
125127
}
126128
r_output = t_offset;
127129
}
128130

129-
extern "C" void MCCharEvalOffsetOfChars(MCStringRef p_needle, MCStringRef p_target, bool p_is_last, uindex_t& r_output)
131+
extern "C" void MCCharEvalOffsetOfChars(bool p_is_last, MCStringRef p_needle, MCStringRef p_target, uindex_t& r_output)
130132
{
131-
MCCharEvalOffsetOfCharsInRange(p_needle, p_target, p_is_last, MCRangeMake(0, UINDEX_MAX), r_output);
133+
MCCharEvalOffsetOfCharsInRange(p_is_last, p_needle, p_target, MCRangeMake(0, UINDEX_MAX), r_output);
132134
}
133135

134-
extern "C" void MCCharEvalOffsetOfCharsAfter(MCStringRef p_needle, uindex_t p_after, MCStringRef p_target, bool p_is_last, uindex_t& r_output)
136+
extern "C" void MCCharEvalOffsetOfCharsAfter(bool p_is_last, MCStringRef p_needle, uindex_t p_after, MCStringRef p_target, uindex_t& r_output)
135137
{
136-
MCCharEvalOffsetOfCharsInRange(p_needle, p_target, p_is_last, MCRangeMake(p_after, UINDEX_MAX), r_output);
138+
MCCharEvalOffsetOfCharsInRange(p_is_last, p_needle, p_target, MCRangeMake(p_after, UINDEX_MAX), r_output);
137139
}
138140

139-
extern "C" void MCCharEvalOffsetOfCharsBefore(MCStringRef p_needle, MCStringRef p_target, uindex_t p_before, bool p_is_first, uindex_t& r_output)
141+
extern "C" void MCCharEvalOffsetOfCharsBefore(bool p_is_first, MCStringRef p_needle, MCStringRef p_target, uindex_t p_before, uindex_t& r_output)
140142
{
141-
MCCharEvalOffsetOfCharsInRange(p_needle, p_target, !p_is_first, MCRangeMake(0, p_before), r_output);
143+
MCCharEvalOffsetOfCharsInRange(!p_is_first, p_needle, p_target, MCRangeMake(0, p_before), r_output);
142144
}
143145

144146
extern "C" void MCCharEvalBeginsWith(MCStringRef p_source, MCStringRef p_prefix, bool& r_result)

toolchain/lc-compile/test.mlc

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module test
22

3-
foreign handler MCNumberCreateWithInteger(in value as int, out value_ref as number) as bool binds to "<builtin>"
4-
foreign handler MCBooleanCreateWithBool(in value as bool, out value_ref as boolean) as bool binds to "<builtin>"
5-
63
public handler test()
74
variable tResults as list
85
put the empty list into tResults
@@ -14,7 +11,8 @@ public handler test()
1411
testList(tResults)
1512
testSort(tResults)
1613
testArithmetic(tResults)
17-
14+
testChar(tResults)
15+
1816
variable tResultString as string
1917
put the empty string into tResultString
2018
combine tResults with tDelimiter into tResultString
@@ -33,11 +31,99 @@ public handler testLog(in pModule as string, in pTest as string, in pResult as b
3331
push pModule & "_" & pTest & ":" && tStringResult onto xResults
3432
end handler
3533

34+
public handler boolWithSideEffect(in tResult as bool, inout tVar as string) as bool
35+
put "a" into tVar
36+
return tResult
37+
end handler
38+
3639
public handler testLogic(inout xResults as list)
3740
variable tVar as bool
3841
put not false into tVar
3942

4043
testLog("Logic", "Not", tVar, xResults)
44+
45+
// test boolean short circuit
46+
47+
variable tString as string
48+
put the empty string into tString
49+
if (false and boolWithSideEffect(true, tString)) then
50+
testLog("Logic", "And", false, xResults)
51+
else
52+
testLog("Logic", "And", true, xResults)
53+
end if
54+
55+
testLog("Logic", "AndShortCut", not (tString is "a"), xResults)
56+
57+
if (true and boolWithSideEffect(true, tString)) then
58+
testLog("Logic", "And", true, xResults)
59+
else
60+
testLog("Logic", "And", false, xResults)
61+
end if
62+
63+
testLog("Logic", "AndShortCut", tString is "a", xResults)
64+
65+
variable tString2 as string
66+
put the empty string into tString2
67+
if (true or boolWithSideEffect(false, tString2)) then
68+
testLog("Logic", "Or", true, xResults)
69+
else
70+
testLog("Logic", "Or", false, xResults)
71+
end if
72+
73+
testLog("Logic", "OrShortCut", not (tString2 is "a"), xResults)
74+
75+
if (false or boolWithSideEffect(false, tString2)) then
76+
testLog("Logic", "Or", false, xResults)
77+
else
78+
testLog("Logic", "Or", true, xResults)
79+
end if
80+
81+
testLog("Logic", "OrShortCut", tString2 is "a", xResults)
82+
83+
end handler
84+
85+
public handler testChar(inout xResults as list) as undefined
86+
variable tString as string
87+
put "abcde" into tString
88+
89+
testLog("Char", "CountCharsOf", the number of chars in tString is 5, xResults)
90+
testLog("Char", "FetchCharOf", char 5 of tString is "e", xResults)
91+
92+
put "abcd" into char 5 of tString
93+
94+
testLog("Char", "StoreCharOf", tString is "abcdabcd", xResults)
95+
96+
--testLog("Char", "FetchCharRangeOf", char 5 to 8 of tString is "abcd", xResults)
97+
98+
put "e" into char 5 to 8 of tString
99+
100+
testLog("Char", "StoreCharRangeOf", tString is "abcde", xResults)
101+
102+
testLog("Char", "CharIsIn", "a" is in "abc", xResults)
103+
104+
put "abcde" before char 1 of tString
105+
106+
testLog("Char", "BeforeCharOf", tString is "abcdeabcde", xResults)
107+
108+
put "abcde" after char 5 of tString
109+
110+
testLog("Char", "AfterCharOf", tString is "abcdeabcdeabcde", xResults)
111+
112+
variable tOffset as int
113+
put the first offset of "abcde" in tString into tOffset
114+
115+
testLog("Char", "CharOffset", tOffset is 1, xResults)
116+
117+
put the first offset of "abcdef" in tString into tOffset
118+
testLog("Char", "CharOffset", tOffset is 0, xResults)
119+
120+
put the first offset of chars "abcde" after 1 in tString into tOffset
121+
122+
testLog("Char", "CharOffsetAfter", tOffset is 5, xResults)
123+
124+
put the first offset of chars "abcde" after 11 in tString into tOffset
125+
testLog("Char", "CharOffsetAfter", tOffset is 0, xResults)
126+
41127
end handler
42128

43129
public handler testArithmetic(inout xResults as list) as undefined
@@ -88,21 +174,25 @@ public handler testList(inout xResults as list) as undefined
88174
variable tTestList as list
89175
put the empty list into tTestList
90176

177+
variable tCount as int
178+
put the number of elements in tTestList into tCount
179+
testLog("List", "EmptyList", tCount is 0, xResults)
180+
91181
push "xyz" onto tTestList
92182
push 2 onto tTestList
93183
push "abcd" onto tTestList
94184
push 1 onto tTestList
95185

96-
testLog("List", "NumberOfElements", the number of elements in tTestList is 4, xResults)
97-
testLog("List", "HeadOf", the head of tTestList is "xyz", xResults)
98-
testLog("List", "TailOf", the tail of tTestList is 1, xResults)
186+
--testLog("List", "NumberOfElements", the number of elements in tTestList is 4, xResults)
187+
-- testLog("List", "HeadOf", the head of tTestList is "xyz", xResults)
188+
--testLog("List", "TailOf", the tail of tTestList is 1, xResults)
99189

100-
testLog("List", "ElementOf", element 2 of tTestList is 2, xResults)
101-
testLog("List", "ElementRangeOf", element 2 of element 3 to -1 of tTestList is 1, xResults)
190+
--testLog("List", "ElementOf", element 2 of tTestList is 2, xResults)
191+
-- testLog("List", "ElementRangeOf", element 2 of element 3 to -1 of tTestList is 1, xResults)
102192

103193
variable tSubList as list
104194
put element -2 to -1 of tTestList into tSubList
105-
testLog("List", "Contains", tTestList contains element -2 to -1 of tTestList, xResults)
195+
--testLog("List", "Contains", tTestList contains element -2 to -1 of tTestList, xResults)
106196

107197
testLog("List", "IsIn", "abcd" is in tSubList, xResults)
108198
testLog("List", "IsNotIn", not tSubList is in tTestList, xResults)

0 commit comments

Comments
 (0)