PageRenderTime 909ms CodeModel.GetById 501ms app.highlight 154ms RepoModel.GetById 168ms app.codeStats 0ms

/src/org/ooc/backend/cdirty/ControlStatementWriter.java

http://github.com/nddrylliog/ooc
Java | 84 lines | 75 code | 9 blank | 0 comment | 7 complexity | b39eb15106be52f06e26974304f71950 MD5 | raw file
 1package org.ooc.backend.cdirty;
 2
 3import java.io.IOException;
 4
 5import org.ooc.frontend.model.Else;
 6import org.ooc.frontend.model.For;
 7import org.ooc.frontend.model.Foreach;
 8import org.ooc.frontend.model.If;
 9import org.ooc.frontend.model.Line;
10import org.ooc.frontend.model.NodeList;
11import org.ooc.frontend.model.RangeLiteral;
12import org.ooc.frontend.model.VariableAccess;
13import org.ooc.frontend.model.While;
14import org.ooc.middle.OocCompilationError;
15
16public class ControlStatementWriter {
17
18	public static void writeIf(If if1, CGenerator cgen) throws IOException {
19		cgen.current.app("if (");
20		if1.getCondition().accept(cgen);
21		cgen.current.app(")");
22		NodeList<Line> body = if1.getBody();
23		cgen.current.openBlock();
24		body.accept(cgen);
25		cgen.current.closeBlock();
26	}
27	
28	public static void writeElse(Else else1, CGenerator cgen) throws IOException {
29		cgen.current.app("else ");
30		NodeList<Line> body = else1.getBody();
31		if(body.size() == 1 && (body.get(0).getStatement() instanceof If)) {
32			body.get(0).getStatement().accept(cgen);
33		} else {
34			cgen.current.openBlock();
35			body.accept(cgen);
36			cgen.current.closeBlock();
37		}
38	}
39
40	public static void writeWhile(While while1, CGenerator cgen) throws IOException {
41		cgen.current.app("while (");
42		while1.getCondition().accept(cgen);
43		cgen.current.app(")").openBlock();
44		while1.getBody().accept(cgen);
45		cgen.current.closeBlock();
46	}
47	
48	public static void writeForeach(Foreach foreach, CGenerator cgen) throws IOException {
49		if(foreach.getCollection() instanceof RangeLiteral) {
50			RangeLiteral range = (RangeLiteral) foreach.getCollection();
51			cgen.current.app("for (");
52			foreach.getVariable().accept(cgen);
53			cgen.current.app(" = ");
54			range.getLower().accept(cgen);
55			cgen.current.app("; ");
56			foreach.getVariable().accept(cgen);
57			cgen.current.app(" < ");
58			range.getUpper().accept(cgen);
59			cgen.current.app("; ");
60			foreach.getVariable().accept(cgen);
61			cgen.current.app("++").app(")").openBlock();
62			foreach.getBody().accept(cgen);
63			cgen.current.closeBlock();
64		} else { 
65			throw new OocCompilationError(foreach.getCollection(), cgen.module,
66					"Iterating over... not a Range but a "+foreach.getCollection().getType());
67		}
68	}
69
70	public static void writeFor(For for1, CGenerator cgen) throws IOException {
71		cgen.current.app("for (");
72		if(!(for1.getInit() instanceof VariableAccess)) {
73			for1.getInit().accept(cgen);
74		}
75		cgen.current.app("; ");
76		for1.getTest().accept(cgen);
77		cgen.current.app("; ");
78		for1.getIter().accept(cgen);
79		cgen.current.app(")").openBlock();
80		for1.getBody().accept(cgen);
81		cgen.current.closeBlock();
82	}
83	
84}