import { IStoreValue } from "./istore_value"; import { ArrayType } from "../../../typeSystem/array_type"; import { IType } from "../../../typeSystem/itype"; import { Type } from "../../../typeSystem/type"; import { StoreValueAddress } from "./store_value_address"; export class ArrayStoreValue implements IStoreValue { public type: IType; public id?: String; public isConst: boolean; public lines: number; public columns?: number; public values: StoreValueAddress[]; constructor(type: ArrayType, values: StoreValueAddress[], lines: number, columns?: number, id?: String, isConst = false) { this.type = type; this.id = id; this.isConst = isConst this.values = values; this.lines = lines; this.columns = columns; } get (): StoreValueAddress[] { return this.values; } /** * @deprecated * Potential not necessary since array access is occuring directly in the StoreObject * @param line * @param column */ getAt (line: number, column?: number): IStoreValue { if(this.isVector) { if(column != null) { throw new Error(this.id + " is not a matrix!"); } column = line; line = 0; } else if (column == null) { const values: StoreValueAddress[] = []; for(let col = 0; col < this.columns!; col += 1) { const index = line * this.lines + col; values.push(this.values[index]); } const array_type = this.type as ArrayType const vector_type = new ArrayType(array_type.innerType as Type, array_type.dimensions - 1); return new ArrayStoreValue(vector_type, values, this.columns!, undefined, this.id, this.isConst); } const index = line * this.lines + column; console.log("Get at: ",index); return this.values[index]; } /** * @deprecated * Potential not necessary since array access is occuring directly in the StoreObject * @param line * @param column */ setAt (value: IStoreValue, line:number, column?: number): void { if(!(this.type as ArrayType).canAccept(value.type)) { throw new Error("!!!Internal Error: Attempting to insert an invalid value inside array "+this.id); } if(this.isVector) { if(column != null) { throw new Error(this.id + " is not a matrix!"); } column = line; line = 0; } else if (column == null) { throw new Error("!!!Internal Error: Attempting to insert line into matrix "+ this.id ); } const pos = line * this.lines + column; // TODO - fix this casting this.values[pos] = value as StoreValueAddress; } inStore () { return this.id != null; } isVector (): boolean { return (this.type as ArrayType).dimensions == 1; } }