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

Commit fc2bdbe

Browse files
author
runrevali
committed
[[ LC Builder ]] Add list versions of min/max
1 parent 9adf6c6 commit fc2bdbe

File tree

4 files changed

+203
-53
lines changed

4 files changed

+203
-53
lines changed

libscript/src/math.mlc

Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public foreign handler MCMathEvalTruncNumber(in Operand as number, out Value as
6464
public foreign handler MCMathEvalMinInteger(in Left as int, in Right as int, out Value as int) as undefined binds to "<builtin>"
6565
public foreign handler MCMathEvalMinReal(in Left as double, in Right as double, out Value as double) as undefined binds to "<builtin>"
6666
public foreign handler MCMathEvalMinNumber(in Left as number, in Right as number, out Value as number) as undefined binds to "<builtin>"
67+
68+
public foreign handler MCMathEvalMinList(in List as list, out Value as number) as undefined binds to "<builtin>"
69+
public foreign handler MCMathEvalMaxList(in List as list, out Value as number) as undefined binds to "<builtin>"
70+
6771
public foreign handler MCMathEvalMaxInteger(in Left as int, in Right as int, out Value as int) as undefined binds to "<builtin>"
6872
public foreign handler MCMathEvalMaxReal(in Left as double, in Right as double, out Value as double) as undefined binds to "<builtin>"
6973
public foreign handler MCMathEvalMaxNumber(in Left as number, in Right as number, out Value as number) as undefined binds to "<builtin>"
@@ -99,26 +103,37 @@ end syntax
99103
--
100104

101105
/*
106+
Syntax: sin(<Operand>)
102107
Summary: Sin operator.
103108
Operand: An expression that evaluates to a number.
104109

105110
Example:
106111
variable tVar as number
107-
put the sin of pi / 2 into tVar -- tVar contains 1
112+
put sin(pi / 2) into tVar -- tVar contains 1
113+
114+
Example:
115+
variable tVar as number
116+
put the sine of pi into tVar -- tVar contains 0
108117

109118
Tags: Math
110119
*/
111120

112121
syntax SinOperator is prefix operator with precedence 1
113-
"the" "sin" "of" <Operand: Expression>
122+
"the" "sine" "of" <Operand: Expression>
114123
begin
115124
MCMathEvalSinNumber(Operand, output)
116125
end syntax
117126

127+
public handler sin(in pOperand as number) as number
128+
variable tVar as number
129+
MCMathEvalSinNumber(pOperand, tVar)
130+
return tVar
131+
end handler
132+
118133
/*
134+
Syntax: cos(<Operand>)
119135
Summary: Cos operator.
120136
Operand: An expression that evaluates to a number.
121-
Syntax: cos(<Operand>)
122137

123138
Example:
124139
variable tVar as number
@@ -140,29 +155,45 @@ public handler cos(in pOperand as number) as number
140155
end handler
141156

142157
/*
158+
Syntax: tan(<Operand>)
143159
Summary: Tan operator.
144160
Operand: An expression that evaluates to a number.
145161

146162
Example:
147163
variable tVar as number
148-
put the tan of pi /4 into tVar -- tVar contains approximately 1
164+
put the tan(pi) into tVar -- tVar contains 0
165+
166+
Example:
167+
variable tVar as number
168+
put the tangent of pi /4 into tVar -- tVar contains approximately 1
149169

150170
Tags: Math
151171
*/
152172

153173
syntax TanOperator is prefix operator with precedence 1
154-
"the" "tan" "of" <Operand: Expression>
174+
"the" "tangent" "of" <Operand: Expression>
155175
begin
156176
MCMathEvalTanNumber(Operand, output)
157177
end syntax
158178

179+
public handler tan(in pOperand as number) as number
180+
variable tVar as number
181+
MCMathEvalTanNumber(pOperand, tVar)
182+
return tVar
183+
end handler
184+
159185
/*
186+
Syntax: asin(<Operand>)
160187
Summary: Arcsin operator.
161188
Operand: An expression that evaluates to a number.
162189

163190
Example:
164191
variable tVar as number
165-
put the arcsin of 1 into tVar -- tVar contains pi / 2
192+
put the asin(-1) into tVar -- tVar contains -pi/2
193+
194+
Example:
195+
variable tVar as number
196+
put the arcsine of 1 into tVar -- tVar contains pi/2
166197

167198
Description:
168199
The inverse of the sin operator.
@@ -171,18 +202,29 @@ Tags: Math
171202
*/
172203

173204
syntax ArcsinOperator is prefix operator with precedence 1
174-
"the" "arcsin" "of" <Operand: Expression>
205+
"the" "arcsine" "of" <Operand: Expression>
175206
begin
176207
MCMathEvalAsinNumber(Operand, output)
177208
end syntax
178209

210+
public handler asin(in pOperand as number) as number
211+
variable tVar as number
212+
MCMathEvalAsinNumber(pOperand, tVar)
213+
return tVar
214+
end handler
215+
179216
/*
217+
Syntax: acos(<Operand>)
180218
Summary: Arccos operator.
181219
Operand: An expression that evaluates to a number.
182220

183221
Example:
184222
variable tVar as number
185-
put the arccos of -1 into tVar -- tVar contains pi
223+
put acos(0) into tVar -- tVar contains pi/2
224+
225+
Example:
226+
variable tVar as number
227+
put the arccosine of -1 into tVar -- tVar contains pi
186228

187229
Description:
188230
The inverse of the cos operator.
@@ -191,18 +233,29 @@ Tags: Math
191233
*/
192234

193235
syntax ArccosOperator is prefix operator with precedence 1
194-
"the" "arccos" "of" <Operand: Expression>
236+
"the" "arccosine" "of" <Operand: Expression>
195237
begin
196238
MCMathEvalAcosNumber(Operand, output)
197239
end syntax
198240

241+
public handler acos(in pOperand as number) as number
242+
variable tVar as number
243+
MCMathEvalAcosNumber(pOperand, tVar)
244+
return tVar
245+
end handler
246+
199247
/*
248+
Syntax: atan(<Operand>)
200249
Summary: Arctan operator.
201250
Operand: An expression that evaluates to a number.
202251

203252
Example:
204253
variable tVar as number
205-
put the arctan of 1 into tVar -- tVar contains pi / 4
254+
put atan(-1) into tVar -- tVar contains -pi / 4
255+
256+
Example:
257+
variable tVar as number
258+
put the arctangent of 1 into tVar -- tVar contains pi / 4
206259

207260
Description:
208261
The inverse of the tan operator.
@@ -211,19 +264,30 @@ Tags: Math
211264
*/
212265

213266
syntax ArctanOperator is prefix operator with precedence 1
214-
"the" "arctan" "of" <Operand: Expression>
267+
"the" "arctangent" "of" <Operand: Expression>
215268
begin
216269
MCMathEvalAtanNumber(Operand, output)
217270
end syntax
218271

272+
public handler atan(in pOperand as number) as number
273+
variable tVar as number
274+
MCMathEvalAtanNumber(pOperand, tVar)
275+
return tVar
276+
end handler
277+
219278
/*
279+
Syntax: atan2(<yCoord>,<xCoord>)
220280
Summary: Binary arctan operator.
221281
yCoord: An expression that evaluates to a number.
222282
xCoord: An expression that evaluates to a number.
223283

224284
Example:
225285
variable tVar as number
226-
put the arctan of -1,-1 into tVar --tVar contains −3π/4.
286+
put atan2(-1,-1) into tVar --tVar contains −3π/4.
287+
288+
Example:
289+
variable tVar as number
290+
put the binary arctangent of -1 and -1 into tVar --tVar contains −3π/4.
227291

228292
Description:
229293
The binary arctan operator returns the angle in radians between the x-axis and the line from the origin to the point (xCoord, yCoord).
@@ -233,14 +297,21 @@ Tags: Math
233297
*/
234298

235299
syntax BinaryArctanOperator is prefix operator with precedence 1
236-
"the" "arctan2" "of" <yCoord: Expression> "," <xCoord: Expression>
300+
"the" "binary" "arctangent" "of" <yCoord: Expression> "and" <xCoord: Expression>
237301
begin
238302
MCMathEvalAtan2Number(yCoord, xCoord, output)
239303
end syntax
240304

305+
public handler atan2(in pY as number, in pX as number) as number
306+
variable tVar as number
307+
MCMathEvalAtan2Number(pX, pY, tVar)
308+
return tVar
309+
end handler
310+
241311
--
242312

243313
/*
314+
Syntax: log(<Operand>)
244315
Summary: Base 10 log operator.
245316
Operand: An expression that evaluates to a number.
246317

@@ -257,15 +328,22 @@ begin
257328
MCMathEvalBase10LogNumber(Operand, output)
258329
end syntax
259330

331+
public handler log10(in pOperand as number) as number
332+
variable tVar as number
333+
MCMathEvalBase10LogNumber(pOperand, tVar)
334+
return tVar
335+
end handler
336+
260337
--
261338

262339
/*
340+
Syntax: ln(<Operand>)
263341
Summary: Natural log operator.
264342
Operand: An expression that evaluates to a number.
265343

266344
Example:
267345
variable tVar as number
268-
put the natural log of the exp of 3 into tVar -- tVar contains approximately 3
346+
put ln(exp(3)) into tVar -- tVar contains approximately 3
269347

270348
Tags: Math
271349

@@ -277,26 +355,38 @@ begin
277355
MCMathEvalNaturalLogNumber(Operand, output)
278356
end syntax
279357

358+
public handler ln(in pOperand as number) as number
359+
variable tVar as number
360+
MCMathEvalNaturalLogNumber(pOperand, tVar)
361+
return tVar
362+
end handler
280363

281364
/*
365+
Syntax: exp(<Operand>)
282366
Summary: Exponentiation operator.
283367
Operand: An expression that evaluates to a number.
284368

285369
Returns: e to the power of <Operand>
286370

287371
Example:
288372
variable tVar as number
289-
put the exp of the natural log of 100 into tVar -- tVar contains approximately 100
373+
put the exp(ln(100)) into tVar -- tVar contains approximately 100
290374

291375
Tags: Math
292376
*/
293377

294378
syntax ExpOperator is prefix operator with precedence 1
295-
"the" "exp" "of" <Operand: Expression>
379+
"the" "exponential" "of" <Operand: Expression>
296380
begin
297381
MCMathEvalExpNumber(Operand, output)
298382
end syntax
299383

384+
public handler exp(in pOperand as number) as number
385+
variable tVar as number
386+
MCMathEvalExpNumber(pOperand, tVar)
387+
return tVar
388+
end handler
389+
300390
--
301391

302392
/*
@@ -378,11 +468,17 @@ Tags: Math
378468
*/
379469

380470
syntax MinOperator is prefix operator with precedence 5
381-
"the" "min" "of" <Left: Expression> "and" <Right: Expression>
471+
"the" "minimum" "of" <Left: Expression> "and" <Right: Expression>
382472
begin
383473
MCMathEvalMinNumber(Left, Right, output)
384474
end syntax
385475

476+
public handler min(in pX as number, in pY as number) as number
477+
variable tVar as number
478+
MCMathEvalMinNumber(pX, pY, tVar)
479+
return tVar
480+
end handler
481+
386482
/*
387483
Summary: Max operator.
388484
Left: An expression that evaluates to a number.
@@ -394,11 +490,17 @@ Tags: Math
394490
*/
395491

396492
syntax MaxOperator is prefix operator with precedence 5
397-
"the" "max" "of" <Left: Expression> "and" <Right: Expression>
493+
"the" "maximum" "of" <Left: Expression> "and" <Right: Expression>
398494
begin
399495
MCMathEvalMaxNumber(Left, Right, output)
400496
end syntax
401497

498+
public handler max(in pX as number, in pY as number) as number
499+
variable tVar as number
500+
MCMathEvalMaxNumber(pX, pY, tVar)
501+
return tVar
502+
end handler
503+
402504
--
403505

404506
/*

libscript/src/module-math.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,41 @@ extern "C" MC_DLLEXPORT void MCMathEvalMinNumber(MCNumberRef p_left, MCNumberRef
290290
MCNumberCreateWithReal(t_result, r_output);
291291
}
292292

293+
static void MCMathEvalMinMaxList(MCProperListRef p_list, bool p_is_min, MCNumberRef& r_output)
294+
{
295+
if (MCProperListIsEmpty(p_list))
296+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list must be non-empty"), nil);
297+
298+
if (!MCProperListIsListOfType(p_list, kMCValueTypeCodeNumber))
299+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list must be numeric"), nil);
300+
301+
double t_minmax, t_cur_real;
302+
t_cur_real = MCNumberFetchAsReal((MCNumberRef)MCProperListFetchElementAtIndex(p_list, 0));
303+
t_minmax = t_cur_real;
304+
305+
bool t_replace;
306+
for (uindex_t i = 1; i < MCProperListGetLength(p_list); i++)
307+
{
308+
t_cur_real = MCNumberFetchAsReal((MCNumberRef)MCProperListFetchElementAtIndex(p_list, i));
309+
t_replace = p_is_min ? t_cur_real < t_minmax : t_cur_real > t_minmax;
310+
311+
if (t_replace)
312+
t_minmax = t_cur_real;
313+
}
314+
315+
MCNumberCreateWithReal(t_minmax, r_output);
316+
}
317+
318+
extern "C" MC_DLLEXPORT void MCMathEvalMinList(MCProperListRef p_list, MCNumberRef& r_output)
319+
{
320+
MCMathEvalMinMaxList(p_list, true, r_output);
321+
}
322+
323+
extern "C" MC_DLLEXPORT void MCMathEvalMaxList(MCProperListRef p_list, MCNumberRef& r_output)
324+
{
325+
MCMathEvalMinMaxList(p_list, false, r_output);
326+
}
327+
293328
extern "C" MC_DLLEXPORT void MCMathEvalMaxInteger(integer_t p_left, integer_t p_right, integer_t& r_output)
294329
{
295330
r_output = MCMax(p_left, p_right);

libscript/src/script-instance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ bool MCScriptCallHandlerOfInstanceInternal(MCScriptInstanceRef self, MCScriptHan
17461746

17471747
// Fetch the signature of the current handler.
17481748
MCTypeInfoRef t_signature;
1749-
t_signature = self -> module -> types[t_frame -> handler -> type] -> typeinfo;
1749+
t_signature = t_frame -> instance -> module -> types[t_frame -> handler -> type] -> typeinfo;
17501750

17511751
MCTypeInfoRef t_output_type;
17521752
t_output_type = MCHandlerTypeInfoGetReturnType(t_signature);

0 commit comments

Comments
 (0)