package calculator.interpreter.ast.vars; import java.util.LinkedList; import calculator.interpreter.AnnotationValue; import calculator.interpreter.Cell; import calculator.interpreter.Environment; import calculator.interpreter.Variable; import calculator.interpreter.ast.Expression; import calculator.interpreter.ast.Statement; import calculator.interpreter.ast.meta.Annotation; import calculator.interpreter.ast.meta.DocumentationLine; /** * The variable definition statement */ public class VarStatement extends Statement { /** the type of the statement ("var" or "let") */ public String type; /** the definition name */ public Name name; /** the statement value (optional) */ public Expression value; @Override public Environment eval(Environment env, Cell result) { env.checkFree(location, name.name()); boolean isVar = "var".equals(type); String docs; if (!this.documentation.isEmpty()) { StringBuilder b = new StringBuilder(); for (DocumentationLine l : this.documentation) { b.append(l.text).append('\n'); } docs = b.toString(); } else { docs = null; } LinkedList<AnnotationValue> evaluatedAnnotations = new LinkedList<AnnotationValue>(); for (Annotation a : this.annotations) { evaluatedAnnotations.add(a.eval(env)); } Variable var = new Variable(name.name(), isVar, location, docs, evaluatedAnnotations); Environment rc = new Environment(env, var); for (AnnotationValue a1 : var.annotations) { a1.process(rc, var); } if (value != null) { var.bind(this.location, value.eval(env)); } if (var.isBound && result != null) { result.value = var.value; } return rc; } }