X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FVariable.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FVariable.java;h=c48232c405fce43c92f4cb3afb43e0b90b99ca01;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java new file mode 100755 index 000000000..c48232c40 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java @@ -0,0 +1,79 @@ +package org.simantics.scl.compiler.elaboration.expressions; + +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; +import org.simantics.scl.compiler.internal.codegen.references.IVal; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.exceptions.UnificationException; +import org.simantics.scl.compiler.types.util.Typed; + +public class Variable implements Typed { + public static final Variable[] EMPTY_ARRAY = new Variable[0]; + + String name; + Type type; + transient private IVal val; + + public Variable(String name) { + this.name = name; + } + + public Variable(String name, Type type) { + this.name = name; + this.type = type; + } + + @Override + public Type getType() { + return type; + } + + public String getName() { + return name; + } + + public void setType(Type type) { + if(type == null) + throw new NullPointerException(); + if(this.type != null) + try { + Types.unify(type, this.type); + } catch(UnificationException e) { + throw new InternalCompilerError(e.getMessage()); + } + else + this.type = type; + } + + @Override + public String toString() { + return name; // + "$" + hashCode(); + } + + public void setVal(IVal val) { + this.val = val; + val.setLabel(name); + } + + public IVal getVal() { + if(val == null) + throw new InternalCompilerError("Variable " + name + " (with type " + type + ") is not given value anywhere."); + return val; + } + + public Variable copy() { + return new Variable(name, type); + } + + public void setName(String name) { + this.name = name; + } + + public static Variable[] concat(Variable[] a, + Variable[] b) { + Variable[] result = new Variable[a.length + b.length]; + System.arraycopy(a, 0, result, 0, a.length); + System.arraycopy(b, 0, result, a.length, b.length); + return result; + } +}