Example #1
0
 public BinaryOpChain(Expression left, Token op, Expression right, TopLevelConstruct owner)
     : base(left.FirstToken, owner)
 {
     this.Left  = left;
     this.Right = right;
     this.Op    = op;
 }
Example #2
0
        internal static ClassDefinition DoClassLookup(TopLevelConstruct currentContainer, Token nameToken, string name, bool failSilently)
        {
            TopLevelConstruct ex = currentContainer.FileScope.FileScopeEntityLookup.DoLookup(name, currentContainer);

            if (ex == null)
            {
                if (failSilently)
                {
                    return(null);
                }

                string message = "No class named '" + name + "' was found.";
                if (name.Contains("."))
                {
                    message += " Did you forget to import a library?";
                }
                throw new ParserException(nameToken, message);
            }

            if (ex is ClassDefinition)
            {
                return((ClassDefinition)ex);
            }

            // Still throw an exception if the found item is not a class. This is used by code to check if
            // something is a valid variable name or a class name. Colliding with something else is bad.
            throw new ParserException(nameToken, "This is not a class.");
        }
Example #3
0
        public ClassDefinition(
            Token classToken,
            Token nameToken,
            IList <Token> subclassTokens,
            IList <string> subclassNames,
            string ns,
            TopLevelConstruct owner,
            Library library,
            Token staticToken,
            Token finalToken,
            FileScope fileScope)
            : base(classToken, owner, fileScope)
        {
            this.Library = library;
            this.ClassID = ClassDefinition.classIdAlloc++;

            this.Namespace             = ns;
            this.NameToken             = nameToken;
            this.BaseClassTokens       = subclassTokens.ToArray();
            this.BaseClassDeclarations = subclassNames.ToArray();
            this.StaticToken           = staticToken;
            this.FinalToken            = finalToken;

            if (staticToken != null && this.BaseClassTokens.Length > 0)
            {
                throw new ParserException(staticToken, "Class cannot be static and have base classes or interfaces.");
            }
        }
Example #4
0
        public ConstructorDefinition(
            Token constructorToken,
            IList <Token> args,
            IList <Expression> defaultValues,
            IList <Expression> baseArgs,
            IList <Executable> code,
            Token baseToken,
            TopLevelConstruct owner)
            : base(constructorToken, owner, owner.FileScope)
        {
            this.IsDefault     = false;
            this.ArgNames      = args.ToArray();
            this.DefaultValues = defaultValues.ToArray();
            this.BaseArgs      = baseArgs.ToArray();
            this.Code          = code.ToArray();
            this.BaseToken     = baseToken;

            TODO.VerifyDefaultArgumentsAreAtTheEnd();

            this.MaxArgCount = this.ArgNames.Length;
            int minArgCount = 0;

            for (int i = 0; i < this.ArgNames.Length; ++i)
            {
                if (this.DefaultValues[i] == null)
                {
                    minArgCount++;
                }
                else
                {
                    break;
                }
            }
            this.MinArgCount = minArgCount;
        }
Example #5
0
 public DotStep(Expression root, Token dotToken, Token stepToken, TopLevelConstruct owner)
     : base(root.FirstToken, owner)
 {
     this.Root      = root;
     this.DotToken  = dotToken;
     this.StepToken = stepToken;
 }
Example #6
0
 public BracketIndex(Expression root, Token bracketToken, Expression index, TopLevelConstruct owner)
     : base(root.FirstToken, owner)
 {
     this.Root         = root;
     this.BracketToken = bracketToken;
     this.Index        = index;
 }
Example #7
0
 public EnumDefinition(Token enumToken, Token nameToken, string ns, TopLevelConstruct owner, Library library, FileScope fileScope)
     : base(enumToken, owner, fileScope)
 {
     this.Library   = library;
     this.NameToken = nameToken;
     this.Name      = nameToken.Value;
     this.Namespace = ns;
 }
Example #8
0
 public ConstStatement(Token constToken, Token nameToken, string ns, TopLevelConstruct owner, Library library, FileScope fileScope)
     : base(constToken, owner, fileScope)
 {
     this.Library   = library;
     this.NameToken = nameToken;
     this.Name      = nameToken.Value;
     this.Namespace = ns;
 }
Example #9
0
 public Executable(Token firstToken, TopLevelConstruct owner)
     : base(firstToken, owner)
 {
     if (owner == null)
     {
         throw new Exception(); // This should never happen.
     }
 }
Example #10
0
        public ConstructorDefinition(TopLevelConstruct owner) : base(null, owner, owner.FileScope)
        {
            this.IsDefault = true;

            this.Code          = new Executable[0];
            this.ArgNames      = new Token[0];
            this.DefaultValues = new Expression[0];
            this.BaseArgs      = new Expression[0];
            this.MaxArgCount   = 0;
            this.MinArgCount   = 0;
        }
        public CoreFunctionInvocation(Token firstToken, Expression[] originalArgs, TopLevelConstruct owner) :
            base(firstToken, owner)
        {
            if (originalArgs.Length == 0 || !(originalArgs[0] is StringConstant))
            {
                throw new ParserException(firstToken, "$$$ invocations must include a string constant containing the function name.");
            }

            this.FunctionId = CoreFunctionIDHelper.GetId((StringConstant)originalArgs[0]);
            List <Expression> args = new List <Expression>(originalArgs);

            args.RemoveAt(0);
            this.Args = args.ToArray();
        }
Example #12
0
        // pardon the double-plurals for the jaggy 2D arrays
        public TryStatement(
            Token tryToken,
            IList <Executable> tryBlock,
            List <Token> catchTokens,
            List <Token> exceptionVariableTokens,
            List <Token[]> catchBlockTypeTokenses, // tricksy tokenses
            List <string[]> catchBlockTypeses,
            List <Executable[]> catchBlockExecutableses,
            Token finallyToken,
            IList <Executable> finallyBlock,
            TopLevelConstruct owner) : base(tryToken, owner)
        {
            this.TryToken = tryToken;
            this.TryBlock = tryBlock.ToArray();

            int catchBlockCount = catchTokens.Count; // individual catch-related inputs are trusted to all have same length

            this.CatchBlocks = new CatchBlock[catchBlockCount];
            for (int i = 0; i < catchBlockCount; ++i)
            {
                Token        catchToken            = catchTokens[i];
                Token        variableName          = exceptionVariableTokens[i];
                Token[]      catchBlockTypeTokens  = catchBlockTypeTokenses[i];
                string[]     catchBlockTypes       = catchBlockTypeses[i];
                Executable[] catchBlockExecutables = catchBlockExecutableses[i];

                this.CatchBlocks[i] = new CatchBlock()
                {
                    CatchToken             = catchToken,
                    Code                   = catchBlockExecutables,
                    ExceptionVariableToken = variableName,
                    Types                  = catchBlockTypes,
                    TypeTokens             = catchBlockTypeTokens,
                    VariableLocalScopeId   = -1,
                };
            }

            this.FinallyToken = finallyToken;
            this.FinallyBlock = finallyBlock == null ? new Executable[0] : finallyBlock.ToArray();

            if (this.CatchBlocks.Length == 0 && this.FinallyBlock == null)
            {
                throw new ParserException(this.TryToken, "Cannot have a try block without a catch or finally block.");
            }
        }
Example #13
0
        internal override void PerformLocalIdAllocation(Parser parser, VariableIdAllocator varIds, VariableIdAllocPhase phase)
        {
            if ((phase & VariableIdAllocPhase.ALLOC) != 0)
            {
                this.LocalScopeId = varIds.GetVarId(this.FirstToken);
                if (this.LocalScopeId == -1)
                {
                    string name = this.FirstToken.Value;
                    if (parser.LibraryManager.IsValidLibraryName(parser, name))
                    {
                        throw new ParserException(this.FirstToken, "'" + name + "' is referenced but not imported in this file.");
                    }

                    TopLevelConstruct owner = this.Owner;
                    while (owner != null && !(owner is ClassDefinition))
                    {
                        owner = owner.Owner;
                    }

                    if (owner != null)
                    {
                        ClassDefinition cd = (ClassDefinition)owner;
                        foreach (FieldDeclaration fd in cd.Fields)
                        {
                            if (fd.NameToken.Value == name)
                            {
                                string message = "'" + name + "' is used like a local variable but it is " + (fd.IsStaticField ? "a static" : "an instance") + " field.";
                                message += " Did you mean '" + (fd.IsStaticField ? cd.NameToken.Value : "this") + "." + name + "' instead of '" + name + "'?";
                                throw new ParserException(this.FirstToken, message);
                            }
                        }
                    }

                    // TODO: But if it's being called like a function then...
                    // - give a better error message "function 'foo' is not defined"
                    // - give an even better error message when there's a class or instance function with the same name
                    //   e.g. "'foo' is a static function and must be invoked with the class name: FooClass.foo(...)
                    // - if there's a method, suggest using "this."
                    throw new ParserException(this.FirstToken, "The variable '" + name + "' is used but is never assigned to.");
                }
            }
        }
Example #14
0
 public FunctionDefinition(
     Token functionToken,
     Library library,
     TopLevelConstruct nullableOwner,
     bool isStaticMethod,
     Token nameToken,
     IList <Annotation> functionAnnotations,
     string namespyace,
     FileScope fileScope)
     : base(functionToken, nullableOwner, fileScope)
 {
     this.Library        = library;
     this.IsStaticMethod = isStaticMethod;
     this.Namespace      = namespyace;
     this.NameToken      = nameToken;
     this.annotations    = new Dictionary <string, Annotation>();
     foreach (Annotation annotation in functionAnnotations)
     {
         this.annotations[annotation.Type] = annotation;
     }
     this.MemberID = -1;
 }
Example #15
0
        public void ResolveBaseClasses()
        {
            List <ClassDefinition> baseClasses       = new List <ClassDefinition>();
            List <Token>           baseClassesTokens = new List <Token>();

            for (int i = 0; i < this.BaseClassDeclarations.Length; ++i)
            {
                string            value             = this.BaseClassDeclarations[i];
                Token             token             = this.BaseClassTokens[i];
                TopLevelConstruct baseClassInstance = this.FileScope.FileScopeEntityLookup.DoLookup(value, this);
                if (baseClassInstance == null)
                {
                    throw new ParserException(token, "No class named '" + token.Value + "' was found.");
                }

                if (baseClassInstance is ClassDefinition)
                {
                    baseClasses.Add((ClassDefinition)baseClassInstance);
                    baseClassesTokens.Add(token);
                }
                // TODO: else if (baseClassInstance is InterfaceDefinition) { ... }
                else
                {
                    throw new ParserException(token, "This is not a class.");
                }
            }

            if (baseClasses.Count > 1)
            {
                throw new ParserException(baseClassesTokens[1], "Multiple base classes found. Did you mean to use an interface?");
            }

            if (baseClasses.Count == 1)
            {
                this.BaseClass = baseClasses[0];
            }
        }
Example #16
0
 public Ternary(Expression condition, Expression trueValue, Expression falseValue, TopLevelConstruct owner)
     : base(condition.FirstToken, owner)
 {
     this.Condition  = condition;
     this.TrueValue  = trueValue;
     this.FalseValue = falseValue;
 }
Example #17
0
 public IsComparison(Expression root, Token isToken, Token firstClassToken, string classNameWithNamespace, TopLevelConstruct owner)
     : base(root.FirstToken, owner)
 {
     this.Expression = root;
     this.IsToken    = isToken;
     this.ClassToken = firstClassToken;
     this.ClassName  = classNameWithNamespace;
 }
Example #18
0
 public NullCoalescer(Expression primaryExpression, Expression secondaryExpression, TopLevelConstruct owner)
     : base(primaryExpression.FirstToken, owner)
 {
     this.PrimaryExpression   = primaryExpression;
     this.SecondaryExpression = secondaryExpression;
 }
Example #19
0
 public FloatConstant(Token startValue, double value, TopLevelConstruct owner)
     : base(startValue, owner)
 {
     this.Value = value;
 }
Example #20
0
 public Expression(Token firstToken, TopLevelConstruct owner)
     : base(firstToken, owner)
 {
     this.Annotations = null;
 }
Example #21
0
 public ForLoop(Token forToken, IList <Executable> init, Expression condition, IList <Executable> step, IList <Executable> code, TopLevelConstruct owner)
     : base(forToken, owner)
 {
     this.Init      = init.ToArray();
     this.Condition = condition ?? new BooleanConstant(forToken, true, owner);
     this.Step      = step.ToArray();
     this.Code      = code.ToArray();
 }
Example #22
0
 public IfStatement(Token ifToken, Expression condition, IList <Executable> trueCode, IList <Executable> falseCode, TopLevelConstruct owner)
     : base(ifToken, owner)
 {
     this.Condition = condition;
     this.TrueCode  = trueCode.ToArray();
     this.FalseCode = falseCode.ToArray();
 }
Example #23
0
 public NegativeSign(Token sign, Expression root, TopLevelConstruct owner)
     : base(sign, owner)
 {
     this.Root = root;
 }
Example #24
0
 public BooleanNot(Token bang, Expression root, TopLevelConstruct owner)
     : base(bang, owner)
 {
     this.Root = root;
 }
Example #25
0
 public WhileLoop(Token whileToken, Expression condition, IList <Executable> code, TopLevelConstruct owner)
     : base(whileToken, owner)
 {
     this.Condition = condition;
     this.Code      = code.ToArray();
 }
Example #26
0
 public Expression CloneValue(Token token, TopLevelConstruct owner)
 {
     return(new FloatConstant(token, this.Value, owner));
 }
Example #27
0
 public ContinueStatement(Token continueToken, TopLevelConstruct owner)
     : base(continueToken, owner)
 {
 }
Example #28
0
 public Namespace(Token namespaceToken, string name, TopLevelConstruct owner, Library library, FileScope fileScope)
     : base(namespaceToken, owner, fileScope)
 {
     this.Library = library;
     this.Name    = name;
 }
Example #29
0
        public ListSlice(Expression root, List <Expression> items, Token bracketToken, TopLevelConstruct owner)
            : base(root.FirstToken, owner)
        {
            this.Root         = root;
            this.BracketToken = bracketToken;
            if (items.Count == 2)
            {
                items.Add(new IntegerConstant(null, 1, owner));
            }

            if (items.Count != 3)
            {
                throw new Exception("Slices must have 2 or 3 components before passed into the constructor.");
            }

            if (items[2] == null)
            {
                items[2] = new IntegerConstant(null, 1, owner);
            }

            this.Items = items.ToArray();
        }
Example #30
0
 public ForEachLoop(Token forToken, Token iterationVariable, Expression iterationExpression, IList <Executable> code, TopLevelConstruct owner)
     : base(forToken, owner)
 {
     this.IterationVariable   = iterationVariable;
     this.IterationExpression = iterationExpression;
     this.Code = code.ToArray();
 }