Exemple #1
0
        private void CompileLet(AstLet node, Syt syt, StringBuilder sb)
        {
            CompileRecursive(node.exprRight, syt, sb);

            if (node.exprLeft is AstIndex)
            {
                var astIndex = (AstIndex)node.exprLeft;
                CompileRecursive(astIndex.exprLeft, syt, sb);
                CompileRecursive(astIndex.exprRight, syt, sb);
                sb.AppendLine("add");
                sb.AppendLine("pop pointer 1");
                sb.AppendLine("pop that 0");
                return;
            }

            if (node.exprLeft is AstVarRef)
            {
                var nodeVarRef = (AstVarRef)node.exprLeft;
                var syte       = syt.Lookup(nodeVarRef.StName);
                if (syte.Ksyte.FIn(Ksyte.Field, Ksyte.Arg, Ksyte.Var, Ksyte.Static))
                {
                    sb.AppendLine("pop {0} {1}".StFormat(StSegment(syte.Ksyte), syte.Isyte));
                }
                return;
            }

            throw new Erparse(node, "not an lvalue");
        }
Exemple #2
0
        private void CompileVarRef(AstVarRef node, Syt syt, StringBuilder sb)
        {
            var syte = syt.Lookup(node.StName);

            if (syte.Ksyte.FIn(Ksyte.Field, Ksyte.Arg, Ksyte.Var, Ksyte.Static))
            {
                sb.AppendLine("push {0} {1}".StFormat(StSegment(syte.Ksyte), syte.Isyte));
            }
            else
            {
                throw new Erparse(node, "not an rvalue");
            }
        }
Exemple #3
0
            public Syte Lookup(string stName)
            {
                if (mpsyteBystName.ContainsKey(stName))
                {
                    return(mpsyteBystName[stName]);
                }

                if (sytBase == null)
                {
                    return(null);
                }

                return(sytBase.Lookup(stName));
            }
Exemple #4
0
        private void CompileCall(AstCall node, Syt syt, StringBuilder sb)
        {
            var cparam = node.rgexprParam.Length;

            if (node.exprFunc is AstVarRef)
            {
                var nodeRef = (AstVarRef) node.exprFunc;
                var syte = syt.Lookup(nodeRef.StName);

                switch (syte.Ksyte)
                {
                    case Ksyte.StaticFunction:
                        break;
                    case Ksyte.MemberFunction:
                        sb.AppendLine("push pointer 0");
                        cparam++;
                        break;
                    default:
                        throw new Erparse(node, "unexpected ksyte " + syte.Ksyte);
                }

                foreach (var nodeParam in node.rgexprParam)
                    CompileRecursive(nodeParam, syt, sb);

                sb.AppendLine("call {0}.{1} {2}".StFormat(node.NodeAncestor<AstClass>().StName, syte.StName, cparam));
            }
            else if (node.exprFunc is AstDot)
            {
                var nodeDot = (AstDot) node.exprFunc;

                var syteLeft = syt.Lookup(nodeDot.varLeft.StName) ?? new Syte(Ksyte.Class, nodeDot.varLeft.StName, 0, nodeDot.varLeft.StName);

                if (!syteLeft.Ksyte.FIn(Ksyte.Arg, Ksyte.Field, Ksyte.Var, Ksyte.Static, Ksyte.Class))
                   throw new Erparse(node, "unexpected ksyte " + syteLeft.Ksyte);

                if (syteLeft.Ksyte.FIn(Ksyte.Arg, Ksyte.Field, Ksyte.Var, Ksyte.Static))
                {
                    cparam++;
                    CompileRecursive(nodeDot.varLeft, syt, sb);
                }

                Ksyte ksyteRight;
                if (syteLeft.Ksyte == Ksyte.Class && syteLeft.StName != node.NodeAncestor<AstClass>().StName)
                    ksyteRight = Ksyte.StaticFunction /*constuctor???*/;
                else
                {
                    var syte = syt.Lookup(nodeDot.varRight.StName);
                    ksyteRight = syte == null ? Ksyte.MemberFunction : syte.Ksyte;
                }

                switch (ksyteRight)
                {
                    case Ksyte.Constructor:
                        if (syteLeft.Ksyte != Ksyte.Class)
                            throw new Erparse(node, "cannot call constructor on instance");
                        break;
                    case Ksyte.StaticFunction:
                    case Ksyte.MemberFunction:
                        break;
                    default:
                        throw new Erparse(node, "unexpected ksyte " + ksyteRight);
                }

                foreach (var nodeParam in node.rgexprParam)
                    CompileRecursive(nodeParam, syt, sb);

                sb.AppendLine("call {0}.{1} {2}".StFormat(syteLeft.StType, nodeDot.varRight.StName, cparam));
            }
        }
Exemple #5
0
 private void CompileVarRef(AstVarRef node, Syt syt, StringBuilder sb)
 {
     var syte = syt.Lookup(node.StName);
     if (syte.Ksyte.FIn(Ksyte.Field, Ksyte.Arg, Ksyte.Var, Ksyte.Static))
         sb.AppendLine("push {0} {1}".StFormat(StSegment(syte.Ksyte), syte.Isyte));
     else
         throw new Erparse(node, "not an rvalue");
 }
Exemple #6
0
        private void CompileLet(AstLet node, Syt syt, StringBuilder sb)
        {
            CompileRecursive(node.exprRight, syt, sb);

            if (node.exprLeft is AstIndex)
            {
                var astIndex = (AstIndex) node.exprLeft;
                CompileRecursive(astIndex.exprLeft, syt, sb);
                CompileRecursive(astIndex.exprRight, syt, sb);
                sb.AppendLine("add");
                sb.AppendLine("pop pointer 1");
                sb.AppendLine("pop that 0");
                return;
            }

            if (node.exprLeft is AstVarRef)
            {
                var nodeVarRef = (AstVarRef) node.exprLeft;
                var syte = syt.Lookup(nodeVarRef.StName);
                if(syte.Ksyte.FIn(Ksyte.Field, Ksyte.Arg, Ksyte.Var, Ksyte.Static))
                    sb.AppendLine("pop {0} {1}".StFormat(StSegment(syte.Ksyte), syte.Isyte));
                return;
            }

            throw new Erparse(node, "not an lvalue");
        }
Exemple #7
0
        private void CompileCall(AstCall node, Syt syt, StringBuilder sb)
        {
            var cparam = node.rgexprParam.Length;

            if (node.exprFunc is AstVarRef)
            {
                var nodeRef = (AstVarRef)node.exprFunc;
                var syte    = syt.Lookup(nodeRef.StName);

                switch (syte.Ksyte)
                {
                case Ksyte.StaticFunction:
                    break;

                case Ksyte.MemberFunction:
                    sb.AppendLine("push pointer 0");
                    cparam++;
                    break;

                default:
                    throw new Erparse(node, "unexpected ksyte " + syte.Ksyte);
                }

                foreach (var nodeParam in node.rgexprParam)
                {
                    CompileRecursive(nodeParam, syt, sb);
                }

                sb.AppendLine("call {0}.{1} {2}".StFormat(node.NodeAncestor <AstClass>().StName, syte.StName, cparam));
            }
            else if (node.exprFunc is AstDot)
            {
                var nodeDot = (AstDot)node.exprFunc;

                var syteLeft = syt.Lookup(nodeDot.varLeft.StName) ?? new Syte(Ksyte.Class, nodeDot.varLeft.StName, 0, nodeDot.varLeft.StName);

                if (!syteLeft.Ksyte.FIn(Ksyte.Arg, Ksyte.Field, Ksyte.Var, Ksyte.Static, Ksyte.Class))
                {
                    throw new Erparse(node, "unexpected ksyte " + syteLeft.Ksyte);
                }

                if (syteLeft.Ksyte.FIn(Ksyte.Arg, Ksyte.Field, Ksyte.Var, Ksyte.Static))
                {
                    cparam++;
                    CompileRecursive(nodeDot.varLeft, syt, sb);
                }

                Ksyte ksyteRight;
                if (syteLeft.Ksyte == Ksyte.Class && syteLeft.StName != node.NodeAncestor <AstClass>().StName)
                {
                    ksyteRight = Ksyte.StaticFunction /*constuctor???*/;
                }
                else
                {
                    var syte = syt.Lookup(nodeDot.varRight.StName);
                    ksyteRight = syte == null ? Ksyte.MemberFunction : syte.Ksyte;
                }

                switch (ksyteRight)
                {
                case Ksyte.Constructor:
                    if (syteLeft.Ksyte != Ksyte.Class)
                    {
                        throw new Erparse(node, "cannot call constructor on instance");
                    }
                    break;

                case Ksyte.StaticFunction:
                case Ksyte.MemberFunction:
                    break;

                default:
                    throw new Erparse(node, "unexpected ksyte " + ksyteRight);
                }

                foreach (var nodeParam in node.rgexprParam)
                {
                    CompileRecursive(nodeParam, syt, sb);
                }

                sb.AppendLine("call {0}.{1} {2}".StFormat(syteLeft.StType, nodeDot.varRight.StName, cparam));
            }
        }