Exemplo n.º 1
0
Arquivo: types.cs Projeto: nboyd/mal
 //
 // General functions
 //
 public static bool _equal_Q(MalVal a, MalVal b)
 {
     Type ota = a.GetType(), otb = b.GetType();
     if (!((ota == otb) ||
         (a is MalList && b is MalList))) {
         return false;
     } else {
         if (a is MalInt) {
             return ((MalInt)a).getValue() ==
                 ((MalInt)b).getValue();
         } else if (a is MalSymbol) {
             return ((MalSymbol)a).getName() ==
                 ((MalSymbol)b).getName();
         } else if (a is MalString) {
             return ((MalString)a).getValue() ==
                 ((MalString)b).getValue();
         } else if (a is MalList) {
             if (((MalList)a).size() != ((MalList)b).size()) {
                 return false;
             }
             for (int i=0; i<((MalList)a).size(); i++) {
                 if (! _equal_Q(((MalList)a)[i], ((MalList)b)[i])) {
                     return false;
                 }
             }
             return true;
         } else {
             return a == b;
         }
     }
 }
Exemplo n.º 2
0
        //
        // General functions
        //

        public static bool _equal_Q(MalVal a, MalVal b)
        {
            Type ota = a.GetType(), otb = b.GetType();

            if (!((ota == otb) ||
                  (a is MalList && b is MalList)))
            {
                return(false);
            }
            else
            {
                if (a is MalInt)
                {
                    return(((MalInt)a).getValue() ==
                           ((MalInt)b).getValue());
                }
                else if (a is MalSymbol)
                {
                    return(((MalSymbol)a).getName() ==
                           ((MalSymbol)b).getName());
                }
                else if (a is MalString)
                {
                    return(((MalString)a).getValue() ==
                           ((MalString)b).getValue());
                }
                else if (a is MalList)
                {
                    if (((MalList)a).size() != ((MalList)b).size())
                    {
                        return(false);
                    }
                    for (int i = 0; i < ((MalList)a).size(); i++)
                    {
                        if (!_equal_Q(((MalList)a)[i], ((MalList)b)[i]))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else
                {
                    return(a == b);
                }
            }
        }
Exemplo n.º 3
0
        //
        // General functions
        //

        public static bool _equal_Q(MalVal a, MalVal b) {
            Type ota = a.GetType(), otb = b.GetType();
            if (!((ota == otb) ||
                (a is MalList && b is MalList))) {
                return false;
            } else {
                if (a is MalInt) {
                    return ((MalInt)a).getValue() ==
                        ((MalInt)b).getValue();
                } else if (a is MalSymbol) {
                    return ((MalSymbol)a).getName() ==
                        ((MalSymbol)b).getName();
                } else if (a is MalString) {
                    return ((MalString)a).getValue() ==
                        ((MalString)b).getValue();
                } else if (a is MalList) {
                    if (((MalList)a).size() != ((MalList)b).size()) {
                        return false;
                    }
                    for (int i=0; i<((MalList)a).size(); i++) {
                        if (! _equal_Q(((MalList)a)[i], ((MalList)b)[i])) {
                            return false;
                        }
                    }
                    return true;
                } else if (a is MalHashMap) {
                    var akeys = ((MalHashMap)a).getValue().Keys;
                    var bkeys = ((MalHashMap)b).getValue().Keys;
                    if (akeys.Count != bkeys.Count) {
                        return false;
                    }
                    foreach (var k in akeys) {
                        if (!_equal_Q(((MalHashMap)a).getValue()[k],
                                      ((MalHashMap)b).getValue()[k])) {
                            return false;
                        }
                    }
                    return true;
                } else {
                    return a == b;
                }
            }
        }
Exemplo n.º 4
0
            // Support the built-in '=' function. False until explicitly proven otherwise.
            // Should be used in preference to == for internal MalVal equality checks.
            public static MalVal EQ(MalVal a, MalVal b)
            {
                if (a.GetType() != b.GetType())
                {
                    // TODO - allow equality comparisons between ints and floats.
                    return(malFalse);
                }
                // If here, they are of the same Mal type. Do they have the same value?
                switch ((object)a)
                {
                case MalSym aSym:
                    if (aSym.getName() == ((MalSym)b).getName())
                    {
                        return(malTrue);
                    }
                    break;

                case MalNum aNumk:
                    if (aNumk.Unbox() == ((MalNum)b).Unbox())
                    {
                        return(malTrue);
                    }
                    break;

                case MalString aString:
                    if (aString.unbox() == ((MalString)b).unbox())
                    {
                        return(malTrue);
                    }
                    break;

                case MalKeyword aKeyWord:
                    if (aKeyWord.unbox() == ((MalKeyword)b).unbox())
                    {
                        return(malTrue);
                    }
                    break;

                case MalNil aNil:
                    if (aNil == ((MalNil)b))
                    {
                        return(malTrue);
                    }
                    break;

                case MalTrue aTrue:
                    if (aTrue == ((MalTrue)b))
                    {
                        return(malTrue);
                    }
                    break;

                case MalFalse aFalse:
                    if (aFalse == ((MalFalse)b))
                    {
                        return(malTrue);
                    }
                    break;

                case MalSeqBase aSeq:
                    // Sequences must be the same length, and each element must be the same.
                    MalSeqBase bSeq = (MalSeqBase)b;
                    if (aSeq.Count() != bSeq.Count())
                    {
                        // They are not of equal length.
                        return(malFalse);
                    }
                    for (var i = 0; i < aSeq.Count(); i++)
                    {
                        // At least one of the elements is not equal.
                        if (EQ(aSeq[i], bSeq[i]) == malFalse)
                        {
                            return(malFalse);
                        }
                    }
                    return(malTrue);

                case MalAtom aAtom:
                    // The atoms must be the same object. Two atoms containing the same value are not equal.
                    // TODO check whether this should be true if dereferencing them should be used instead. This isn't specified in the tests.
                    if (aAtom == ((MalAtom)b))
                    {
                        return(malTrue);
                    }
                    break;

                default:
                    throw new MalInternalError("Can't yet compare '" + a.GetType() + "' with '" + b.GetType() + "'");
                }
                return(malFalse);
            }
Exemplo n.º 5
0
        //
        // General functions
        //

        public static bool _equal_Q(MalVal a, MalVal b)
        {
            Type ota = a.GetType(), otb = b.GetType();

            if (!((ota == otb) ||
                  (a is MalList && b is MalList)))
            {
                return(false);
            }
            else
            {
                if (a is MalInt)
                {
                    return(((MalInt)a).getValue() ==
                           ((MalInt)b).getValue());
                }
                else if (a is MalSymbol)
                {
                    return(((MalSymbol)a).getName() ==
                           ((MalSymbol)b).getName());
                }
                else if (a is MalString)
                {
                    return(((MalString)a).getValue() ==
                           ((MalString)b).getValue());
                }
                else if (a is MalList)
                {
                    if (((MalList)a).size() != ((MalList)b).size())
                    {
                        return(false);
                    }
                    for (int i = 0; i < ((MalList)a).size(); i++)
                    {
                        if (!_equal_Q(((MalList)a)[i], ((MalList)b)[i]))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else if (a is MalHashMap)
                {
                    var akeys = ((MalHashMap)a).getValue().Keys;
                    var bkeys = ((MalHashMap)b).getValue().Keys;
                    if (akeys.Count != bkeys.Count)
                    {
                        return(false);
                    }
                    foreach (var k in akeys)
                    {
                        if (!_equal_Q(((MalHashMap)a).getValue()[k],
                                      ((MalHashMap)b).getValue()[k]))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else
                {
                    return(a == b);
                }
            }
        }
Exemplo n.º 6
0
            // Support the built-in '=' function. False until explicitly proven otherwise.
            public static MalVal EQ(MalVal a, MalVal b)
            {
                if (a.GetType() != b.GetType())
                {
                    // TODO - allow equality comparisons between ints and floats.
                    return(malFalse);
                }
                // If here, they are of the same Mal type. Do they have the same value?
                switch ((object)a)
                {
                case MalSym aSym:
                    if (aSym.getName() == ((MalSym)b).getName())
                    {
                        return(malTrue);
                    }
                    break;

                case MalNum aNumk:
                    if (aNumk == ((MalNum)b))
                    {
                        return(malTrue);
                    }
                    break;

                case MalString aString:
                    if (aString.ToString() == ((MalString)b).ToString())
                    {
                        return(malTrue);
                    }
                    break;

                case MalKeyword aKeyWord:
                    if (aKeyWord.ToString() == ((MalKeyword)b).ToString())
                    {
                        return(malTrue);
                    }
                    break;

                case MalSeq aSeq:
                    // Sequences must be the same length, and each element must be the same.
                    MalSeq bSeq = (MalSeq)b;
                    if (aSeq.Count() != bSeq.Count())
                    {
                        // They are not of equal length.
                        return(malFalse);
                    }
                    for (var i = 0; i < aSeq.Count(); i++)
                    {
                        // At least one of the elements is not equal.
                        if (EQ(aSeq[i], bSeq[i]) == malFalse)
                        {
                            return(malFalse);
                        }
                    }
                    return(malTrue);

                default:
                    throw new MalInternalError("Can't yet compare '" + a.GetType() + "' with '" + b.GetType() + "'");
                }
                return(malFalse);
            }