コード例 #1
0
        public override Type Typecheck(TypecheckingState state)
        {
            Type = Expression.Typecheck(state);
            state.EnterScope();
            state.BindVariable(Name, Type);
            var type = Recipient.Typecheck(state);

            state.ExitScope();
            return(type);
        }
コード例 #2
0
        // public string Name;

        public override Type Typecheck(TypecheckingState state)
        {
            try
            {
                var type = state.LookupVariable(Name);
                return(type);
            } catch (TypechekingStateException e)
            {
                TypeError(e.Message);
            }

            throw new ImpossibleException();
        }
コード例 #3
0
        // public string Name;
        // public Expression Argument;

        public override Type Typecheck(TypecheckingState state)
        {
            try
            {
                var fun          = state.LookupFunction(Name);
                var argumentType = Argument.Typecheck(state);

                if (argumentType != fun.ArgumentType)
                {
                    TypeError($"Argument type mismatch in call to {Name}");
                }

                return(fun.ReturnType);
            } catch (TypechekingStateException e)
            {
                TypeError(e.Message);
            }

            throw new ImpossibleException();
        }
コード例 #4
0
        public override Type Typecheck(TypecheckingState state)
        {
            var type1 = Expression1.Typecheck(state);
            var type2 = Expression2.Typecheck(state);

            OperandType = type1;

            switch (Operator)
            {
            case BinaryOperator.Add:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("+ expects number operands");
                }
                return(Type.NumType);

            case BinaryOperator.Sub:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("- expects number operands");
                }
                return(Type.NumType);

            case BinaryOperator.Eq:
                if (type1 != type2)
                {
                    TypeError("== expects operands of the same type");
                }
                return(Type.BoolType);

            case BinaryOperator.Lt:
                if (type1 != Type.NumType && type2 != Type.NumType)
                {
                    TypeError("- expects number operands");
                }
                return(Type.BoolType);
            }

            throw new ImpossibleException();
        }
コード例 #5
0
        // public string Name;
        // public string ArgumentName;
        // public Type ArgumentType;
        // public Type ReturnType;
        // public Expression Body;
        // public Expression Recipient;

        public override Type Typecheck(TypecheckingState state)
        {
            state.EnterScope();
            state.BindFunction(Name, this);

            // type check the body once
            state.EnterScope();
            state.BindVariable(ArgumentName, ArgumentType);
            var returnType = Body.Typecheck(state);

            if (returnType != ReturnType)
            {
                TypeError("Return type mismatch");
            }
            state.ExitScope();

            var type = Recipient.Typecheck(state);

            state.ExitScope();

            return(type);
        }
コード例 #6
0
 public abstract Type Typecheck(TypecheckingState state);
コード例 #7
0
        // public bool Value;

        public override Type Typecheck(TypecheckingState state)
        {
            return(Type.BoolType);
        }
コード例 #8
0
        // public Expression Expression1;
        // public Expression Expression2;

        public override Type Typecheck(TypecheckingState state)
        {
            Expression1.Typecheck(state);
            return(Expression2.Typecheck(state));
        }
コード例 #9
0
 public override Type Typecheck(TypecheckingState state)
 {
     throw new NotImplementedException();
 }