/*----------------------------------------------------------------------*/ static ast_expression_t *parse_function_call(parse_state_t *p) { token_t *token; ast_expression_t *expression; ast_expression_list_t *param, *new_param; expression = ALLOC(sizeof(ast_expression_t)); BZERO(expression); expression->tag = AST_EXPRESSION_FUNCTION_CALL; /* function identifier */ token = next_token(p); EXPECT(token, TK_IDENTIFIER, "function name expected"); expression->u.function_call.identifier = token; /* opening bracket */ token = next_token(p); EXPECT(token, TK_LBRACKET, "'(' expected"); /* parameter list */ param = NULL; expression->u.function_call.parameter_count = 0; for (;;) { token = peek_token(p); if (test_token(token, TK_RBRACKET)) break; /* parameter */ new_param = ALLOC(sizeof(ast_expression_list_t)); new_param->next = NULL; new_param->expr = parse_logical_expression(p); if (new_param->expr == NULL) error(p, "expression expected"); if (param == NULL) { expression->u.function_call.parameter_expr_list = new_param; } else { param->next = new_param; } param = new_param; expression->u.function_call.parameter_count += 1; /* comma? */ token = peek_token(p); if (test_token(token, TK_RBRACKET)) break; if (test_token(token, TK_COMMA)) { next_token(p); continue; } error(p, "',' or ')' expected"); } /* closing bracket */ token = next_token(p); EXPECT(token, TK_RBRACKET, "')' expected"); return expression; }
/*----------------------------------------------------------------------*/ static ast_statement_t *parse_statement(parse_state_t *p) { token_t *token; token = peek_token(p); if (token == NULL) { return NULL; } else if (test_token(token, TK_ELSE) || test_token(token, TK_END)) { return NULL; } else if (test_token(token, TK_VAR)) { return parse_declare_var(p); } else if (test_token(token, TK_FUNCTION)) { return parse_define_function(p); } else if (test_token(token, TK_RETURN)) { return parse_return(p); } else if (test_token(token, TK_IF)) { return parse_if(p); } else if (test_token(token, TK_FOR)) { return parse_for(p); } else if (test_token(token, TK_IDENTIFIER)) { return parse_assignment(p); } next_token(p); error(p, "statement expected"); return NULL; /* unreachable */ }
/*----------------------------------------------------------------------*/ static ast_statement_t *parse_if(parse_state_t *p) { token_t *token; ast_statement_t *statement; /* if */ token = next_token(p); EXPECT(token, TK_IF, "'if' expected"); statement = ALLOC(sizeof(ast_statement_t)); BZERO(statement); statement->tag = AST_STATEMENT_IF; /* if predicate / block */ statement->u.if_.condition = parse_logical_expression(p); statement->u.if_.if_block = parse_statement_list(p); /* else? */ token = peek_token(p); if (test_token(token, TK_ELSE)) { next_token(p); /* else block */ statement->u.if_.else_block = parse_statement_list(p); } else { statement->u.if_.else_block = NULL; } /* end */ token = next_token(p); EXPECT(token, TK_END, "'end' expected"); return statement; }
constexpr char_type peek_token ( cstr_type begin , cstr_type end , TTokens && ...tokens ) noexcept { return begin < end ? test_token (*begin, std::forward<TTokens> (tokens)...) : null_char ; }
constexpr char_type test_token ( char_type test , char_type head , TTail && ...tail ) noexcept { return test == head ? test : test_token (test, std::forward<TTail> (tail)...) ; }
int Work() { str s,token; printf("\nkeyword: "); scanf("%s",s); getchar(); if(strcmp(s,"-q")==0) return 0; if(strcmp(s,"-cls")==0) { system("clear"); return 1; } if(test_token()==0) return 1; int i; for(i=0;i!=N;++i) if(contain(i,s)) print(i); return 1; }
/* * Main simply calls each of the tests in turn. */ int main (int argc, char *argv[]) { test_token(); printf ("Starting tests.\n"); fflush (stdout); initialize_array (); copy_string (); bubble_sort (); return (0); }
// statement = declaration | return_statement | if_statement // assignment | expression [ ";" ] static enum v7_err parse_statement(struct v7 *v7, int *is_return_statement) { if (*v7->cursor == '_' || is_alpha(*v7->cursor)) { TRY(parse_identifier(v7)); // Load identifier into v7->tok, v7->tok_len if (test_token(v7, "var", 3)) { TRY(parse_declaration(v7)); } else if (test_token(v7, "return", 6)) { TRY(parse_return_statement(v7)); if (is_return_statement != NULL) *is_return_statement = 1; } else if (test_token(v7, "if", 2)) { TRY(parse_if_statement(v7)); } else { v7->cursor = v7->tok; TRY(parse_expression(v7)); } } else { TRY(parse_expression(v7)); } // Skip optional semicolons while (*v7->cursor == ';') match(v7, *v7->cursor); //return is_return_statement; return V7_OK; }
/*----------------------------------------------------------------------*/ static ast_expression_t *parse_argument(parse_state_t *p) { token_t *token; ast_expression_t *expression; token = peek_token(p); if (test_token(token, TK_LBRACKET)) { next_token(p); expression = parse_logical_expression(p); token = next_token(p); EXPECT(token, TK_RBRACKET, "')' expected"); return expression; } else { return parse_value(p); } }
/*----------------------------------------------------------------------*/ static ast_expression_t *parse_value(parse_state_t *p) { token_t *token = peek_token(p); if (token == NULL) { } else if (token->type == TK_INT_LITERAL || token->type == TK_FLOAT_LITERAL || token->type == TK_DOUBLE_LITERAL || token->type == TK_STRING_LITERAL) { return parse_literal(p); } else if (token->type == TK_IDENTIFIER) { next_token(p); if (test_token(peek_token(p), TK_LBRACKET)) { rewind_token(p); return parse_function_call(p); } else { rewind_token(p); return parse_variable(p); } } error(p, "value expected"); return NULL; /* unreachable */ }
// factor = number | string_literal | "(" expression ")" | // variable | "this" | "null" | "true" | "false" | // "{" object_literal "}" | // "[" array_literal "]" | // function_definition | // function_call static enum v7_err parse_factor(struct v7 *v7) { int old_sp = v7_sp(v7); if (*v7->cursor == '(') { TRY(match(v7, '(')); TRY(parse_expression(v7)); TRY(match(v7, ')')); } else if (*v7->cursor == '\'' || *v7->cursor == '"') { TRY(parse_string_literal(v7)); } else if (*v7->cursor == '{') { TRY(parse_object_literal(v7)); } else if (is_alpha(*v7->cursor) || *v7->cursor == '_') { TRY(parse_identifier(v7)); if (test_token(v7, "this", 4)) { inc_stack(v7, 1); v7_top(v7)[-1] = &v7->scopes[v7->current_scope]; } else if (test_token(v7, "null", 4)) { TRY(v7_make_and_push(v7, V7_NULL)); } else if (test_token(v7, "true", 4)) { TRY(v7_make_and_push(v7, V7_BOOL)); v7_top(v7)[-1]->v.num = 1; } else if (test_token(v7, "false", 5)) { TRY(v7_make_and_push(v7, V7_BOOL)); v7_top(v7)[-1]->v.num = 0; } else if (test_token(v7, "function", 8)) { TRY(parse_function_definition(v7, NULL, 0)); } else if (test_token(v7, "delete", 6)) { TRY(parse_delete(v7)); } else { TRY(parse_variable(v7)); } } else { TRY(parse_num(v7)); } if (*v7->cursor == '(') { TRY(parse_function_call(v7)); } // Don't leave anything on stack if no execution flag is set if (v7->no_exec) { inc_stack(v7, old_sp - v7->sp); } return V7_OK; }
/*----------------------------------------------------------------------*/ static ast_statement_t *parse_return(parse_state_t *p) { token_t *token; ast_statement_t *statement; statement = ALLOC(sizeof(ast_statement_t)); BZERO(statement); statement->tag = AST_STATEMENT_RETURN; /* return keyword */ token = next_token(p); EXPECT(token, TK_RETURN, "'return' expected"); /* return value? */ token = peek_token(p); if (test_token(token, TK_END)) { /* no return value */ statement->u.return_.expr = NULL; } else { /* return value */ statement->u.return_.expr = parse_logical_expression(p); } return statement; }
/*----------------------------------------------------------------------*/ static ast_statement_t *parse_for(parse_state_t *p) { token_t *token; ast_statement_t *statement_for; ast_statement_t **next_statement; statement_for = ALLOC(sizeof(ast_statement_t)); BZERO(statement_for); statement_for->tag = AST_STATEMENT_FOR; statement_for->u.for_.initialize = NULL; statement_for->u.for_.condition = NULL; statement_for->u.for_.increment = NULL; /* for */ token = next_token(p); EXPECT(token, TK_FOR, "'for' expected"); /* init statements */ next_statement = &statement_for->u.for_.initialize; for (;;) { token = peek_token(p); if (test_token(token, TK_SEMICOLON)) { next_token(p); break; } *next_statement = parse_statement(p); if (*next_statement == NULL) { error(p, "statement expected"); } next_statement = &((*next_statement)->next); *next_statement = NULL; token = peek_token(p); if (test_token(token, TK_SEMICOLON)) { next_token(p); break; } else if (test_token(token, TK_COMMA)) { next_token(p); } else { error(p, "';' or ',' expected"); } } /* conditional */ token = peek_token(p); if (test_token(token, TK_SEMICOLON)) { next_token(p); } else { statement_for->u.for_.condition = parse_logical_expression(p); if (statement_for->u.for_.condition == NULL) { error(p, "conditional expression expected"); } token = peek_token(p); EXPECT(token, TK_SEMICOLON, "';' expected"); next_token(p); } /* increment statements */ next_statement = &statement_for->u.for_.increment; for (;;) { token = peek_token(p); if (test_token(token, TK_DO)) { next_token(p); break; } *next_statement = parse_statement(p); if (*next_statement == NULL) { error(p, "increment statement or 'do' expected"); } next_statement = &((*next_statement)->next); *next_statement = NULL; token = peek_token(p); if (test_token(token, TK_DO)) { next_token(p); break; } else if (test_token(token, TK_COMMA)) { next_token(p); } else { error(p, "'do' or ',' expected"); } } /* block */ statement_for->u.for_.block = parse_statement_list(p); /* end */ token = next_token(p); EXPECT(token, TK_END, "'end' expected"); return statement_for; }
/*----------------------------------------------------------------------*/ static ast_statement_t *parse_define_function(parse_state_t *p) { token_t *token; ast_statement_t *statement; ast_function_parameter_t *parameter = NULL; ast_function_parameter_t *prev_parameter = NULL; statement = ALLOC(sizeof(ast_statement_t)); BZERO(statement); statement->tag = AST_STATEMENT_DEFINE_FUNCTION; /* function keyword */ token = next_token(p); EXPECT(token, TK_FUNCTION, "'function' expected"); /* function name */ token = next_token(p); EXPECT(token, TK_IDENTIFIER, "function name expected"); statement->u.define_function.name_token = token; /* function parameters */ token = peek_token(p); if (test_token(token, TK_LBRACKET)) { /* opening bracket? */ next_token(p); for (;;) { token = peek_token(p); if (token == NULL || token->type != TK_IDENTIFIER) break; next_token(p); prev_parameter = parameter; parameter = ALLOC(sizeof(ast_function_parameter_t)); if (prev_parameter == NULL) { statement->u.define_function.first_parameter = parameter; } else { prev_parameter->next = parameter; } /* parameter name */ parameter->identifier_token = token; /* colon before parameter type */ token = next_token(p); EXPECT(token, TK_COLON, "':' expected"); /* parameter type */ token = next_token(p); EXPECT(token, TK_IDENTIFIER, "parameter type expected"); parameter->type_token = token; /* comma? */ token = peek_token(p); if (!test_token(token, TK_COMMA)) break; next_token(p); } /* closing bracket */ token = next_token(p); EXPECT(token, TK_RBRACKET, "')' expected"); } /* colon before return type? */ token = peek_token(p); if (test_token(token, TK_COLON)) { next_token(p); /* return type */ token = next_token(p); statement->u.define_function.return_type_token = token; EXPECT(token, TK_IDENTIFIER, "return type expected"); } /* function body statement list */ statement->u.define_function.block = parse_statement_list(p); /* end */ token = next_token(p); EXPECT(token, TK_END, "'end' expected"); return statement; }
int main(int argc, char **argv) { int optidx= 0; char *user; char *password; char ***list, **p; size_t num_list, i, j, k; int failed = 0; setprogname (argv[0]); if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) usage (1); if (help_flag) usage (0); if (version_flag) { print_version (NULL); return 0; } if (optidx != argc) usage (1); if (principals.num_strings == 0) errx(1, "no principals"); user = estrdup(principals.strings[0]); password = strchr(user, ':'); if (password == NULL) errx(1, "password missing from %s", user); *password++ = 0; if (slaves.num_strings == 0) errx(1, "no principals"); if (logfile_str) { printf("open logfile %s\n", logfile_str); logfile = fopen(logfile_str, "w+"); if (logfile == NULL) err(1, "failed to open: %s", logfile_str); } /* * */ list = permutate_all(&slaves, &num_list); /* * Set up connection to all clients */ printf("Connecting to slaves\n"); for (i = 0; i < slaves.num_strings; i++) connect_client(slaves.strings[i]); /* * Test acquire credentials */ printf("Test acquire credentials\n"); for (i = 0; i < slaves.num_strings; i++) { int32_t hCred, val; val = acquire_cred(clients[i], user, password, 1, &hCred); if (val != GSMERR_OK) { warnx("Failed to acquire_cred on host %s: %d", clients[i]->moniker, (int)val); failed = 1; } else toast_resource(clients[i], hCred); } if (failed) goto out; /* * First test if all slaves can build context to them-self. */ printf("Self context tests\n"); for (i = 0; i < num_clients; i++) { int32_t hCred, val, delegCred; int32_t clientC, serverC; struct client *c = clients[i]; if (c->target_name == NULL) continue; printf("%s connects to self using %s\n", c->moniker, c->target_name); val = acquire_cred(c, user, password, 1, &hCred); if (val != GSMERR_OK) errx(1, "failed to acquire_cred: %d", (int)val); val = build_context(c, c, GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG| GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG| GSS_C_DELEG_FLAG|GSS_C_MUTUAL_FLAG, hCred, &clientC, &serverC, &delegCred); if (val == GSMERR_OK) { test_token(c, clientC, c, serverC, wrap_ext); toast_resource(c, clientC); toast_resource(c, serverC); if (delegCred) toast_resource(c, delegCred); } else { warnx("build_context failed: %d", (int)val); } /* * */ val = build_context(c, c, GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG, hCred, &clientC, &serverC, &delegCred); if (val == GSMERR_OK) { test_token(c, clientC, c, serverC, wrap_ext); toast_resource(c, clientC); toast_resource(c, serverC); if (delegCred) toast_resource(c, delegCred); } else { warnx("build_context failed: %d", (int)val); } toast_resource(c, hCred); } /* * Build contexts though all entries in each lists, including the * step from the last entry to the first, ie treat the list as a * circle. * * Only follow the delegated credential, but test "all" * flags. (XXX only do deleg|mutual right now. */ printf("\"All\" permutation tests\n"); for (i = 0; i < num_list; i++) { int32_t hCred, val, delegCred = 0; int32_t clientC = 0, serverC = 0; struct client *client, *server; p = list[i]; client = get_client(p[0]); val = acquire_cred(client, user, password, 1, &hCred); if (val != GSMERR_OK) errx(1, "failed to acquire_cred: %d", (int)val); for (j = 1; j < num_clients + 1; j++) { server = get_client(p[j % num_clients]); if (server->target_name == NULL) break; for (k = 1; k < j; k++) printf("\t"); printf("%s -> %s\n", client->moniker, server->moniker); val = build_context(client, server, GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG| GSS_C_INTEG_FLAG|GSS_C_CONF_FLAG| GSS_C_DELEG_FLAG|GSS_C_MUTUAL_FLAG, hCred, &clientC, &serverC, &delegCred); if (val != GSMERR_OK) { warnx("build_context failed: %d", (int)val); break; } val = test_token(client, clientC, server, serverC, wrap_ext); if (val) break; toast_resource(client, clientC); toast_resource(server, serverC); if (!delegCred) { warnx("no delegated cred on %s", server->moniker); break; } toast_resource(client, hCred); hCred = delegCred; client = server; } if (hCred) toast_resource(client, hCred); } /* * Close all connections to clients */ out: printf("sending goodbye and waiting for log sockets\n"); for (i = 0; i < num_clients; i++) { goodbye(clients[i]); if (clients[i]->logsock) { #ifdef ENABLE_PTHREAD_SUPPORT pthread_join(&clients[i]->thr, NULL); #else waitpid(clients[i]->child, NULL, 0); #endif } } printf("done\n"); return 0; }