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].integerArray = new Array!int(dim);
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:
throw new TypeMismatch();
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
{
int var;
@@ -568,7 +636,7 @@ class PopGArray : Code
throw new TypeMismatch();
}
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;
vm.pop(v);

View File

@@ -136,6 +136,19 @@ class Compiler
auto binop = cast(BinaryOperator)exp;
compileExpression(binop.item1);
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);
}
break;

View File

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

View File

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

View File

@@ -109,9 +109,10 @@ class Array(T)
this(int[] dim)
{
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];
dimCount = dim.length;