1
0

多次元配列実装途中

This commit is contained in:
otya128
2015-06-12 21:46:43 +09:00
parent 9b9951e07e
commit 1e51bd37ca
5 changed files with 92 additions and 6 deletions

View File

@@ -545,12 +545,80 @@ class NewArray : Code
vm.global[index].type = ValueType.IntegerArray; vm.global[index].type = ValueType.IntegerArray;
vm.global[index].integerArray = new Array!int(dim); vm.global[index].integerArray = new Array!int(dim);
break; break;
case ValueType.Double:
vm.global[index].type = ValueType.DoubleArray;
vm.global[index].doubleArray = new Array!double(dim);
break;
case ValueType.String:
vm.global[index].type = ValueType.StringArray;
vm.global[index].stringArray = new Array!wstring(dim);
break;
default: default:
throw new TypeMismatch(); throw new TypeMismatch();
break; break;
} }
} }
} }
class PushArray : Code
{
int dim;
this(int dim)
{
this.dim = dim;
}
override void execute(VM vm)
{
int[4] index;
for(int i = dim - 1; i >= 0; i--)
{
Value v;
vm.pop(v);
if(v.type == ValueType.Integer)
{
index[i] = v.integerValue;
continue;
}
if(v.type == ValueType.Double)
{
index[i] = cast(int)v.doubleValue;
continue;
}
throw new TypeMismatch();
}
Value array;
vm.pop(array);
if(!array.isArray)
{
throw new TypeMismatch();
}
if(array.type == ValueType.IntegerArray)
{
vm.push(Value(array.integerArray[index[0..dim]]));
return;
}
if(array.type == ValueType.DoubleArray)
{
vm.push(Value(array.doubleArray[index[0..dim]]));
return;
}
if(array.type == ValueType.StringArray)
{
vm.push(Value(array.stringArray[index[0..dim]]));
return;
}
if(array.type == ValueType.String)
{
if(dim != 1)
{
//TODO:syntaxError
throw new TypeMismatch();
}
vm.push(Value(array.stringValue[index[0]].to!wstring));
return;
}
throw new TypeMismatch();
}
}
class PopGArray : Code class PopGArray : Code
{ {
int var; int var;
@@ -568,7 +636,7 @@ class PopGArray : Code
throw new TypeMismatch(); throw new TypeMismatch();
} }
int[4] index; int[4] index;
for(int i = dim - 1; i >= 0; i--) for(int i = 0; i < dim; i++)//for(int i = dim - 1; i >= 0; i--)
{ {
Value v; Value v;
vm.pop(v); vm.pop(v);

View File

@@ -136,6 +136,19 @@ class Compiler
auto binop = cast(BinaryOperator)exp; auto binop = cast(BinaryOperator)exp;
compileExpression(binop.item1); compileExpression(binop.item1);
compileExpression(binop.item2); compileExpression(binop.item2);
if(binop.operator == TokenType.LBracket)
{
IndexExpressions ie = cast(IndexExpressions)binop.item2;
if(ie)
{
genCode(new PushArray(ie.expressions.length));
}
else
{
genCode(new PushArray(1));
}
break;
}
genCodeOP(binop.operator); genCodeOP(binop.operator);
} }
break; break;

View File

@@ -54,8 +54,8 @@ FOR I=0 TO 10
?I ?I
NEXT NEXT
VAR V[10] VAR V[10]
V[0]=12 V[0,1]=12
?V[0] ?V[1]
?\"ABC\"[1][0] ?\"ABC\"[1][0]
GOSUB @A GOSUB @A
END END

View File

@@ -868,13 +868,17 @@ class Parser
auto tt = token.type; auto tt = token.type;
op.operator = token.type; op.operator = token.type;
lex.popFront(); lex.popFront();
op.item2 = term(order - 1, node);
if(tt == TokenType.LBracket) if(tt == TokenType.LBracket)
{ {
op.item2 = indexExpressions();
//[演算子 //[演算子
if(lex.front().type != TokenType.RBracket) syntaxError(); if(lex.front().type != TokenType.RBracket) syntaxError();
lex.popFront(); lex.popFront();
} }
else
{
op.item2 = term(order - 1, node);
}
token = lex.front(); token = lex.front();
version(none) version(none)
write(tt, " "); write(tt, " ");

View File

@@ -109,9 +109,10 @@ class Array(T)
this(int[] dim) this(int[] dim)
{ {
int len = 1; int len = 1;
foreach(int i; dim) foreach(int i, j; dim)
{ {
len *= i; len *= j;
this.dim[i] = dim[i];
} }
array = new T[len]; array = new T[len];
dimCount = dim.length; dimCount = dim.length;