Пример #1
0
        private void generateAssignment(Node root)
        {
            Node             assignOpNode   = root.children[0];
            Node             idNode         = assignOpNode.children[0];
            Node             valueNode      = assignOpNode.children[1];
            Tuple <int, int> scopeLocations = findScope(idNode.name);
            int scopeIndex      = scopeLocations.Item1;
            int locationInScope = scopeLocations.Item2;

            if (scopeIndex != -1 && locationInScope != -1)
            {
                ScopeElement           idToModify = this.scopes[scopeIndex].scopeMembers[locationInScope];
                Tuple <string, Object> exprValues = generateExpr(valueNode);
                String exprType = exprValues.Item1.ToString();
                if (exprType == idToModify.type)
                {
                    idToModify.value         = exprValues.Item2;
                    idToModify.isInitialized = true;
                    buildPrintMessage("id: " + idNode.name + " assigned " + exprValues.Item2.ToString() + ".");
                }
                else if (exprType != "error")
                {
                    buildPrintMessage("~~~Error: id " + idNode.name + " of type " + idToModify.type + " is does not match the type of the expression: " + exprType + ".");
                    this.errorCount++;
                }
            }
            else
            {
                buildPrintMessage("~~~Error: id " + idNode.name + " is not declared in its scope or any parent scope.");
                this.errorCount++;
            }
        }
Пример #2
0
        private Tuple <string, Object> getIdInfo(string id)
        {
            Tuple <int, int> idLocation = findScope(id);
            ScopeElement     idElement  = this.scopes[idLocation.Item1].scopeMembers[idLocation.Item2];

            return(new Tuple <string, Object>(idElement.type, idElement.value));
        }
Пример #3
0
        private Tuple <string, Object> generateExpr(Node root)
        {
            Tuple <string, Object> returnValues;

            if (isDigit.IsMatch(root.name))
            {
                returnValues = new Tuple <string, Object>("int", Int32.Parse(root.name));
            }
            else if (root.name == "==" || root.name == "!=" || root.name == "false" || root.name == "true")
            {
                returnValues = generateBooleanExpr(root);
            }
            else if (isString.IsMatch(root.name))
            {
                returnValues = new Tuple <string, Object>("string", root.name);
            }
            else if (root.name == "+")
            {
                returnValues = generateIntExpr(root);
            }
            else if (isChar.IsMatch(root.name))
            {
                Tuple <int, int> scopeLocations = findScope(root.name);
                int scopeIndex      = scopeLocations.Item1;
                int locationInScope = scopeLocations.Item2;
                if (scopeIndex != -1 && locationInScope != -1)
                {
                    ScopeElement idToUse = this.scopes[scopeIndex].scopeMembers[locationInScope];
                    idToUse.isUsed = true;
                    if (!idToUse.isInitialized)
                    {
                        buildPrintMessage("~~~Warning, id " + root.name + " is used without being initialized.");
                    }
                    returnValues = new Tuple <string, Object>(idToUse.type, idToUse.value);
                }
                else
                {
                    buildPrintMessage("~~~Error: id " + root.name + " is not declared in its scope or any parent scope.");
                    this.errorCount++;
                    returnValues = new Tuple <string, Object>("error", "You've met with a terrible fate, haven't you?");
                }
            }
            else
            {
                // Should be impossible to reach when this function is finished, but I felt having detailed
                // if/else blocks was more desirable than letting whatever the end branch was going to else.
                this.errorCount++;
                returnValues = new Tuple <string, Object>("error", "You've met with a terrible fate, haven't you?");
            }
            return(returnValues);
        }
Пример #4
0
 public void addChildScope(ScopeElement childScope)
 {
     this.scopeMembers.Add(childScope);
 }