2017-03-05 14:47:28 +00:00
/*************************************************************************/
/* visual_script_expression.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2017-03-05 14:47:28 +00:00
/*************************************************************************/
2022-01-03 20:27:34 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 14:47:28 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2016-09-04 13:34:40 +00:00
# include "visual_script_expression.h"
2017-03-05 15:44:50 +00:00
bool VisualScriptExpression : : _set ( const StringName & p_name , const Variant & p_value ) {
if ( String ( p_name ) = = " expression " ) {
expression = p_value ;
expression_dirty = true ;
2016-09-04 13:34:40 +00:00
ports_changed_notify ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " out_type " ) {
output_type = Variant : : Type ( int ( p_value ) ) ;
expression_dirty = true ;
2016-09-04 13:34:40 +00:00
ports_changed_notify ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " sequenced " ) {
sequenced = p_value ;
2016-09-04 13:34:40 +00:00
ports_changed_notify ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " input_count " ) {
int from = inputs . size ( ) ;
2016-09-04 13:34:40 +00:00
inputs . resize ( int ( p_value ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = from ; i < inputs . size ( ) ; i + + ) {
2018-07-25 01:11:03 +00:00
inputs . write [ i ] . name = String : : chr ( ' a ' + i ) ;
2017-03-05 15:44:50 +00:00
if ( from = = 0 ) {
2018-07-25 01:11:03 +00:00
inputs . write [ i ] . type = output_type ;
2016-09-04 13:34:40 +00:00
} else {
2018-07-25 01:11:03 +00:00
inputs . write [ i ] . type = inputs [ from - 1 ] . type ;
2016-09-04 13:34:40 +00:00
}
}
2017-03-05 15:44:50 +00:00
expression_dirty = true ;
2016-09-04 13:34:40 +00:00
ports_changed_notify ( ) ;
2021-02-10 20:18:45 +00:00
notify_property_list_changed ( ) ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-07-01 00:30:17 +00:00
if ( String ( p_name ) . begins_with ( " input_ " ) ) {
int idx = String ( p_name ) . get_slicec ( ' _ ' , 1 ) . get_slicec ( ' / ' , 0 ) . to_int ( ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX_V ( idx , inputs . size ( ) , false ) ;
2016-09-04 13:34:40 +00:00
2017-07-01 00:30:17 +00:00
String what = String ( p_name ) . get_slice ( " / " , 1 ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
if ( what = = " type " ) {
2018-07-25 01:11:03 +00:00
inputs . write [ idx ] . type = Variant : : Type ( int ( p_value ) ) ;
2017-03-05 15:44:50 +00:00
} else if ( what = = " name " ) {
2018-07-25 01:11:03 +00:00
inputs . write [ idx ] . name = p_value ;
2016-09-04 13:34:40 +00:00
} else {
return false ;
}
2017-03-05 15:44:50 +00:00
expression_dirty = true ;
2016-09-04 13:34:40 +00:00
ports_changed_notify ( ) ;
return true ;
}
return false ;
}
2017-03-05 15:44:50 +00:00
bool VisualScriptExpression : : _get ( const StringName & p_name , Variant & r_ret ) const {
if ( String ( p_name ) = = " expression " ) {
r_ret = expression ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " out_type " ) {
r_ret = output_type ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " sequenced " ) {
r_ret = sequenced ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " input_count " ) {
r_ret = inputs . size ( ) ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-07-01 00:30:17 +00:00
if ( String ( p_name ) . begins_with ( " input_ " ) ) {
int idx = String ( p_name ) . get_slicec ( ' _ ' , 1 ) . get_slicec ( ' / ' , 0 ) . to_int ( ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX_V ( idx , inputs . size ( ) , false ) ;
2016-09-04 13:34:40 +00:00
2017-07-01 00:30:17 +00:00
String what = String ( p_name ) . get_slice ( " / " , 1 ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
if ( what = = " type " ) {
r_ret = inputs [ idx ] . type ;
} else if ( what = = " name " ) {
r_ret = inputs [ idx ] . name ;
2016-09-04 13:34:40 +00:00
} else {
return false ;
}
return true ;
}
return false ;
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
void VisualScriptExpression : : _get_property_list ( List < PropertyInfo > * p_list ) const {
String argt = " Any " ;
for ( int i = 1 ; i < Variant : : VARIANT_MAX ; i + + ) {
argt + = " , " + Variant : : get_type_name ( Variant : : Type ( i ) ) ;
2016-09-04 13:34:40 +00:00
}
2021-11-03 22:06:17 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : STRING , " expression " , PROPERTY_HINT_NONE , " " , PROPERTY_USAGE_NO_EDITOR ) ) ;
2017-03-05 15:44:50 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : INT , " out_type " , PROPERTY_HINT_ENUM , argt ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : INT , " input_count " , PROPERTY_HINT_RANGE , " 0,64,1 " ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : BOOL , " sequenced " ) ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < inputs . size ( ) ; i + + ) {
2017-07-01 00:30:17 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : INT , " input_ " + itos ( i ) + " /type " , PROPERTY_HINT_ENUM , argt ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : STRING , " input_ " + itos ( i ) + " /name " ) ) ;
2016-09-04 13:34:40 +00:00
}
}
int VisualScriptExpression : : get_output_sequence_port_count ( ) const {
2017-03-05 15:44:50 +00:00
return sequenced ? 1 : 0 ;
2016-09-04 13:34:40 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
bool VisualScriptExpression : : has_input_sequence_port ( ) const {
2016-09-04 13:34:40 +00:00
return sequenced ;
}
2017-03-05 15:44:50 +00:00
String VisualScriptExpression : : get_output_sequence_port_text ( int p_port ) const {
2016-09-04 13:34:40 +00:00
return String ( ) ;
}
2017-03-05 15:44:50 +00:00
int VisualScriptExpression : : get_input_value_port_count ( ) const {
2016-09-04 13:34:40 +00:00
return inputs . size ( ) ;
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
int VisualScriptExpression : : get_output_value_port_count ( ) const {
2016-09-04 13:34:40 +00:00
return 1 ;
}
2017-03-05 15:44:50 +00:00
PropertyInfo VisualScriptExpression : : get_input_value_port_info ( int p_idx ) const {
return PropertyInfo ( inputs [ p_idx ] . type , inputs [ p_idx ] . name ) ;
2016-09-04 13:34:40 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
PropertyInfo VisualScriptExpression : : get_output_value_port_info ( int p_idx ) const {
return PropertyInfo ( output_type , " result " ) ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
String VisualScriptExpression : : get_caption ( ) const {
2022-03-11 15:36:16 +00:00
return RTR ( " Expression " ) ;
2016-09-04 13:34:40 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
String VisualScriptExpression : : get_text ( ) const {
2016-09-04 13:34:40 +00:00
return expression ;
}
2017-03-05 15:44:50 +00:00
Error VisualScriptExpression : : _get_token ( Token & r_token ) {
2016-09-04 13:34:40 +00:00
while ( true ) {
2017-03-05 15:44:50 +00:00
# define GET_CHAR() (str_ofs >= expression.length() ? 0 : expression[str_ofs++])
2016-09-04 13:34:40 +00:00
2020-07-27 10:43:20 +00:00
char32_t cchar = GET_CHAR ( ) ;
2017-03-05 15:44:50 +00:00
if ( cchar = = 0 ) {
r_token . type = TK_EOF ;
2016-09-04 13:34:40 +00:00
return OK ;
}
2017-03-05 15:44:50 +00:00
switch ( cchar ) {
2016-09-04 13:34:40 +00:00
case 0 : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_EOF ;
2016-09-04 13:34:40 +00:00
return OK ;
} break ;
case ' { ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_CURLY_BRACKET_OPEN ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' } ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_CURLY_BRACKET_CLOSE ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' [ ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_BRACKET_OPEN ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' ] ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_BRACKET_CLOSE ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' ( ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_PARENTHESIS_OPEN ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' ) ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_PARENTHESIS_CLOSE ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' , ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_COMMA ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' : ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_COLON ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' . ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_PERIOD ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' = ' : {
2017-03-05 15:44:50 +00:00
cchar = GET_CHAR ( ) ;
if ( cchar = = ' = ' ) {
r_token . type = TK_OP_EQUAL ;
2016-09-04 13:34:40 +00:00
} else {
_set_error ( " Expected '=' " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
}
return OK ;
} ;
case ' ! ' : {
2017-03-05 15:44:50 +00:00
if ( expression [ str_ofs ] = = ' = ' ) {
r_token . type = TK_OP_NOT_EQUAL ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
} else {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_NOT ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} ;
case ' > ' : {
2017-03-05 15:44:50 +00:00
if ( expression [ str_ofs ] = = ' = ' ) {
r_token . type = TK_OP_GREATER_EQUAL ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
2017-03-05 15:44:50 +00:00
} else if ( expression [ str_ofs ] = = ' > ' ) {
r_token . type = TK_OP_SHIFT_RIGHT ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
} else {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_GREATER ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} ;
case ' < ' : {
2017-03-05 15:44:50 +00:00
if ( expression [ str_ofs ] = = ' = ' ) {
r_token . type = TK_OP_LESS_EQUAL ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
2017-03-05 15:44:50 +00:00
} else if ( expression [ str_ofs ] = = ' < ' ) {
r_token . type = TK_OP_SHIFT_LEFT ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
} else {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_LESS ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} ;
case ' + ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_ADD ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' - ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_SUB ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' / ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_DIV ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' * ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_MUL ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' % ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_MOD ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' & ' : {
2017-03-05 15:44:50 +00:00
if ( expression [ str_ofs ] = = ' & ' ) {
r_token . type = TK_OP_AND ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
} else {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_BIT_AND ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} ;
case ' | ' : {
2017-03-05 15:44:50 +00:00
if ( expression [ str_ofs ] = = ' | ' ) {
r_token . type = TK_OP_OR ;
2016-09-04 13:34:40 +00:00
str_ofs + + ;
} else {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_BIT_OR ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} ;
case ' ^ ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_BIT_XOR ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' ~ ' : {
2017-03-05 15:44:50 +00:00
r_token . type = TK_OP_BIT_INVERT ;
2016-09-04 13:34:40 +00:00
return OK ;
} ;
case ' " ' : {
String str ;
2022-01-30 13:44:07 +00:00
char32_t prev = 0 ;
2017-03-05 15:44:50 +00:00
while ( true ) {
2020-07-27 10:43:20 +00:00
char32_t ch = GET_CHAR ( ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
if ( ch = = 0 ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Unterminated String " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
2017-03-05 15:44:50 +00:00
} else if ( ch = = ' " ' ) {
2016-09-04 13:34:40 +00:00
break ;
2017-03-05 15:44:50 +00:00
} else if ( ch = = ' \\ ' ) {
2016-09-04 13:34:40 +00:00
//escaped characters...
2020-07-27 10:43:20 +00:00
char32_t next = GET_CHAR ( ) ;
2017-03-05 15:44:50 +00:00
if ( next = = 0 ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Unterminated String " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
2016-09-04 13:34:40 +00:00
}
2020-07-27 10:43:20 +00:00
char32_t res = 0 ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
switch ( next ) {
2020-05-10 11:00:47 +00:00
case ' b ' :
res = 8 ;
break ;
case ' t ' :
res = 9 ;
break ;
case ' n ' :
res = 10 ;
break ;
case ' f ' :
res = 12 ;
break ;
case ' r ' :
res = 13 ;
break ;
2022-01-30 13:44:07 +00:00
case ' U ' :
2016-09-04 13:34:40 +00:00
case ' u ' : {
2022-01-30 13:44:07 +00:00
// Hexadecimal sequence.
int hex_len = ( next = = ' U ' ) ? 6 : 4 ;
for ( int j = 0 ; j < hex_len ; j + + ) {
2020-07-27 10:43:20 +00:00
char32_t c = GET_CHAR ( ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
if ( c = = 0 ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Unterminated String " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
}
2022-02-04 08:32:20 +00:00
if ( ! is_hex_digit ( c ) ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Malformed hex constant in string " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
}
2020-07-27 10:43:20 +00:00
char32_t v ;
2022-02-04 08:32:20 +00:00
if ( is_digit ( c ) ) {
2017-03-05 15:44:50 +00:00
v = c - ' 0 ' ;
} else if ( c > = ' a ' & & c < = ' f ' ) {
v = c - ' a ' ;
v + = 10 ;
} else if ( c > = ' A ' & & c < = ' F ' ) {
v = c - ' A ' ;
v + = 10 ;
2016-09-04 13:34:40 +00:00
} else {
2020-02-13 10:37:37 +00:00
ERR_PRINT ( " Bug parsing hex constant. " ) ;
2017-03-05 15:44:50 +00:00
v = 0 ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
res < < = 4 ;
res | = v ;
2016-09-04 13:34:40 +00:00
}
} break ;
default : {
res = next ;
} break ;
}
2022-01-30 13:44:07 +00:00
// Parse UTF-16 pair.
if ( ( res & 0xfffffc00 ) = = 0xd800 ) {
if ( prev = = 0 ) {
prev = res ;
continue ;
} else {
_set_error ( " Invalid UTF-16 sequence in string, unpaired lead surrogate " ) ;
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
}
} else if ( ( res & 0xfffffc00 ) = = 0xdc00 ) {
if ( prev = = 0 ) {
_set_error ( " Invalid UTF-16 sequence in string, unpaired trail surrogate " ) ;
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
} else {
res = ( prev < < 10UL ) + res - ( ( 0xd800 < < 10UL ) + 0xdc00 - 0x10000 ) ;
prev = 0 ;
}
}
if ( prev ! = 0 ) {
_set_error ( " Invalid UTF-16 sequence in string, unpaired lead surrogate " ) ;
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
}
2017-03-05 15:44:50 +00:00
str + = res ;
2016-09-04 13:34:40 +00:00
} else {
2022-01-30 13:44:07 +00:00
if ( prev ! = 0 ) {
_set_error ( " Invalid UTF-16 sequence in string, unpaired lead surrogate " ) ;
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
}
2017-03-05 15:44:50 +00:00
str + = ch ;
2016-09-04 13:34:40 +00:00
}
}
2022-01-30 13:44:07 +00:00
if ( prev ! = 0 ) {
_set_error ( " Invalid UTF-16 sequence in string, unpaired lead surrogate " ) ;
r_token . type = TK_ERROR ;
return ERR_PARSE_ERROR ;
}
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
r_token . type = TK_CONSTANT ;
r_token . value = str ;
2016-09-04 13:34:40 +00:00
return OK ;
} break ;
default : {
2017-03-05 15:44:50 +00:00
if ( cchar < = 32 ) {
2016-09-04 13:34:40 +00:00
break ;
}
2022-02-04 08:32:20 +00:00
if ( is_digit ( cchar ) ) {
2016-09-04 13:34:40 +00:00
//a number
String num ;
# define READING_SIGN 0
# define READING_INT 1
# define READING_DEC 2
# define READING_EXP 3
# define READING_DONE 4
2017-03-05 15:44:50 +00:00
int reading = READING_INT ;
2016-09-04 13:34:40 +00:00
2020-07-27 10:43:20 +00:00
char32_t c = cchar ;
2017-03-05 15:44:50 +00:00
bool exp_sign = false ;
bool exp_beg = false ;
bool is_float = false ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
switch ( reading ) {
2016-09-04 13:34:40 +00:00
case READING_INT : {
2022-02-04 08:32:20 +00:00
if ( is_digit ( c ) ) {
2016-09-04 13:34:40 +00:00
//pass
2017-03-05 15:44:50 +00:00
} else if ( c = = ' . ' ) {
reading = READING_DEC ;
is_float = true ;
} else if ( c = = ' e ' ) {
reading = READING_EXP ;
2016-09-04 13:34:40 +00:00
} else {
2017-03-05 15:44:50 +00:00
reading = READING_DONE ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
} break ;
2016-09-04 13:34:40 +00:00
case READING_DEC : {
2022-02-04 08:32:20 +00:00
if ( is_digit ( c ) ) {
2017-03-05 15:44:50 +00:00
} else if ( c = = ' e ' ) {
reading = READING_EXP ;
2016-09-04 13:34:40 +00:00
} else {
2017-03-05 15:44:50 +00:00
reading = READING_DONE ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
} break ;
2016-09-04 13:34:40 +00:00
case READING_EXP : {
2022-02-04 08:32:20 +00:00
if ( is_digit ( c ) ) {
2017-03-05 15:44:50 +00:00
exp_beg = true ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
} else if ( ( c = = ' - ' | | c = = ' + ' ) & & ! exp_sign & & ! exp_beg ) {
2020-05-14 14:41:43 +00:00
if ( c = = ' - ' ) {
2017-03-05 15:44:50 +00:00
is_float = true ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
exp_sign = true ;
2016-09-04 13:34:40 +00:00
} else {
2017-03-05 15:44:50 +00:00
reading = READING_DONE ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
} break ;
2016-09-04 13:34:40 +00:00
}
2020-05-14 14:41:43 +00:00
if ( reading = = READING_DONE ) {
2016-09-04 13:34:40 +00:00
break ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
num + = String : : chr ( c ) ;
2016-09-04 13:34:40 +00:00
c = GET_CHAR ( ) ;
}
str_ofs - - ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_CONSTANT ;
2016-09-04 13:34:40 +00:00
2020-05-14 14:41:43 +00:00
if ( is_float ) {
2020-07-24 18:07:57 +00:00
r_token . value = num . to_float ( ) ;
2020-05-14 14:41:43 +00:00
} else {
2017-03-05 15:44:50 +00:00
r_token . value = num . to_int ( ) ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
return OK ;
2022-02-04 08:32:20 +00:00
} else if ( is_ascii_char ( cchar ) | | cchar = = ' _ ' ) {
2016-09-04 13:34:40 +00:00
String id ;
2017-03-05 15:44:50 +00:00
bool first = true ;
2016-09-04 13:34:40 +00:00
2022-02-04 08:32:20 +00:00
while ( is_ascii_char ( cchar ) | | cchar = = ' _ ' | | ( ! first & & is_digit ( cchar ) ) ) {
2017-03-05 15:44:50 +00:00
id + = String : : chr ( cchar ) ;
cchar = GET_CHAR ( ) ;
first = false ;
2016-09-04 13:34:40 +00:00
}
str_ofs - - ; //go back one
2017-03-05 15:44:50 +00:00
if ( id = = " in " ) {
r_token . type = TK_OP_IN ;
} else if ( id = = " null " ) {
r_token . type = TK_CONSTANT ;
r_token . value = Variant ( ) ;
} else if ( id = = " true " ) {
r_token . type = TK_CONSTANT ;
r_token . value = true ;
} else if ( id = = " false " ) {
r_token . type = TK_CONSTANT ;
r_token . value = false ;
} else if ( id = = " PI " ) {
r_token . type = TK_CONSTANT ;
r_token . value = Math_PI ;
2017-11-04 09:34:27 +00:00
} else if ( id = = " TAU " ) {
r_token . type = TK_CONSTANT ;
r_token . value = Math_TAU ;
2017-02-06 22:44:22 +00:00
} else if ( id = = " INF " ) {
r_token . type = TK_CONSTANT ;
2021-07-21 08:40:31 +00:00
r_token . value = INFINITY ;
2017-02-06 22:44:22 +00:00
} else if ( id = = " NAN " ) {
r_token . type = TK_CONSTANT ;
2021-07-21 08:40:31 +00:00
r_token . value = NAN ;
2017-03-05 15:44:50 +00:00
} else if ( id = = " not " ) {
r_token . type = TK_OP_NOT ;
} else if ( id = = " or " ) {
r_token . type = TK_OP_OR ;
} else if ( id = = " and " ) {
r_token . type = TK_OP_AND ;
} else if ( id = = " self " ) {
r_token . type = TK_SELF ;
2016-09-04 13:34:40 +00:00
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < Variant : : VARIANT_MAX ; i + + ) {
if ( id = = Variant : : get_type_name ( Variant : : Type ( i ) ) ) {
r_token . type = TK_BASIC_TYPE ;
r_token . value = i ;
2016-09-04 13:34:40 +00:00
return OK ;
}
}
2016-09-06 22:12:28 +00:00
VisualScriptBuiltinFunc : : BuiltinFunc bifunc = VisualScriptBuiltinFunc : : find_function ( id ) ;
2017-03-05 15:44:50 +00:00
if ( bifunc ! = VisualScriptBuiltinFunc : : FUNC_MAX ) {
r_token . type = TK_BUILTIN_FUNC ;
r_token . value = bifunc ;
2016-09-06 22:12:28 +00:00
return OK ;
}
2017-03-05 15:44:50 +00:00
r_token . type = TK_IDENTIFIER ;
r_token . value = id ;
2016-09-04 13:34:40 +00:00
}
return OK ;
} else {
_set_error ( " Unexpected character. " ) ;
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
}
}
}
}
2017-03-05 15:44:50 +00:00
r_token . type = TK_ERROR ;
2016-09-04 13:34:40 +00:00
return ERR_PARSE_ERROR ;
}
2017-03-05 15:44:50 +00:00
const char * VisualScriptExpression : : token_name [ TK_MAX ] = {
" CURLY BRACKET OPEN " ,
" CURLY BRACKET CLOSE " ,
" BRACKET OPEN " ,
" BRACKET CLOSE " ,
" PARENTHESIS OPEN " ,
" PARENTHESIS CLOSE " ,
" IDENTIFIER " ,
" BUILTIN FUNC " ,
" SELF " ,
" CONSTANT " ,
" BASIC TYPE " ,
" COLON " ,
" COMMA " ,
" PERIOD " ,
" OP IN " ,
" OP EQUAL " ,
" OP NOT EQUAL " ,
" OP LESS " ,
" OP LESS EQUAL " ,
" OP GREATER " ,
" OP GREATER EQUAL " ,
" OP AND " ,
" OP OR " ,
" OP NOT " ,
" OP ADD " ,
" OP SUB " ,
" OP MUL " ,
" OP DIV " ,
" OP MOD " ,
" OP SHIFT LEFT " ,
" OP SHIFT RIGHT " ,
" OP BIT AND " ,
" OP BIT OR " ,
" OP BIT XOR " ,
" OP BIT INVERT " ,
" EOF " ,
" ERROR "
2016-09-04 13:34:40 +00:00
} ;
2017-03-05 15:44:50 +00:00
VisualScriptExpression : : ENode * VisualScriptExpression : : _parse_expression ( ) {
2016-09-04 13:34:40 +00:00
Vector < Expression > expression ;
2017-03-05 15:44:50 +00:00
while ( true ) {
2016-09-04 13:34:40 +00:00
//keep appending stuff to expression
2020-04-01 23:20:12 +00:00
ENode * expr = nullptr ;
2016-09-04 13:34:40 +00:00
Token tk ;
_get_token ( tk ) ;
2020-05-14 14:41:43 +00:00
if ( error_set ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
switch ( tk . type ) {
2016-09-04 13:34:40 +00:00
case TK_CURLY_BRACKET_OPEN : {
//a dictionary
DictionaryNode * dn = alloc_node < DictionaryNode > ( ) ;
2017-03-05 15:44:50 +00:00
while ( true ) {
int cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_CURLY_BRACKET_CLOSE ) {
2016-09-04 13:34:40 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
str_ofs = cofs ; //revert
2016-09-04 13:34:40 +00:00
//parse an expression
2019-02-12 20:10:08 +00:00
ENode * expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2019-02-12 20:10:08 +00:00
dn - > dict . push_back ( expr2 ) ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_COLON ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Expected ':' " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
2019-02-12 20:10:08 +00:00
expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2019-02-12 20:10:08 +00:00
dn - > dict . push_back ( expr2 ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_COMMA ) {
2016-09-04 13:34:40 +00:00
//all good
2017-03-05 15:44:50 +00:00
} else if ( tk . type = = TK_CURLY_BRACKET_CLOSE ) {
str_ofs = cofs ;
2016-09-04 13:34:40 +00:00
} else {
_set_error ( " Expected ',' or '}' " ) ;
}
}
2017-03-05 15:44:50 +00:00
expr = dn ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_BRACKET_OPEN : {
//an array
ArrayNode * an = alloc_node < ArrayNode > ( ) ;
2017-03-05 15:44:50 +00:00
while ( true ) {
int cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_BRACKET_CLOSE ) {
2016-09-04 13:34:40 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
str_ofs = cofs ; //revert
2016-09-04 13:34:40 +00:00
//parse an expression
2019-02-12 20:10:08 +00:00
ENode * expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2019-02-12 20:10:08 +00:00
an - > array . push_back ( expr2 ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_COMMA ) {
2016-09-04 13:34:40 +00:00
//all good
2017-03-05 15:44:50 +00:00
} else if ( tk . type = = TK_BRACKET_CLOSE ) {
str_ofs = cofs ;
2016-09-04 13:34:40 +00:00
} else {
_set_error ( " Expected ',' or ']' " ) ;
}
}
2017-03-05 15:44:50 +00:00
expr = an ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_PARENTHESIS_OPEN : {
//a suexpression
2017-03-05 15:44:50 +00:00
ENode * e = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( error_set ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_PARENTHESIS_CLOSE ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Expected ')' " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
expr = e ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_IDENTIFIER : {
String what = tk . value ;
2017-03-05 15:44:50 +00:00
int index = - 1 ;
for ( int i = 0 ; i < inputs . size ( ) ; i + + ) {
if ( what = = inputs [ i ] . name ) {
index = i ;
2016-09-04 13:34:40 +00:00
break ;
}
}
2017-03-05 15:44:50 +00:00
if ( index ! = - 1 ) {
2016-09-04 13:34:40 +00:00
InputNode * input = alloc_node < InputNode > ( ) ;
2017-03-05 15:44:50 +00:00
input - > index = index ;
expr = input ;
2016-09-04 13:34:40 +00:00
} else {
2017-03-05 15:44:50 +00:00
_set_error ( " Invalid input identifier ' " + what + " '. For script variables, use self (locals are for inputs). " + what ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
} break ;
case TK_SELF : {
SelfNode * self = alloc_node < SelfNode > ( ) ;
2017-03-05 15:44:50 +00:00
expr = self ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_CONSTANT : {
ConstantNode * constant = alloc_node < ConstantNode > ( ) ;
2017-03-05 15:44:50 +00:00
constant - > value = tk . value ;
expr = constant ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_BASIC_TYPE : {
//constructor..
Variant : : Type bt = Variant : : Type ( int ( tk . value ) ) ;
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_PARENTHESIS_OPEN ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Expected '(' " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
ConstructorNode * constructor = alloc_node < ConstructorNode > ( ) ;
2017-03-05 15:44:50 +00:00
constructor - > data_type = bt ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
int cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
2016-09-04 13:34:40 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
str_ofs = cofs ; //revert
2016-09-04 13:34:40 +00:00
//parse an expression
2019-02-12 20:10:08 +00:00
ENode * expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2019-02-12 20:10:08 +00:00
constructor - > arguments . push_back ( expr2 ) ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_COMMA ) {
2016-09-04 13:34:40 +00:00
//all good
2017-03-05 15:44:50 +00:00
} else if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
str_ofs = cofs ;
2016-09-04 13:34:40 +00:00
} else {
_set_error ( " Expected ',' or ')' " ) ;
}
}
2017-03-05 15:44:50 +00:00
expr = constructor ;
2016-09-04 13:34:40 +00:00
2016-09-06 22:12:28 +00:00
} break ;
case TK_BUILTIN_FUNC : {
//builtin function
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_PARENTHESIS_OPEN ) {
2016-09-06 22:12:28 +00:00
_set_error ( " Expected '(' " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-06 22:12:28 +00:00
}
BuiltinFuncNode * bifunc = alloc_node < BuiltinFuncNode > ( ) ;
2017-03-05 15:44:50 +00:00
bifunc - > func = VisualScriptBuiltinFunc : : BuiltinFunc ( int ( tk . value ) ) ;
2016-09-06 22:12:28 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
int cofs = str_ofs ;
2016-09-06 22:12:28 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
2016-09-06 22:12:28 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
str_ofs = cofs ; //revert
2016-09-06 22:12:28 +00:00
//parse an expression
2019-02-12 20:10:08 +00:00
ENode * expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-06 22:12:28 +00:00
2019-02-12 20:10:08 +00:00
bifunc - > arguments . push_back ( expr2 ) ;
2016-09-06 22:12:28 +00:00
2017-03-05 15:44:50 +00:00
cofs = str_ofs ;
2016-09-06 22:12:28 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_COMMA ) {
2016-09-06 22:12:28 +00:00
//all good
2017-03-05 15:44:50 +00:00
} else if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
str_ofs = cofs ;
2016-09-06 22:12:28 +00:00
} else {
_set_error ( " Expected ',' or ')' " ) ;
}
}
int expected_args = VisualScriptBuiltinFunc : : get_func_argument_count ( bifunc - > func ) ;
if ( bifunc - > arguments . size ( ) ! = expected_args ) {
2017-03-05 15:44:50 +00:00
_set_error ( " Builtin func ' " + VisualScriptBuiltinFunc : : get_func_name ( bifunc - > func ) + " ' expects " + itos ( expected_args ) + " arguments. " ) ;
2016-09-06 22:12:28 +00:00
}
2017-03-05 15:44:50 +00:00
expr = bifunc ;
2016-09-06 22:12:28 +00:00
2016-09-04 13:34:40 +00:00
} break ;
case TK_OP_SUB : {
Expression e ;
2017-03-05 15:44:50 +00:00
e . is_op = true ;
e . op = Variant : : OP_NEGATE ;
2016-09-04 13:34:40 +00:00
expression . push_back ( e ) ;
continue ;
} break ;
case TK_OP_NOT : {
Expression e ;
2017-03-05 15:44:50 +00:00
e . is_op = true ;
e . op = Variant : : OP_NOT ;
2016-09-04 13:34:40 +00:00
expression . push_back ( e ) ;
continue ;
} break ;
default : {
_set_error ( " Expected expression. " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
} break ;
}
//before going to operators, must check indexing!
2017-03-05 15:44:50 +00:00
while ( true ) {
int cofs2 = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2020-05-14 14:41:43 +00:00
if ( error_set ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
bool done = false ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
switch ( tk . type ) {
2016-09-04 13:34:40 +00:00
case TK_BRACKET_OPEN : {
//value indexing
IndexNode * index = alloc_node < IndexNode > ( ) ;
2017-03-05 15:44:50 +00:00
index - > base = expr ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
ENode * what = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! what ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
index - > index = what ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_BRACKET_CLOSE ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Expected ']' at end of index. " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
expr = index ;
2016-09-04 13:34:40 +00:00
} break ;
case TK_PERIOD : {
//named indexing or function call
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type ! = TK_IDENTIFIER ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Expected identifier after '.' " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
StringName identifier = tk . value ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
int cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_PARENTHESIS_OPEN ) {
2016-09-04 13:34:40 +00:00
//function call
CallNode * func_call = alloc_node < CallNode > ( ) ;
2017-03-05 15:44:50 +00:00
func_call - > method = identifier ;
func_call - > base = expr ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
2019-02-12 20:10:08 +00:00
int cofs3 = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
2016-09-04 13:34:40 +00:00
break ;
}
2019-02-12 20:10:08 +00:00
str_ofs = cofs3 ; //revert
2016-09-04 13:34:40 +00:00
//parse an expression
2019-02-12 20:10:08 +00:00
ENode * expr2 = _parse_expression ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! expr2 ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2019-02-12 20:10:08 +00:00
func_call - > arguments . push_back ( expr2 ) ;
2016-09-04 13:34:40 +00:00
2019-02-12 20:10:08 +00:00
cofs3 = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2017-03-05 15:44:50 +00:00
if ( tk . type = = TK_COMMA ) {
2016-09-04 13:34:40 +00:00
//all good
2017-03-05 15:44:50 +00:00
} else if ( tk . type = = TK_PARENTHESIS_CLOSE ) {
2019-02-12 20:10:08 +00:00
str_ofs = cofs3 ;
2016-09-04 13:34:40 +00:00
} else {
_set_error ( " Expected ',' or ')' " ) ;
}
}
2017-03-05 15:44:50 +00:00
expr = func_call ;
2016-09-04 13:34:40 +00:00
} else {
//named indexing
2017-03-05 15:44:50 +00:00
str_ofs = cofs ;
2016-09-04 13:34:40 +00:00
NamedIndexNode * index = alloc_node < NamedIndexNode > ( ) ;
2017-03-05 15:44:50 +00:00
index - > base = expr ;
index - > name = identifier ;
expr = index ;
2016-09-04 13:34:40 +00:00
}
} break ;
default : {
2017-03-05 15:44:50 +00:00
str_ofs = cofs2 ;
done = true ;
2016-09-04 13:34:40 +00:00
} break ;
}
2020-05-14 14:41:43 +00:00
if ( done ) {
2016-09-04 13:34:40 +00:00
break ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
}
//push expression
{
Expression e ;
2017-03-05 15:44:50 +00:00
e . is_op = false ;
e . node = expr ;
2016-09-04 13:34:40 +00:00
expression . push_back ( e ) ;
}
//ok finally look for an operator
2017-03-05 15:44:50 +00:00
int cofs = str_ofs ;
2016-09-04 13:34:40 +00:00
_get_token ( tk ) ;
2020-05-14 14:41:43 +00:00
if ( error_set ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
Variant : : Operator op = Variant : : OP_MAX ;
2017-03-05 15:44:50 +00:00
switch ( tk . type ) {
2020-05-10 11:00:47 +00:00
case TK_OP_IN :
op = Variant : : OP_IN ;
break ;
case TK_OP_EQUAL :
op = Variant : : OP_EQUAL ;
break ;
case TK_OP_NOT_EQUAL :
op = Variant : : OP_NOT_EQUAL ;
break ;
case TK_OP_LESS :
op = Variant : : OP_LESS ;
break ;
case TK_OP_LESS_EQUAL :
op = Variant : : OP_LESS_EQUAL ;
break ;
case TK_OP_GREATER :
op = Variant : : OP_GREATER ;
break ;
case TK_OP_GREATER_EQUAL :
op = Variant : : OP_GREATER_EQUAL ;
break ;
case TK_OP_AND :
op = Variant : : OP_AND ;
break ;
case TK_OP_OR :
op = Variant : : OP_OR ;
break ;
case TK_OP_NOT :
op = Variant : : OP_NOT ;
break ;
case TK_OP_ADD :
op = Variant : : OP_ADD ;
break ;
case TK_OP_SUB :
op = Variant : : OP_SUBTRACT ;
break ;
case TK_OP_MUL :
op = Variant : : OP_MULTIPLY ;
break ;
case TK_OP_DIV :
op = Variant : : OP_DIVIDE ;
break ;
case TK_OP_MOD :
op = Variant : : OP_MODULE ;
break ;
case TK_OP_SHIFT_LEFT :
op = Variant : : OP_SHIFT_LEFT ;
break ;
case TK_OP_SHIFT_RIGHT :
op = Variant : : OP_SHIFT_RIGHT ;
break ;
case TK_OP_BIT_AND :
op = Variant : : OP_BIT_AND ;
break ;
case TK_OP_BIT_OR :
op = Variant : : OP_BIT_OR ;
break ;
case TK_OP_BIT_XOR :
op = Variant : : OP_BIT_XOR ;
break ;
case TK_OP_BIT_INVERT :
op = Variant : : OP_BIT_NEGATE ;
break ;
2019-04-09 15:08:36 +00:00
default : {
} ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
if ( op = = Variant : : OP_MAX ) { //stop appending stuff
str_ofs = cofs ;
2016-09-04 13:34:40 +00:00
break ;
}
//push operator and go on
{
Expression e ;
2017-03-05 15:44:50 +00:00
e . is_op = true ;
e . op = op ;
2016-09-04 13:34:40 +00:00
expression . push_back ( e ) ;
}
}
2021-03-12 13:35:16 +00:00
/* Reduce the set of expressions and place them in an operator tree, respecting precedence */
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
while ( expression . size ( ) > 1 ) {
int next_op = - 1 ;
int min_priority = 0xFFFFF ;
bool is_unary = false ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < expression . size ( ) ; i + + ) {
2016-09-04 13:34:40 +00:00
if ( ! expression [ i ] . is_op ) {
continue ;
}
int priority ;
2017-03-05 15:44:50 +00:00
bool unary = false ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
switch ( expression [ i ] . op ) {
case Variant : : OP_BIT_NEGATE :
priority = 0 ;
unary = true ;
break ;
case Variant : : OP_NEGATE :
priority = 1 ;
unary = true ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_MULTIPLY :
priority = 2 ;
break ;
case Variant : : OP_DIVIDE :
priority = 2 ;
break ;
case Variant : : OP_MODULE :
priority = 2 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_ADD :
priority = 3 ;
break ;
case Variant : : OP_SUBTRACT :
priority = 3 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_SHIFT_LEFT :
priority = 4 ;
break ;
case Variant : : OP_SHIFT_RIGHT :
priority = 4 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_BIT_AND :
priority = 5 ;
break ;
case Variant : : OP_BIT_XOR :
priority = 6 ;
break ;
case Variant : : OP_BIT_OR :
priority = 7 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_LESS :
priority = 8 ;
break ;
case Variant : : OP_LESS_EQUAL :
priority = 8 ;
break ;
case Variant : : OP_GREATER :
priority = 8 ;
break ;
case Variant : : OP_GREATER_EQUAL :
priority = 8 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_EQUAL :
priority = 8 ;
break ;
case Variant : : OP_NOT_EQUAL :
priority = 8 ;
break ;
2016-09-04 13:34:40 +00:00
2020-05-10 11:00:47 +00:00
case Variant : : OP_IN :
priority = 10 ;
break ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
case Variant : : OP_NOT :
priority = 11 ;
unary = true ;
break ;
2020-05-10 11:00:47 +00:00
case Variant : : OP_AND :
priority = 12 ;
break ;
case Variant : : OP_OR :
priority = 13 ;
break ;
2016-09-04 13:34:40 +00:00
default : {
2017-03-05 15:44:50 +00:00
_set_error ( " Parser bug, invalid operator in expression: " + itos ( expression [ i ] . op ) ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
}
2017-03-05 15:44:50 +00:00
if ( priority < min_priority ) {
2016-09-04 13:34:40 +00:00
// < is used for left to right (default)
// <= is used for right to left
2017-03-05 15:44:50 +00:00
next_op = i ;
min_priority = priority ;
is_unary = unary ;
2016-09-04 13:34:40 +00:00
}
}
2017-03-05 15:44:50 +00:00
if ( next_op = = - 1 ) {
2016-09-04 13:34:40 +00:00
_set_error ( " Yet another parser bug.... " ) ;
2020-04-01 23:20:12 +00:00
ERR_FAIL_V ( nullptr ) ;
2016-09-04 13:34:40 +00:00
}
// OK! create operator..
if ( is_unary ) {
2017-03-05 15:44:50 +00:00
int expr_pos = next_op ;
while ( expression [ expr_pos ] . is_op ) {
2016-09-04 13:34:40 +00:00
expr_pos + + ;
2017-03-05 15:44:50 +00:00
if ( expr_pos = = expression . size ( ) ) {
2016-09-04 13:34:40 +00:00
//can happen..
2018-04-22 17:36:01 +00:00
_set_error ( " Unexpected end of expression... " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
}
2020-03-11 17:59:18 +00:00
//consecutively do unary operators
2017-03-05 15:44:50 +00:00
for ( int i = expr_pos - 1 ; i > = next_op ; i - - ) {
2016-09-04 13:34:40 +00:00
OperatorNode * op = alloc_node < OperatorNode > ( ) ;
2017-03-05 15:44:50 +00:00
op - > op = expression [ i ] . op ;
op - > nodes [ 0 ] = expression [ i + 1 ] . node ;
2020-04-01 23:20:12 +00:00
op - > nodes [ 1 ] = nullptr ;
2018-07-25 01:11:03 +00:00
expression . write [ i ] . is_op = false ;
expression . write [ i ] . node = op ;
2021-07-03 22:17:03 +00:00
expression . remove_at ( i + 1 ) ;
2016-09-04 13:34:40 +00:00
}
} else {
2017-03-05 15:44:50 +00:00
if ( next_op < 1 | | next_op > = ( expression . size ( ) - 1 ) ) {
2018-04-22 17:36:01 +00:00
_set_error ( " Parser bug... " ) ;
2020-04-01 23:20:12 +00:00
ERR_FAIL_V ( nullptr ) ;
2016-09-04 13:34:40 +00:00
}
OperatorNode * op = alloc_node < OperatorNode > ( ) ;
2017-03-05 15:44:50 +00:00
op - > op = expression [ next_op ] . op ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
if ( expression [ next_op - 1 ] . is_op ) {
2018-04-22 17:36:01 +00:00
_set_error ( " Parser bug... " ) ;
2020-04-01 23:20:12 +00:00
ERR_FAIL_V ( nullptr ) ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
if ( expression [ next_op + 1 ] . is_op ) {
2016-09-04 13:34:40 +00:00
// this is not invalid and can really appear
// but it becomes invalid anyway because no binary op
2017-09-02 14:19:06 +00:00
// can be followed by a unary op in a valid combination,
2017-03-24 20:45:31 +00:00
// due to how precedence works, unaries will always disappear first
2016-09-04 13:34:40 +00:00
_set_error ( " Unexpected two consecutive operators. " ) ;
2020-04-01 23:20:12 +00:00
return nullptr ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
op - > nodes [ 0 ] = expression [ next_op - 1 ] . node ; //expression goes as left
op - > nodes [ 1 ] = expression [ next_op + 1 ] . node ; //next expression goes as right
2016-09-04 13:34:40 +00:00
//replace all 3 nodes by this operator and make it an expression
2018-07-25 01:11:03 +00:00
expression . write [ next_op - 1 ] . node = op ;
2021-07-03 22:17:03 +00:00
expression . remove_at ( next_op ) ;
expression . remove_at ( next_op ) ;
2016-09-04 13:34:40 +00:00
}
}
return expression [ 0 ] . node ;
}
bool VisualScriptExpression : : _compile_expression ( ) {
2020-05-14 14:41:43 +00:00
if ( ! expression_dirty ) {
2016-09-04 13:34:40 +00:00
return error_set ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
if ( nodes ) {
memdelete ( nodes ) ;
2020-04-01 23:20:12 +00:00
nodes = nullptr ;
root = nullptr ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
error_str = String ( ) ;
error_set = false ;
str_ofs = 0 ;
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
root = _parse_expression ( ) ;
2016-09-04 13:34:40 +00:00
if ( error_set ) {
2020-04-01 23:20:12 +00:00
root = nullptr ;
2016-09-04 13:34:40 +00:00
if ( nodes ) {
memdelete ( nodes ) ;
}
2020-04-01 23:20:12 +00:00
nodes = nullptr ;
2016-09-04 13:34:40 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
expression_dirty = false ;
2016-09-04 13:34:40 +00:00
return false ;
}
class VisualScriptNodeInstanceExpression : public VisualScriptNodeInstance {
public :
2017-03-05 15:44:50 +00:00
VisualScriptInstance * instance ;
2016-09-04 13:34:40 +00:00
VisualScriptExpression * expression ;
2022-04-05 10:40:26 +00:00
//virtual int get_working_memory_size() const override { return 0; }
2016-09-04 13:34:40 +00:00
//execute by parsing the tree directly
2020-02-19 19:27:19 +00:00
virtual bool _execute ( const Variant * * p_inputs , VisualScriptExpression : : ENode * p_node , Variant & r_ret , String & r_error_str , Callable : : CallError & ce ) {
2017-03-05 15:44:50 +00:00
switch ( p_node - > type ) {
case VisualScriptExpression : : ENode : : TYPE_INPUT : {
const VisualScriptExpression : : InputNode * in = static_cast < const VisualScriptExpression : : InputNode * > ( p_node ) ;
r_ret = * p_inputs [ in - > index ] ;
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_CONSTANT : {
const VisualScriptExpression : : ConstantNode * c = static_cast < const VisualScriptExpression : : ConstantNode * > ( p_node ) ;
r_ret = c - > value ;
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_SELF : {
r_ret = instance - > get_owner_ptr ( ) ;
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_OPERATOR : {
const VisualScriptExpression : : OperatorNode * op = static_cast < const VisualScriptExpression : : OperatorNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Variant a ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , op - > nodes [ 0 ] , a , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
Variant b ;
if ( op - > nodes [ 1 ] ) {
2017-03-05 15:44:50 +00:00
ret = _execute ( p_inputs , op - > nodes [ 1 ] , b , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
bool valid = true ;
Variant : : evaluate ( op - > op , a , b , r_ret , valid ) ;
2016-09-04 13:34:40 +00:00
if ( ! valid ) {
2017-03-05 15:44:50 +00:00
r_error_str = " Invalid operands to operator " + Variant : : get_operator_name ( op - > op ) + " : " + Variant : : get_type_name ( a . get_type ( ) ) + " and " + Variant : : get_type_name ( b . get_type ( ) ) + " . " ;
2016-09-04 13:34:40 +00:00
return true ;
}
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_INDEX : {
const VisualScriptExpression : : IndexNode * index = static_cast < const VisualScriptExpression : : IndexNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Variant base ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , index - > base , base , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
Variant idx ;
2017-03-05 15:44:50 +00:00
ret = _execute ( p_inputs , index - > index , idx , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
bool valid ;
2017-03-05 15:44:50 +00:00
r_ret = base . get ( idx , & valid ) ;
2016-09-04 13:34:40 +00:00
if ( ! valid ) {
2017-03-05 15:44:50 +00:00
r_error_str = " Invalid index of type " + Variant : : get_type_name ( idx . get_type ( ) ) + " for base of type " + Variant : : get_type_name ( base . get_type ( ) ) + " . " ;
2016-09-04 13:34:40 +00:00
return true ;
}
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_NAMED_INDEX : {
const VisualScriptExpression : : NamedIndexNode * index = static_cast < const VisualScriptExpression : : NamedIndexNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Variant base ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , index - > base , base , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
bool valid ;
2020-11-07 01:29:22 +00:00
r_ret = base . get_named ( index - > name , valid ) ;
2016-09-04 13:34:40 +00:00
if ( ! valid ) {
2017-03-05 15:44:50 +00:00
r_error_str = " Invalid index ' " + String ( index - > name ) + " ' for base of type " + Variant : : get_type_name ( base . get_type ( ) ) + " . " ;
2016-09-04 13:34:40 +00:00
return true ;
}
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_ARRAY : {
const VisualScriptExpression : : ArrayNode * array = static_cast < const VisualScriptExpression : : ArrayNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Array arr ;
arr . resize ( array - > array . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < array - > array . size ( ) ; i + + ) {
2016-09-04 13:34:40 +00:00
Variant value ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , array - > array [ i ] , value , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
arr [ i ] = value ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
r_ret = arr ;
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_DICTIONARY : {
const VisualScriptExpression : : DictionaryNode * dictionary = static_cast < const VisualScriptExpression : : DictionaryNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Dictionary d ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < dictionary - > dict . size ( ) ; i + = 2 ) {
2016-09-04 13:34:40 +00:00
Variant key ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , dictionary - > dict [ i + 0 ] , key , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
Variant value ;
2017-03-05 15:44:50 +00:00
ret = _execute ( p_inputs , dictionary - > dict [ i + 1 ] , value , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
2017-03-05 15:44:50 +00:00
d [ key ] = value ;
2016-09-04 13:34:40 +00:00
}
2017-03-05 15:44:50 +00:00
r_ret = d ;
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_CONSTRUCTOR : {
const VisualScriptExpression : : ConstructorNode * constructor = static_cast < const VisualScriptExpression : : ConstructorNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Vector < Variant > arr ;
2017-03-05 15:44:50 +00:00
Vector < const Variant * > argp ;
2016-09-04 13:34:40 +00:00
arr . resize ( constructor - > arguments . size ( ) ) ;
argp . resize ( constructor - > arguments . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < constructor - > arguments . size ( ) ; i + + ) {
2016-09-04 13:34:40 +00:00
Variant value ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , constructor - > arguments [ i ] , value , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2018-07-25 01:11:03 +00:00
arr . write [ i ] = value ;
argp . write [ i ] = & arr [ i ] ;
2016-09-04 13:34:40 +00:00
}
2020-11-09 03:19:09 +00:00
Variant : : construct ( constructor - > data_type , r_ret , ( const Variant * * ) argp . ptr ( ) , argp . size ( ) , ce ) ;
2016-09-04 13:34:40 +00:00
2020-02-19 19:27:19 +00:00
if ( ce . error ! = Callable : : CallError : : CALL_OK ) {
2017-03-05 15:44:50 +00:00
r_error_str = " Invalid arguments to construct ' " + Variant : : get_type_name ( constructor - > data_type ) + " '. " ;
2016-09-04 13:34:40 +00:00
return true ;
}
2016-09-06 22:12:28 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_BUILTIN_FUNC : {
const VisualScriptExpression : : BuiltinFuncNode * bifunc = static_cast < const VisualScriptExpression : : BuiltinFuncNode * > ( p_node ) ;
2016-09-06 22:12:28 +00:00
Vector < Variant > arr ;
2017-03-05 15:44:50 +00:00
Vector < const Variant * > argp ;
2016-09-06 22:12:28 +00:00
arr . resize ( bifunc - > arguments . size ( ) ) ;
argp . resize ( bifunc - > arguments . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < bifunc - > arguments . size ( ) ; i + + ) {
2016-09-06 22:12:28 +00:00
Variant value ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , bifunc - > arguments [ i ] , value , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-06 22:12:28 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2018-07-25 01:11:03 +00:00
arr . write [ i ] = value ;
argp . write [ i ] = & arr [ i ] ;
2016-09-06 22:12:28 +00:00
}
2017-11-25 03:07:54 +00:00
VisualScriptBuiltinFunc : : exec_func ( bifunc - > func , ( const Variant * * ) argp . ptr ( ) , & r_ret , ce , r_error_str ) ;
2016-09-06 22:12:28 +00:00
2020-02-19 19:27:19 +00:00
if ( ce . error ! = Callable : : CallError : : CALL_OK ) {
2017-03-05 15:44:50 +00:00
r_error_str = " Builtin Call Failed. " + r_error_str ;
2016-09-06 22:12:28 +00:00
return true ;
}
2016-09-04 13:34:40 +00:00
} break ;
2017-03-05 15:44:50 +00:00
case VisualScriptExpression : : ENode : : TYPE_CALL : {
const VisualScriptExpression : : CallNode * call = static_cast < const VisualScriptExpression : : CallNode * > ( p_node ) ;
2016-09-04 13:34:40 +00:00
Variant base ;
2017-03-05 15:44:50 +00:00
bool ret = _execute ( p_inputs , call - > base , base , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2016-09-04 13:34:40 +00:00
Vector < Variant > arr ;
2017-03-05 15:44:50 +00:00
Vector < const Variant * > argp ;
2016-09-04 13:34:40 +00:00
arr . resize ( call - > arguments . size ( ) ) ;
argp . resize ( call - > arguments . size ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < call - > arguments . size ( ) ; i + + ) {
2016-09-04 13:34:40 +00:00
Variant value ;
2019-02-12 20:10:08 +00:00
bool ret2 = _execute ( p_inputs , call - > arguments [ i ] , value , r_error_str , ce ) ;
2020-05-14 14:41:43 +00:00
if ( ret2 ) {
2016-09-04 13:34:40 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2018-07-25 01:11:03 +00:00
arr . write [ i ] = value ;
argp . write [ i ] = & arr [ i ] ;
2016-09-04 13:34:40 +00:00
}
2022-03-09 13:58:40 +00:00
base . callp ( call - > method , ( const Variant * * ) argp . ptr ( ) , argp . size ( ) , r_ret , ce ) ;
2016-09-04 13:34:40 +00:00
2020-02-19 19:27:19 +00:00
if ( ce . error ! = Callable : : CallError : : CALL_OK ) {
2017-03-05 15:44:50 +00:00
r_error_str = " On call to ' " + String ( call - > method ) + " ': " ;
2016-09-04 13:34:40 +00:00
return true ;
}
} break ;
}
return false ;
}
2022-04-05 10:40:26 +00:00
virtual int step ( const Variant * * p_inputs , Variant * * p_outputs , StartMode p_start_mode , Variant * p_working_mem , Callable : : CallError & r_error , String & r_error_str ) override {
2016-09-04 13:34:40 +00:00
if ( ! expression - > root | | expression - > error_set ) {
2017-03-05 15:44:50 +00:00
r_error_str = expression - > error_str ;
2020-02-19 19:27:19 +00:00
r_error . error = Callable : : CallError : : CALL_ERROR_INVALID_METHOD ;
2016-09-04 13:34:40 +00:00
return 0 ;
}
2017-03-05 15:44:50 +00:00
bool error = _execute ( p_inputs , expression - > root , * p_outputs [ 0 ] , r_error_str , r_error ) ;
2020-02-19 19:27:19 +00:00
if ( error & & r_error . error = = Callable : : CallError : : CALL_OK ) {
r_error . error = Callable : : CallError : : CALL_ERROR_INVALID_METHOD ;
2016-09-04 13:34:40 +00:00
}
# ifdef DEBUG_ENABLED
2017-03-05 15:44:50 +00:00
if ( ! error & & expression - > output_type ! = Variant : : NIL & & ! Variant : : can_convert_strict ( p_outputs [ 0 ] - > get_type ( ) , expression - > output_type ) ) {
r_error_str + = " Can't convert expression result from " + Variant : : get_type_name ( p_outputs [ 0 ] - > get_type ( ) ) + " to " + Variant : : get_type_name ( expression - > output_type ) + " . " ;
2020-02-19 19:27:19 +00:00
r_error . error = Callable : : CallError : : CALL_ERROR_INVALID_METHOD ;
2016-09-04 13:34:40 +00:00
}
2016-09-05 21:50:30 +00:00
# endif
2016-09-04 13:34:40 +00:00
return 0 ;
}
} ;
2021-06-17 22:03:09 +00:00
VisualScriptNodeInstance * VisualScriptExpression : : instantiate ( VisualScriptInstance * p_instance ) {
2016-09-04 13:34:40 +00:00
_compile_expression ( ) ;
2017-03-05 15:44:50 +00:00
VisualScriptNodeInstanceExpression * instance = memnew ( VisualScriptNodeInstanceExpression ) ;
instance - > instance = p_instance ;
instance - > expression = this ;
2016-09-04 13:34:40 +00:00
return instance ;
}
2021-02-11 17:18:45 +00:00
void VisualScriptExpression : : reset_state ( ) {
if ( nodes ) {
memdelete ( nodes ) ;
nodes = nullptr ;
root = nullptr ;
}
error_str = String ( ) ;
error_set = false ;
str_ofs = 0 ;
inputs . clear ( ) ;
}
2017-03-05 15:44:50 +00:00
VisualScriptExpression : : VisualScriptExpression ( ) {
2016-09-04 13:34:40 +00:00
}
VisualScriptExpression : : ~ VisualScriptExpression ( ) {
if ( nodes ) {
memdelete ( nodes ) ;
}
}
void register_visual_script_expression_node ( ) {
2017-03-05 15:44:50 +00:00
VisualScriptLanguage : : singleton - > add_register_func ( " operators/expression " , create_node_generic < VisualScriptExpression > ) ;
2016-09-04 13:34:40 +00:00
}