From 361a8e04c1d0debb8ee77729f04638c5fef0adad Mon Sep 17 00:00:00 2001 From: otya128 Date: Sat, 3 Sep 2016 16:07:30 +0900 Subject: [PATCH] =?UTF-8?q?MAX/MIN=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/TEST.txt | 12 ++++++++++ SMILEBASIC/builtinfunctions.d | 44 +++++++++++++++++++++++++++++++++++ SMILEBASIC/type.d | 24 ++++++++++++++++--- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/SMILEBASIC/TEST.txt b/SMILEBASIC/TEST.txt index b4bf3b7..b3ded55 100644 --- a/SMILEBASIC/TEST.txt +++ b/SMILEBASIC/TEST.txt @@ -1,4 +1,16 @@ '--no-direct-mode --file TEST.TXT +DEF MAXMINTEST + ASSERT__ MAX(0,1,2,3,4,5,4,3,2,1,0)==5,"MAX" + ASSERT__ MIN(5,4,3,2,1,0,1,2,3,4,5)==0,"MIN" + DIM A[5] + FOR I=0 TO LEN(A)-1 + A[I]=I+1 + NEXT + ASSERT__ MAX(A)==5,"MAX" + ASSERT__ MIN(A)==1,"MIN" +END +MAXMINTEST + DEF SORTTEST DIM A[4] DIM B$[4] diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index 6048f0d..0d2f6df 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -1831,6 +1831,50 @@ class BuiltinFunction } } } + static double MAX(Value array) + { + import std.algorithm.searching; + if (array.type == ValueType.IntegerArray) + { + return minPos!"a > b"(array.integerArray.array)[0]; + } + if (array.type == ValueType.DoubleArray) + { + return minPos!"a > b"(array.doubleArray.array)[0]; + } + throw new TypeMismatch(); + } + static double MAX(Value[] args) + { + import std.algorithm.searching; + if (args.length == 0) + { + throw new IllegalFunctionCall("MAX"); + } + return minPos!"a.castDouble > b.castDouble"(args)[0].castDouble; + } + static double MIN(Value array) + { + import std.algorithm.searching; + if (array.type == ValueType.IntegerArray) + { + return minPos!"a < b"(array.integerArray.array)[0]; + } + if (array.type == ValueType.DoubleArray) + { + return minPos!"a < b"(array.doubleArray.array)[0]; + } + throw new TypeMismatch(); + } + static double MIN(Value[] args) + { + import std.algorithm.searching; + if (args.length == 0) + { + throw new IllegalFunctionCall("MIN"); + } + return minPos!"a.castDouble < b.castDouble"(args)[0].castDouble; + } //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; static BuiltinFunctions[wstring] builtinFunctions; static wstring getBasicName(BFD)(const wstring def) diff --git a/SMILEBASIC/type.d b/SMILEBASIC/type.d index 1e55475..d2a7fd3 100644 --- a/SMILEBASIC/type.d +++ b/SMILEBASIC/type.d @@ -136,15 +136,33 @@ struct Value } int castInteger() { - return this.type == ValueType.Integer ? this.integerValue : (this.type == ValueType.Double ? cast(int)this.doubleValue : 0); + if (this.type == ValueType.Integer) + { + return this.integerValue; + } + if (this.type == ValueType.Double) + { + return cast(int)this.doubleValue; + } + throw new TypeMismatch(); } double castDouble() { - return this.type == ValueType.Integer ? this.integerValue : (this.type == ValueType.Double ? this.doubleValue : 0); + if (this.type == ValueType.Integer) + { + return this.integerValue; + } + if (this.type == ValueType.Double) + { + return this.doubleValue; + } + throw new TypeMismatch(); } wstring castString() { - return this.type == ValueType.String ? this.stringValue : ""; + if (this.type == ValueType.String) + return this.stringValue; + throw new TypeMismatch(); } string toString() {