summaryrefslogtreecommitdiff
path: root/vaporize/Kettle.java
blob: 4d7a3db78afc70aba1c9c270d2fd39b1ecb22532 (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
112
113
114
115
116
package vaporize;

import cs132.vapor.ast.*;
import misc.*;

import java.util.ArrayList;

/**
 * This class contains various generic methods for
 * assembling common-use vaporm strings and managing
 * the original input program.
 *
 * Robert Martin cries
 */
class Kettle {

    ArrayList<String> vapor;
    ArrayList<String> vaporm;

    protected Kettle(ArrayList<String> vapor) {
        MinimalLogger.info("Pouring program into kettle...");
        this.vapor = vapor;
        this.vaporm = new ArrayList<String>();
    }

    protected String get(Node n) {
        /**
         * Given the source position of a Node, returns the original line.
         */
        return this.vapor.get(this.indexOf(n));
    }

    protected void replaceFunctionDeclare(VFunction prev, int in,
                                          int out, int local) {
        /**
         * Replaces a function declaraction in the program
         * with the vapor equivalent.
         */
        String next = String.format("func %s [in %d, out %d, local %d]",
                                    this.parseFuncName(prev),
                                    in,
                                    out,
                                    local);

        MinimalLogger.info(String.format("Replacing function declaraction %s with %s",
                                         this.get(prev),
                                         next));

        this.set(prev, next);
    }

    protected String parseFuncName(VFunction func) {
        /**
         * Helper for replaceFunctionDeclare
         * "func A_foo(this t.1)"  -> "A_foo"
         */
        String str = this.get(func);
        int openParenIndex = str.indexOf('(');
        return str.substring(5, openParenIndex).trim();
    }

    protected String spill() {
        return null;
    }

    protected String backup() {
        return null;
    }

    protected String dump() {
        return String.join("\n", this.vapor);
    }

    private void set(Node n, String s) {
        /**
         * Sets the position of Node n in the original
         * file to String s.
         */
        this.vapor.set(this.indexOf(n), s);
    }

    protected int indexOf(Node n) {
        return n.sourcePos.line-1;
    }

    // METHODS INTENDED TO BE USED WITH CFG

    /**
     * Needed because VBranch doesn't seem to
     * keep track of this...
     */
    protected int findLabelIndex(String str) {
        int index = -3;
        // is this guarenteed?
        String search = str.substring(1);
        String comp;
        for (int i = 0; i < this.vapor.size(); ++i) {
            if (!this.vapor.get(i).isEmpty()) {
                comp = this.vapor.get(i).trim();
                if (comp.substring(0, comp.length()-1)
                    .equals(search)) {
                    index = i;
                    break;
                }
            }
        }

        if (index == -3) {
            MinimalLogger.severe(String.format("findLabelIndex could not compute label for %s!",
                                               str.trim()));
        }
        // go to the section AFTER the label, and account for starting at zero
        return index+2;
    }

}