Пример #1
0
        public override string Compile(int tabs = 0)
        {
            if (assingBlock.Type != Block.BlockType.CLASS)
            {
                return("");
            }

            if (isOneNotDefined)
            {
                return("");
            }

            if (variable is Variable)
            {
                ((Variable)variable).Check();
            }

            if (this.setter is Block)
            {
                ((Block)this.setter).variables.Add("value", new Assign(new Variable(new Token(Token.Type.STRING, "value"), assingBlock, ((Variable)variable).GetDateType()), new Token(Token.Type.ASIGN, "="), new Null(), assingBlock));
            }

            string var_name = variable.TryVariable().Value;
            string tab      = DoTabs(tabs + 1);
            string ret      = "";

            ret += assingBlock.getClass() + ".prototype.Property$" + var_name + " = {\n";
            if (isAutoDefined)
            {
                ret += "  $value: ''" + (this.setter != null || this.getter != null?",\n":"");
            }
            if (this.getter != null)
            {
                ret += "  get: function(){\n";
                if (isAutoDefined)
                {
                    ret += "    return this.$value;\n";
                }
                else
                {
                    ret += getter.Compile(tabs + 2);
                }
                ret += "  }" + (this.setter != null?",":"") + "\n";
            }
            if (this.setter != null)
            {
                ret += "  set: function(value){\n";
                if (isAutoDefined)
                {
                    ret += "    this.$value = value;\n";
                }
                else
                {
                    ret += setter.Compile(tabs + 2);
                }
                ret += "  }\n";
            }
            ret += "}";
            return(ret);
        }
Пример #2
0
        public Properties(Types variable, Types getter = null, Types setter = null, Block current_block = null)
        {
            this.variable = variable;
            this.setter   = setter;
            this.getter   = getter;
            current_block.SymbolTable.Add(variable.TryVariable().Value, this);

            if (getter is Block gb && gb.children.Count == 0)
            {
                if (setter != null && setter is Block sesb && sesb.children.Count != 0)
                {
                    isOneNotDefined = true;
                }
            }
            if (setter is Block sb && sb.children.Count == 0)
            {
                if (getter != null && getter is Block gegb && gegb.children.Count != 0)
                {
                    isOneNotDefined = true;
                }
            }
            if ((getter is Block gbb && gbb.children.Count == 0) && (setter is Block ssb && ssb.children.Count == 0))
            {
                isAutoDefined = true;
            }
        }
Пример #3
0
 public override void Semantic()
 {
     if (!isIterable)
     {
         Interpreter.semanticError.Add(new Error("#500 " + source.TryVariable().Value + " with class '" + className + "' is not Iterable", Interpreter.ErrorType.ERROR, source.getToken()));
     }
     if (source is UnaryOp uoq && ((UnaryOp)source).Op == "call")
     {
         uoq.Semantic();
     }
 }
Пример #4
0
 public string GetName(bool real = false)
 {
     if (!real)
     {
         if (newName == "")
         {
             attcount++;
             newName = (_class == null ? "attribute$" + attcount : "attribute$" + attcount + "$" + _class.TryVariable().Value);
         }
         return(newName);
     }
     if (_class == null)
     {
         _class = assingBlock.SymbolTable.Get(nclass.Value);
     }
     return(_class?.TryVariable().Value);
 }
Пример #5
0
        public override string Compile(int tabs = 0)
        {
            if (right != null)
            {
                right.assingBlock = assingBlock;
                right.endit       = false;
            }
            left.assingBlock = assingBlock;
            left.endit       = false;

            if (right is Variable)
            {
                ((Variable)right).Check();
            }
            if (left is Variable)
            {
                ((Variable)left).Check();
            }

            Variable v = null;

            string o = Variable.GetOperatorStatic(op.type);

            if (o == "is")
            {
                right = block.SymbolTable.Get(rtok.Value);
                string vname = left.TryVariable().Value;

                string classname = right.TryVariable().Value;

                string rt = "";
                if (right is Generic)
                {
                    rt = "(" + vname + ".constructor.name == this.generic$" + classname + " ? true : false)";
                }
                else
                {
                    rt = "(" + vname + ".constructor.name == '" + classname + "' ? true : false)";
                }

                outputType = new Token(Token.Type.BOOL, "bool");
                return((inParen ? "(" : "") + rt + (inParen ? ")" : ""));
            }
            if (left is Number)
            {
                v = new Variable(((Number)left).getToken(), block, new Token(Token.Type.CLASS, "int"));
                var saveOut = outputType;
                outputType = v.OutputType(op.type, left, right);
                if (outputType.type == Token.Type.CLASS && outputType.Value == "int")
                {
                    outputType = saveOut;
                }
            }
            else if (left is CString)
            {
                v          = new Variable(((CString)left).getToken(), block, new Token(Token.Type.CLASS, "string"));
                outputType = v.OutputType(op.type, left, right);
                if (right is UnaryOp ruop)
                {
                    ruop.isInString = true;
                }
            }
            else if (left is Variable)
            {
                v = ((Variable)left);
                if (v.GetDateType().Value == "auto")
                {
                    v.Check();
                }
                outputType = ((Variable)left).OutputType(op.type, left, right);
            }
            else if (left is UnaryOp leuo)
            {
                if (leuo.Op == "call")
                {
                    if (leuo.usingFunction == null)
                    {
                        leuo.Compile();
                    }
                    if (leuo.usingFunction != null)
                    {
                        Function f = leuo.usingFunction;
                        outputType = f.Returnt;
                    }
                }
                if (leuo.Op == "..")
                {
                    if (assingBlock?.SymbolTable.Get("Range") != null)
                    {
                        outputType = ((Class)assingBlock?.SymbolTable.Get("Range")).Name;
                    }
                }
                if (op.Value == "dot" && right is UnaryOp riuo)
                {
                    riuo.Block = assingBlock?.SymbolTable.Get(outputType.Value).assingBlock;
                }
                else if (op.Value == "dot" && right is Variable riva)
                {
                    var fnd = assingBlock?.SymbolTable.Get(leuo.OutputType.Value);
                    if (fnd != null && !(fnd is Error))
                    {
                        Types t = ((Class)fnd).Block.SymbolTable.Get(riva.Value);
                        if (t is Assign ta)
                        {
                            if (ta.Left is Variable tav)
                            {
                                riva.setType(tav.GetDateType());
                            }
                        }
                        else if (t is Variable tv)
                        {
                            riva.setType(tv.GetDateType());
                        }
                        outputType = riva.GetDateType();
                    }
                }
                v = left.TryVariable();
            }
            else if (left is BinOp)
            {
                left.Compile();
                v = left.TryVariable();
            }
            else
            {
                left.Compile();
                v = left.TryVariable();
            }
            if (((v._class != null && v.class_ == null) || (v.class_ != null && v.class_.JSName != "")))
            {
                if (op.Value == "dot")
                {
                    return((inParen ? "(" : "") + left.Compile(0) + Variable.GetOperatorStatic(op.type) + right.Compile(0) + (inParen ? ")" : ""));
                }
                else
                {
                    return((inParen ? "(" : "") + left.Compile(0) + " " + Variable.GetOperatorStatic(op.type) + " " + right.Compile(0) + (inParen ? ")" : ""));
                }
            }
            else if (v.class_ != null)
            {
                if (op.Value == "dot")
                {
                    return((inParen ? "(" : "") + left.Compile(0) + "." + right.Compile(0) + (inParen ? ")" : ""));
                }
                Types oppq = v.class_.block.SymbolTable.Get("operator " + Variable.GetOperatorNameStatic(op.type));
                if (oppq is Error)
                {
                    return("");
                }
                Function opp = (Function)oppq;
                if (op.type == Token.Type.NOTEQUAL)
                {
                    return((inParen ? "(" : "") + "!(" + left.Compile(0) + "." + opp.Name + "(" + right.Compile(0) + "))" + (inParen ? ")" : ""));
                }
                else if (op.type == Token.Type.MORE || op.type == Token.Type.LESS)
                {
                    if (op.type == Token.Type.MORE)
                    {
                        return((inParen ? "(" : "") + left.Compile(0) + "." + opp.Name + "(" + right.Compile(0) + ") > 0" + (inParen ? ")" : ""));
                    }
                    else
                    {
                        return((inParen ? "(" : "") + left.Compile(0) + "." + opp.Name + "(" + right.Compile(0) + ") < 0" + (inParen ? ")" : ""));
                    }
                }
                else
                {
                    return((inParen ? "(" : "") + left.Compile(0) + "." + opp.Name + "(" + right.Compile(0) + ")" + (inParen ? ")" : ""));
                }
            }
            return("");
        }
Пример #6
0
        public override void Semantic()
        {
            left.Semantic();
            if (op.type == Token.Type.DOT)
            {
                if (left is UnaryOp && ((UnaryOp)left).Op == "call" && right is Variable)
                {
                    if (((UnaryOp)left).usingFunction != null)
                    {
                        Function f = ((UnaryOp)left).usingFunction;
                        ((Variable)right).setType(f.Returnt);
                    }
                }
            }
            right.Semantic();

            if (left.assingBlock == null)
            {
                left.assingBlock = assingBlock;
            }
            if (right.assingBlock == null)
            {
                right.assingBlock = assingBlock;
            }

            Variable v = left.TryVariable();
            Variable r = right.TryVariable();

            if (left is BinOp bi)
            {
                if (bi.op.Value == "dot")
                {
                    Console.WriteLine("xxx");
                }
            }
            if (op.Value == "dot" && left is UnaryOp leuop && right is Variable riva)
            {
                if (!(assingBlock.SymbolTable.Get(leuop.OutputType.Value) is Error))
                {
                    Types t = ((Class)assingBlock.SymbolTable.Get(leuop.OutputType.Value)).Block.SymbolTable.Get(riva.Value);
                    if (t is Assign ta)
                    {
                        if (ta.Left is Variable tav)
                        {
                            riva.setType(tav.GetDateType());
                        }
                    }
                    else if (t is Variable tv)
                    {
                        riva.setType(tv.GetDateType());
                    }
                }
            }

            if (op.Value == "dot")
            {
                this.outputType = right.TryVariable().GetDateType();
            }
            else
            {
                this.outputType = v.OutputType(op.type, v, r);
            }

            if (v.Type != "auto")
            {
                if (!v.SupportOp(op.type))
                {
                    Interpreter.semanticError.Add(new Error("#300 Varible type '" + v.Type + "' not support operator " + Variable.GetOperatorStatic(op.type), Interpreter.ErrorType.ERROR, left.getToken()));
                }
                else if (!v.SupportSecond(op.type, right, r))
                {
                    Interpreter.semanticError.Add(new Error("#301 Operator " + Variable.GetOperatorStatic(op.type) + " cannot be applied for '" + v.Type + "' and '" + r.Type + "'", Interpreter.ErrorType.ERROR, op));
                }
            }

            if (op.Value == "dot")
            {
                Console.WriteLine("UnaryOp dot semantic: " + left.getToken().Value + " dot " + right.getToken().Value);
            }
        }