public void TestType() { Assert.AreEqual("int", Sara.Type.Int.Lexeme); Assert.AreEqual(Tag.BASIC, Sara.Type.Int.TagValue); Assert.AreEqual(4, Sara.Type.Int.Width); Assert.AreEqual("float", Sara.Type.Float.Lexeme); Assert.AreEqual(Tag.BASIC, Sara.Type.Float.TagValue); Assert.AreEqual(8, Sara.Type.Float.Width); Assert.AreEqual("char", Sara.Type.Char.Lexeme); Assert.AreEqual(Tag.BASIC, Sara.Type.Char.TagValue); Assert.AreEqual(1, Sara.Type.Char.Width); Assert.AreEqual("bool", Sara.Type.Bool.Lexeme); Assert.AreEqual(Tag.BASIC, Sara.Type.Bool.TagValue); Assert.AreEqual(1, Sara.Type.Bool.Width); Assert.IsTrue(Sara.Type.Numeric(Sara.Type.Int)); Assert.IsTrue(Sara.Type.Numeric(Sara.Type.Float)); Assert.IsTrue(Sara.Type.Numeric(Sara.Type.Char)); Assert.IsFalse(Sara.Type.Numeric(Sara.Type.Bool)); var list = new Sara.Type("list", Tag.BASIC, 4); Assert.AreEqual("list", list.Lexeme); Assert.AreEqual(Tag.BASIC, list.TagValue); Assert.AreEqual(4, list.Width); Assert.AreEqual(Sara.Type.Float, Sara.Type.Max(Sara.Type.Float, Sara.Type.Int)); Assert.AreEqual(Sara.Type.Int, Sara.Type.Max(Sara.Type.Char, Sara.Type.Int)); Assert.IsNull(Sara.Type.Max(Sara.Type.Bool, Sara.Type.Float)); }
public static Type Max(Type lhs, Type rhs) { if (!Type.Numeric(lhs) || !Type.Numeric(rhs)) return null; else if (lhs == Type.Float || rhs == Type.Float) return Type.Float; else if (lhs == Type.Int || rhs == Type.Int) return Type.Int; else return Type.Char; }
public Array(int sz, Type type) : base("[]",Tag.INDEX, sz * type.Width) { this.Size = sz; this.Of = type; }
public static bool Numeric(Type type) { return type == Type.Char || type == Type.Int || type == Type.Float; }