Пример #1
0
        protected override void Preprocess(Stream stream)
        {
            this.writer = new StreamWriter(stream);

            var globalAnaxNode = this.nodes.Where(x => x is AnaxNode).Select(x => x as AnaxNode).ToList();
            int stackCount     = globalAnaxNode.Select(x => int.Parse(x.Length.Value)).Sum();

            this.writer.WriteLine("'i'c");
            this.writer.WriteLine("nta 4 f5 krz f2 f5@ ; (global) allocate variables");
            this.writer.WriteLine("krz f5 f2");
            this.writer.WriteLine("nta {0} f5", stackCount * 4);

            for (int i = 0; i < globalAnaxNode.Count; i++)
            {
                AnaxNode anax = globalAnaxNode[i];

                if (this.globalVariables.ContainsKey(anax.Name))
                {
                    throw new ApplicationException($"Duplication variable name: {anax.Name}");
                }

                this.globalVariables.Add(anax.Name, (uint)(-stackCount * 4));
                ToAnax(anax, null);

                stackCount -= int.Parse(anax.Length.Value);
            }

            this.writer.WriteLine("inj fasal xx f5@");
            this.writer.WriteLine("krz f2 f5 ; (global) restore stack poiter");
            this.writer.WriteLine("krz f5@ f2 ata 4 f5");
            this.writer.WriteLine("krz f5@ xx ; (global) application end");
            this.writer.WriteLine();
        }
Пример #2
0
        protected override void ToAnax(AnaxNode node, IDictionary <IdentifierNode, uint> anaxDictionary)
        {
            if (node.Expression != null)
            {
                if (node.Length.Value != "1")
                {
                    throw new ApplicationException($"Invalid operation: {node}");
                }

                OutputExpression(node.Expression, "f0", anaxDictionary);

                if (anaxDictionary != null && anaxDictionary.ContainsKey(node.Name))
                {
                    this.writer.WriteLine("krz f0 f3+{0}@ ; anax #{1} el f0", anaxDictionary[node.Name], node.Name.Value);
                }
                else if (this.globalVariables.ContainsKey(node.Name))
                {
                    this.writer.WriteLine("krz f0 f2+{0}@ ; anax #{1} el f0", this.globalVariables[node.Name], node.Name.Value);
                }
                else
                {
                    throw new ApplicationException($"Not found variable name : #{node.Name.Value}");
                }
            }
        }
Пример #3
0
        private AnaxNode GetAnaxNode(IList <Token> tokens, ref int index)
        {
            AnaxNode anaxNode = new AnaxNode();
            int      count    = tokens.Count;

            Token token = tokens[index];

            if (token is Identifier)
            {
                string name = (token as Identifier).Name;

                if (name.All(char.IsDigit) && char.IsDigit((name ?? "0").FirstOrDefault()))
                {
                    throw new ApplicationException($"Invalid identifier: anax {name}");
                }
                else
                {
                    anaxNode.Name = new IdentifierNode
                    {
                        Value = name
                    };
                }
            }
            else
            {
                throw new ApplicationException($"Invalid value: anax {token}");
            }

            index++;
            if (index < count && tokens[index].Type == TokenType.ARRAY_SIGN)
            {
                index++;

                ExpressionNode node = GetValueNode(tokens, ref index);

                if (node is ConstantNode)
                {
                    anaxNode.Length = node as ConstantNode;
                }
                else
                {
                    throw new ApplicationException("Array length is only constant value");
                }
            }

            if (index < count && tokens[index].Type == TokenType.EL)
            {
                index++;
                anaxNode.Expression = GetCompareNode(tokens, ref index);
            }

            return(anaxNode);
        }
Пример #4
0
        protected override void ToFal(FalNode node, IDictionary <IdentifierNode, uint> anaxDictionary)
        {
            // 内部で使用する変数領域の確保
            var anaxList = node.Syntaxes.Where(x => x is AnaxNode)
                           .Select(x => (x as AnaxNode)).ToList();
            int stackCount = anaxList.Select(x => int.Parse(x.Length.Value)).Sum();
            int block      = this.blockCount++;

            this.writer.WriteLine("nta {0} f5 ; allocate variables in fal", stackCount * 4);

            // 条件式
            this.writer.WriteLine("nll --fal-rinyv-{0}--", block);
            OutputExpression(node.CompareExpression, "f0", anaxDictionary);
            this.writer.WriteLine("fi f0 0 clo malkrz --fal-situv-{0}-- xx ; rinyv fal", block);

            int count = 0;

            for (int i = 0; i < anaxList.Count; i++)
            {
                AnaxNode anax = anaxList[i];

                if (anaxDictionary.ContainsKey(anax.Name))
                {
                    throw new ApplicationException($"Duplication variable name: {anax.Name} in fal");
                }

                count += int.Parse(anax.Length.Value);
                anaxDictionary.Add(anax.Name, (uint)(-(this.cersvaStackCount + count) * 4));
                ToAnax(anax, anaxDictionary);
            }

            // 文の処理
            OutputSyntax(node.Syntaxes, anaxDictionary);

            // 内部で使用した変数領域の解放
            foreach (var anax in anaxList)
            {
                anaxDictionary.Remove(anax.Name);
            }

            this.writer.WriteLine("krz --fal-rinyv-{0}-- xx", block);
            this.writer.WriteLine("nll --fal-situv-{0}-- ata {1} f5 ; situv fal", block, stackCount * 4);
        }
Пример #5
0
        private List <AnaxNode> GetArguments(IList <Token> tokens, ref int index)
        {
            List <AnaxNode> anaxNodeList = new List <AnaxNode>();
            int             count        = tokens.Count;

            if (tokens[index].Type == TokenType.R_CIRC)
            {
                return(anaxNodeList);
            }

            while (index < count)
            {
                AnaxNode anaxNode = new AnaxNode();
                Token    token    = tokens[index];
                if (token is Identifier)
                {
                    string name = (token as Identifier).Name;

                    if (name.All(char.IsDigit) && char.IsDigit((name ?? "0").FirstOrDefault()))
                    {
                        throw new ApplicationException($"Invalid identifier: anax {name}");
                    }
                    else
                    {
                        anaxNode.Name = new IdentifierNode
                        {
                            Value = name
                        };

                        anaxNodeList.Add(anaxNode);
                    }
                }
                else
                {
                    throw new ApplicationException($"Invalid value: anax {token}");
                }

                index++;
                if (index < count)
                {
                    token = tokens[index];

                    if (token.Type == TokenType.COMMA)
                    {
                        index++;
                    }
                    else if (token.Type == TokenType.R_CIRC)
                    {
                        break;
                    }
                    else
                    {
                        throw new ApplicationException($"Invalid value: {token}");
                    }
                }
                else
                {
                    throw new ApplicationException($"End of file in {anaxNode}");
                }
            }

            return(anaxNodeList);
        }
Пример #6
0
 protected abstract void ToAnax(AnaxNode node, IDictionary <IdentifierNode, uint> anaxDictionary);