static bool evaluate_expression (char* const expr) { char *p = strtok (expr, " "); while ( NULL != p ) { char *endptr = NULL; int r = 0, s = 0, item = (int) strtol (p, &endptr, 10); // mozna cislo if ( ( '\0' == *endptr ) && ( '\0' != *p ) ) { if ( !push (item) ) { return false; } else { goto finish; } } if ( '\0' != *(p+1) ) { return false; } // mozna symbol if ( get_symbol_value (*p, &item) ) { if ( !push (item) ) { return false; } else { goto finish; } } // mozna operand if ( !pop (&r) || !pop (&s) ) { return false; } switch ( *p ) { case '+': if ( !push (r + s) ) return false; break; case '*': if ( !push (r * s) ) return false; break; case '-': if ( !push (r - s) ) return false; break; default: return false; break; } finish: p = strtok (NULL, " "); // get next token } // end while return true; }
static be_jse_symbol_t* start() { be_jse_int_t ret = -1; be_jse_symbol_t* arg = be_jse_handle_single_arg_function(); be_jse_symbol_t* port_symbol = NULL; be_jse_symbol_t* service_symbol = NULL; be_jse_symbol_t* uart_port_symbol = NULL; be_jse_symbol_t* uart_baudrate_symbol = NULL; be_jse_symbol_t* value_port_symbol = NULL; be_jse_symbol_t* value_service_symbol = NULL; be_jse_symbol_t* value_uart_port_symbol = NULL; be_jse_symbol_t* value_uart_baudrate_symbol = NULL; if (arg == NULL) goto end; port_symbol = lookup_named_child_symbol(get_symbol_node_id(arg), "port", false); service_symbol = lookup_named_child_symbol(get_symbol_node_id(arg), "service", false); uart_port_symbol = lookup_named_child_symbol(get_symbol_node_id(arg), "uartPort", false); uart_baudrate_symbol = lookup_named_child_symbol(get_symbol_node_id(arg), "uartBaudrate", false); if (port_symbol == NULL || service_symbol == NULL || uart_port_symbol == NULL || uart_baudrate_symbol == NULL) { ret = -2; goto end; } value_port_symbol = get_symbol_value(port_symbol); value_service_symbol = get_symbol_value(service_symbol); value_uart_port_symbol = get_symbol_value(uart_port_symbol); value_uart_baudrate_symbol = get_symbol_value(uart_baudrate_symbol); if (value_port_symbol == NULL || value_service_symbol == NULL || value_uart_port_symbol == NULL || value_uart_baudrate_symbol == NULL) { ret = -3; goto end; } zigbee.uart_dev.port = (uint8_t)get_symbol_int(value_uart_port_symbol); zigbee.uart_dev.config.baud_rate = (uint32_t)get_symbol_int(value_uart_baudrate_symbol); zigbee.uart_dev.config.data_width = DATA_WIDTH_8BIT; zigbee.uart_dev.config.parity = NO_PARITY; zigbee.uart_dev.config.stop_bits = STOP_BITS_1; zigbee.uart_dev.config.flow_control = FLOW_CONTROL_DISABLED; zigbee.on_message = on_message; zigbee.on_connect = on_connect; zigbee.port = (uint8_t)get_symbol_int(value_port_symbol); symbol_to_str(value_service_symbol, zigbee.nt, symbol_str_len(value_service_symbol)); ret = bone_zigbee_start(&zigbee); end: symbol_unlock(arg); symbol_unlock(port_symbol); symbol_unlock(service_symbol); symbol_unlock(uart_port_symbol); symbol_unlock(uart_baudrate_symbol); symbol_unlock(value_port_symbol); symbol_unlock(value_service_symbol); symbol_unlock(value_uart_port_symbol); symbol_unlock(value_uart_baudrate_symbol); return new_int_symbol(ret); }
static void command_get_context(char * token, Channel * c) { int err = 0; char id[256]; Symbol sym; json_read_string(&c->inp, id, sizeof(id)); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); if (id2symbol(id, &sym) < 0) err = errno; write_stringz(&c->out, "R"); write_stringz(&c->out, token); write_errno(&c->out, err); if (err == 0) { char * name = NULL; int type_class = TYPE_CLASS_UNKNOWN; Symbol type; size_t size = 0; void * value = NULL; unsigned long length = 0; unsigned long offset = 0; ContextAddress address = 0; int frame = STACK_NO_FRAME; /* TODO: symbol frame */ write_stream(&c->out, '{'); json_write_string(&c->out, "ID"); write_stream(&c->out, ':'); json_write_string(&c->out, id); write_stream(&c->out, ','); json_write_string(&c->out, "ExeID"); write_stream(&c->out, ':'); json_write_string(&c->out, container_id(sym.ctx)); write_stream(&c->out, ','); if (get_symbol_name(&sym, &name) == 0 && name != NULL) { json_write_string(&c->out, "Name"); write_stream(&c->out, ':'); json_write_string(&c->out, name); write_stream(&c->out, ','); loc_free(name); } if (get_symbol_type_class(&sym, &type_class) == 0 && type_class != TYPE_CLASS_UNKNOWN) { json_write_string(&c->out, "TypeClass"); write_stream(&c->out, ':'); json_write_long(&c->out, type_class); write_stream(&c->out, ','); } if (get_symbol_type(&sym, &type) == 0) { json_write_string(&c->out, "TypeID"); write_stream(&c->out, ':'); json_write_string(&c->out, symbol2id(&type)); write_stream(&c->out, ','); } if (get_symbol_base_type(&sym, &type) == 0) { json_write_string(&c->out, "BaseTypeID"); write_stream(&c->out, ':'); json_write_string(&c->out, symbol2id(&type)); write_stream(&c->out, ','); } if (get_symbol_index_type(&sym, &type) == 0) { json_write_string(&c->out, "IndexTypeID"); write_stream(&c->out, ':'); json_write_string(&c->out, symbol2id(&type)); write_stream(&c->out, ','); } if (get_symbol_size(&sym, frame, &size) == 0) { json_write_string(&c->out, "Size"); write_stream(&c->out, ':'); json_write_long(&c->out, size); write_stream(&c->out, ','); } if (get_symbol_length(&sym, frame, &length) == 0) { json_write_string(&c->out, "Length"); write_stream(&c->out, ':'); json_write_long(&c->out, length); write_stream(&c->out, ','); } if (sym.sym_class == SYM_CLASS_REFERENCE) { if (get_symbol_offset(&sym, &offset) == 0) { json_write_string(&c->out, "Offset"); write_stream(&c->out, ':'); json_write_long(&c->out, offset); write_stream(&c->out, ','); } if (get_symbol_address(&sym, frame, &address) == 0) { json_write_string(&c->out, "Address"); write_stream(&c->out, ':'); json_write_long(&c->out, address); write_stream(&c->out, ','); } } if (sym.sym_class == SYM_CLASS_VALUE && get_symbol_value(&sym, &value, &size) == 0) { json_write_string(&c->out, "Value"); write_stream(&c->out, ':'); json_write_binary(&c->out, value, size); write_stream(&c->out, ','); } json_write_string(&c->out, "Class"); write_stream(&c->out, ':'); json_write_long(&c->out, sym.sym_class); write_stream(&c->out, '}'); write_stream(&c->out, 0); } else { write_stringz(&c->out, "null"); } write_stream(&c->out, MARKER_EOM); }
/* Handles parsing of set_asm constant arguments. A constant argument may have two forms: . an int const of the form: <field> = int_value . a string const of the form: <field> = str_value pf -> pointer to the format of the instruction this argument is being set to field_str -> <field> field of the instruction format which will be constant map -> identifies the <ac_asm_map> containing the sconst_field to be translated iconst_field, sconst_field -> integer and string const field. Only one must be valid at a time. A value of NULL in sconst_field indicates this constant argument is of type int. Otherwise it's a string type Constant argument does not alter the 'formatted_arg_str'. Instead, it uses c_image to store the const value assigned by the user in an ArchC source file. examples: ArchC source: "imm = 10" . 10 will be encoded in its right position within the format field 'imm' and saved in 'c_image' ArchC source: "imm = reg.map_to("$ra")" . first the value of '$ra' will be found in map "reg", then it'll encoded and saved in 'c_image' */ int acpp_asm_parse_const_asm_argument(ac_dec_format *pf, char *field_str, char *map, int iconst_field, char *sconst_field, char *error_msg) { /* if str const type, find the symbol value */ if (sconst_field != NULL) { long int symbol_value; if (map != NULL) { // parsing constructions like "mapname.map_to("valname")" if (find_mapping_marker(map) == NULL) { sprintf(error_msg, "Undefined map name '%s' in argument declaration: '%s.map_to(\"%s\")'", map, map, sconst_field); } if (!get_symbol_value(sconst_field, map, &symbol_value)) { sprintf(error_msg, "Invalid map symbol assigned in argument declaration: '%s.%s'", map, sconst_field); in_error = 1; return 0; } } else { // parsing constructions like ""valname"" if (!get_symbol_value(sconst_field, NULL, &symbol_value)) { sprintf(error_msg, "Invalid map symbol assigned in argument declaration: '%s'", sconst_field); in_error = 1; return 0; } } iconst_field = symbol_value; } /* find the field */ ac_dec_field *pfield = pf->fields; for(; pfield != NULL && strcmp(field_str, pfield->name); pfield = pfield->next); if( pfield == NULL ) { sprintf(error_msg, "Invalid field used as argument: '%s'", field_str); in_error = 1; return 0; } ac_const_field_list *cfield = (ac_const_field_list *)malloc(sizeof(ac_const_field_list)); cfield->next = NULL; cfield->value = iconst_field; cfield->field.name = (char *)malloc(strlen(pfield->name)+1); strcpy(cfield->field.name, pfield->name); cfield->field.size = pfield->size; cfield->field.first_bit = pfield->first_bit; cfield->field.id = 0; /* not used */ if (current_insn->const_fields == NULL) { current_insn->const_fields = cfield; current_insn->const_fields->next = NULL; } else { ac_const_field_list *head = current_insn->const_fields; while (head->next != NULL) head = head->next; head->next = cfield; } /* encode it, saving in 'c_image' note that there will be only 1 encoding for each format field, so it's ok to use the OR operation */ // c_image |= ac_asm_encode_insn_field(iconst_field, pf, pfield); return 1; }