Skip to content

Commit 3ef628d

Browse files
author
runrevali
committed
[[ StdMlc ]] Change test.mlc file
1 parent 2fd2c15 commit 3ef628d

4 files changed

Lines changed: 187 additions & 32 deletions

File tree

libscript/src/arithmetic.mlc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public foreign handler MCArithmeticEvalNumberIsLessThanOrEqualToNumber(in Left a
5757

5858

5959
public foreign handler MCArithmeticEvalEqualToInteger(in Left as int, in Right as int, out Value as bool) as undefined binds to "<builtin>"
60+
public foreign handler MCArithmeticEvalEqualToReal(in Left as real, in Right as real, out Value as bool) as undefined binds to "<builtin>"
61+
public foreign handler MCArithmeticEvalEqualToNumber(in Left as number, in Right as number, out Value as bool) as undefined binds to "<builtin>"
6062
--
6163

6264
/*
@@ -72,7 +74,7 @@ Example:
7274
Description:
7375
Adds the number <Value> to <Target>.
7476

75-
>Note: It is a syntax error if <Target> does not evaluate to a variable.
77+
>*Note:* It is a syntax error if <Target> does not evaluate to a variable.
7678
*/
7779

7880
syntax AddNumberTo is statement
@@ -97,7 +99,7 @@ Example:
9799
Description:
98100
Subtracts the number <Value> from <Target>.
99101

100-
>Note: It is a syntax error if <Target> does not evaluate to a variable.
102+
>*Note:* It is a syntax error if <Target> does not evaluate to a variable.
101103

102104
*/
103105

@@ -122,7 +124,7 @@ Example:
122124
Description:
123125
Multiplies the number <Target> by <Value>.
124126

125-
>Note: It is a syntax error if <Target> does not evaluate to a variable.
127+
>*Note:* It is a syntax error if <Target> does not evaluate to a variable.
126128
*/
127129

128130
syntax MultiplyNumberBy is statement
@@ -143,12 +145,18 @@ Example:
143145
put 3 into tVar
144146
divide tVar by 2 -- tVar contains 1.5
145147

148+
Example:
149+
// int gets truncated
150+
variable tVar as int
151+
put 3 into tVar
152+
divide tVar by 2 -- tVar contains 1
153+
146154
Description:
147155
Divides the number <Target> by <Value>.
148156

157+
>*Note:* If tVar is typed as an int, then the result will be truncated.
149158

150-
151-
>Note: It is a syntax error if <Target> does not evaluate to a variable.
159+
>*Note:* It is a syntax error if <Target> does not evaluate to a variable.
152160

153161
*/
154162

@@ -362,13 +370,17 @@ syntax IsEqualTo is neutral binary operator with precedence 4
362370
<Left: Expression> "=" <Right: Expression>
363371
begin
364372
MCArithmeticEvalEqualToInteger(Left, Right, output)
373+
MCArithmeticEvalEqualToReal(Left, Right, output)
374+
MCArithmeticEvalEqualToNumber(Left, Right, output)
365375
end syntax
366376

367377

368378
syntax IsNumber is neutral binary operator with precedence 5
369379
<Left: Expression> "is" <Right: Expression>
370380
begin
371-
MCArithmeticEvalEqualToInteger(Left, Right, output)
381+
--MCArithmeticEvalEqualToInteger(Left, Right, output)
382+
MCArithmeticEvalEqualToReal(Left, Right, output)
383+
MCArithmeticEvalEqualToNumber(Left, Right, output)
372384
end syntax
373385

374386
end module

libscript/src/module-arithmetic.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ extern "C" void MCArithmeticExecAddNumberToNumber(MCNumberRef p_number, MCNumber
4242

4343
MCArithmeticExecAddRealToReal(t_number, t_target);
4444

45-
//if no error
46-
MCValueRelease(x_target);
47-
48-
MCNumberCreateWithReal(t_target, x_target);
45+
MCAutoNumberRef t_new_number;
46+
MCNumberCreateWithReal(t_target, &t_new_number);
47+
48+
MCValueAssign(x_target, *t_new_number);
4949
}
5050

5151
extern "C" void MCArithmeticExecSubtractIntegerFromInteger(integer_t p_number, integer_t& x_target)
@@ -73,10 +73,10 @@ extern "C" void MCArithmeticExecSubtractNumberFromNumber(MCNumberRef p_number, M
7373

7474
MCArithmeticExecSubtractRealFromReal(t_number, t_target);
7575

76-
//if no error
77-
MCValueRelease(x_target);
76+
MCAutoNumberRef t_new_number;
77+
MCNumberCreateWithReal(t_target, &t_new_number);
7878

79-
MCNumberCreateWithReal(t_target, x_target);
79+
MCValueAssign(x_target, *t_new_number);
8080
}
8181

8282
extern "C" void MCArithmeticExecMultiplyIntegerByInteger(integer_t& x_target, integer_t p_number)
@@ -104,10 +104,10 @@ extern "C" void MCArithmeticExecMultiplyNumberByNumber(MCNumberRef& x_target, MC
104104

105105
MCArithmeticExecMultiplyRealByReal(t_number, t_target);
106106

107-
//if no error
108-
MCValueRelease(x_target);
107+
MCAutoNumberRef t_new_number;
108+
MCNumberCreateWithReal(t_target, &t_new_number);
109109

110-
MCNumberCreateWithReal(t_target, x_target);
110+
MCValueAssign(x_target, *t_new_number);
111111
}
112112

113113
extern "C" void MCArithmeticExecDivideIntegerByInteger(integer_t& x_target, integer_t p_number)
@@ -131,10 +131,10 @@ extern "C" void MCArithmeticExecDivideNumberByNumber(MCNumberRef& x_target, MCNu
131131

132132
MCArithmeticExecDivideRealByReal(t_number, t_target);
133133

134-
//if no error
135-
MCValueRelease(x_target);
134+
MCAutoNumberRef t_new_number;
135+
MCNumberCreateWithReal(t_target, &t_new_number);
136136

137-
MCNumberCreateWithReal(t_target, x_target);
137+
MCValueAssign(x_target, *t_new_number);
138138
}
139139

140140
extern "C" void MCArithmeticEvalIntegerPlusInteger(integer_t p_left, integer_t p_right, integer_t& r_output)
@@ -422,3 +422,13 @@ extern "C" void MCArithmeticEvalEqualToInteger(integer_t p_left, integer_t p_rig
422422
{
423423
r_output = p_left == p_right;
424424
}
425+
426+
extern "C" void MCArithmeticEvalEqualToReal(double p_left, double p_right, bool& r_output)
427+
{
428+
r_output = p_left == p_right;
429+
}
430+
431+
extern "C" void MCArithmeticEvalEqualToNumber(MCNumberRef p_left, MCNumberRef p_right, bool& r_output)
432+
{
433+
r_output = MCNumberFetchAsReal(p_left) == MCNumberFetchAsReal(p_right);
434+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@
840840
);
841841
runOnlyForDeploymentPostprocessing = 0;
842842
shellPath = /bin/sh;
843-
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/type.mlc\" \"${SRCROOT}/../../libscript/src/arithmetic.mlc\" \"${SRCROOT}/../../libscript/src/bitwise.mlc\" \"${SRCROOT}/../../libscript/src/byte.mlc\" \"${SRCROOT}/../../libscript/src/char.mlc\" \"${SRCROOT}/../../engine/src/canvas.mlc\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
843+
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\"\nif [ \"$?\" != \"0\" ]; then exit 1; fi\n";
844844
showEnvVarsInLog = 0;
845845
};
846846
/* End PBXShellScriptBuildPhase section */

toolchain/lc-compile/test.mlc

Lines changed: 145 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,152 @@ module test
33
foreign handler MCNumberCreateWithInteger(in value as int, out value_ref as number) as bool binds to "<builtin>"
44
foreign handler MCBooleanCreateWithBool(in value as bool, out value_ref as boolean) as bool binds to "<builtin>"
55

6+
foreign handler MCProperListCreate(in pValues as optional pointer, in pValueCount as uint, out value_ref as list) as bool binds to "<builtin>"
7+
68
public handler test()
7-
variable tVar as number
8-
variable tResult as boolean
9-
MCBooleanCreateWithBool(MCNumberCreateWithInteger(100, tVar), tResult)
10-
if MCNumberCreateWithInteger(200, tVar) then
9+
variable tResults as list
10+
MCProperListCreate(undefined, 0, tResults)
11+
12+
variable tDelimiter as string
13+
put newline into tDelimiter
14+
15+
--testLogic(tResults)
16+
-- testList(tResults)
17+
--testSort(tResults)
18+
--testArithmetic(tResults)
19+
20+
variable tVar as list
21+
MCProperListCreate(undefined, 0, tVar)
22+
push 2 onto tVar
23+
if the head of tVar is 2 then
24+
push "success" onto tResults
25+
end if
26+
27+
variable tResultString as string
28+
put "" into tResultString
29+
combine tResults with tDelimiter into tResultString
30+
31+
return tResultString
32+
end handler
33+
34+
public handler testLog(in pModule as string, in pTest as string, in pResult as bool, inout xResults as list)
35+
variable tStringResult as string
36+
if pResult then
37+
put "SUCCESS" into tStringResult
38+
else
39+
put "FAILURE" into tStringResult
1140
end if
12-
variable tVar2 as string
13-
put "" into tVar2
14-
put "Hello World" after tVar2
15-
put "!" after tVar2
16-
put 0 into tVar
17-
add 100 to tVar
18-
return tVar2
41+
42+
push pModule & "_" & pTest & ":" && tStringResult onto xResults
43+
end handler
44+
45+
public handler testLogic(inout xResults as list)
46+
variable tVar as bool
47+
put not false into tVar
48+
49+
testLog("Logic", "Not", tVar, xResults)
50+
end handler
51+
52+
public handler testArithmetic(inout xResults as list) as undefined
53+
54+
variable tInt as int
55+
variable tReal as real
56+
--variable tNumber
57+
58+
put 10 into tInt
59+
put 10 into tReal
60+
--put 10 into tNumber
61+
62+
add 2 to tInt
63+
add 2 to tReal
64+
--add 2 to tNumber
65+
66+
testLog("Arithmetic", "AddToInt", tInt is 12, xResults)
67+
testLog("Arithmetic", "AddToReal", tReal is 12, xResults)
68+
--testLog("Arithmetic", "AddToNum", tNumber is 12, xResults)
69+
70+
subtract 2 from tInt
71+
subtract 2 from tReal
72+
--subtract 2 from tNumber
73+
74+
testLog("Arithmetic", "SubtractFromInt", tInt is 10, xResults)
75+
testLog("Arithmetic", "SubtractFromReal", tReal is 10, xResults)
76+
--testLog("Arithmetic", "SubtractFromNum", tNumber is 10, xResults)
77+
78+
multiply tInt by 2
79+
multiply tReal by 2
80+
--multiply tNumber by 2
81+
82+
testLog("Arithmetic", "MultiplyInt", tInt is 20, xResults)
83+
testLog("Arithmetic", "MultiplyReal", tReal is 20, xResults)
84+
--testLog("Arithmetic", "MultiplyNum", tNumber is 20, xResults)
85+
86+
divide tInt by 40
87+
divide tReal by 40
88+
--divide tNumber by 40
89+
90+
testLog("Arithmetic", "DivideInt", tInt is 0, xResults)
91+
testLog("Arithmetic", "DivideReal", tReal is 0.5, xResults)
92+
--testLog("Arithmetic", "DivideNum", tNumber is 0.5, xResults)
93+
94+
end handler
95+
96+
public handler testList(inout xResults as list) as undefined
97+
variable tTestList as list
98+
MCProperListCreate(undefined, 0, tTestList)
99+
100+
push "xyz" onto tTestList
101+
push 2 onto tTestList
102+
push "abcd" onto tTestList
103+
push 1 onto tTestList
104+
105+
testLog("List", "NumberOfElements", the number of elements in tTestList is 4, xResults)
106+
testLog("List", "HeadOf", the head of tTestList is "xyz", xResults)
107+
testLog("List", "TailOf", the tail of tTestList is 1, xResults)
108+
109+
testLog("List", "ElementOf", element 2 of tTestList is 2, xResults)
110+
testLog("List", "ElementRangeOf", element 2 of element 3 to -1 of tTestList is 1, xResults)
111+
112+
variable tSubList as list
113+
put element -2 to -1 of tTestList into tSubList
114+
testLog("List", "Contains", tTestList contains element -2 to -1 of tTestList, xResults)
115+
116+
testLog("List", "IsIn", "abcd" is in tSubList, xResults)
117+
testLog("List", "IsNotIn", not tSubList is in tTestList, xResults)
118+
119+
variable tString as string
120+
pop front of tSubList into tString
121+
122+
testLog("List", "Pop", tString is "abcd", xResults)
123+
testLog("List", "Pop", the number of elements in tSubList is 1, xResults)
124+
125+
splice tTestList after element 1 of tSubList
126+
127+
testLog("List", "SpliceAfter", the number of elements in tSubList is 5, xResults)
128+
testLog("List", "SpliceAfter", the tail of tSubList is 1, xResults)
129+
testLog("List", "SpliceAfter", tSubList contains tTestList, xResults)
130+
131+
end handler
132+
133+
public handler testSort(inout xResults as list) as undefined
134+
variable tTestList as list
135+
MCProperListCreate(undefined, 0, tTestList)
136+
137+
push "xyz" onto tTestList
138+
push 2 onto tTestList
139+
push "abcd" onto tTestList
140+
push 1 onto tTestList
141+
142+
sort tTestList in ascending numeric order
143+
144+
testLog("Sort", "AscendingNumeric", the head of tTestList is 1, xResults)
145+
testLog("Sort", "NumericStable", the tail of tTestList is "abcd", xResults)
146+
147+
sort tTestList in ascending text order
148+
149+
testLog("Sort", "AscendingText", the head of tTestList is "abcd", xResults)
150+
testLog("Sort", "TextStable", the tail of tTestList is 2, xResults)
151+
19152
end handler
20153

21-
end module
154+
end module

0 commit comments

Comments
 (0)