Skip to content

Commit 8d6b3fa

Browse files
author
runrevali
committed
[[ StdMlc ]] Various fixes and docs updates
1 parent 9b1328c commit 8d6b3fa

File tree

4 files changed

+101
-32
lines changed

4 files changed

+101
-32
lines changed

libscript/src/arithmetic.mlc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ public foreign handler MCArithmeticEvalIntegerIsLessThanOrEqualToInteger(in Left
5959
public foreign handler MCArithmeticEvalRealIsLessThanOrEqualToReal(in Left as double, in Right as double, out Value as bool) as undefined binds to "<builtin>"
6060
public foreign handler MCArithmeticEvalNumberIsLessThanOrEqualToNumber(in Left as number, in Right as number, out Value as bool) as undefined binds to "<builtin>"
6161

62-
6362
public foreign handler MCArithmeticEvalEqualToInteger(in Left as int, in Right as int, out Value as bool) as undefined binds to "<builtin>"
6463
public foreign handler MCArithmeticEvalEqualToReal(in Left as double, in Right as double, out Value as bool) as undefined binds to "<builtin>"
6564
public foreign handler MCArithmeticEvalEqualToNumber(in Left as number, in Right as number, out Value as bool) as undefined binds to "<builtin>"
65+
public foreign handler MCArithmeticEvalNotEqualToInteger(in Left as int, in Right as int, out Value as bool) as undefined binds to "<builtin>"
66+
public foreign handler MCArithmeticEvalNotEqualToReal(in Left as double, in Right as double, out Value as bool) as undefined binds to "<builtin>"
67+
public foreign handler MCArithmeticEvalNotEqualToNumber(in Left as number, in Right as number, out Value as bool) as undefined binds to "<builtin>"
68+
6669
--
6770

6871
/*
@@ -395,12 +398,20 @@ begin
395398
MCArithmeticEvalEqualToNumber(Left, Right, output)
396399
end syntax
397400

398-
syntax IsNumber is neutral binary operator with precedence 5
401+
syntax Is is neutral binary operator with precedence 5
399402
<Left: Expression> "is" <Right: Expression>
400403
begin
401404
MCArithmeticEvalEqualToInteger(Left, Right, output)
402405
MCArithmeticEvalEqualToReal(Left, Right, output)
403406
MCArithmeticEvalEqualToNumber(Left, Right, output)
404407
end syntax
405408

409+
syntax IsNot is neutral binary operator with precedence 5
410+
<Left: Expression> "is" "not" <Right: Expression>
411+
begin
412+
MCArithmeticEvalNotEqualToInteger(Left, Right, output)
413+
MCArithmeticEvalNotEqualToReal(Left, Right, output)
414+
MCArithmeticEvalNotEqualToNumber(Left, Right, output)
415+
end syntax
416+
406417
end module

libscript/src/module-arithmetic.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ extern "C" void MCArithmeticExecDivideIntegerByInteger(integer_t& x_target, inte
119119

120120
extern "C" void MCArithmeticExecDivideRealByReal(double& x_target, double p_number)
121121
{
122-
if (p_number > 0 && p_number < 1 && FLT_MAX * p_number < x_target)
123-
// overflow
124-
return;
125122
x_target /= p_number;
126123
}
127124

@@ -174,15 +171,15 @@ extern "C" void MCArithmeticEvalIntegerMinusInteger(integer_t p_left, integer_t
174171
MCArithmeticExecSubtractIntegerFromInteger(p_right, p_left);
175172

176173
//if no error
177-
r_output = p_right;
174+
r_output = p_left;
178175
}
179176

180177
extern "C" void MCArithmeticEvalRealMinusReal(double p_left, double p_right, double& r_output)
181178
{
182179
MCArithmeticExecSubtractRealFromReal(p_right, p_left);
183180

184181
//if no error
185-
r_output = p_right;
182+
r_output = p_left;
186183
}
187184

188185
extern "C" void MCArithmeticEvalNumberMinusNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
@@ -204,15 +201,15 @@ extern "C" void MCArithmeticEvalIntegerTimesInteger(integer_t p_left, integer_t
204201
MCArithmeticExecMultiplyIntegerByInteger(p_left, p_right);
205202

206203
//if no error
207-
r_output = p_right;
204+
r_output = p_left;
208205
}
209206

210207
extern "C" void MCArithmeticEvalRealTimesReal(double p_left, double p_right, double& r_output)
211208
{
212209
MCArithmeticExecMultiplyRealByReal(p_left, p_right);
213210

214211
//if no error
215-
r_output = p_right;
212+
r_output = p_left;
216213
}
217214

218215
extern "C" void MCArithmeticEvalNumberTimesNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
@@ -234,15 +231,15 @@ extern "C" void MCArithmeticEvalIntegerOverInteger(integer_t p_left, integer_t p
234231
MCArithmeticExecDivideIntegerByInteger(p_left, p_right);
235232

236233
//if no error
237-
r_output = p_right;
234+
r_output = p_left;
238235
}
239236

240237
extern "C" void MCArithmeticEvalRealOverReal(double p_left, double p_right, double& r_output)
241238
{
242239
MCArithmeticExecDivideRealByReal(p_left, p_right);
243240

244241
//if no error
245-
r_output = p_right;
242+
r_output = p_left;
246243
}
247244

248245
extern "C" void MCArithmeticEvalNumberOverNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
@@ -434,3 +431,18 @@ extern "C" void MCArithmeticEvalEqualToNumber(MCNumberRef p_left, MCNumberRef p_
434431
{
435432
r_output = MCNumberFetchAsReal(p_left) == MCNumberFetchAsReal(p_right);
436433
}
434+
435+
extern "C" void MCArithmeticEvalNotEqualToInteger(integer_t p_left, integer_t p_right, bool& r_output)
436+
{
437+
r_output = p_left != p_right;
438+
}
439+
440+
extern "C" void MCArithmeticEvalNotEqualToReal(double p_left, double p_right, bool& r_output)
441+
{
442+
r_output = p_left != p_right;
443+
}
444+
445+
extern "C" void MCArithmeticEvalNotEqualToNumber(MCNumberRef p_left, MCNumberRef p_right, bool& r_output)
446+
{
447+
r_output = MCNumberFetchAsReal(p_left) != MCNumberFetchAsReal(p_right);
448+
}

libscript/src/string.mlc

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Left: An expression which evaluates to a string.
9797
Right: An expression which evaluates to a string.
9898
output: The result of concatenating <Left> and <Right>.
9999

100+
Example:
101+
variable tVar as string
102+
put "car" & "pet" into tVar -- tVar contains "carpet"
103+
100104
Description:
101105
The result consists of the chars of <Left> followed by those of <Right>.
102106

@@ -115,6 +119,10 @@ Left: An expression which evaluates to a string.
115119
Right: An expression which evaluates to a string.
116120
output: The result of concatenating <Left> and <Right> with a space between.
117121

122+
Example:
123+
variable tVar as string
124+
put "This" && "is" && "a" && "sentence." into tVar -- tVar contains "This is a sentence."
125+
118126
Description:
119127
The result consists of the chars of <Left> followed by a space, and then the chars of <Right>.
120128
*/
@@ -133,7 +141,7 @@ Source: An expression which evaluates to a string.
133141
output: <Source> with each of its chars uppercased.
134142

135143
Description:
136-
144+
Lowercase letters, including special characters with diacritical marks, are converted to the uppercase equivalents. All other characters, including uppercase letters, numbers, punctuation, and special characters with no upper or lower case, are left unchanged.
137145
*/
138146

139147

@@ -148,7 +156,8 @@ Summary: Lowercases <Source>.
148156
Source: An expression which evaluates to a string.
149157
output: <Source> with each of its chars lowercased.
150158

151-
Description:
159+
Description:
160+
Uppercase letters, including special characters with diacritical marks, are converted to the lowercase equivalents. All other characters, including lowercase letters, numbers, punctuation, and special characters with no upper or lower case, are left unchanged.
152161
*/
153162

154163

@@ -166,7 +175,10 @@ Summary: Determines whether <Left> and <Right> are equal or not.
166175
Left: An expression which evaluates to a string.
167176
Right: An expression which evaluates to a string.
168177

169-
output: Returns true if the result of evaluating <Left> is the same as that of <Right>, and false otherwise.
178+
output: Returns true if <Left> is identical to <Right>.
179+
180+
Description:
181+
The ```is``` operator is case sensitive.
170182

171183
*/
172184

@@ -182,8 +194,10 @@ Summary: Determines whether <Left> and <Right> are equal or not.
182194
Left: An expression which evaluates to a string.
183195
Right: An expression which evaluates to a string.
184196

185-
output: Returns false if the result of evaluating <Left> is the same as that of <Right>, and true otherwise.
197+
output: Returns true if <Left> is not identical to <Right>.
186198

199+
Description:
200+
The ```is not``` operator is case sensitive.
187201
*/
188202

189203
syntax IsNotEqualTo is neutral binary operator with precedence 1
@@ -198,8 +212,10 @@ Summary: Determines whether <Left> is less than <Right> under a char by char
198212
Left: An expression which evaluates to a string.
199213
Right: An expression which evaluates to a string.
200214

201-
output: Returns true if <Left> and <Right> are not equal, and the first char in <Right> that is not equal to the
202-
corresponding char in <Left> is of greater value.
215+
output: Returns true if <Left> is less than <Right>
216+
217+
Description:
218+
<Left> is greater than <Right> if and only if <Left> and <Right> are not equal, and the unicode codepoint of the first char in <Left> that is not equal to the corresponding char in <Right> is of greater value.
203219
*/
204220

205221
syntax LessThan is neutral binary operator with precedence 1
@@ -213,9 +229,10 @@ Summary: Determines whether <Left> is greater than <Right> under a char by ch
213229

214230
Left: An expression which evaluates to a string.
215231
Right: An expression which evaluates to a string.
232+
output: Returns true if <Left> is greater than <Right>.
216233

217-
output: Returns true if <Left> and <Right> are not equal, and the first char in <Left> that is not equal to the
218-
corresponding char in <Right> is of greater value.
234+
Description:
235+
<Left> is greater than <Right> if and only if <Left> and <Right> are not equal, and the unicode codepoint of the first char in <Left> that is not equal to the corresponding char in <Right> is of greater value.
219236
*/
220237

221238
syntax GreaterThan is neutral binary operator with precedence 1
@@ -226,6 +243,21 @@ end syntax
226243

227244
--
228245

246+
/*
247+
248+
Summary: Designates the string consisting of zero chars
249+
250+
Example:
251+
variable tVar as string
252+
variable tCount as int
253+
put the empty string into tVar
254+
put the number of chars in tVar into tCount -- tCount is 0
255+
256+
Description:
257+
Use ```the empty string``` to initialise a string variable. The empty string is synonymous with the string literal ""
258+
259+
*/
260+
229261
syntax EmptyString is expression
230262
"the" "empty" "string"
231263
begin

toolchain/lc-compile/test.mlc

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ public handler test()
66

77
variable tDelimiter as string
88
put newline into tDelimiter
9-
9+
1010
testLogic(tResults)
11-
testList(tResults)
12-
testSort(tResults)
11+
--testList(tResults)
12+
--testSort(tResults)
1313
testArithmetic(tResults)
1414
testChar(tResults)
1515
testBitwise(tResults)
1616
testArray(tResults)
17+
testString(tResults)
1718

1819
variable tResultString as string
1920
put the empty string into tResultString
@@ -51,7 +52,10 @@ public handler testString(inout xResults as list)
5152
testLog("String", "PutBeforeString", tString is "abcdefghij", xResults)
5253

5354
testLog("String", "ConcatenateString", tString is "abcde" & "fghij", xResults)
55+
testLog("String", "ConcatenateString", "a" & "b" & "c" is "abc", xResults)
56+
5457
testLog("String", "ConcatenateWithSpace", "ab" && "cd" is "ab cd", xResults)
58+
testLog("String", "ConcatenateWithSpace", "a" && "b" && "c" is "a b c", xResults)
5559

5660
put "purple" into tString
5761
replace "p" with "t" in tString
@@ -62,7 +66,7 @@ public handler testString(inout xResults as list)
6266

6367
testLog("String", "LessThan", "!" < "A", xResults)
6468
testLog("String", "GreaterThan", "a" > "A", xResults)
65-
69+
6670
end handler
6771

6872
public handler testArray(inout xResults as list)
@@ -154,7 +158,7 @@ public handler testLogic(inout xResults as list)
154158

155159
end handler
156160

157-
public handler testChar(inout xResults as list) as undefined
161+
public handler testChar(inout xResults as list)
158162
variable tString as string
159163
put "abcde" into tString
160164

@@ -221,7 +225,7 @@ public handler testChar(inout xResults as list) as undefined
221225
testLog("Char", "CharExample", tVar is "1,2,3", xResults)
222226
end handler
223227

224-
public handler testArithmetic(inout xResults as list) as undefined
228+
public handler testArithmetic(inout xResults as list)
225229

226230
variable tInt as int
227231
variable tReal as double
@@ -269,24 +273,34 @@ public handler testArithmetic(inout xResults as list) as undefined
269273
put 10 into tLeft
270274
put 2 into tRight
271275

276+
variable tLeftReal as double
277+
variable tRightReal as double
278+
279+
put 10 into tLeftReal
280+
put 2 into tRightReal
281+
272282
testLog("Arithmetic", "PlusInt", tLeft + tRight is 12, xResults)
283+
testLog("Arithmetic", "PlusReal", tLeftReal + tRightReal is 12, xResults)
273284
testLog("Arithmetic", "PlusNum", 10 + 2 is 12, xResults)
274285

275286
testLog("Arithmetic", "MinusInt", tLeft - tRight is 8, xResults)
287+
testLog("Arithmetic", "MinusReal", tLeftReal - tRightReal is 8, xResults)
276288
testLog("Arithmetic", "MinusNum", 10 - 2 is 8, xResults)
277289

278290
testLog("Arithmetic", "TimesInt", tLeft * tRight is 20, xResults)
291+
testLog("Arithmetic", "TimesReal", tLeftReal * tRightReal is 20, xResults)
279292
testLog("Arithmetic", "TimesNum", 10 * 2 is 20, xResults)
280293

294+
/*
281295
testLog("Arithmetic", "DivideInt", tLeft / tRight is 5, xResults)
296+
testLog("Arithmetic", "DivideReal", tLeftReal / tRightReal is 5, xResults)
282297
testLog("Arithmetic", "DivideNum", 10 / 2 is 5, xResults)
283298

284299
variable tOne as int
285300
put 1 into tOne
286301

287302
testLog("Arithmetic", "ModInt", tLeft mod (tRight + tOne) is 1, xResults)
288303
testLog("Arithmetic", "ModNum", 5 mod 3 is 2, xResults)
289-
290304
testLog("Arithmetic", "Wrap", 5 wrap 3 is 2, xResults)
291305
testLog("Arithmetic", "Wrap", -3 wrap 3 is -3, xResults)
292306

@@ -309,10 +323,10 @@ public handler testArithmetic(inout xResults as list) as undefined
309323
testLog("Arithmetic", "LessThanOrEqualToInt", not tLeft <= tRight, xResults)
310324
testLog("Arithmetic", "LessThanOrEqualToNum", 1 <= 1, xResults)
311325
testLog("Arithmetic", "LessThanOrEqualToNum", not 2 <= 1, xResults)
312-
326+
*/
313327
end handler
314328

315-
public handler testList(inout xResults as list) as undefined
329+
public handler testList(inout xResults as list)
316330
variable tTestList as list
317331
put the empty list into tTestList
318332

@@ -356,25 +370,25 @@ public handler testList(inout xResults as list) as undefined
356370

357371
end handler
358372

359-
public handler testSort(inout xResults as list) as undefined
373+
public handler testSort(inout xResults as list)
360374
variable tTestList as list
361375
put the empty list into tTestList
362376

363377
push "xyz" onto tTestList
364378
push 2 onto tTestList
365379
push "abcd" onto tTestList
366380
push 1 onto tTestList
367-
381+
368382
sort tTestList in ascending numeric order
369-
383+
/*
370384
testLog("Sort", "AscendingNumeric", the head of tTestList is 1, xResults)
371385
testLog("Sort", "NumericStable", the tail of tTestList is "abcd", xResults)
372386

373387
sort tTestList in ascending text order
374388

375389
testLog("Sort", "AscendingText", the head of tTestList is "abcd", xResults)
376390
testLog("Sort", "TextStable", the tail of tTestList is 2, xResults)
377-
391+
*/
378392
end handler
379393

380394
end module

0 commit comments

Comments
 (0)