PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 197 lines | 176 code | 21 blank | 0 comment | 3 complexity | 59423fd23761611b1ef19ba0d95e1134 MD5 | raw file
  1package org.ooc.frontend.model;
  2
  3import java.io.IOException;
  4import java.util.Iterator;
  5
  6import org.ooc.frontend.Visitor;
  7import org.ooc.frontend.model.tokens.Token;
  8
  9public class OpDecl extends Declaration {
 10
 11	public enum OpType {
 12		ADD,
 13		ADD_ASS,
 14		SUB,
 15		SUB_ASS,
 16		MUL,
 17		MUL_ASS,
 18		DIV,
 19		DIV_ASS,
 20		ASS,
 21		NOT,
 22		MOD,
 23		L_OR,
 24		L_AND,
 25		B_XOR,
 26		B_XOR_ASS,
 27		B_OR,
 28		B_OR_ASS,
 29		B_AND,
 30		B_AND_ASS,
 31		LSHIFT,
 32		B_LSHIFT_ASS,
 33		RSHIFT,
 34		B_RSHIFT_ASS,
 35		IDX,
 36		IDX_ASS,
 37		GT,
 38		GTE,
 39		LT,
 40		LTE,
 41		EQ,
 42		NE,
 43		AS,
 44		B_NEG;
 45		
 46		public String toPrettyString() {
 47			switch(this) {
 48			case ADD:
 49				return "+";
 50			case DIV:
 51				return "/";
 52			case IDX_ASS:
 53				return "[]=";
 54			case IDX:
 55				return "[]";
 56			case MUL:
 57				return "*";
 58			case SUB:
 59				return "-";
 60			case B_AND:
 61				return "&";
 62			case B_OR:
 63				return "|";
 64			case B_XOR:
 65				return "^";
 66			case L_AND:
 67				return "&&";
 68			case L_OR:
 69				return "||";
 70			case MOD:
 71				return "%";
 72			case EQ:
 73				return "==";
 74			case GT:
 75				return ">";
 76			case GTE:
 77				return ">=";
 78			case LT:
 79				return "<";
 80			case LTE:
 81				return "<=";
 82			case NE:
 83				return "!=";
 84			case NOT:
 85				return "!";
 86			case ASS:
 87				return "=";
 88			case ADD_ASS:
 89				return "+=";
 90			case DIV_ASS:
 91				return "/=";
 92			case MUL_ASS:
 93				return "*=";
 94			case SUB_ASS:
 95				return "-=";
 96			case B_LSHIFT_ASS:
 97				return "<<=";
 98			case B_RSHIFT_ASS:
 99				return ">>=";
100			case B_XOR_ASS:
101				return "^=";
102			case LSHIFT:
103				return ">>";
104			case RSHIFT:
105				return "<<";
106			case B_OR_ASS:
107				return "|=";
108			case B_AND_ASS:
109				return "&=";
110			case AS:
111				return "as";
112			case B_NEG:
113				return "~";
114			}
115			
116			return "unknown";
117		}
118
119		public boolean isNumeric() {
120			switch(this) {
121			case ADD: case SUB: case MUL: case DIV:
122			case ADD_ASS: case SUB_ASS: case MUL_ASS: case DIV_ASS:
123			case B_AND: case B_OR: case B_XOR: case B_NEG:
124				return true;
125			default:
126				return false;
127			}
128		}
129	}
130	
131	protected OpType opType;
132	protected FunctionDecl func;
133	
134	public OpDecl(OpType opType, FunctionDecl func, Token startToken, Module module) {
135		super("Operator "+opType, startToken, module);
136		this.opType = opType;
137		this.func = func;
138		String name = "__OP_"+opType.toString();
139		Iterator<Argument> iter = func.getArguments().iterator();
140		while(iter.hasNext()) {
141			name += "_" + iter.next().getType().getMangledName();
142		}
143		if(!func.getReturnType().isVoid()) {
144			name += "__" + func.getReturnType().getMangledName();
145		}
146		func.setName(name);
147	}
148
149	@Override
150	public boolean replace(Node oldie, Node kiddo) {
151		if(oldie == func) {
152			func = (FunctionDecl) kiddo;
153			return true;
154		}
155		
156		return false;
157	}
158
159	public Type getType() {
160		return new Type("Operator", Token.defaultToken);
161	}
162	
163	public OpType getOpType() {
164		return opType;
165	}
166	
167	public FunctionDecl getFunc() {
168		return func;
169	}
170
171	public void accept(Visitor visitor) throws IOException {
172		visitor.visit(this);
173	}
174
175	public void acceptChildren(Visitor visitor) throws IOException {
176		func.accept(visitor);
177	}
178
179	public boolean hasChildren() {
180		return true;
181	}
182
183	public String getOpString() {
184		return opType.toPrettyString();
185	}
186	
187	@Override
188	public String toString() {
189		return "operator "+getOpString()+" "+func.getArgsRepr()+" -> "+func.getReturnType();
190	}
191	
192	@Override
193	public TypeDecl getTypeDecl() {
194		throw new Error("getting type decl of an "+getClass().getSimpleName()+", wtf?");
195	}
196
197}