add new array validation

This commit is contained in:
Dave Holoway
2020-06-18 14:54:12 +01:00
parent d869afe2fa
commit f05b34171c
4 changed files with 70 additions and 8 deletions

View File

@@ -6,7 +6,9 @@
* @typedef {import('java-mti').JavaType} JavaType
*/
const { Expression } = require("./Expression");
const { ArrayType } = require('java-mti');
const { ArrayType, PrimitiveType } = require('java-mti');
const { FixedLengthArrayType, SourceArrayType } = require('../source-types');
const ParseProblem = require('../parsetypes/parse-problem');
class NewArray extends Expression {
/**
@@ -19,18 +21,45 @@ class NewArray extends Expression {
this.new_token = new_token;
this.element_type = element_type;
this.dimensions = dimensions;
this.array_type = new ArrayType(element_type.resolved, 1);
}
/**
* @param {ResolveInfo} ri
*/
resolveExpression(ri) {
return this.array_type;
/** @type {ResolvedIdent[]} */
const fixed_dimensions = [];
const type = this.dimensions.types[0];
for (let x = type; ;) {
if (x instanceof FixedLengthArrayType) {
fixed_dimensions.unshift(x.length);
x = x.parent_type;
continue;
}
if (x instanceof SourceArrayType) {
x = x.parent_type;
continue;
}
break;
}
const arrdims = type instanceof ArrayType ? type.arrdims : 1;
const array_type = new ArrayType(this.element_type.resolved, arrdims);
fixed_dimensions.forEach(d => {
const idx = d.resolveExpression(ri);
if (idx instanceof PrimitiveType) {
if (!/^[BSI]$/.test(idx.typeSignature)) {
ri.problems.push(ParseProblem.Error(d.tokens, `Expression of type '${idx.label}' is not valid as an array dimension`));
}
return;
}
ri.problems.push(ParseProblem.Error(d.tokens, `Integer value expected`));
})
return array_type;
}
tokens() {
return [this.new_token, ...this.element_type.tokens, ...this.dimensions.tokens];
return [this.new_token, ...this.dimensions.tokens];
}
}