Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / models / algebra / Constant.java
package models.algebra;

import generators.JavaImplementationVisitor;

import java.util.ArrayList;

public class Constant extends Term {
	
	public Constant(String value) {
		super(new Symbol(value, 0), new ArrayList<Expression>());
	}
	
	public Constant(String value, Type type) {
		super(new Symbol((type == null ? value : type.valueToRepresentation(value)), 0), new ArrayList<Expression>());
		symbol.setSignature(new Type[]{type});
	}
	
	public Constant(Symbol symbol) {
		super(symbol);
	}
	
	@Override
	public boolean equals(Object another) {
		if (!(another instanceof Constant)) return false;
		return symbol.equals(((Constant) another).symbol);
	}
	
	@Override
	public Object clone() {
		Constant c = new Constant(symbol);
		c.setType(type);
		return c;
	}
	
	public String toString() {
		return symbol.getName();
	}
	
	public Object getValue() {
		if (getType() != null) {
			return getType().representationToValue(symbol.getName());
		}
		return symbol.getName();
	}
	
	public String toImplementation(String[] sideEffects) {
		return accept(new JavaImplementationVisitor(), sideEffects).toString();
	}
	
	@Override
	public code.ast.Expression accept(IExpressionVisitor visitor, String[] sideEffects) {
		return visitor.visit(this, sideEffects);
	}
}