Beispiel #1
0
        public DMASTProcStatementVarDeclaration(DMASTPath path, DMASTExpression value)
        {
            int       varElementIndex = path.Path.FindElement("var");
            DreamPath typePath        = path.Path.FromElements(varElementIndex + 1, -2);

            Type  = (typePath.Elements.Length > 0) ? new DMASTPath(typePath) : null;
            Name  = path.Path.LastElement;
            Value = value;
        }
Beispiel #2
0
        public ObjVarDeclInfo(DreamPath path)
        {
            string[]      elements    = path.Elements;
            var           readIdx     = 0;
            List <string> currentPath = new();

            while (readIdx < elements.Length && elements[readIdx] != "var")
            {
                currentPath.Add(elements[readIdx]);
                readIdx += 1;
            }
            ObjectPath = new DreamPath(path.Type, currentPath.ToArray());
            if (ObjectPath.Elements.Length == 0)
            {
                IsToplevel = true;
            }
            currentPath.Clear();
            readIdx += 1;
            while (readIdx < elements.Length - 1)
            {
                var elem = elements[readIdx];
                if (elem == "static" || elem == "global")
                {
                    IsStatic = true;
                }
                else if (elem == "const")
                {
                    IsConst = true;
                }
                else if (elem == "list")
                {
                    IsList = true;
                }
                else if (elem == "tmp")
                {
                    IsTmp = true;
                }
                else
                {
                    currentPath.Add(elem);
                }
                readIdx += 1;
            }
            if (currentPath.Count > 0)
            {
                TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray());
            }
            else
            {
                TypePath = null;
            }
            VarName = elements[elements.Length - 1];
        }
Beispiel #3
0
        public DreamObject GetValueAsDreamObjectOfType(DreamPath type)
        {
            DreamObject value = GetValueAsDreamObject();

            if (value?.IsSubtypeOf(type) == true)
            {
                return(value);
            }
            else
            {
                throw new Exception("Value " + this + " was not of type '" + type + "'");
            }
        }
Beispiel #4
0
        public DMType(DreamTypeJson json)
        {
            Json = json;
            Path = new DreamPath(Json.Path);

            InitProc = Json.InitProc.HasValue ? Program.Procs[Json.InitProc.Value] : null;

            Procs = new(json.Procs?.Count ?? 0);
            if (Json.Procs != null)
            {
                foreach (List <int> procIds in Json.Procs)
                {
                    DMProc proc = Program.Procs[procIds[^ 1]];
Beispiel #5
0
        public DreamObject GetValueAsDreamObjectOfType(DreamPath type)
        {
            DreamObject value = GetValueAsDreamObject();

            if (value != null && !value.Deleted && value.IsSubtypeOf(type))
            {
                return(value);
            }
            else
            {
                throw new Exception("Value " + this + " was not of type '" + type + "'");
            }
        }
Beispiel #6
0
        public bool TryGetValueAsPath(out DreamPath path)
        {
            if (IsType(DreamValueType.DreamPath))
            {
                path = (DreamPath)Value;

                return(true);
            }
            else
            {
                path = DreamPath.Root;

                return(false);
            }
        }
Beispiel #7
0
        public void VisitUpwardPathSearch(DMASTUpwardPathSearch constant)
        {
            DMExpression.TryConstant(_dmObject, _proc, constant.Path, out var pathExpr);
            if (pathExpr is not Expressions.Path)
            {
                throw new CompileErrorException(constant.Location, "Cannot do an upward path search on " + pathExpr);
            }

            DreamPath path      = ((Expressions.Path)pathExpr).Value;
            DreamPath?foundPath = DMObjectTree.UpwardSearch(path, constant.Search.Path);

            if (foundPath == null)
            {
                throw new CompileErrorException(constant.Location, $"Invalid path {path}.{constant.Search.Path}");
            }

            Result = new Expressions.Path(constant.Location, foundPath.Value);
        }
Beispiel #8
0
        public ProcParameterDeclInfo(DreamPath path)
        {
            string[]      elements    = path.Elements;
            var           readIdx     = 0;
            List <string> currentPath = new();

            if (elements[readIdx] == "var")
            {
                readIdx++;
            }
            while (readIdx < elements.Length - 1)
            {
                var elem = elements[readIdx];
                if (elem == "static" || elem == "global")
                {
                    //No effect
                }
                else if (elem == "const")
                {
                    //TODO: Parameters can be constant
                    //If they are they can't be assigned to but still cannot be used in const-only contexts (such as switch cases)
                }
                else if (elem == "list")
                {
                    IsList = true;
                }
                else
                {
                    currentPath.Add(elem);
                }
                readIdx += 1;
            }
            if (currentPath.Count > 0)
            {
                TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray());
            }
            else
            {
                TypePath = null;
            }
            VarName = elements[elements.Length - 1];
        }
Beispiel #9
0
        protected DreamProc(DreamPath owningType, string name, DreamProc superProc, ProcAttributes attributes, List <String>?argumentNames, List <DMValueType>?argumentTypes, string?verbName, string?verbCategory, string?verbDesc, sbyte?invisibility)
        {
            OwningType    = owningType;
            Name          = name;
            SuperProc     = superProc;
            Attributes    = attributes;
            ArgumentNames = argumentNames;
            ArgumentTypes = argumentTypes;

            VerbName = verbName;
            if (verbCategory is not null)
            {
                // (de)serialization meme to reduce JSON size
                // It's string.Empty by default but we invert it to null to prevent serialization
                // Explicit null becomes treated as string.Empty
                VerbCategory = verbCategory == string.Empty ? null : verbCategory;
            }
            VerbDesc     = verbDesc;
            Invisibility = invisibility;
        }
Beispiel #10
0
        public DMASTObjectVarDefinition(DMASTPath astPath, DMASTExpression value)
        {
            DreamPath path = astPath.Path;

            int globalElementIndex = path.FindElement("global");

            if (globalElementIndex != -1)
            {
                path     = path.RemoveElement(globalElementIndex);
                IsGlobal = true;
            }

            int       varElementIndex = path.FindElement("var");
            DreamPath varPath         = path.FromElements(varElementIndex + 1, -1);

            ObjectPath = (varElementIndex > 1) ? new DMASTPath(path.FromElements(0, varElementIndex)) : null;
            Type       = (varPath.Elements.Length > 1) ? new DMASTPath(varPath.FromElements(0, -2)) : null;
            Name       = varPath.LastElement;
            Value      = value;
        }
Beispiel #11
0
        public ProcVarDeclInfo(DreamPath path)
        {
            string[]      elements    = path.Elements;
            var           readIdx     = 0;
            List <string> currentPath = new();

            if (elements[readIdx] == "var")
            {
                readIdx++;
            }
            while (readIdx < elements.Length - 1)
            {
                var elem = elements[readIdx];
                if (elem == "static" || elem == "global")
                {
                    IsStatic = true;
                }
                else if (elem == "const")
                {
                    IsConst = true;
                }
                else if (elem == "list")
                {
                    IsList = true;
                }
                else
                {
                    currentPath.Add(elem);
                }
                readIdx += 1;
            }
            if (currentPath.Count > 0)
            {
                TypePath = new DreamPath(DreamPath.PathType.Absolute, currentPath.ToArray());
            }
            else
            {
                TypePath = null;
            }
            VarName = elements[elements.Length - 1];
        }
Beispiel #12
0
 public NewPath(Location location, DreamPath targetPath, ArgumentList arguments) : base(location)
 {
     TargetPath = targetPath;
     Arguments  = arguments;
 }
Beispiel #13
0
 public DMASTPath(DreamPath path)
 {
     Path = path;
 }
Beispiel #14
0
 public Path(Location location, DreamPath value) : base(location)
 {
     Value = value;
 }
Beispiel #15
0
 public DMProc(DreamPath owningType, string name, DreamProc superProc, List <String> argumentNames, List <DMValueType> argumentTypes, byte[] bytecode, int maxStackSize, ProcAttributes attributes, string?verbName, string?verbCategory, string?verbDesc, sbyte?invisibility)
     : base(owningType, name, superProc, attributes, argumentNames, argumentTypes, verbName, verbCategory, verbDesc, invisibility)
 {
     Bytecode      = bytecode;
     _maxStackSize = maxStackSize;
 }
Beispiel #16
0
 public IsTypeInferred(Location location, DMExpression expr, DreamPath path) : base(location)
 {
     _expr = expr;
     _path = path;
 }
Beispiel #17
0
 public TreeEntry(DreamPath path)
 {
     Path = path;
 }
Beispiel #18
0
        public void ProcessProcDefinition(DMASTProcDefinition procDefinition)
        {
            string   procName = procDefinition.Name;
            DMObject dmObject = _currentObject;

            try {
                if (procDefinition.ObjectPath.HasValue)
                {
                    dmObject = DMObjectTree.GetDMObject(_currentObject.Path.Combine(procDefinition.ObjectPath.Value));
                }

                if (!procDefinition.IsOverride && dmObject.HasProc(procName))
                {
                    throw new CompileErrorException(procDefinition.Location, $"Type {dmObject.Path} already has a proc named \"{procName}\"");
                }

                DMProc proc;

                if (procDefinition.ObjectPath == null)
                {
                    if (DMObjectTree.TryGetGlobalProc(procDefinition.Name, out _))
                    {
                        throw new CompileErrorException(new CompilerError(procDefinition.Location, $"proc {procDefinition.Name} is already defined in global scope"));
                    }

                    proc = DMObjectTree.CreateDMProc(dmObject, procDefinition);
                    DMObjectTree.AddGlobalProc(proc.Name, proc.Id);
                }
                else
                {
                    proc = DMObjectTree.CreateDMProc(dmObject, procDefinition);
                    dmObject.AddProc(procName, proc);
                }

                if (procDefinition.Body != null)
                {
                    foreach (var stmt in GetStatements(procDefinition.Body))
                    {
                        // TODO multiple var definitions.
                        if (stmt is DMASTProcStatementVarDeclaration varDeclaration && varDeclaration.IsGlobal)
                        {
                            DMVariable variable = proc.CreateGlobalVariable(varDeclaration.Type, varDeclaration.Name, varDeclaration.IsConst);
                            variable.Value = new Expressions.Null(varDeclaration.Location);

                            if (varDeclaration.Value != null)
                            {
                                DMVisitorExpression._scopeMode = "static";
                                DMExpression expression = DMExpression.Create(dmObject, proc, varDeclaration.Value, varDeclaration.Type);
                                DMVisitorExpression._scopeMode = "normal";
                                DMObjectTree.AddGlobalInitAssign(dmObject, proc.GetGlobalVariableId(varDeclaration.Name).Value, expression);
                            }
                        }
                    }
                }

                if (procDefinition.IsVerb && (dmObject.IsSubtypeOf(DreamPath.Atom) || dmObject.IsSubtypeOf(DreamPath.Client)) && !DMCompiler.Settings.NoStandard)
                {
                    Expressions.Field  field    = new Expressions.Field(Location.Unknown, dmObject.GetVariable("verbs"));
                    DreamPath          procPath = new DreamPath(".proc/" + procName);
                    Expressions.Append append   = new Expressions.Append(Location.Unknown, field, new Expressions.Path(Location.Unknown, procPath));

                    dmObject.InitializationProcExpressions.Add(append);
                }
            } catch (CompileErrorException e) {
                DMCompiler.Error(e.Error);
            }
        }
Beispiel #19
0
 public LocateInferred(Location location, DreamPath path, DMExpression container) : base(location)
 {
     _path      = path;
     _container = container;
 }
Beispiel #20
0
 public bool TryGetValueAsDreamObjectOfType(DreamPath type, out DreamObject dreamObject)
 {
     return(TryGetValueAsDreamObject(out dreamObject) && dreamObject != null && dreamObject.IsSubtypeOf(type));
 }
Beispiel #21
0
 public NativeProc(DreamPath owningType, string name, DreamProc superProc, List <String> argumentNames, List <DMValueType> argumentTypes, Dictionary <string, DreamValue> defaultArgumentValues, HandlerFn handler, string?verbName, string?verbCategory, string?verbDesc, sbyte?invisibility)
     : base(owningType, name, superProc, ProcAttributes.None, argumentNames, argumentTypes, verbName, verbCategory, verbDesc, invisibility)
 {
     _defaultArgumentValues = defaultArgumentValues;
     Handler = handler;
 }
Beispiel #22
0
 public DreamValue(DreamPath value)
 {
     Type  = DreamValueType.DreamPath;
     Value = value;
 }
Beispiel #23
0
 public AsyncNativeProc(DreamPath owningType, string name, DreamProc superProc, List <String> argumentNames, List <DMValueType> argumentTypes, Dictionary <string, DreamValue> defaultArgumentValues, Func <State, Task <DreamValue> > taskFunc, string?verbName, string?verbCategory, string?verbDesc, sbyte?invisibility)
     : base(owningType, name, superProc, ProcAttributes.None, argumentNames, argumentTypes, verbName, verbCategory, verbDesc, invisibility)
 {
     _defaultArgumentValues = defaultArgumentValues;
     _taskFunc = taskFunc;
 }