summaryrefslogtreecommitdiff
path: root/src/commit.c
blob: 28da8c1d08b2529aae162702016c6205e1595e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2015-2018 Adrian Thurston <thurston@colm.net>
 *
 * 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.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "config.h"
#include "debug.h"
#include "pdarun.h"
#include "bytecode.h"
#include "tree.h"
#include "pool.h"
#include "internal.h"

void commit_clear_kid_list( program_t *prg, tree_t **sp, kid_t *kid )
{
	kid_t *next;
	while ( kid ) {
		colm_tree_downref( prg, sp, kid->tree );
		next = kid->next;
		kid_free( prg, kid );
		kid = next;
	}
}

void commit_clear_parse_tree( program_t *prg, tree_t **sp,
		struct pda_run *pda_run, parse_tree_t *pt )
{
	tree_t **top = vm_ptop();

	if ( pt == 0 )
		return;

free_tree:
	if ( pt->next != 0 ) {
		vm_push_ptree( pt->next );
	}

	if ( pt->left_ignore != 0 ) {
		vm_push_ptree( pt->left_ignore );
	}

	if ( pt->child != 0 ) {
		vm_push_ptree( pt->child );
	}

	if ( pt->right_ignore != 0 ) {
		vm_push_ptree( pt->right_ignore );
	}

	/* Only the root level of the stack has tree 
	 * shadows and we are below that. */
	assert( pt->shadow == 0 );
	parse_tree_free( pda_run, pt );

	/* Any trees to downref? */
	if ( sp != top ) {
		pt = vm_pop_ptree();
		goto free_tree;
	}
}

static int been_committed( parse_tree_t *parse_tree )
{
	return parse_tree->flags & PF_COMMITTED;
}

void commit_reduce( program_t *prg, tree_t **root, struct pda_run *pda_run )
{
	tree_t **sp = root;
	parse_tree_t *pt = pda_run->stack_top;

	/* The top level of the stack is linked right to left. This is the
	 * traversal order we need for committing. */
	while ( pt != 0 && !been_committed( pt ) ) {
		vm_push_ptree( pt );
		pt = pt->next;
	}

	while ( sp != root ) {
		pt = vm_pop_ptree();

		prg->rtd->commit_reduce_forward( prg, sp, pda_run, pt );
		pt->child = 0;

		pt->flags |= PF_COMMITTED;
		pt = pt->next;
	}
}