From fd74c8b0718cb4c8a525a76bb944b1cf00b21c8a Mon Sep 17 00:00:00 2001 From: otya128 Date: Fri, 12 Jun 2015 21:53:39 +0900 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E5=88=97=E3=81=AE=E6=B7=BB=E3=81=88?= =?UTF-8?q?=E5=AD=97,=E6=AC=A1=E5=85=83=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/error.d | 17 +++++++++++++++++ SMILEBASIC/type.d | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/SMILEBASIC/error.d b/SMILEBASIC/error.d index f07023f..84c92e1 100644 --- a/SMILEBASIC/error.d +++ b/SMILEBASIC/error.d @@ -19,6 +19,14 @@ class SmileBasicError : Exception super(message); } } +class SyntaxError : SmileBasicError +{ + this() + { + this.errnum = 3; + super("Syntax error"); + } +} class TypeMismatch : SmileBasicError { this() @@ -43,3 +51,12 @@ class ReturnWithoutGosub : SmileBasicError super("RETURN without GOSUB"); } } +class SubscriptOutOfRange : SmileBasicError +{ + this() + { + this.errnum = 31; + super("Subscript out of range"); + } +} + diff --git a/SMILEBASIC/type.d b/SMILEBASIC/type.d index ad22132..a120aa5 100644 --- a/SMILEBASIC/type.d +++ b/SMILEBASIC/type.d @@ -1,4 +1,5 @@ module otya.smilebasic.type; +import otya.smilebasic.error; enum ValueType : byte { Void,//DEF A()ENDの返り値とか未初期化の変数とか @@ -153,34 +154,50 @@ class Array(T) } T opIndex(int i1) { + if(dimCount != 1) throw new SyntaxError(); + if(i1 >= dim[0]) throw new SubscriptOutOfRange(); return array[i1]; } T opIndex(int i1, int i2) { + if(dimCount != 2) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1]) throw new SubscriptOutOfRange(); return array[i1 * dim[0] + i2]; } T opIndex(int i1, int i2, int i3) { + if(dimCount != 3) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1] && i3 >= dim[2]) throw new SubscriptOutOfRange(); return array[i1 * dim[0] * dim[1] + i2 * dim[1] + i3]; } T opIndex(int i1, int i2, int i3, int i4) { + if(dimCount != 4) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1] && i3 >= dim[2] && i4 >= dim[3]) throw new SubscriptOutOfRange(); return array[i1];//array[i1 * dim[0] * dim[1] * dim[2] + i2 * dim[1] + i3]; } T opIndexAssign(T v, int i1) { + if(dimCount != 1) throw new SyntaxError(); + if(i1 >= dim[0]) throw new SubscriptOutOfRange(); return array[i1] = v; } T opIndexAssign(T v, int i1, int i2) { + if(dimCount != 2) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1]) throw new SubscriptOutOfRange(); return array[i1 * dim[0] + i2] = v; } T opIndexAssign(T v, int i1, int i2, int i3) { + if(dimCount != 3) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1] && i3 >= dim[2]) throw new SubscriptOutOfRange(); return array[i1 * dim[0] * dim[1] + i2 * dim[1] + i3] = v; } T opIndexAssign(T v, int i1, int i2, int i3, int i4) { + if(dimCount != 4) throw new SyntaxError(); + if(i1 >= dim[0] && i2 >= dim[1] && i3 >= dim[2] && i4 >= dim[3]) throw new SubscriptOutOfRange(); return array[i1] = v;//array[i1 * dim[0] * dim[1] * dim[2] + i2 * dim[1] + i3] = v; }