public void Visit(Assignment asg) { if (asg.LoadExp != null) { asg.LoadExp.Accept(this); } asg.Exp.Accept(this); var varName = asg.VarName; StaticEnvBase.VarInfo varInfo; if (asg.LoadExp != null) { var structType = asg.LoadExp.Type as StructType; Raise <TypeCheckException> .IfIsNull(structType, "Can apply dot only on structs"); var field = structType.Fields.FirstOrDefault(f => f.Name == varName); Raise <TypeCheckException> .IfIsNull(field, "Given struct type has not given field"); Raise <TypeCheckException> .IfAreNotSame(field.Type, asg.Exp.Type, "Wrong type in assignment"); varInfo = new StaticEnvBase.VarInfo(varName, asg.Exp.Type, StaticEnvBase.Kind.Field, field); } else if (_staticEnv.TryGetVariable(varName, out varInfo)) { if (!varInfo.Type.Equals(_result)) { throw new TypeCheckException(string.Format( "Cannot re-assign {0} with a value of different type", varName)); } } else { var info = _currFunc.AddVariable(varName, asg.Exp.Type); varInfo = new StaticEnvBase.VarInfo(varName, asg.Exp.Type, StaticEnvBase.Kind.Var, info); _staticEnv.SetVariable(varName, varInfo); } asg.Var = varInfo; }
public override bool TryGetVariable(string name, out VarInfo v) { return(_locals.TryGetValue(name, out v) || _parent.TryGetVariable(name, out v)); }