예제 #1
0
        public void AssignReference(DMReference reference, DreamValue value)
        {
            switch (reference.RefType)
            {
            case DMReference.Type.Self: Result = value; break;

            case DMReference.Type.Argument: _localVariables[reference.Index] = value; break;

            case DMReference.Type.Local: _localVariables[ArgumentCount + reference.Index] = value; break;

            case DMReference.Type.SrcField: Instance.SetVariable(reference.Name, value); break;

            case DMReference.Type.Global: DreamManager.Globals[reference.Index] = value; break;

            case DMReference.Type.Src:
                //TODO: src can be assigned to non-DreamObject values
                if (!value.TryGetValueAsDreamObject(out Instance))
                {
                    throw new Exception($"Cannot assign src to {value}");
                }

                break;

            case DMReference.Type.Field: {
                DreamValue owner = Pop();
                if (!owner.TryGetValueAsDreamObject(out var ownerObj) || ownerObj == null)
                {
                    throw new Exception($"Cannot assign field \"{reference.Name}\" on {owner}");
                }

                ownerObj.SetVariable(reference.Name, value);
                break;
            }

            case DMReference.Type.ListIndex: {
                DreamValue index = Pop();
                DreamValue list  = Pop();
                if (!list.TryGetValueAsDreamList(out var listObj))
                {
                    throw new Exception($"Cannot assign to index {index} of {list}");
                }

                listObj.SetValue(index, value);
                break;
            }

            default: throw new Exception($"Cannot assign to reference type {reference.RefType}");
            }
        }