Beispiel #1
0
 public PreOpDeclaration(String keyword,
     bool delim, CodeBody body, VariableType returnType,
     FunctionParameter operand, List<Specifier> specs,
     SourcePosition start, SourcePosition end)
     : base(ASTNodeType.PrefixOperator, keyword, delim, body, returnType, specs, start, end)
 {
     Operand = operand;
 }
Beispiel #2
0
 public VariableDeclaration(VariableType type, List<Specifier> specs,
     List<VariableIdentifier> names, SourcePosition start, SourcePosition end)
     : base(ASTNodeType.VariableDeclaration, start, end)
 {
     Specifiers = specs;
     VarType = type;
     Variables = names;
 }
Beispiel #3
0
        // TODO: this is only for text decompiling, should extend to a full ast for modification.
        public Class ConvertClass()
        {
            VariableType parent;
            if (Object.SuperField != null)
                parent = new VariableType(Object.SuperField.Name, null, null);
            else
                parent = new VariableType("object", null, null);

            VariableType outer;
            if (Object.OuterClass != null)
                outer = new VariableType(Object.OuterClass.Name, null, null);
            else
                outer = new VariableType(parent.Name, null, null);
            // TODO: specifiers
            // TODO: operators
            // TODO: components
            // TODO: constants
            // TODO: interfaces

            var Types = new List<VariableType>();
            foreach (var member in Object.Structs)
                Types.Add(ConvertStruct(member));
            foreach (var member in Object.Enums)
                Types.Add(ConvertEnum(member));

            var Vars = new List<VariableDeclaration>();
            foreach (var member in Object.Variables)
                Vars.Add(ConvertVariable(member));

            var Funcs = new List<Function>();
            foreach (var member in Object.DefinedFunctions)
                Funcs.Add(ConvertFunction(member));

            var States = new List<State>();
            foreach (var member in Object.States)
                States.Add(ConvertState(member));

            AST = new Class(Object.Name, new List<Specifier>(), Vars, Types, Funcs,
                States, parent, outer, new List<OperatorDeclaration>(), null, null);

            // Ugly quick fix:
            foreach (var member in Types)
                member.Outer = AST;
            foreach (var member in Vars)
                member.Outer = AST;
            foreach (var member in Funcs)
                member.Outer = AST;
            foreach (var member in States)
                member.Outer = AST;

            var propObject = PCC.GetExportObject(Object.DefaultPropertyIndex);
            if (propObject != null && propObject.DefaultProperties != null && propObject.DefaultProperties.Count != 0)
                AST.DefaultProperties = ConvertDefaultProperties(propObject.DefaultProperties);

            return AST;
        }
Beispiel #4
0
 public InOpDeclaration(String keyword, int precedence,
 bool delim, CodeBody body, VariableType returnType,
 FunctionParameter leftOp, FunctionParameter rightOp,
 List<Specifier> specs, SourcePosition start, SourcePosition end)
     : base(ASTNodeType.InfixOperator, keyword, delim, body, returnType, specs, start, end)
 {
     LeftOperand = leftOp;
     RightOperand = rightOp;
     Precedence = precedence;
 }
Beispiel #5
0
 public Struct(String name, List<Specifier> specs,
     List<VariableDeclaration> members,
     SourcePosition start, SourcePosition end, VariableType parent = null)
     : base(name, start, end)
 {
     Type = ASTNodeType.Struct;
     Specifiers = specs;
     Members = members;
     Parent = parent;
 }
Beispiel #6
0
 public OperatorDeclaration(ASTNodeType type, String keyword, 
     bool delim, CodeBody body, VariableType returnType,
     List<Specifier> specs, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
     OperatorKeyword = keyword;
     isDelimiter = delim;
     Body = body;
     ReturnType = returnType;
     Specifiers = specs;
     Locals = new List<VariableDeclaration>();
 }
Beispiel #7
0
 public Function(String name, VariableType returntype, CodeBody body,
     List<Specifier> specs, List<FunctionParameter> parameters,
     SourcePosition start, SourcePosition end)
     : base(ASTNodeType.Function, start, end)
 {
     Name = name;
     Body = body;
     ReturnType = returntype;
     Specifiers = specs;
     Parameters = parameters;
     Locals = new List<VariableDeclaration>();
 }
Beispiel #8
0
        public bool GetInOperator(out InOpDeclaration op, String name, VariableType lhs, VariableType rhs)
        {
            op = null;
            var lookup = Operators.FirstOrDefault(opdecl => opdecl.Value.Type == ASTNodeType.InfixOperator && opdecl.Value.OperatorKeyword == name
                && (opdecl.Value as InOpDeclaration).LeftOperand.VarType.Name.ToLower() == lhs.Name.ToLower()
                && (opdecl.Value as InOpDeclaration).RightOperand.VarType.Name.ToLower() == rhs.Name.ToLower());
            if (lookup.Equals(new KeyValuePair<String, OperatorDeclaration>()))
                return false;

            op = lookup.Value as InOpDeclaration;
            return true;
        }
Beispiel #9
0
 public Class(String name, List<Specifier> specs, 
     List<VariableDeclaration> vars, List<VariableType> types, List<Function> funcs,
     List<State> states, VariableType parent, VariableType outer, List<OperatorDeclaration> ops,
     SourcePosition start, SourcePosition end)
     : base(name, start, end)
 {
     Parent = parent;
     OuterClass = outer;
     Specifiers = specs;
     VariableDeclarations = vars;
     TypeDeclarations = types;
     Functions = funcs;
     States = states;
     Operators = ops;
     Type = ASTNodeType.Class;
 }
Beispiel #10
0
 public bool VisitNode(VariableType node)
 {
     Append(node.Name);
     return true;
 }
Beispiel #11
0
 public CastExpression(VariableType type, Expression expr, SourcePosition start, SourcePosition end)
     : base(ASTNodeType.CastExpression, start, end)
 {
     CastType = type;
     CastTarget = expr;
 }
Beispiel #12
0
        public void BasicClassTest()
        {
            var source =
                "class Test Deprecated Transient; \n" +
                "var enum ETestnumeration {\n" +
                "     TEST_value1,\n" +
                "     TEST_value2,\n" +
                "     TEST_value3,\n" +
                "} inlineNumeration, testnum2;\n" +
                "var private deprecated int X; \n" +
                "VAR INT Y, Z; \n" +
                "var ETestnumeration testnum;\n" +
                "struct transient testStruct\n" +
                "{ var float a, b, c; };\n" +
                "var private struct transient twoStruct extends testStruct\n" +
                "{\n" +
                "   var etestnumeration num;\n" +
                "} structA, structB;\n" +
                "function float funcB( testStruct one, float two ) \n" +
                "{\n" +
                "   local float c;" +
                "   one.a = 1.3 * c;" +
                "   while (true)" +
                "   {" +
                "       c = c - c - c;" +
                "   }" +
                "   if (false) {" +
                "       switch(one.a) {" +
                "           case one.a:" +
                "               c = one.a;" +
                "               break;" +
                "           case 1.2:" +
                "           case 1.3:" +
                "               c = c + 0.5;" +
                "               break;" +
                "           default:" +
                "               c = 6.6;" +
                "       }" +
                "   }" +
                "   return one.a + 0.33 * (0.66 + 0.1) * 1.5;\n" +
                "}\n" +
                "private simulated function float MyFunc( out testStruct one, coerce optional float two ) \n" +
                "{\n" +
                "   return one.b + funcB(one, two);\n" +
                "}\n" +
                "auto state MyState\n" +
                "{\n" +
                "ignores MyFunc;\n" +
                "function StateFunc()\n" +
                "{\n" +
                "}\n" +
                "\n" +
                "Begin:\n" +
                "       moredragons\n" +
                "}\n" +
                "\n" +
                "final static operator(254) int >>>( coerce float left, coerce float right )\n" +
                "{\n" +
                "   all the dragons\n" +
                "}\n" +
                "\n" +
                "\n" +
                "\n";

            var parser = new ClassOutlineParser(new TokenStream<String>(new StringLexer(source)), log);
            var symbols = new SymbolTable();

            Class obj = new Class("Object", null, null, null, null, null, null, null, null, null, null);
            obj.OuterClass = obj;
            symbols.PushScope(obj.Name);
            symbols.AddSymbol(obj.Name, obj);

            VariableType integer = new VariableType("int", null, null);
            symbols.AddSymbol(integer.Name, integer);
            VariableType floatingpoint = new VariableType("float", null, null);
            symbols.AddSymbol(floatingpoint.Name, floatingpoint);

            InOpDeclaration plus_float = new InOpDeclaration("+", 20, false, null, floatingpoint, new FunctionParameter(floatingpoint, null, null, null, null),
                new FunctionParameter(floatingpoint, null, null, null, null), null, null, null);
            symbols.AddOperator(plus_float);

            InOpDeclaration sub_float = new InOpDeclaration("-", 20, false, null, floatingpoint, new FunctionParameter(floatingpoint, null, null, null, null),
                new FunctionParameter(floatingpoint, null, null, null, null), null, null, null);
            symbols.AddOperator(sub_float);

            InOpDeclaration mult_float = new InOpDeclaration("*", 16, false, null, floatingpoint, new FunctionParameter(floatingpoint, null, null, null, null),
                new FunctionParameter(floatingpoint, null, null, null, null), null, null, null);
            symbols.AddOperator(mult_float);

            Class node = (Class)parser.ParseDocument();
            var ClassValidator = new ClassValidationVisitor(log, symbols);
            node.AcceptVisitor(ClassValidator);

            symbols.GoDirectlyToStack(node.GetInheritanceString());
            foreach (Function f in node.Functions)
            {
                symbols.PushScope(f.Name);
                var p = new CodeBodyParser(new TokenStream<String>(new StringLexer(source)), f.Body, symbols, f, log);
                var b = p.ParseBody();
                symbols.PopScope();
            }

            var CodeBuilder = new CodeBuilderVisitor();
            node.AcceptVisitor(CodeBuilder);
            Console.Write(CodeBuilder.GetCodeString());

            Assert.IsTrue(log.AllErrors.Count == 0);

            return;
        }
Beispiel #13
0
 public Variable(List<Specifier> specs, VariableIdentifier name,
     VariableType type, SourcePosition start, SourcePosition end)
     : base(type, specs, new List<VariableIdentifier> { name }, start, end)
 {
     Type = ASTNodeType.Variable;
 }
Beispiel #14
0
 public FunctionParameter(VariableType type, List<Specifier> specs,
     VariableIdentifier variable, SourcePosition start, SourcePosition end)
     : base(specs, variable, type, start, end)
 {
     Type = ASTNodeType.FunctionParameter;
 }
Beispiel #15
0
 public ObjectLiteral(NameLiteral objectName, VariableType @class = null, SourcePosition start = null, SourcePosition end = null) : base(ASTNodeType.ObjectLiteral, start, end)
 {
     Class = @class;
     Name  = objectName;
 }
Beispiel #16
0
 private bool TypeEquals(VariableType a, VariableType b)
 {
     return a.Name.ToLower() == b.Name.ToLower();
 }
Beispiel #17
0
 public StaticArrayType(VariableType elementType, int size, SourcePosition start = null, SourcePosition end = null) : base(elementType.Name, start, end)
 {
     ElementType = elementType;
     Size        = size;
 }
 public bool VisitNode(VariableType node)
 {
     // This should never be called.
     throw new NotImplementedException();
 }
Beispiel #19
0
 public ClassType(VariableType classLimiter, SourcePosition start = null, SourcePosition end = null) : base(CLASS, start, end, EPropertyType.Object)
 {
     ClassLimiter = classLimiter;
 }
Beispiel #20
0
        public Class TryParseClass()
        {
            Func<ASTNode> classParser = () =>
                {
                    if (Tokens.ConsumeToken(TokenType.Class) == null)
                        return Error("Expected class declaration!");

                    var name = Tokens.ConsumeToken(TokenType.Word);
                    if (name == null)
                        return Error("Expected class name!");

                    var parentClass = TryParseParent();
                    if (parentClass == null)
                    {
                        Log.LogMessage("No parent class specified for " + name.Value + ", interiting from Object");
                        parentClass = new VariableType("Object", null, null);
                    }

                    var outerClass = TryParseOuter();

                    var specs = ParseSpecifiers(GlobalLists.ClassSpecifiers);

                    if (Tokens.ConsumeToken(TokenType.SemiColon) == null)
                        return Error("Expected semi-colon!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1));

                    var variables = new List<VariableDeclaration>();
                    var types = new List<VariableType>();
                    while (CurrentTokenType == TokenType.InstanceVariable
                        || CurrentTokenType == TokenType.Struct
                        || CurrentTokenType == TokenType.Enumeration)
                    {
                        if (CurrentTokenType == TokenType.InstanceVariable)
                        {
                            var variable = TryParseVarDecl();
                            if (variable == null)
                                return Error("Malformed instance variable!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1));

                            variables.Add(variable);
                        }
                        else
                        {
                            var type = TryParseEnum() ?? TryParseStruct() ?? new VariableType("INVALID", null, null);
                            if (type.Name == "INVALID")
                                return Error("Malformed type declaration!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1));

                            types.Add(type);

                            if (Tokens.ConsumeToken(TokenType.SemiColon) == null)
                                return Error("Expected semi-colon!", CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1));
                        }
                    }

                    List<Function> funcs = new List<Function>();
                    List<State> states = new List<State>();
                    List<OperatorDeclaration> ops = new List<OperatorDeclaration>();
                    ASTNode declaration;
                    do
                    {
                        declaration = (ASTNode)TryParseFunction() ??
                                        (ASTNode)TryParseOperatorDecl() ??
                                        (ASTNode)TryParseState() ??
                                        (ASTNode)null;
                        if (declaration == null && !Tokens.AtEnd())
                            return Error("Expected function/state/operator declaration!",
                                CurrentPosition, CurrentPosition.GetModifiedPosition(0, 1, 1));

                        if (declaration.Type == ASTNodeType.Function)
                            funcs.Add((Function)declaration);
                        else if (declaration.Type == ASTNodeType.State)
                            states.Add((State)declaration);
                        else
                            ops.Add((OperatorDeclaration)declaration);
                    } while (!Tokens.AtEnd());

                    // TODO: should AST-nodes accept null values? should they make sure they dont present any?
                    return new Class(name.Value, specs, variables, types, funcs, states, parentClass, outerClass, ops, name.StartPosition, name.EndPosition);
                };
            return (Class)Tokens.TryGetTree(classParser);
        }
Beispiel #21
0
 public DynamicArrayType(VariableType elementType, SourcePosition start = null, SourcePosition end = null) : base(elementType.Name, start, end)
 {
     ElementType = elementType;
 }