summaryrefslogtreecommitdiff
path: root/Typecheck.java
blob: f08e1e74a129689d82d0197eff698643c3cd8ad1 (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
import java.io.*;
import visitor.*;
import parse.*;
import syntaxtree.*;
import java.util.*;
import st.*;
import misc.*;
import typecheck.library.*;

public class Typecheck {
    public static void main(String[] args) {
        Node root = null;
        try {
            root = new MiniJavaParser(System.in).Goal();

            // Pretty-print the tree. PPrinter inherits from
            // GJDepthFirst<R,A>. R=Void, A=String.
            PPrinter<Void,String> pp = new PPrinter<Void,String>();
            root.accept(pp, "");
            PrintFilter.print("===================================================", true);

            // // Build the symbol table. Top-down visitor, inherits from
            // // GJDepthFirst<R,A>. R=Void, A=Integer.
            SymTableVis<Void,Integer> pv =
                new SymTableVis<Void,Integer>();
            root.accept(pv, 0);
            HashMap<String, AbstractInstance> symt = pv.symt;
            PrintFilter.print("===================================================", true);

            // Do type checking. Bottom-up visitor, also inherits from
            // GJDepthFirst. Visit functions return MyTpe (=R), and
            // take a symbol table (HashMap<String,String>) as
            // argument (=A). You may implement things differently of
            // course!
            TypeCheckSimp ts = new TypeCheckSimp();
            TypeInstance res = root.accept(ts, symt);

            // Ugly code not to be inspired from: "my" way of storing
            // type info / typecheck property: if some of my internal
            // structure is empty, then things don't typecheck for
            // me. This is specific to my own implementation.
            // if (res != null && res.type_array.size() > 0)
            if (res.get_type() != TypeEnum.ERROR)
                System.out.println("Program type checked successfully");
            else
                System.out.println("Type error");
        }
        catch (ParseException e) {
            System.out.println(e.toString());
            System.exit(1);
        }

    }
}