Charj: Forgot to include VariableInitializer.
authorJonathan Lifflander <jliffl2@illinois.edu>
Thu, 1 Jul 2010 21:32:36 +0000 (16:32 -0500)
committerJonathan Lifflander <jliffl2@illinois.edu>
Thu, 1 Jul 2010 21:32:36 +0000 (16:32 -0500)
src/langs/charj/src/charj/translator/VariableInitializer.java [new file with mode: 0644]

diff --git a/src/langs/charj/src/charj/translator/VariableInitializer.java b/src/langs/charj/src/charj/translator/VariableInitializer.java
new file mode 100644 (file)
index 0000000..066b7b9
--- /dev/null
@@ -0,0 +1,60 @@
+package charj.translator;
+
+import org.antlr.runtime.*;
+import org.antlr.runtime.tree.*;
+import org.antlr.stringtemplate.*;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
+
+/** 
+ *  Encapsulates a "variableInitializer" in the CharjAST, used for sending the
+ *  reference of the AST to the emitter. 
+ */
+public class VariableInitializer {
+    private CharjAST init;
+    private CharjAST ident;
+
+    public VariableInitializer(CharjAST init_, CharjAST ident_) {
+        init = init_;
+        ident = ident_;
+    }
+    
+    public static TreeAdaptor m_adaptor = new CommonTreeAdaptor() {
+        public Object create(Token token) {
+            return new CharjAST(token);
+        }
+        
+        public Object dupNode(Object t) {
+            if (t == null) {
+                return null;
+            }
+            return create(((CharjAST)t).token);
+        }
+    };
+
+    public String emit() {
+        CommonTree m_ast = (CommonTree)init;
+        CommonTreeNodeStream m_nodes = new CommonTreeNodeStream(m_ast);
+        m_nodes.setTreeAdaptor(m_adaptor);
+        CharjEmitter emitter = new CharjEmitter(m_nodes);
+        emitter.setTemplateLib(Translator.
+                               getTemplates(Translator.templateFile));
+        try {
+            StringTemplate st = 
+                (StringTemplate)emitter.expression().getTemplate();
+
+            StringTemplate st2 = new StringTemplate
+                ("<name> = <init>;", AngleBracketTemplateLexer.class);
+
+            st2.setAttribute("name", ident.def.name);
+            st2.setAttribute("init", st.toString());
+
+            return st2.toString();
+        } catch (RecognitionException ex) {
+            //TODO: Decouple Translator's error handling and mix with this
+            System.err.println(ex.getMessage());
+            ex.printStackTrace(System.err);
+            return "";
+        }
+    }
+}
\ No newline at end of file