Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 15235ad

Browse files
committed
[[ StdMlc ]] Add testing for math module
1 parent 4efb3db commit 15235ad

File tree

10 files changed

+155
-89
lines changed

10 files changed

+155
-89
lines changed

libscript/libscript.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
4D79B8ED1A375F4700DD750C /* type-convert.mlc in Sources */ = {isa = PBXBuildFile; fileRef = 4D79B67D1A273BA000DD750C /* type-convert.mlc */; };
5959
4DDA20451A136FF4001B0CA2 /* script-builder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4DDA20441A136FF4001B0CA2 /* script-builder.cpp */; };
6060
4DDA20701A139BC0001B0CA2 /* script-test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4DDA206F1A139BC0001B0CA2 /* script-test.cpp */; };
61+
5DDE27B91A37909500D5ABD2 /* math-foundation.mlc in Sources */ = {isa = PBXBuildFile; fileRef = 4D79B6781A273BA000DD750C /* math-foundation.mlc */; };
6162
766113441A2F298E0042DE7F /* list.mlc in Sources */ = {isa = PBXBuildFile; fileRef = 4D79B6751A273BA000DD750C /* list.mlc */; };
6263
766113891A31FB640042DE7F /* module-array.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 766113881A31FB640042DE7F /* module-array.cpp */; };
6364
7661138A1A31FB690042DE7F /* module-array.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 766113881A31FB640042DE7F /* module-array.cpp */; };
@@ -532,6 +533,7 @@
532533
isa = PBXSourcesBuildPhase;
533534
buildActionMask = 2147483647;
534535
files = (
536+
5DDE27B91A37909500D5ABD2 /* math-foundation.mlc in Sources */,
535537
7661138D1A31FE1B0042DE7F /* array.mlc in Sources */,
536538
766113441A2F298E0042DE7F /* list.mlc in Sources */,
537539
4D79B6CF1A27405600DD750C /* arithmetic.mlc in Sources */,

libscript/src/arithmetic.mlc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Right: An expression that evaluates to a number.
327327
output: True if <Left> is greater than <Right>, and false otherwise.
328328
*/
329329

330-
syntax IsGreaterThan is neutral binary operator with precedence 1
330+
syntax IsGreaterThan is neutral binary operator with precedence 3
331331
<Left: Expression> ">" <Right: Expression>
332332
begin
333333
MCArithmeticEvalIntegerIsGreaterThanInteger(Left, Right, output)
@@ -357,7 +357,7 @@ Right: An expression that evaluates to a number.
357357
output: True if <Left> is less than <Right>, and false otherwise.
358358
*/
359359

360-
syntax IsLessThan is neutral binary operator with precedence 1
360+
syntax IsLessThan is neutral binary operator with precedence 3
361361
<Left: Expression> "<" <Right: Expression>
362362
begin
363363
MCArithmeticEvalIntegerIsLessThanInteger(Left, Right, output)

libscript/src/binary.mlc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ output: Returns true if <Left> and <Right> are not equal, and the first byte
118118
corresponding byte in <Left> is of greater value.
119119
*/
120120

121-
syntax LessThan is neutral binary operator with precedence 1
121+
syntax LessThan is neutral binary operator with precedence 3
122122
<Left: Expression> "<" <Right: Expression>
123123
begin
124124
MCBinaryEvalIsLessThan(Left, Right, output)
@@ -134,7 +134,7 @@ output: Returns true if <Left> and <Right> are not equal, and the first byte
134134
corresponding byte in <Right> is of greater value.
135135
*/
136136

137-
syntax GreaterThan is neutral binary operator with precedence 1
137+
syntax GreaterThan is neutral binary operator with precedence 3
138138
<Left: Expression> ">" <Right: Expression>
139139
begin
140140
MCBinaryEvalIsGreaterThan(Left, Right, output)

libscript/src/math-foundation.mlc

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@ module com.livecode.mathfoundation
22

33
--
44

5-
foreign handler ExecRoundRealToNearest(inout Target as real) as undefined binds to "<builtin>"
6-
foreign handler ExecRoundNumberToNearest(inout Target as number) as undefined binds to "<builtin>"
7-
foreign handler ExecRoundRealDown(inout Target as real) as undefined binds to "<builtin>"
8-
foreign handler ExecRoundNumberDown(inout Target as number) as undefined binds to "<builtin>"
9-
foreign handler ExecRoundRealUp(inout Target as real) as undefined binds to "<builtin>"
10-
foreign handler ExecRoundNumberUp(inout Target as number) as undefined binds to "<builtin>"
11-
foreign handler EvalFloorOfReal(in Target as real, out Value as real) as undefined binds to "<builtin>"
12-
foreign handler EvalFloorOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
13-
foreign handler EvalCeilOfReal(in Target as real, out Value as real) as undefined binds to "<builtin>"
14-
foreign handler EvalCeilOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
5+
public foreign handler MCMathFoundationExecRoundRealToNearest(inout Target as double) as undefined binds to "<builtin>"
6+
public foreign handler MCMathFoundationExecRoundNumberToNearest(inout Target as number) as undefined binds to "<builtin>"
7+
public foreign handler MCMathFoundationEvalFloorOfReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
8+
public foreign handler MCMathFoundationEvalFloorOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
9+
public foreign handler MCMathFoundationEvalCeilOfReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
10+
public foreign handler MCMathFoundationEvalCeilOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
1511

1612
--
1713

18-
public constant pi = 3.14159265358979323846
14+
//public constant pi is 3.14159265358979323846
1915

2016
--
2117

@@ -35,39 +31,8 @@ Target: An expression that evaluates to a number.
3531
syntax RoundToNearest is statement
3632
"round" <Target: Expression>
3733
begin
38-
ExecRoundRealToNearest(Target)
39-
ExecRoundNumberToNearest(Target)
40-
end syntax
41-
42-
/*
43-
Summary: Rounds <Target> to the nearest integer.
44-
If <Target> is exactly halfway between two integers, rounds to the greatest integer less than <Target>.
45-
Target: An expression that evaluates to a number.
46-
47-
48-
Round half down
49-
*/
50-
51-
syntax RoundDown is statement
52-
"round" <Target: Expression> "down"
53-
begin
54-
ExecRoundRealDown(Target)
55-
ExecRoundNumberDown(Target)
56-
end syntax
57-
58-
/*
59-
Summary: Rounds <Target> to the least integer greater than or equal to <Target>.
60-
If <Target> is exactly halfway between two integers, rounds to the least integer greater than <Target>.
61-
Target: An expression that evaluates to a number.
62-
63-
Round half up
64-
*/
65-
66-
syntax RoundUp is statement
67-
"round" <Target: Expression> "up"
68-
begin
69-
ExecRoundRealUp(Target)
70-
ExecRoundNumberUp(Target)
34+
MCMathFoundationExecRoundRealToNearest(Target)
35+
MCMathFoundationExecRoundNumberToNearest(Target)
7136
end syntax
7237

7338
--
@@ -83,8 +48,8 @@ output: The greatest integer less than or equal to <Target>.
8348
syntax FloorOperator is prefix operator with precedence 1
8449
"the" "floor" "of" <Target: Expression>
8550
begin
86-
EvalFloorOfReal(Target, output)
87-
EvalFloorOfNumber(Target, output)
51+
MCMathFoundationEvalFloorOfReal(Target, output)
52+
MCMathFoundationEvalFloorOfNumber(Target, output)
8853
end syntax
8954

9055
/*
@@ -97,8 +62,8 @@ output: The least integer greater than or equal to <Target>.
9762
syntax CeilOperator is prefix operator with precedence 1
9863
"the" "ceiling" "of" <Target: Expression>
9964
begin
100-
EvalCeilOfReal(Target, output)
101-
EvalCeilOfNumber(Target, output)
65+
MCMathFoundationEvalCeilOfReal(Target, output)
66+
MCMathFoundationEvalCeilOfNumber(Target, output)
10267
end syntax
10368

10469
end module

libscript/src/math.mlc

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public foreign handler MCMathEvalMaxNumber(in Left as number, in Right as number
4040
public foreign handler MCMathEvalRandomReal(out Value as double) as undefined binds to "<builtin>"
4141

4242
public foreign handler MCMathEvalConvertBase(in Operand as string, in Source as int, in Target as int, out Value as string) as undefined binds to "<builtin>"
43-
public foreign handler MCMathEvalConvertFromBase(in Operand as string, in Source as int, out Value as int) as undefined binds to "<builtin>"
44-
public foreign handler MCMathEvalConvertToBase(in Operand as int, in Target as int, out Value as string) as undefined binds to "<builtin>"
43+
public foreign handler MCMathEvalConvertToBase10(in Operand as string, in Source as int, out Value as int) as undefined binds to "<builtin>"
44+
public foreign handler MCMathEvalConvertFromBase10(in Operand as int, in Target as int, out Value as string) as undefined binds to "<builtin>"
4545

4646
--
4747

@@ -222,20 +222,59 @@ end syntax
222222

223223
--
224224

225+
/*
226+
Summary: Converts the base of <Operand>
227+
Operand: An expression that evaluates to a string.
228+
Source: An expression that evaluates to an integer.
229+
output: <Operand> converted to base 10.
230+
231+
Description:
232+
Interprets a string in the desired base and converts it to decimal.
233+
234+
>*Note:* The source base must be an integer between 2 and 32.
235+
*/
236+
237+
syntax BaseConvertFrom is left binary operator with precedence 1
238+
<Operand: Expression> "converted" "from" "base" <Source: Expression>
239+
begin
240+
MCMathEvalConvertToBase10(Operand, Source, output)
241+
end syntax
242+
225243
/*
226244
Summary: Converts the base of <Operand>
227245
Operand: An expression that evaluates to an integer.
228-
From: An expression that evaluates to an integer.
229-
To: An expression that evaluates to an integer.
230-
output: A string representation of the value of <Operand> when interpreted as a number in base <From>, converted to base <To>.
246+
Target: An expression that evaluates to an integer.
247+
output: A string representation of <Operand> in base <Target>.
248+
249+
Description:
250+
Converts a decimal into the desired base, and returns a string representation.
251+
252+
>*Note:* The destination base must be an integer between 2 and 32.
253+
*/
254+
255+
syntax BaseConvertTo is left binary operator with precedence 1
256+
<Operand: Expression> "converted" "to" "base" <Target: Expression>
257+
begin
258+
MCMathEvalConvertFromBase10(Operand, Target, output)
259+
end syntax
260+
261+
/*
262+
Summary: Converts the base of <Operand>
263+
Operand: An expression that evaluates to a string.
264+
Source: An expression that evaluates to an integer.
265+
Target: An expression that evaluates to an integer.
266+
output: A string representation <Operand> interpreted as a number in base <From>, converted to base <To>.
267+
268+
Description:
269+
Interprets a string in the desired source base and converts it to the desired target base, and returns a string representation.
270+
271+
>*Note:* The source and destination bases must be integers between 2 and 32.
231272
*/
232273

233-
syntax BaseConvert is postfix operator with precedence 1
234-
<Operand: Expression> "converted" ["from" "base" <Source: Expression> ] ["to" "base" <Target: Expression> ]
274+
syntax BaseConvert is left binary operator with precedence 1
275+
<Operand: Expression> "converted" "from" "base" <Source: Expression> "to" "base" <Target: Expression>
235276
begin
236277
MCMathEvalConvertBase(Operand, Source, Target, output)
237-
MCMathEvalConvertFromBase(Operand, Target, output)
238-
MCMathEvalConvertToBase(Operand, Source, output)
239278
end syntax
240279

241280
--

libscript/src/module-math.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ extern "C" MC_DLLEXPORT void MCMathEvalTruncNumber(MCNumberRef p_operand, MCNumb
190190
if (MCNumberIsInteger(p_operand))
191191
{
192192
integer_t t_abs;
193-
MCMathEvalAbsInteger(MCNumberFetchAsInteger(p_operand), t_abs);
193+
MCMathEvalTruncInteger(MCNumberFetchAsInteger(p_operand), t_abs);
194194
MCNumberCreateWithInteger(t_abs, r_output);
195195
return;
196196
}
197197

198198
double t_abs_real;
199-
MCMathEvalAbsReal(MCNumberFetchAsReal(p_operand), t_abs_real);
199+
MCMathEvalTruncReal(MCNumberFetchAsReal(p_operand), t_abs_real);
200200
MCNumberCreateWithReal(t_abs_real, r_output);
201201
}
202202

libscript/src/module-math_foundation.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ void MCMathFoundationExecRoundUpReal(double& x_target)
2626
x_target = ceil(x_target - 0.5);
2727
}
2828

29-
void MCMathFoundationExecRoundToNearestReal(double& x_target)
29+
extern "C" void MCMathFoundationExecRoundToNearestReal(double& x_target)
3030
{
3131
if (x_target < 0.0)
3232
x_target = ceil(x_target - 0.5);
3333
else
3434
x_target = floor(x_target + 0.5);
3535
}
3636

37-
void MCMathFoundationExecRoundDownNumber(MCNumberRef& x_target)
37+
extern "C" void MCMathFoundationExecRoundDownNumber(MCNumberRef& x_target)
3838
{
3939
double t_target = MCNumberFetchAsReal(x_target);
4040
MCMathFoundationExecRoundDownReal(t_target);
@@ -43,7 +43,7 @@ void MCMathFoundationExecRoundDownNumber(MCNumberRef& x_target)
4343
MCNumberCreateWithReal(t_target, x_target);
4444
}
4545

46-
void MCMathFoundationExecRoundUpNumber(MCNumberRef& x_target)
46+
extern "C" void MCMathFoundationExecRoundUpNumber(MCNumberRef& x_target)
4747
{
4848
double t_target = MCNumberFetchAsReal(x_target);
4949
MCMathFoundationExecRoundUpReal(t_target);
@@ -52,7 +52,7 @@ void MCMathFoundationExecRoundUpNumber(MCNumberRef& x_target)
5252
MCNumberCreateWithReal(t_target, x_target);
5353
}
5454

55-
void MCMathFoundationExecRoundToNearestNumber(MCNumberRef& x_target)
55+
extern "C" void MCMathFoundationExecRoundToNearestNumber(MCNumberRef& x_target)
5656
{
5757
double t_target = MCNumberFetchAsReal(x_target);
5858
MCMathFoundationExecRoundToNearestReal(t_target);
@@ -61,30 +61,30 @@ void MCMathFoundationExecRoundToNearestNumber(MCNumberRef& x_target)
6161
MCNumberCreateWithReal(t_target, x_target);
6262
}
6363

64-
void MCMathFoundationEvalFloorReal(double& x_target)
64+
extern "C" void MCMathFoundationEvalFloorReal(double p_target, double& r_output)
6565
{
66-
x_target = floor(x_target);
66+
r_output = floor(p_target);
6767
}
6868

69-
void MCMathFoundationEvalCeilingReal(double& x_target)
69+
extern "C" void MCMathFoundationEvalCeilingReal(double p_target, double& r_output)
7070
{
71-
x_target = ceil(x_target);
71+
r_output = ceil(p_target);
7272
}
7373

74-
void MCMathFoundationEvalFloorNumber(MCNumberRef& x_target)
74+
extern "C" void MCMathFoundationEvalFloorNumber(MCNumberRef p_target, MCNumberRef& r_output)
7575
{
76-
double t_target = MCNumberFetchAsReal(x_target);
77-
MCMathFoundationEvalFloorReal(t_target);
76+
double t_target = MCNumberFetchAsReal(p_target);
77+
MCMathFoundationEvalFloorReal(t_target, t_target);
7878

79-
MCValueRelease(x_target);
80-
MCNumberCreateWithReal(t_target, x_target);
79+
if (!MCNumberCreateWithReal(t_target, r_output))
80+
return;
8181
}
8282

83-
void MCMathFoundationEvalCeilingNumber(MCNumberRef& x_target)
83+
void MCMathFoundationEvalCeilingNumber(MCNumberRef p_target, MCNumberRef& r_output)
8484
{
85-
double t_target = MCNumberFetchAsReal(x_target);
86-
MCMathFoundationEvalCeilingReal(t_target);
85+
double t_target = MCNumberFetchAsReal(p_target);
86+
MCMathFoundationEvalCeilingReal(t_target, t_target);
8787

88-
MCValueRelease(x_target);
89-
MCNumberCreateWithReal(t_target, x_target);
88+
if (!MCNumberCreateWithReal(t_target, r_output))
89+
return;
9090
}

libscript/src/string.mlc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Description:
218218
<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.
219219
*/
220220

221-
syntax LessThan is neutral binary operator with precedence 1
221+
syntax LessThan is neutral binary operator with precedence 3
222222
<Left: Expression> "<" <Right: Expression>
223223
begin
224224
MCStringEvalIsLessThan(Left, Right, output)
@@ -235,7 +235,7 @@ Description:
235235
<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.
236236
*/
237237

238-
syntax GreaterThan is neutral binary operator with precedence 1
238+
syntax GreaterThan is neutral binary operator with precedence 3
239239
<Left: Expression> ">" <Right: Expression>
240240
begin
241241
MCStringEvalIsGreaterThan(Left, Right, output)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,14 +848,15 @@
848848
"$(SRCROOT)/../../libscript/src/array.mlc",
849849
"$(SRCROOT)/../../engine/src/canvas.mlc",
850850
"$(SRCROOT)/../../engine/src/widget.mlc",
851+
"$(SRCROOT)/../../libscript/src/math.mlc",
851852
);
852853
name = Bootstrap;
853854
outputPaths = (
854855
"$(SRCROOT)/src/grammar_full.g",
855856
);
856857
runOnlyForDeploymentPostprocessing = 0;
857858
shellPath = /bin/sh;
858-
shellScript = "cd src\nmkdir -p \"_G_\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -bootstrap -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\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
859+
shellScript = "cd src\nmkdir -p \"_G_\"\n\"${BUILT_PRODUCTS_DIR}/lc-bootstrap-compile\" -bootstrap -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\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
859860
showEnvVarsInLog = 0;
860861
};
861862
/* End PBXShellScriptBuildPhase section */

0 commit comments

Comments
 (0)