Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
 Type MustBe(Type t, string msg)
 {
     Raise<TypeCheckException>.IfAreNotSame(_result, t, msg);
     return t;
 }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 6
0
 public void Visit(Id id)
 {
     var v = _staticEnv.GetVariable(id.Name);
     id.Var = v;
     id.Type = _result = v.Type;
 }
Exemplo n.º 7
0
 public void Visit(IntLiteral il)
 {
     _result = il.Type = _intType;
 }
Exemplo n.º 8
0
 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);
 }
Exemplo n.º 9
0
 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;
 }
Exemplo n.º 10
0
 public void Visit(BoolLiteral bl)
 {
     _result = bl.Type = _boolType;
 }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
 public FieldInfo(string name, Type type)
 {
     Name = name;
     Type = type;
 }
Exemplo n.º 14
0
 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;
 }
Exemplo n.º 15
0
 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;
 }