void ResultIsBoolAndBothOperandsMustBeBool(BinaryOp binaryOp) { binaryOp.Left.Accept(this); MustBeBool("The type of the left operand of " + binaryOp + " must be bool"); binaryOp.Right.Accept(this); MustBeBool("The type of the right operand of " + binaryOp + " must be bool"); binaryOp.Type = _result = _boolType; }
void ResultIsIntAndBothOperandsMustBeInt(BinaryOp binaryOp) { binaryOp.Left.Accept(this); MustBeInt("The type of the left operand of " + binaryOp + " must be integer"); binaryOp.Right.Accept(this); MustBeInt("The type of the right operand of " + binaryOp + " must be integer"); binaryOp.Type = _result = _intType; }
Type AddType(string name, TypeReference reference) { name = name.ToLower(); Raise<TypeCheckException>.If(_types.ContainsKey(name), "Type already existing."); var type = new Type(name, reference); _types.Add(name, type); return type; }
Type MustBe(Type t, string msg) { Raise<TypeCheckException>.IfAreNotSame(_result, t, msg); return t; }
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(Id id) { var v = _staticEnv.GetVariable(id.Name); id.Var = v; id.Type = _result = v.Type; }
public void Visit(IntLiteral il) { _result = il.Type = _intType; }
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(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(BoolLiteral bl) { _result = bl.Type = _boolType; }
public void Visit(Equal eq) { eq.Left.Accept(this); eq.Right.Accept(this); if (!eq.Left.Type.Equals(eq.Right.Type)) { throw new TypeCheckException("Both operands of == must have the same type"); } eq.Type = _result = _boolType; }
public TypecheckVisitor() { Assembly = AssemblyDefinition.ReadAssembly("DanglingLang.Runner.exe"); Module = Assembly.MainModule; _boolType = AddType("bool", Module.TypeSystem.Boolean); _intType = AddType("int", Module.TypeSystem.Int32); _voidType = AddType("void", Module.TypeSystem.Void); // To allow "load" functionality... _types.Add("boolean", _boolType); _types.Add("int32", _intType); }
public FieldInfo(string name, Type type) { Name = name; Type = type; }
public FieldInfo AddField(string name, Type type) { Raise<TypeCheckException>.If(_fields.Any(f => f.Name == name), "Field with existing name"); var fieldInfo = new FieldInfo(name, type); _fields.Add(fieldInfo); return fieldInfo; }
public VarInfo(string name, Type type, Kind kind, object info) { Name = name; Type = type; Kind = kind; Debug.Assert(info is FunctionDecl.ParamInfo || info is FunctionDecl.VarInfo || info is StructType.FieldInfo); Info = info; }