static ast_t* duplicate(ast_t* parent, ast_t* ast) { if(ast == NULL) return NULL; assert(ast_id(ast) != TK_PROGRAM && ast_id(ast) != TK_PACKAGE && ast_id(ast) != TK_MODULE); ast_t* n = ast_token(token_dup(ast->t)); n->data = ast->data; n->flags = ast->flags & AST_ALL_FLAGS; // We don't actually want to copy the orphan flag, but the following if // always explicitly sets or clears it. if(parent == NULL) set_scope_no_parent(n, ast->parent); else set_scope_and_parent(n, parent); n->child = duplicate(n, ast->child); n->type = duplicate(n, ast->type); if(ast->symtab != NULL) n->symtab = symtab_dup(ast->symtab); if(parent != NULL) n->sibling = duplicate(parent, ast->sibling); return n; }
ast_t* ast_from_float(ast_t* ast, double value) { assert(ast != NULL); token_t* t = token_dup(ast->t); token_set_id(t, TK_FLOAT); token_set_float(t, value); ast_t* new_ast = ast_token(t); set_scope_no_parent(new_ast, ast->parent); return new_ast; }
ast_t* ast_from_int(ast_t* ast, uint64_t value) { assert(ast != NULL); token_t* t = token_dup(ast->t); token_set_id(t, TK_INT); lexint_t lexint = {value, 0}; token_set_int(t, &lexint); ast_t* new_ast = ast_token(t); set_scope_no_parent(new_ast, ast->parent); return new_ast; }
ast_t* ast_from_string(ast_t* ast, const char* name) { if(name == NULL) return ast_from(ast, TK_NONE); token_t* t = token_dup(ast->t); token_set_id(t, TK_ID); token_set_string(t, name, 0); ast_t* new_ast = ast_token(t); set_scope_no_parent(new_ast, ast->parent); return new_ast; }
token_t* token_dup_new_id(token_t* token, token_id id) { token_t* new_token = token_dup(token); new_token->id = id; return new_token; }