public FieldInfo GetField(string name) { var field = _fields.FirstOrDefault(f => f.Name == name); Raise <TypeCheckException> .IfIsNull(field); return(field); }
public void Visit(Dot dot) { dot.Left.Accept(this); var st = dot.Left.Type as StructType; Raise <TypeCheckException> .IfIsNull(st, "Cannot call dot on struct types!"); Debug.Assert(st != null); // To keep ReSharper quiet :) dot.Type = _result = st.GetField(dot.Right).Type; }
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; }