update check for cast expression

This commit is contained in:
Dave Holoway
2020-06-07 13:26:46 +01:00
parent 18ff477d34
commit 37dddc48d9

View File

@@ -1378,6 +1378,34 @@ function isExpressionStart(token) {
return /^(ident|primitive-type|[\w-]+-literal|(inc|plumin|unary)-operator|open-bracket|new-operator)$/.test(token.kind);
}
/**
* @param {Token} token first token following the close bracket
* @param {ResolvedIdent} matches - the bracketed expression
*/
function isCastExpression(token, matches) {
// working out if this is supposed to be a cast expression is problematic.
// (a) + b -> cast or binary expression (depends on how a is resolved)
// if the bracketed expression cannot be resolved:
// (a) b -> assumed to be a cast
// (a) + b -> assumed to be an expression
// (a) 5 -> assumed to be a cast
// (a) + 5 -> assumed to be an expression
if (matches.types[0] && !(matches.types[0] instanceof AnyType)) {
// resolved type - this must be a cast
return true;
}
if (!matches.types[0]) {
// not a type - this must be an expression
return false;
}
// if we reach here, the type is AnyType - we assume a cast if the next
// value is the start of an expression, except for +/-
if (token.kind === 'plumin-operator') {
return false;
}
return this.isExpressionStart(token);
}
/**
* @param {TokenList} tokens
* @param {Local[]} locals
@@ -1478,7 +1506,7 @@ function rootTerm(tokens, locals, method, imports, typemap) {
matches = expression(tokens, locals, method, imports, typemap);
const close_bracket = tokens.current;
tokens.expectValue(')');
if (isExpressionStart(tokens.current)) {
if (isCastExpression(tokens.current, matches)) {
// typecast
const type = matches.types[0];
if (!type) {