Пример #1
0
        public void HandleIntegerAssignment(ASTNode node, int variableScope, string[] tempAddressBytes)
        {
            int value = HandleIntegerExpression(node.Descendants[1]);

            if (value != -1)
            {
                StaticTemp.GetTempTableEntry(node.Descendants[0].Token.Text, variableScope).Value = value.ToString("X2");
                Image.WriteByte("A9");
                Image.WriteByte(value.ToString("X2"));
                Image.WriteByte("8D");
                Image.WriteByte(tempAddressBytes[0]);
                Image.WriteByte(tempAddressBytes[1]);
            }
            else if (value == -1)
            {
                string[] splitAdditionAddress = AdditionAddress.Split(" ");
                Image.WriteByte("AD");
                Image.WriteByte(splitAdditionAddress[0]);
                Image.WriteByte(splitAdditionAddress[1]);
                Image.WriteByte("8D");
                Image.WriteByte(tempAddressBytes[0]);
                Image.WriteByte(tempAddressBytes[1]);
                ResetAdditionMemory();
            }
            node.Descendants[0].Visited = true;
            node.Descendants[1].Visited = true;
        }
Пример #2
0
        public void HandlePrintIdentifier(ASTNode node)
        {
            TempTableEntry staticEntry  = StaticTemp.GetTempTableEntry(node.Token.Text, node.ReferenceScope);
            string         variableType = staticEntry.Type;

            string[] tempAddressBytes = staticEntry.Address.Split(" ");
            switch (variableType)
            {
            case "int":
                Image.WriteByte("AC");
                Image.WriteByte(tempAddressBytes[0]);
                Image.WriteByte(tempAddressBytes[1]);
                Image.WriteByte("A2");
                Image.WriteByte("01");
                Image.WriteByte("FF");
                break;

            case "boolean":
                HandlePrintBoolean((int)EvaluateBooleanSubtree(node));
                break;

            case "string":
                HandlePrintString(node);
                break;
            }
        }
Пример #3
0
        public void HandleAssignment(ASTNode node)
        {
            string         variableName  = node.Descendants[0].Token.Text;
            int            variableScope = SemanticAnalyser.VariableChecker.FindSymbol(node.Descendants[0], node.AppearsInScope);
            TempTableEntry variableTemp  = StaticTemp.GetTempTableEntry(variableName, variableScope);
            string         type          = variableTemp.Type;

            string[] tempAddressBytes = variableTemp.Address.Split(" ");
            switch (type)
            {
            case "int":
                HandleIntegerAssignment(node, variableScope, tempAddressBytes);
                break;

            case "boolean":
                HandleBooleanAssignment(node, variableScope, tempAddressBytes);
                break;

            case "string":
                HandleStringAssignment(node, variableScope, tempAddressBytes);
                break;
            }
            node.Descendants[0].Visited = true;
            node.Descendants[1].Visited = true;
        }
Пример #4
0
 public void HandleStringAssignment(ASTNode node, int variableScope, string[] tempAddressBytes)
 {
     // write string in heap
     // store heap address in temp pointer position
     InsertStringInHeap(node.Descendants[1].Token.Text);
     StaticTemp.GetTempTableEntry(tempAddressBytes[0] + " " + tempAddressBytes[1]).Value = Image.GetLastHeapAddress();
     Image.WriteByte("A9");
     Image.WriteByte(StaticTemp.GetTempTableEntry(tempAddressBytes[0] + " " + tempAddressBytes[1]).Value.Split(" ") [0]);
     Image.WriteByte("8D");
     Image.WriteByte(tempAddressBytes[0]);
     Image.WriteByte(tempAddressBytes[1]);
     node.Descendants[1].Visited = true;
 }
Пример #5
0
        public void CreateAdditionTreeStream(ASTNode node)
        {
            switch (node.Token.Kind)
            {
            case TokenKind.DigitToken:
                AdditionTreeStream.Add(int.Parse(node.Token.Text));
                break;

            case TokenKind.IdentifierToken:
                string value = StaticTemp.GetTempTableEntry(node.Token.Text, node.ReferenceScope).Value;
                AdditionTreeStream.Add(int.Parse(value));
                break;
            }
        }
Пример #6
0
 public void HandlePrintBoolean(int booleanValue)
 {
     string[] valueAddress;
     if (booleanValue == 1)
     {
         valueAddress = StaticTemp.GetTempTableEntry("true", 0).Address.Split(" ");
     }
     else
     {
         valueAddress = StaticTemp.GetTempTableEntry("false", 0).Address.Split(" ");
     }
     Image.WriteByte("AC");
     Image.WriteByte(valueAddress[0]);
     Image.WriteByte(valueAddress[1]);
     Image.WriteByte("A2");
     Image.WriteByte("02");
     Image.WriteByte("FF");
 }
Пример #7
0
        public void HandleVarDecl(ASTNode node)
        {
            Image.WriteByte("A9");
            Image.WriteByte("00");
            string varType  = node.Descendants[0].Token.Text;
            string varName  = node.Descendants[1].Token.Text;
            int    varScope = SemanticAnalyser.VariableChecker.FindSymbol(node.Descendants[1], node.AppearsInScope);

            node.Visited = true;
            node.Descendants[0].Visited = true;
            node.Descendants[1].Visited = true;
            string[] tempAddressBytes = new string[2];
            StaticTemp.NewStaticEntry(varName, "00", varType, varScope);
            tempAddressBytes = StaticTemp.GetTempTableEntry(varName, varScope).Address.Split(" ");
            Image.WriteByte("8D");
            Image.WriteByte(tempAddressBytes[0]);
            Image.WriteByte(tempAddressBytes[1]);
        }
Пример #8
0
 public object EvaluateBooleanSubtree(ASTNode node)
 {
     if (node.Descendants.Count == 0)
     {
         if (node.Token.Kind == TokenKind.TrueToken)
         {
             return(1);
         }
         else if (node.Token.Kind == TokenKind.FalseToken)
         {
             return(0);
         }
         else if (node.Token.Kind == TokenKind.IdentifierToken)
         {
             return(StaticTemp.GetTempTableEntry(node.Token.Text, node.ReferenceScope).Value);
         }
         else if (node.Token.Kind == TokenKind.DigitToken)
         {
             return(int.Parse(node.Token.Text));
         }
         else
         {
             return(0);
         }
     }
     else
     {
         if (node.Token.Kind == TokenKind.EquivalenceToken || node.Token.Kind == TokenKind.NotEqualToken)
         {
             if (node.Token.Kind == TokenKind.EquivalenceToken)
             {
                 return(HandleEquivalence(node));
             }
             else
             {
                 return(HandleNotEqual(node));
             }
         }
         else
         {
             return(0);
         }
     }
 }
Пример #9
0
        public void HandlePrintString(ASTNode node)
        {
            switch (node.Token.Kind)
            {
            case TokenKind.IdentifierToken:
                string[] valueAddress = StaticTemp.GetTempTableEntry(node.Token.Text, node.ReferenceScope).Address.Split(" ");
                HandlePrintString(valueAddress);
                break;

            case TokenKind.StringToken:
                InsertStringInHeap(node.Token.Text);
                string stringAddress = Image.GetLastHeapAddress();
                HandlePrintString(stringAddress);
                break;

            default:
                break;
            }
        }
Пример #10
0
        public void HandleBooleanAssignment(ASTNode node, int variableScope, string[] tempAddressBytes)
        {
            int boolVal = (int)EvaluateBooleanSubtree(node.Descendants[1]);

            StaticTemp.GetTempTableEntry(node.Descendants[0].Token.Text, variableScope).Value = boolVal.ToString("X2");
            Image.WriteByte("A9");
            if (boolVal == 1)
            {
                Image.WriteByte("01");
            }
            else
            {
                Image.WriteByte("00");
            }
            Image.WriteByte("8D");
            Image.WriteByte(tempAddressBytes[0]);
            Image.WriteByte(tempAddressBytes[1]);
            node.Descendants[0].Visited = true;
            node.Descendants[1].Visited = true;
        }
Пример #11
0
        public int HandleIntegerExpression(ASTNode node)
        {
            int outInteger;

            switch (node.Token.Kind)
            {
            case TokenKind.DigitToken:
                int.TryParse(node.Token.Text, out outInteger);
                return(outInteger);

            case TokenKind.IdentifierToken:
                int.TryParse(StaticTemp.GetTempTableEntry(node.Token.Text, node.ReferenceScope).Value, out outInteger);
                return(outInteger);

            case TokenKind.AdditionToken:
                HandleAddition(node);
                return(-1);

            default:
                return(-2);
            }
        }