public void Visit(Return ret) { if (ret.ReturnExp != null) { ret.ReturnExp.Accept(this); Raise <TypeCheckException> .IfAreNotSame(ret.ReturnExp.Type, _currFunc.ReturnType, "Wrong return type!"); } else { Raise <TypeCheckException> .IfAreNotSame(_voidType, _currFunc.ReturnType, "Wrong return type!"); } }
public void Visit(StructValue sv) { var st = GetStructType(sv.Name); Raise <TypeCheckException> .IfAreNotEqual(st.Fields.Count, sv.Values.Count, "Wrong field count"); for (var i = 0; i < st.Fields.Count; ++i) { sv.Values[i].Accept(this); Raise <TypeCheckException> .IfAreNotSame(st.Fields[i].Type, sv.Values[i].Type, "Wrong field type"); } sv.Type = _result = st; sv.Temp = _currFunc.AddVariable("$" + _tempCounter++, st); }
public void Visit(FunctionCall fc) { Raise <TypeCheckException> .If(!_funcDecls.ContainsKey(fc.FunctionName), "Function not existing"); var fd = _funcDecls[fc.FunctionName]; Raise <TypeCheckException> .IfAreNotEqual(fc.Arguments.Count, fd.Params.Count, "Wrong argument count!"); for (var i = 0; i < fd.Params.Count; ++i) { fc.Arguments[i].Accept(this); Raise <TypeCheckException> .IfAreNotSame(fc.Arguments[i].Type, fd.Params[i].Type, "Wrong parameter type"); } fc.Function = fd; fc.Type = _result = fd.ReturnType; }
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; }
Type MustBe(Type t, string msg) { Raise <TypeCheckException> .IfAreNotSame(_result, t, msg); return(t); }