From 6a24c69232901de51f50beab9c3d3b3760dee334 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Sat, 16 Mar 2024 22:06:32 -0600 Subject: (Nonfunctional) Implemented many type-checking rules --- Typecheck.java | 69 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'Typecheck.java') diff --git a/Typecheck.java b/Typecheck.java index 039e94d..be979f5 100644 --- a/Typecheck.java +++ b/Typecheck.java @@ -8,43 +8,44 @@ import minijava.*; public class Typecheck { public static void main(String[] args) { - Node root = null; - try { - root = new MiniJavaParser(System.in).Goal(); + Node root = null; + try { + root = new MiniJavaParser(System.in).Goal(); - // Pretty-print the tree. PPrinter inherits from - // GJDepthFirst. R=Void, A=String. - // PPrinter pp = new PPrinter(); - // root.accept(pp, ""); + // Pretty-print the tree. PPrinter inherits from + // GJDepthFirst. R=Void, A=String. + PPrinter pp = new PPrinter(); + root.accept(pp, ""); - // // Build the symbol table. Top-down visitor, inherits from - // // GJDepthFirst. R=Void, A=Integer. - // SymTableVis pv = - // new SymTableVis(); - // root.accept(pv, 0); - // HashMap symt = pv.symt; + // // Build the symbol table. Top-down visitor, inherits from + // // GJDepthFirst. R=Void, A=Integer. + // SymTableVis pv = + // new SymTableVis(); + // root.accept(pv, 0); + // HashMap symt = pv.symt; - // // Do type checking. Bottom-up visitor, also inherits from - // // GJDepthFirst. Visit functions return MyTpe (=R), and - // // take a symbol table (HashMap) as - // // argument (=A). You may implement things differently of - // // course! - // TypeCheckSimp ts = new TypeCheckSimp(); - // MyType res = root.accept(ts, symt); + // Do type checking. Bottom-up visitor, also inherits from + // GJDepthFirst. Visit functions return MyTpe (=R), and + // take a symbol table (HashMap) as + // argument (=A). You may implement things differently of + // course! + TypeCheckSimp ts = new TypeCheckSimp(); + TypeInstance res = root.accept(ts, null); + + // 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); + } - // // 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) - // System.out.println("Program type checked successfully"); - // else - // System.out.println("Type error"); - System.out.println("Type error"); - } - catch (ParseException e) { - System.out.println(e.toString()); - System.exit(1); - } } } -- cgit v1.2.3