PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 50 lines | 39 code | 10 blank | 1 comment | 1 complexity | 2357afe814d0c9fb53902475939894a2 MD5 | raw file
 1package org.ooc.backend.cdirty;
 2
 3import java.io.IOException;
 4import java.math.BigInteger;
 5
 6import org.ooc.frontend.model.FloatLiteral;
 7import org.ooc.frontend.model.IntLiteral;
 8import org.ooc.frontend.model.StringLiteral;
 9import org.ubi.SourceReader;
10
11public class LiteralWriter {
12
13	private static BigInteger  INT_MAX = new BigInteger("2").pow(32);
14	
15	public static void writeFloat(FloatLiteral floatLiteral, CGenerator cgen) throws IOException {
16		cgen.current.app(Double.toString(floatLiteral.getValue()));
17	}
18
19	public static void writeString(StringLiteral stringLiteral, CGenerator cgen) throws IOException {
20		cgen.current.app('"');
21		SourceReader.spelled(stringLiteral.getValue(), cgen.current);
22		cgen.current.app('"');
23	}
24
25	public static void writeInt(IntLiteral numberLiteral, CGenerator cgen) throws IOException {
26		switch(numberLiteral.getFormat()) {
27		case HEX:
28		case BIN: // C has no binary literals, write it as hex
29			cgen.current.app("0x");
30			cgen.current.app(numberLiteral.getValue().toString(16));
31			break;
32		case OCT:
33			cgen.current.app('0');
34			cgen.current.app(numberLiteral.getValue().toString(8));
35			break;
36		default:
37			cgen.current.app(numberLiteral.getValue().toString());
38		}
39		
40		// if it's greater than an int's max, then it's a Long
41		if(numberLiteral.getValue().compareTo(INT_MAX) > -1) {
42			cgen.current.app("L");
43		}
44	}
45
46	public static void writeNull(CGenerator cgen) throws IOException {
47		cgen.current.app("NULL");		
48	}
49
50}