Expr* factor(const char* string, Expr* expr) { if(string[VAL_STRING] == '(') { expr->op = eat_char(string, '('); expr->data.expr[0] = parse(string); eat_char(string, ')'); } else if(isdigit(string[VAL_STRING])) { expr->op = 'd'; expr->data.terminal = eat_int(string); } return expr; }
static double read_time(struct bstr *data) { struct bstr s = *data; bool ok = true; double t1 = read_int_2(&s); ok = eat_char(&s, ':') && ok; double t2 = read_int_2(&s); ok = eat_char(&s, ':') && ok; double t3 = read_int_2(&s); ok = ok && t1 >= 0 && t2 >= 0 && t3 >= 0; return ok ? t1 * 60.0 + t2 + t3 * SECS_PER_CUE_FRAME : 0; }
static void parse_packet(Octstr *packet, Octstr *out) { int opcode, sequence; int i; eat_checksum(packet); opcode = eat_number(packet); if (opcode < 0 || eat_char(packet, ':') < 0) return; sequence = eat_number(packet); if (sequence < 0) return; for (i = 0; handlers[i].opcode >= 0; i++) { if (handlers[i].opcode == opcode) { (handlers[i].handler)(packet, out, sequence); break; } } if (handlers[i].opcode < 0) { /* Loop failed */ if (logging == LOG_packets) printf("RCV: unknown operation %ld\n", (long) handlers[i].opcode); send_error(out, 98, sequence, "1", "unexpected operation"); } }
//reads the format from gen_hgf. void Player::load_hgf(Board board, Node * node, FILE * fd){ char c, buf[101]; eat_whitespace(fd); assert(fscanf(fd, "(;%c[%100[^]]]", &c, buf) > 0); assert(board.toplay() == (c == 'W' ? 1 : 2)); node->move = Move(buf); board.move(node->move); assert(fscanf(fd, "C[%100[^]]]", buf) > 0); vecstr entry, parts = explode(string(buf), ", "); assert(parts[0] == "mcts"); entry = explode(parts[1], ":"); assert(entry[0] == "sims"); uword sims = from_str<uword>(entry[1]); entry = explode(parts[2], ":"); assert(entry[0] == "avg"); double avg = from_str<double>(entry[1]); uword wins = sims*avg; node->exp.addwins(wins); node->exp.addlosses(sims - wins); entry = explode(parts[3], ":"); assert(entry[0] == "outcome"); node->outcome = from_str<int>(entry[1]); entry = explode(parts[4], ":"); assert(entry[0] == "best"); node->bestmove = Move(entry[1]); eat_whitespace(fd); if(fpeek(fd) != ')'){ create_children_simple(board, node); while(fpeek(fd) != ')'){ Node child; load_hgf(board, & child, fd); Node * i = find_child(node, child.move); *i = child; //copy the child experience to the tree i->swap_tree(child); //move the child subtree to the tree assert(child.children.empty()); eat_whitespace(fd); } } eat_char(fd, ')'); return; }
Expr* factor_tail(const char* string, Expr* expr) { Expr* new_expr; switch(string[VAL_STRING]) { case '*': case '/': if(NULL == (new_expr = (Expr*)malloc(sizeof(Expr)))) { exit(1); } if(NULL == (new_expr->data.expr[1] = (Expr*)malloc(sizeof(Expr)))) { exit(1); } new_expr->op = eat_char(string, string[VAL_STRING]); new_expr->data.expr[0] = expr; new_expr->data.expr[1] = factor(string, new_expr->data.expr[1]); new_expr = factor_tail(string, new_expr); return new_expr; case '+': case '-': case ')': case 0: return expr; default: error_message(string);return NULL; } }
void next () { //Skip whitespace while (curch == ' ' || curch == '\r' || curch == '\n' || curch == '\t') next_char(); //Treat preprocessor lines as line comments if ( curch == '#' || (curch == '/' && (next_char() == '/' || prev_char('/')))) { while (curch != '\n' && !feof(input)) next_char(); //Restart the function (to skip subsequent whitespace, comments and pp) next(); return; } buflength = 0; token = token_other; //Identifier, keyword or integer literal if (isalpha(curch) || isdigit(curch)) { token = isalpha(curch) ? token_ident : token_int; while (token == token_ident ? (isalnum(curch) || curch == '_') && !feof(input) : isdigit(curch) && !feof(input)) eat_char(); //String or character literal } else if (curch == '\'' || curch == '"') { token = curch == '"' ? token_str : token_char; //Can't retrieve this from the buffer - mini-c only has int reads char delimiter = curch; eat_char(); while (curch != delimiter && !feof(input)) { if (curch == '\\') eat_char(); eat_char(); } eat_char(); //Two char operators } else if ( curch == '+' || curch == '-' || curch == '|' || curch == '&' || curch == '=' || curch == '!' || curch == '>' || curch == '<') { eat_char(); if ((curch == buffer[0] && curch != '!') || curch == '=') eat_char(); } else eat_char(); (buffer + buflength++)[0] = 0; }
static char *read_quoted(void *talloc_ctx, struct bstr *data) { *data = bstr_lstrip(*data); if (!eat_char(data, '"')) return NULL; int end = bstrchr(*data, '"'); if (end < 0) return NULL; struct bstr res = bstr_splice(*data, 0, end); *data = bstr_cut(*data, end + 1); return bstrto0(talloc_ctx, res); }
static struct bstr read_quoted(struct bstr *data) { *data = bstr_lstrip(*data); if (!eat_char(data, '"')) return (struct bstr) {0}; int end = bstrchr(*data, '"'); if (end < 0) return (struct bstr) {0}; struct bstr res = bstr_splice(*data, 0, end); *data = bstr_cut(*data, end + 1); return res; } // Read a 2 digit unsigned decimal integer. // Return -1 on failure. static int read_int_2(struct bstr *data) { *data = bstr_lstrip(*data); if (data->len && data->start[0] == '-') return -1; struct bstr s = *data; int res = (int)bstrtoll(s, &s, 10); if (data->len == s.len || data->len - s.len > 2) return -1; *data = s; return res; } static double read_time(struct bstr *data) { struct bstr s = *data; bool ok = true; double t1 = read_int_2(&s); ok = eat_char(&s, ':') && ok; double t2 = read_int_2(&s); ok = eat_char(&s, ':') && ok; double t3 = read_int_2(&s); ok = ok && t1 >= 0 && t2 >= 0 && t3 >= 0; return ok ? t1 * 60.0 + t2 + t3 * SECS_PER_CUE_FRAME : 0; } static struct bstr skip_utf8_bom(struct bstr data) { return bstr_startswith0(data, "\xEF\xBB\xBF") ? bstr_cut(data, 3) : data; } // Check if the text in data is most likely CUE data. This is used by the // demuxer code to check the file type. // data is the start of the probed file, possibly cut off at a random point. bool mp_probe_cue(struct bstr data) { bool valid = false; data = skip_utf8_bom(data); for (;;) { enum cue_command cmd = read_cmd(&data, NULL); // End reached. Since the line was most likely cut off, don't use the // result of the last parsing call. if (data.len == 0) break; if (cmd == CUE_ERROR) return false; if (cmd != CUE_EMPTY) valid = true; } return valid; }