Merge pull request #20149 from Overblob/shader_float_typing

Shader language - Add optional float typings
This commit is contained in:
Juan Linietsky 2018-08-10 18:21:28 -03:00 committed by GitHub
commit 0fc1c4eda8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 12 deletions

View File

@ -6410,7 +6410,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int
}
// check for dot or underscore or 'x' for hex notation in floating point number
if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) {
if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f') && !in_word && prev_is_number && !is_number) {
is_number = true;
is_symbol = false;
is_char = false;

View File

@ -528,13 +528,14 @@ ShaderLanguage::Token ShaderLanguage::_get_token() {
bool hexa_found = false;
bool sign_found = false;
bool minus_exponent_found = false;
bool float_suffix_found = false;
String str;
int i = 0;
while (true) {
if (GETCHAR(i) == '.') {
if (period_found || exponent_found)
if (period_found || exponent_found || hexa_found || float_suffix_found)
return _make_token(TK_ERROR, "Invalid numeric constant");
period_found = true;
} else if (GETCHAR(i) == 'x') {
@ -542,11 +543,16 @@ ShaderLanguage::Token ShaderLanguage::_get_token() {
return _make_token(TK_ERROR, "Invalid numeric constant");
hexa_found = true;
} else if (GETCHAR(i) == 'e') {
if (hexa_found || exponent_found)
if (hexa_found || exponent_found || float_suffix_found)
return _make_token(TK_ERROR, "Invalid numeric constant");
exponent_found = true;
} else if (GETCHAR(i) == 'f') {
if (hexa_found || exponent_found)
return _make_token(TK_ERROR, "Invalid numeric constant");
float_suffix_found = true;
} else if (_is_number(GETCHAR(i))) {
//all ok
if (float_suffix_found)
return _make_token(TK_ERROR, "Invalid numeric constant");
} else if (hexa_found && _is_hex(GETCHAR(i))) {
} else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
@ -562,21 +568,60 @@ ShaderLanguage::Token ShaderLanguage::_get_token() {
i++;
}
if (!_is_number(str[str.length() - 1]))
CharType last_char = str[str.length() - 1];
if (hexa_found) {
//hex integers eg."0xFF" or "0x12AB", etc - NOT supported yet
return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant - Not supported");
} else if (period_found || float_suffix_found) {
//floats
if (period_found) {
if (float_suffix_found) {
//checks for eg "1.f" or "1.99f" notations
if (last_char != 'f') {
return _make_token(TK_ERROR, "Invalid (float) numeric constant");
}
} else {
//checks for eg. "1." or "1.99" notations
if (last_char != '.' && !_is_number(last_char)) {
return _make_token(TK_ERROR, "Invalid (float) numeric constant");
}
}
} else if (float_suffix_found) {
// if no period found the float suffix must be the last character, like in "2f" for "2.0"
if (last_char != 'f') {
return _make_token(TK_ERROR, "Invalid (float) numeric constant");
}
}
if (float_suffix_found) {
//strip the suffix
str = str.left(str.length() - 1);
//compensate reading cursor position
char_idx += 1;
}
if (!str.is_valid_float()) {
return _make_token(TK_ERROR, "Invalid (float) numeric constant");
}
} else {
//integers
if (!_is_number(last_char)) {
return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
}
if (!str.is_valid_integer()) {
return _make_token(TK_ERROR, "Invalid numeric constant");
}
}
char_idx += str.length();
Token tk;
if (period_found || minus_exponent_found)
if (period_found || minus_exponent_found || float_suffix_found)
tk.type = TK_REAL_CONSTANT;
else
tk.type = TK_INT_CONSTANT;
if (!str.is_valid_float()) {
return _make_token(TK_ERROR, "Invalid numeric constant");
}
tk.constant = str.to_double();
tk.constant = str.to_double(); //wont work with hex
tk.line = tk_line;
return tk;