Esempio n. 1
0
 public Method()
 {
     Name         = "";
     Visibility   = AccessVisibility.Private;
     Arguments    = new List <string>();
     Static       = false;
     Variables    = new Dictionary <string, Variant>();
     Instructions = new List <Instruction>();
     Position     = -1;
     Filename     = "";
     Line         = 0;
 }
Esempio n. 2
0
        private bool Const()
        {
            int peekIndex = 0;

            if (Parser.IsVisibility(TokenStream.Peek(peekIndex)))
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value != "const")
            {
                return(false);
            }

            AccessVisibility visibility = GetVisibility();

            TokenStream.Expect("const");

            do
            {
                string ident = TokenStream.ReadWord();
                if (IsField(ident) || IsMethod(ident) || IsProperty(ident) || IsConstant(ident) || IsEnumeration(ident))
                {
                    throw new CompilerException("Redeclaration of constant '{0}'.", ident);
                }

                if (Parser.IsReservedIdent(ident))
                {
                    throw new CompilerException("'{0}' is a reserved symbol.", ident);
                }

                TokenStream.Expect(TokenType.Delimiter, "=");
                Variant value = TokenStream.ReadVariant();
                TokenStream.Accept(TokenType.Delimiter, ";");

                currentClass.Constants.Add(new Field
                {
                    Visibility = visibility,
                    Name       = ident,
                    Value      = value
                });
            }while (TokenStream.Accept(TokenType.Delimiter, ","));

            return(true);
        }
Esempio n. 3
0
        private bool Property()
        {
            int peekIndex = 0;

            if (Parser.IsVisibility(TokenStream.Peek(peekIndex)))
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value != "property")
            {
                return(false);
            }

            AccessVisibility visibility = GetVisibility();

            TokenStream.Expect("property");

            string ident = TokenStream.ReadWord();

            if (IsField(ident) || IsMethod(ident) || IsProperty(ident) || IsConstant(ident) || IsEnumeration(ident))
            {
                throw new CompilerException("Redeclaration of property '{0}'.", ident);
            }

            if (Parser.IsReservedIdent(ident))
            {
                throw new CompilerException("'{0}' is a reserved symbol.", ident);
            }

            currentClass.Fields.Add(new Field
            {
                Name       = "__property_" + ident,
                Value      = new Variant(),
                Visibility = visibility
            });

            TokenStream.Expect(TokenType.Delimiter, "{");

            while (!TokenStream.Accept(TokenType.Delimiter, "}"))
            {
                AccessVisibility accessorVisibility = Parser.IsVisibility(TokenStream.Peek()) ? GetVisibility() : visibility;

                if (TokenStream.Accept(TokenType.Word, "get"))
                {
                    if (TokenStream.Accept(TokenType.Delimiter, ";"))
                    {
                        Method getAccessor = new Method
                        {
                            Name       = "__get_" + ident,
                            Visibility = accessorVisibility
                        };

                        getAccessor.Instructions.Add(new Instruction(Opcode.GetThis));
                        getAccessor.Instructions.Add(new Instruction(Opcode.ClassGetField, "__property_" + ident));
                        getAccessor.Instructions.Add(new Instruction(Opcode.Return));

                        currentClass.Methods.Add(getAccessor);
                    }
                    else if (TokenStream.Accept(TokenType.Delimiter, "{"))
                    {
                        Method getAccessor = new Method
                        {
                            Name       = "__get_" + ident,
                            Visibility = accessorVisibility,
                            Position   = TokenStream.Position - 1
                        };

                        currentClass.Methods.Add(getAccessor);

                        while (!TokenStream.Accept(TokenType.Delimiter, "}"))
                        {
                            TokenStream.Read();
                        }
                    }
                    else
                    {
                        TokenStream.Expected("; or body");
                    }
                }
                else if (TokenStream.Accept(TokenType.Word, "set"))
                {
                    if (TokenStream.Accept(TokenType.Delimiter, ";"))
                    {
                        Method setAccessor = new Method
                        {
                            Name       = "__set_" + ident,
                            Visibility = accessorVisibility,
                            Arguments  = new List <string> {
                                "value"
                            }
                        };

                        setAccessor.Instructions.Add(new Instruction(Opcode.GetThis));
                        setAccessor.Instructions.Add(new Instruction(Opcode.ClassSetField, "__property_" + ident));
                        setAccessor.Instructions.Add(new Instruction(Opcode.Return));

                        currentClass.Methods.Add(setAccessor);
                    }
                    else if (TokenStream.Accept(TokenType.Delimiter, "{"))
                    {
                        Method setAccessor = new Method
                        {
                            Name       = "__set_" + ident,
                            Visibility = accessorVisibility,
                            Arguments  = new List <string> {
                                "value"
                            },
                            Variables = new Dictionary <string, Variant> {
                                { "value", new Variant() }
                            },
                            Position = TokenStream.Position - 1
                        };

                        currentClass.Methods.Add(setAccessor);

                        while (!TokenStream.Accept(TokenType.Delimiter, "}"))
                        {
                            TokenStream.Read();
                        }
                    }
                    else
                    {
                        TokenStream.Expect("; or body");
                    }
                }
                else
                {
                    TokenStream.Expected("get or set accessor");
                }
            }

            return(true);
        }
Esempio n. 4
0
        private bool Enum()
        {
            int peekIndex = 0;

            if (Parser.IsVisibility(TokenStream.Peek(peekIndex)))
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value != "enum")
            {
                return(false);
            }

            Dictionary <string, long> values = new Dictionary <string, long>();
            long value = 0;

            AccessVisibility visibility = GetVisibility();

            TokenStream.Expect("enum");
            string ident = TokenStream.ReadWord();

            if (IsField(ident) || IsMethod(ident) || IsProperty(ident) || IsConstant(ident) || IsEnumeration(ident))
            {
                throw new CompilerException("Redeclaration of enum '{0}'.", ident);
            }

            if (Parser.IsReservedIdent(ident))
            {
                throw new CompilerException("'{0}' is a reserved symbol.", ident);
            }

            TokenStream.Expect(TokenType.Delimiter, "{");

            if (!TokenStream.Accept(TokenType.Delimiter, "}"))
            {
                do
                {
                    string itemIdent = TokenStream.ReadWord();
                    if (TokenStream.Accept(TokenType.Delimiter, "="))
                    {
                        Variant variant = TokenStream.ReadVariant();
                        if (variant.Type != VariantType.Int64)
                        {
                            TokenStream.Expected("enum value to be of integer type");
                        }

                        value = variant.IntValue;
                    }

                    values.Add(itemIdent, value++);
                }while (TokenStream.Accept(TokenType.Delimiter, ","));

                TokenStream.Expect(TokenType.Delimiter, "}");
            }

            currentClass.Enums.Add(new Enumeration
            {
                Name       = ident,
                Visibility = visibility,
                Values     = values
            });

            TokenStream.Accept(TokenType.Delimiter, ";");

            return(true);
        }
Esempio n. 5
0
        private bool Method()
        {
            int peekIndex = 0;

            if (Parser.IsVisibility(TokenStream.Peek(peekIndex)))
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value == "static")
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value != "method")
            {
                return(false);
            }

            AccessVisibility visibility = GetVisibility();
            bool             isStatic   = TokenStream.Accept(TokenType.Word, "static");

            TokenStream.Expect("method");

            string ident = TokenStream.Peek().Value == "("
                                                        ? IdentConstructor
                                                        : TokenStream.ReadWord();

            if (IsField(ident) || IsProperty(ident) || IsConstant(ident) || IsEnumeration(ident))
            {
                throw new CompilerException("Redeclaration of method '{0}'.", ident);
            }

            if (Parser.IsReservedIdent(ident))
            {
                throw new CompilerException("'{0}' is a reserved symbol.", ident);
            }

            Dictionary <string, Variant> arguments = new Dictionary <string, Variant>();

            TokenStream.Expect(TokenType.Delimiter, "(");
            if (!TokenStream.Accept(TokenType.Delimiter, ")"))
            {
                do
                {
                    TokenStream.Expect("var");
                    string  varIdent = TokenStream.ReadWord();
                    Variant varValue = ParseAnnotatedVariable();

                    if (TokenStream.Accept(TokenType.Delimiter, "="))
                    {
                        varValue = TokenStream.ReadVariant();
                    }

                    arguments.Add(varIdent, varValue);
                }while (TokenStream.Accept(TokenType.Delimiter, ","));

                TokenStream.Expect(TokenType.Delimiter, ")");
            }

            if (GetMethod(ident, arguments.Count, currentClass) != null)
            {
                throw new CompilerException("Ambiguous overload of method '{0}' declared. Original method declared on line {1}.",
                                            ident, GetMethod(ident, arguments.Count, currentClass).Line);
            }

            currentClass.Methods.Add(new Method
            {
                Visibility = visibility,
                Name       = ident,
                Variables  = arguments,
                Position   = TokenStream.Position,
                Static     = isStatic,
                Filename   = TokenStream.Filename,
                Line       = TokenStream.CurrentLine
            });

            foreach (var arg in arguments)
            {
                currentClass.Methods.Last().Arguments.Add(arg.Key);
            }

            TokenStream.Expect(TokenType.Delimiter, "{");
            int braceCount = 1;

            do
            {
                if (TokenStream.Accept(TokenType.Delimiter, "{"))
                {
                    ++braceCount;
                }
                else if (TokenStream.Accept(TokenType.Delimiter, "}"))
                {
                    --braceCount;
                }
                else
                {
                    TokenStream.Read();
                }
            }while (braceCount > 0 && !TokenStream.EndOfStream);

            if (braceCount > 0)
            {
                throw new CompilerException("Unmatched brace at end of file.");
            }

            return(true);
        }
Esempio n. 6
0
        private bool Field()
        {
            int peekIndex = 0;

            if (Parser.IsVisibility(TokenStream.Peek(peekIndex)))
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value == "static")
            {
                ++peekIndex;
            }

            if (TokenStream.Peek(peekIndex).Value != "var")
            {
                return(false);
            }

            AccessVisibility visibility = GetVisibility();
            bool             isStatic   = TokenStream.Accept(TokenType.Word, "static");

            TokenStream.Expect("var");

            do
            {
                string ident = TokenStream.ReadWord();
                if (IsField(ident) || IsMethod(ident) || IsProperty(ident) || IsConstant(ident) || IsEnumeration(ident))
                {
                    throw new CompilerException("Redeclaration of field '{0}'.", ident);
                }

                if (Parser.IsReservedIdent(ident))
                {
                    throw new CompilerException("'{0}' is a reserved symbol.", ident);
                }

                Variant value = ParseAnnotatedVariable();

                if (TokenStream.Accept(TokenType.Delimiter, "="))
                {
                    value = TokenStream.ReadVariant();
                }

                if (value.Type == VariantType.List)
                {
                    value.ObjectName = "__list";
                }

                currentClass.Fields.Add(new Field
                {
                    Name       = ident,
                    Visibility = visibility,
                    Value      = value,
                    Static     = isStatic
                });
            }while (TokenStream.Accept(TokenType.Delimiter, ","));

            TokenStream.Accept(TokenType.Delimiter, ";");

            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecurityResult"/> struct.
 /// </summary>
 /// <param name="accessGranted">if set to <c>true</c> [allowed].</param>
 /// <param name="visibility">The visibility.</param>
 public SecurityResult(bool accessGranted, AccessVisibility visibility)
 {
     AccessGranted = accessGranted;
     Visibility    = visibility;
 }