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

/src/org/ooc/frontend/model/VersionBlock.java

http://github.com/nddrylliog/ooc
Java | 130 lines | 99 code | 25 blank | 6 comment | 5 complexity | d420d84761bbb95d927ce9bf47a81c4e MD5 | raw file
  1package org.ooc.frontend.model;
  2
  3import java.io.IOException;
  4import java.util.HashMap;
  5import java.util.Map;
  6
  7import org.ooc.frontend.Target;
  8import org.ooc.frontend.Visitor;
  9import org.ooc.frontend.model.VersionNodes.VersionAnd;
 10import org.ooc.frontend.model.VersionNodes.VersionName;
 11import org.ooc.frontend.model.VersionNodes.VersionNegation;
 12import org.ooc.frontend.model.VersionNodes.VersionNode;
 13import org.ooc.frontend.model.VersionNodes.VersionNodeVisitor;
 14import org.ooc.frontend.model.VersionNodes.VersionOr;
 15import org.ooc.frontend.model.VersionNodes.VersionParen;
 16import org.ooc.frontend.model.interfaces.MustBeResolved;
 17import org.ooc.frontend.model.tokens.Token;
 18import org.ooc.middle.OocCompilationError;
 19import org.ooc.middle.hobgoblins.Resolver;
 20import org.ubi.CompilationFailedError;
 21
 22public class VersionBlock extends Block implements MustBeResolved {
 23
 24	protected static Map<String, String> map = new HashMap<String, String>();
 25
 26	static {
 27		
 28		// Java's excuse for a Map literal
 29		map.put("windows", 		"__WIN32__) || defined(__WIN64__"); // FIXME: does that imply that we're not 64 ?
 30		map.put("linux", 		"__linux__");
 31		map.put("solaris", 		"__sun");
 32		map.put("unix", 		"__unix__");
 33		map.put("beos", 		"__BEOS__");
 34		map.put("haiku", 		"__HAIKU__");
 35		map.put("apple", 		"__APPLE__");
 36		map.put("gnuc", 		"__GNUC__");
 37		map.put("i386", 		"__i386__");
 38		map.put("x86", 			"__X86__");
 39		map.put("x86_64", 		"__x86_64__");
 40		map.put("ppc", 			"__ppc__");
 41		map.put("ppc64",		"__ppc64__");
 42		map.put("64", 			"__x86_64__) || defined(__ppc64__");
 43		map.put("gc",			"__OOC_USE_GC__");
 44		
 45	}
 46	
 47	private VersionNode version;
 48
 49	/**
 50	 * Default constructor
 51	 * @param location
 52	 * @param id The id of this version. One of "linux", "windows", etc.
 53	 */
 54	public VersionBlock(VersionNode version, Token startToken) {
 55		super(startToken);
 56		this.version = version;
 57	}
 58	
 59	public VersionNode getVersion() {
 60		return version;
 61	}
 62
 63	public boolean isResolved() {
 64		return false;
 65	}
 66
 67	public Response resolve(final NodeList<Node> stack, Resolver res, boolean fatal) {
 68		
 69		try {
 70			version.accept(new VersionNodeVisitor() {
 71				
 72				public void visit(VersionOr versionOr) throws IOException {
 73					versionOr.acceptChildren(this);
 74				}
 75				
 76				public void visit(VersionAnd versionAnd) throws IOException {
 77					versionAnd.acceptChildren(this);
 78				}
 79				
 80				public void visit(VersionNegation versionNegation) throws IOException {
 81					versionNegation.acceptChildren(this);				
 82				}
 83				
 84				public void visit(VersionName versionName) {
 85					if(versionName.solved) return;
 86					String match = map.get(versionName.name.toLowerCase());
 87					if(match != null) {
 88						versionName.name = match;
 89						versionName.solved = true;
 90					} else {
 91						System.out.println(new OocCompilationError(VersionBlock.this, stack,
 92								"Unknown version id: '" + versionName.name
 93								+ "', compiling anyway (who knows?)").toString());
 94					}
 95				}
 96
 97				public void visit(VersionParen versionParen) throws IOException {
 98					versionParen.acceptChildren(this);
 99				}
100			});
101		} catch (IOException e) {
102			throw new CompilationFailedError(e);
103		}
104		
105		return Response.OK;
106		
107	}
108	
109	@Override
110	public void accept(Visitor visitor) throws IOException {
111		visitor.visit(this);
112	}
113	
114	@Override
115	public boolean equals(Object o) {
116		if(!(o instanceof VersionBlock)) return super.equals(o);
117		VersionBlock vb = (VersionBlock) o;
118		return this.version.toString().equals(vb.version.toString());
119	}
120
121	public boolean isSatisfied(Target target) {
122		return version.isSatisfied(target);
123	}
124	
125	@Override
126	public String toString() {
127		return version.toString();
128	}
129
130}