private HashSet <TypeNode> deduceType(string value) { HashSet <TypeNode> _set_of_types = new HashSet <TypeNode>(); bool out_bool; if (Boolean.TryParse(value, out out_bool)) { _set_of_types.Add(NamedTypeNode.BoolType(true)); } byte out_byte; if (Byte.TryParse(value, out out_byte)) { _set_of_types.Add(NamedTypeNode.ByteType(true)); } int out_int; if (Int32.TryParse(value, out out_int)) { _set_of_types.Add(NamedTypeNode.IntType(true)); } char out_char; if (value.Length == 3 && Char.TryParse(value.Trim(new char[] { '\'' }), out out_char)) { _set_of_types.Add(NamedTypeNode.CharType(true)); } return(_set_of_types); }
internal static NamedTypeNode MakePrimitiveType(PrimitiveType type, Mutablitity isConstant) { NamedTypeNode result = null; switch (type) { case PrimitiveType.Int: result = NamedTypeNode.IntType(); break; case PrimitiveType.Byte: result = NamedTypeNode.ByteType(); break; case PrimitiveType.Char: result = NamedTypeNode.CharType(); break; case PrimitiveType.Bool: result = NamedTypeNode.BoolType(); break; case PrimitiveType.Void: result = NamedTypeNode.VoidType(); break; default: throw new Exception("Wrong primitive type"); } result.IsConstant = isConstant == Mutablitity.Constant; return(result); }
public void TypeNode_NamedTypeNodeHash_Test() { var hashSet = new HashSet <TypeNode>(); hashSet.Add(NamedTypeNode.BoolType()); Assert.AreEqual(1, hashSet.Count); Assert.IsTrue(hashSet.Contains(NamedTypeNode.BoolType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.ByteType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.CharType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.IntType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.VoidType())); hashSet.Add(NamedTypeNode.BoolType()); Assert.AreEqual(1, hashSet.Count); hashSet.Add(NamedTypeNode.ByteType()); Assert.AreEqual(2, hashSet.Count); Assert.IsTrue(hashSet.Contains(NamedTypeNode.BoolType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.ByteType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.CharType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.IntType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.VoidType())); hashSet.Add(NamedTypeNode.ByteType()); Assert.AreEqual(2, hashSet.Count); hashSet.Add(NamedTypeNode.CharType()); Assert.AreEqual(3, hashSet.Count); Assert.IsTrue(hashSet.Contains(NamedTypeNode.BoolType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.ByteType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.CharType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.IntType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.VoidType())); hashSet.Add(NamedTypeNode.CharType()); Assert.AreEqual(3, hashSet.Count); hashSet.Add(NamedTypeNode.IntType()); Assert.AreEqual(4, hashSet.Count); Assert.IsTrue(hashSet.Contains(NamedTypeNode.BoolType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.ByteType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.CharType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.IntType())); Assert.IsFalse(hashSet.Contains(NamedTypeNode.VoidType())); hashSet.Add(NamedTypeNode.IntType()); Assert.AreEqual(4, hashSet.Count); hashSet.Add(NamedTypeNode.VoidType()); Assert.AreEqual(5, hashSet.Count); Assert.IsTrue(hashSet.Contains(NamedTypeNode.BoolType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.ByteType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.CharType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.IntType())); Assert.IsTrue(hashSet.Contains(NamedTypeNode.VoidType())); hashSet.Add(NamedTypeNode.VoidType()); Assert.AreEqual(5, hashSet.Count); }
public void TypeNode_NamedTypeNodeEquals_Test() { Assert.IsTrue(NamedTypeNode.BoolType().Equals(NamedTypeNode.BoolType())); Assert.IsTrue(NamedTypeNode.ByteType().Equals(NamedTypeNode.ByteType())); Assert.IsTrue(NamedTypeNode.CharType().Equals(NamedTypeNode.CharType())); Assert.IsTrue(NamedTypeNode.IntType().Equals(NamedTypeNode.IntType())); Assert.IsTrue(NamedTypeNode.VoidType().Equals(NamedTypeNode.VoidType())); Assert.IsFalse(NamedTypeNode.ByteType().Equals(NamedTypeNode.VoidType())); Assert.IsFalse(NamedTypeNode.BoolType().Equals(NamedTypeNode.VoidType())); Assert.IsFalse(NamedTypeNode.IntType().Equals(NamedTypeNode.VoidType())); Assert.IsFalse(NamedTypeNode.CharType().Equals(NamedTypeNode.VoidType())); Assert.IsFalse(NamedTypeNode.BoolType().Equals(NamedTypeNode.IntType())); Assert.IsFalse(NamedTypeNode.ByteType().Equals(NamedTypeNode.IntType())); Assert.IsFalse(NamedTypeNode.CharType().Equals(NamedTypeNode.IntType())); Assert.IsFalse(NamedTypeNode.BoolType().Equals(NamedTypeNode.CharType())); Assert.IsFalse(NamedTypeNode.ByteType().Equals(NamedTypeNode.CharType())); Assert.IsFalse(NamedTypeNode.BoolType().Equals(NamedTypeNode.ByteType())); Assert.IsFalse(NamedTypeNode.ByteType().Equals(NamedTypeNode.BoolType())); }
//for every pair of arguments type should be proper types public override void Visit(OperatorNode node) { base.Visit(node); List <ExpressionNode> arguments_list = new List <ExpressionNode>(node.Arguments); switch (node.Operator) { // = case OperatorType.ASSIGN: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (!arguments_list[0].ExpressionType.Equals(arguments_list[1].ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = arguments_list[0].ExpressionType; break; //+= -= *= /= %= case OperatorType.PLUS_ASSIGN: case OperatorType.MINUS_ASSIGN: case OperatorType.MUL_ASSIGN: case OperatorType.DIV_ASSIGN: case OperatorType.MOD_ASSIGN: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list.Count() >= 1 && arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (NamedTypeNode.IntType().Equals(arguments_list[0].ExpressionType, false) && NamedTypeNode.IntType().Equals(arguments_list[1].ExpressionType, false)) { node.ExpressionType = NamedTypeNode.IntType(); } else if (NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType, false) && NamedTypeNode.ByteType().Equals(arguments_list[1].ExpressionType, false)) { node.ExpressionType = NamedTypeNode.ByteType(); } else { throw new TypeCheckException("Wrong argument type."); } break; //+ - * / % case OperatorType.PLUS: case OperatorType.MINUS: case OperatorType.MUL: case OperatorType.DIV: case OperatorType.MOD: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (NamedTypeNode.IntType().Equals(arguments_list[0].ExpressionType, false) && NamedTypeNode.IntType().Equals(arguments_list[1].ExpressionType, false)) { node.ExpressionType = NamedTypeNode.IntType(); } else if (NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType, false) && NamedTypeNode.ByteType().Equals(arguments_list[1].ExpressionType, false)) { node.ExpressionType = NamedTypeNode.ByteType(); } else { throw new TypeCheckException("Wrong argument type."); } break; // << >> case OperatorType.BIT_SHIFT_UP: case OperatorType.BIT_SHIFT_DOWN: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (!NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType, false) || !NamedTypeNode.IntType().Equals(arguments_list[1].ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = NamedTypeNode.ByteType(); break; // <<= >>= case OperatorType.BIT_SHIFT_UP_ASSIGN: case OperatorType.BIT_SHIFT_DOWN_ASSIGN: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (!NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType, false) || !NamedTypeNode.IntType().Equals(arguments_list[1].ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = NamedTypeNode.ByteType(); break; //| ^ & case OperatorType.BIT_OR: case OperatorType.BIT_XOR: case OperatorType.BIT_AND: foreach (var argument in node.Arguments) { if (!NamedTypeNode.ByteType().Equals(argument.ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } } node.ExpressionType = NamedTypeNode.ByteType(); break; //&= ^= |= case OperatorType.BIT_AND_ASSIGN: case OperatorType.BIT_XOR_ASSIGN: case OperatorType.BIT_OR_ASSIGN: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (!NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType, false) || !NamedTypeNode.ByteType().Equals(arguments_list[1].ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = NamedTypeNode.ByteType(); break; // || && case OperatorType.OR: case OperatorType.AND: foreach (var argument in node.Arguments) { if (!NamedTypeNode.BoolType().Equals(argument.ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } } node.ExpressionType = NamedTypeNode.BoolType(); break; // == != < <= > >= case OperatorType.EQUAL: case OperatorType.NOT_EQUAL: case OperatorType.LESS: case OperatorType.LESS_EQUAL: case OperatorType.GREATER: case OperatorType.GREATER_EQUAL: if (arguments_list.Count() != 2) { throw new TypeCheckException("Inproper numbers of arguments."); } if (!NamedTypeNode.IntType().Equals(arguments_list[0].ExpressionType, false) || !NamedTypeNode.IntType().Equals(arguments_list[1].ExpressionType, false)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = NamedTypeNode.BoolType(); break; // PRE ++ -- + - POST ++ -- case OperatorType.PRE_INCREMENT: case OperatorType.PRE_DECREMENT: case OperatorType.UNARY_PLUS: case OperatorType.UNARY_MINUS: case OperatorType.POST_INCREMENT: case OperatorType.POST_DECREMENT: if (arguments_list.Count() != 1) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (NamedTypeNode.IntType().Equals(arguments_list[0].ExpressionType)) { node.ExpressionType = NamedTypeNode.IntType(); } else if (NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType)) { node.ExpressionType = NamedTypeNode.ByteType(); } else { throw new TypeCheckException("Wrong argument type."); } break; // PRE ! ~ case OperatorType.NOT: case OperatorType.BIT_NOT: if (arguments_list.Count() != 1) { throw new TypeCheckException("Inproper numbers of arguments."); } if (arguments_list[0].ExpressionType.IsConstant) { throw new TypeCheckException("Variable to assign is constant."); } if (!NamedTypeNode.ByteType().Equals(arguments_list[0].ExpressionType)) { throw new TypeCheckException("Wrong argument type."); } node.ExpressionType = NamedTypeNode.ByteType(); break; } }