Пример #1
0
        private void CompileSubroutine(AstSubroutine node, Syt syt, StringBuilder sb)
        {
            var sytFunc = new Syt(syt);

            sb.AppendLine("function {0} {1}".StFormat(node.FQName(), node.RgVarDeclDecl.Length));

            if (node.Ksubroutine == Ksubroutine.Constructor)
            {
                var csize = node.NodeAncestor <AstClass>().RgclassDecl.Length;
                sb.AppendLine("push constant {0}".StFormat(csize));
                sb.AppendLine("call Memory.alloc 1");
                sb.AppendLine("pop pointer 0");
            }
            else if (node.Ksubroutine == Ksubroutine.MemberFunction)
            {
                sb.AppendLine("push argument 0");
                sb.AppendLine("pop pointer 0");
                sytFunc.Add(Ksyte.Arg, "this", node.NodeAncestor <AstClass>().StName);
            }

            CompileRecursive(node.RgParam, sytFunc, sb);
            CompileRecursive(node.RgVarDeclDecl, sytFunc, sb);

            CompileRecursive(node.Body, sytFunc, sb);

            if (node.Type.Ktype == Ktype.Void)
            {
                sb.AppendLine("push constant 0");
                sb.AppendLine("return");
            }
        }
Пример #2
0
        private void CompileClass(AstClass node, Syt syt, StringBuilder sb)
        {
            syt.Add(Ksyte.Class, node.StName, node.StName);

            CompileRecursive(node.RgclassDecl, syt, sb);

            foreach (var astSubroutine in node.Rgsubroutine)
            {
                Ksyte ksyte;
                switch (astSubroutine.Ksubroutine)
                {
                case Ksubroutine.Constructor: ksyte = Ksyte.Constructor; break;

                case Ksubroutine.MemberFunction: ksyte = Ksyte.MemberFunction; break;

                case Ksubroutine.StaticFunction: ksyte = Ksyte.StaticFunction; break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                syt.Add(ksyte, astSubroutine.StName, null);
            }
            CompileRecursive(node.Rgsubroutine, syt, sb);
        }
Пример #3
0
 private void CompileVarDecl(AstVarDecl node, Syt syt, StringBuilder sb)
 {
     syt.Add(node.FLocal ? Ksyte.Var : Ksyte.Arg, node.StName, node.Type.stType);
 }
Пример #4
0
        private void CompileSubroutine(AstSubroutine node, Syt syt, StringBuilder sb)
        {
            var sytFunc = new Syt(syt);

            sb.AppendLine("function {0} {1}".StFormat(node.FQName(), node.RgVarDeclDecl.Length));

            if (node.Ksubroutine == Ksubroutine.Constructor)
            {
                var csize = node.NodeAncestor<AstClass>().RgclassDecl.Length;
                sb.AppendLine("push constant {0}".StFormat(csize));
                sb.AppendLine("call Memory.alloc 1");
                sb.AppendLine("pop pointer 0");

            }
            else if (node.Ksubroutine == Ksubroutine.MemberFunction)
            {
                sb.AppendLine("push argument 0");
                sb.AppendLine("pop pointer 0");
                sytFunc.Add(Ksyte.Arg, "this", node.NodeAncestor<AstClass>().StName);
            }

            CompileRecursive(node.RgParam, sytFunc, sb);
            CompileRecursive(node.RgVarDeclDecl, sytFunc, sb);

            CompileRecursive(node.Body, sytFunc, sb);

            if (node.Type.Ktype == Ktype.Void)
            {
                sb.AppendLine("push constant 0");
                sb.AppendLine("return");
            }
        }
Пример #5
0
 private void CompileClassDecl(AstClassDecl node, Syt syt, StringBuilder sb)
 {
     syt.Add(node.KClassDecl == KClassDecl.Field ? Ksyte.Field : Ksyte.Static, node.StName, node.Type.stType);
 }
Пример #6
0
        private void CompileClass(AstClass node, Syt syt, StringBuilder sb)
        {
            syt.Add(Ksyte.Class, node.StName, node.StName);

            CompileRecursive(node.RgclassDecl, syt, sb);

            foreach (var astSubroutine in node.Rgsubroutine)
            {
                Ksyte ksyte;
                switch (astSubroutine.Ksubroutine)
                {
                    case Ksubroutine.Constructor: ksyte = Ksyte.Constructor; break;
                    case Ksubroutine.MemberFunction: ksyte = Ksyte.MemberFunction; break;
                    case Ksubroutine.StaticFunction: ksyte = Ksyte.StaticFunction; break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                syt.Add(ksyte, astSubroutine.StName, null);
            }
            CompileRecursive(node.Rgsubroutine, syt, sb);
        }
Пример #7
0
 private void CompileClassDecl(AstClassDecl node, Syt syt, StringBuilder sb)
 {
     syt.Add(node.KClassDecl == KClassDecl.Field ? Ksyte.Field : Ksyte.Static, node.StName, node.Type.stType);
 }
Пример #8
0
 private void CompileVarDecl(AstVarDecl node, Syt syt, StringBuilder sb)
 {
     syt.Add(node.FLocal ? Ksyte.Var : Ksyte.Arg, node.StName, node.Type.stType);
 }