Exemplo n.º 1
0
        private static Expression ParseBaseExpression(Scanner scanner)
        {
            int    intvalue;
            string strvalue;

            if (scanner.TryGetIdentifier("null"))
            {
                return(new NullExpression());
            }
            else if (scanner.TryGetIdentifier("true"))
            {
                return(new TrueExpression());
            }
            else if (scanner.TryGetIdentifier("false"))
            {
                return(new FalseExpression());
            }
            else if (scanner.TryGetIdentifier("new"))
            {
                scanner.GetIdentifier(out strvalue);
                //return new NewExpression(strvalue);
                throw new NotImplementedException();
            }
            else if (scanner.TryGetIdentifier(out strvalue))
            {
                return(new VariableExpression(strvalue));
            }
            else if (scanner.TryGetInteger(out intvalue))
            {
                return(new IntegerLiteralExpression(intvalue));
            }
            else if (scanner.TryGetString(out strvalue))
            {
                return(new StringLiteralExpression(strvalue));
            }
            else if (scanner.TryGetSpecial(out strvalue))
            {
                return(new EnvironmentExpression(strvalue));
            }
            else if (scanner.TryGetDelimiter("("))
            {
                Expression expression = ParseExpression(scanner);
                scanner.GetDelimiter(")");
                return(expression);
            }
            else
            {
                throw new ApplicationException();
            }
        }
Exemplo n.º 2
0
        private static Expression ParsePostfixExpression(Scanner scanner)
        {
            Expression inner = ParseBaseExpression(scanner);

            while (true)
            {
                if (scanner.TryGetDelimiter("++"))
                {
                    //inner = new PostfixIncrementExpression(inner);
                    throw new NotImplementedException();
                }
                else if (scanner.TryGetDelimiter("--"))
                {
                    //inner = new PostfixDecrementExpression(inner);
                    throw new NotImplementedException();
                }
                else if (scanner.TryGetDelimiter("("))
                {
                    List <Expression> arguments = new List <Expression>();
                    if (!scanner.TryGetDelimiter(")"))
                    {
                        do
                        {
                            arguments.Add(ParseExpression(scanner));
                        }while (scanner.TryGetDelimiter(","));
                        scanner.GetDelimiter(")");
                    }
                    inner = new CallExpression(inner, arguments.ToArray());
                }
                else if (scanner.TryGetDelimiter("["))
                {
                    throw new NotImplementedException();
                }
                else if (scanner.TryGetDelimiter("->"))
                {
                    string member;
                    scanner.GetIdentifier(out member);
                    inner = new MemberExpression(inner, member);
                }
                else
                {
                    return(inner);
                }
            }
        }
Exemplo n.º 3
0
 public static Definition ParseDefinition(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("function"))
     {
         string returntype;
         string name;
         List<string> parametertypes = new List<string>();
         List<string> parameternames = new List<string>();
         scanner.GetIdentifier(out returntype);
         scanner.GetIdentifier(out name);
         scanner.GetDelimiter("(");
         if (!scanner.TryGetDelimiter(")"))
         {
             do
             {
                 string parametertype;
                 string parametername;
                 scanner.GetIdentifier(out parametertype);
                 scanner.GetIdentifier(out parametername);
                 parametertypes.Add(parametertype);
                 parameternames.Add(parametername);
             }
             while (scanner.TryGetDelimiter(","));
             scanner.GetDelimiter(")");
         }
         scanner.GetDelimiter("{");
         Statement statement = CodeParser.ParseBlock(scanner.ScanAcross("{","}"));
         return new FunctionDefinition(name,returntype,parametertypes.ToArray(),parameternames.ToArray(),statement);
     }
     else if (scanner.TryGetIdentifier("linedefspecial"))
     {
         List<LinedefSpecialAttribute> attributes = new List<LinedefSpecialAttribute>();
         do
         {
             int number;
             scanner.GetInteger(out number);
             LinedefSpecialAttribute attribute = new LinedefSpecialAttribute(number);
             scanner.GetDelimiter("(");
             while (!scanner.TryGetDelimiter(")"))
             {
                 if (scanner.TryGetIdentifier("auto"))
                     attribute.ActivationType |= LinedefActivationType.Automatic;
                 else if (scanner.TryGetIdentifier("use"))
                     attribute.ActivationType |= LinedefActivationType.Use;
                 else if (scanner.TryGetIdentifier("monsteruse"))
                     attribute.ActivationType |= LinedefActivationType.MonsterUse;
                 else if (scanner.TryGetIdentifier("cross"))
                     attribute.ActivationType |= LinedefActivationType.Cross;
                 else if (scanner.TryGetIdentifier("monstercross"))
                     attribute.ActivationType |= LinedefActivationType.MonsterCross;
                 else if (scanner.TryGetIdentifier("shoot"))
                     attribute.ActivationType |= LinedefActivationType.Shoot;
                 else if (scanner.TryGetIdentifier("monstershoot"))
                     attribute.ActivationType |= LinedefActivationType.MonsterShoot;
                 else if (scanner.TryGetIdentifier("repeatable"))
                     attribute.Repeatable = true;
                 else
                     throw new ApplicationException();
             }
             attributes.Add(attribute);
         }
         while (scanner.TryGetDelimiter(","));
         scanner.GetDelimiter("{");
         Statement statement = CodeParser.ParseBlock(scanner.ScanAcross("{","}"));
         return new LinedefSpecialDefinition(statement,attributes);
     }
     else
     {
         throw new ApplicationException();
     }
 }
Exemplo n.º 4
0
 private static Expression ParseBaseExpression(Scanner scanner)
 {
     int intvalue;
     string strvalue;
     if (scanner.TryGetIdentifier("null"))
     {
         return new NullExpression();
     }
     else if (scanner.TryGetIdentifier("true"))
     {
         return new TrueExpression();
     }
     else if (scanner.TryGetIdentifier("false"))
     {
         return new FalseExpression();
     }
     else if (scanner.TryGetIdentifier("new"))
     {
         scanner.GetIdentifier(out strvalue);
         //return new NewExpression(strvalue);
         throw new NotImplementedException();
     }
     else if (scanner.TryGetIdentifier(out strvalue))
     {
         return new VariableExpression(strvalue);
     }
     else if (scanner.TryGetInteger(out intvalue))
     {
         return new IntegerLiteralExpression(intvalue);
     }
     else if (scanner.TryGetString(out strvalue))
     {
         return new StringLiteralExpression(strvalue);
     }
     else if (scanner.TryGetSpecial(out strvalue))
     {
         return new EnvironmentExpression(strvalue);
     }
     else if (scanner.TryGetDelimiter("("))
     {
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         return expression;
     }
     else
     {
         throw new ApplicationException();
     }
 }
Exemplo n.º 5
0
 private static Expression ParsePostfixExpression(Scanner scanner)
 {
     Expression inner = ParseBaseExpression(scanner);
     while (true)
     {
         if (scanner.TryGetDelimiter("++"))
         {
             //inner = new PostfixIncrementExpression(inner);
             throw new NotImplementedException();
         }
         else if (scanner.TryGetDelimiter("--"))
         {
             //inner = new PostfixDecrementExpression(inner);
             throw new NotImplementedException();
         }
         else if (scanner.TryGetDelimiter("("))
         {
             List<Expression> arguments = new List<Expression>();
             if (!scanner.TryGetDelimiter(")"))
             {
                 do
                 {
                     arguments.Add(ParseExpression(scanner));
                 }
                 while (scanner.TryGetDelimiter(","));
                 scanner.GetDelimiter(")");
             }
             inner = new CallExpression(inner,arguments.ToArray());
         }
         else if (scanner.TryGetDelimiter("["))
         {
             throw new NotImplementedException();
         }
         else if (scanner.TryGetDelimiter("->"))
         {
             string member;
             scanner.GetIdentifier(out member);
             inner = new MemberExpression(inner,member);
         }
         else
         {
             return inner;
         }
     }
 }
Exemplo n.º 6
0
 private static Statement ParseStatement(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("if"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement thenstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new IfStatement(condition,thenstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("while"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new WhileStatement(condition,loopstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("foreach"))
     {
         scanner.GetDelimiter("(");
         string variabletype;
         string variablename;
         scanner.GetIdentifier(out variabletype);
         scanner.GetIdentifier(out variablename);
         scanner.GetIdentifier("in");
         Expression collection = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return new ForEachStatement(variabletype,variablename,collection,loopstatement,elsestatement);
     }
     else if (scanner.TryGetIdentifier("var"))
     {
         string variablename;
         scanner.GetIdentifier(out variablename);
         scanner.GetDelimiter("=");
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return new VarStatement(variablename,expression);
     }
     else if (scanner.TryGetIdentifier("return"))
     {
         if (scanner.TryGetDelimiter(";"))
         {
             return new ReturnStatement(null);
         }
         else
         {
             Expression expression = ParseExpression(scanner);
             scanner.GetDelimiter(";");
             return new ReturnStatement(expression);
         }
     }
     else if (scanner.TryGetDelimiter("{"))
     {
         return CodeParser.ParseBlock(scanner.ScanAcross("{","}"));
     }
     else if (scanner.TryGetDelimiter(";"))
     {
         return new NullStatement();
     }
     else
     {
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return new ExpressionStatement(expression);
     }
 }
Exemplo n.º 7
0
 public static Definition ParseDefinition(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("function"))
     {
         string        returntype;
         string        name;
         List <string> parametertypes = new List <string>();
         List <string> parameternames = new List <string>();
         scanner.GetIdentifier(out returntype);
         scanner.GetIdentifier(out name);
         scanner.GetDelimiter("(");
         if (!scanner.TryGetDelimiter(")"))
         {
             do
             {
                 string parametertype;
                 string parametername;
                 scanner.GetIdentifier(out parametertype);
                 scanner.GetIdentifier(out parametername);
                 parametertypes.Add(parametertype);
                 parameternames.Add(parametername);
             }while (scanner.TryGetDelimiter(","));
             scanner.GetDelimiter(")");
         }
         scanner.GetDelimiter("{");
         Statement statement = CodeParser.ParseBlock(scanner.ScanAcross("{", "}"));
         return(new FunctionDefinition(name, returntype, parametertypes.ToArray(), parameternames.ToArray(), statement));
     }
     else if (scanner.TryGetIdentifier("linedefspecial"))
     {
         List <LinedefSpecialAttribute> attributes = new List <LinedefSpecialAttribute>();
         do
         {
             int number;
             scanner.GetInteger(out number);
             LinedefSpecialAttribute attribute = new LinedefSpecialAttribute(number);
             scanner.GetDelimiter("(");
             while (!scanner.TryGetDelimiter(")"))
             {
                 if (scanner.TryGetIdentifier("auto"))
                 {
                     attribute.ActivationType |= LinedefActivationType.Automatic;
                 }
                 else if (scanner.TryGetIdentifier("use"))
                 {
                     attribute.ActivationType |= LinedefActivationType.Use;
                 }
                 else if (scanner.TryGetIdentifier("monsteruse"))
                 {
                     attribute.ActivationType |= LinedefActivationType.MonsterUse;
                 }
                 else if (scanner.TryGetIdentifier("cross"))
                 {
                     attribute.ActivationType |= LinedefActivationType.Cross;
                 }
                 else if (scanner.TryGetIdentifier("monstercross"))
                 {
                     attribute.ActivationType |= LinedefActivationType.MonsterCross;
                 }
                 else if (scanner.TryGetIdentifier("shoot"))
                 {
                     attribute.ActivationType |= LinedefActivationType.Shoot;
                 }
                 else if (scanner.TryGetIdentifier("monstershoot"))
                 {
                     attribute.ActivationType |= LinedefActivationType.MonsterShoot;
                 }
                 else if (scanner.TryGetIdentifier("repeatable"))
                 {
                     attribute.Repeatable = true;
                 }
                 else
                 {
                     throw new ApplicationException();
                 }
             }
             attributes.Add(attribute);
         }while (scanner.TryGetDelimiter(","));
         scanner.GetDelimiter("{");
         Statement statement = CodeParser.ParseBlock(scanner.ScanAcross("{", "}"));
         return(new LinedefSpecialDefinition(statement, attributes));
     }
     else
     {
         throw new ApplicationException();
     }
 }
Exemplo n.º 8
0
 private static Statement ParseStatement(Scanner scanner)
 {
     if (scanner.TryGetIdentifier("if"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement thenstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new IfStatement(condition, thenstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("while"))
     {
         scanner.GetDelimiter("(");
         Expression condition = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new WhileStatement(condition, loopstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("foreach"))
     {
         scanner.GetDelimiter("(");
         string variabletype;
         string variablename;
         scanner.GetIdentifier(out variabletype);
         scanner.GetIdentifier(out variablename);
         scanner.GetIdentifier("in");
         Expression collection = ParseExpression(scanner);
         scanner.GetDelimiter(")");
         Statement loopstatement = ParseStatement(scanner);
         Statement elsestatement = new NullStatement();
         if (scanner.TryGetIdentifier("else"))
         {
             elsestatement = ParseStatement(scanner);
         }
         return(new ForEachStatement(variabletype, variablename, collection, loopstatement, elsestatement));
     }
     else if (scanner.TryGetIdentifier("var"))
     {
         string variablename;
         scanner.GetIdentifier(out variablename);
         scanner.GetDelimiter("=");
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return(new VarStatement(variablename, expression));
     }
     else if (scanner.TryGetIdentifier("return"))
     {
         if (scanner.TryGetDelimiter(";"))
         {
             return(new ReturnStatement(null));
         }
         else
         {
             Expression expression = ParseExpression(scanner);
             scanner.GetDelimiter(";");
             return(new ReturnStatement(expression));
         }
     }
     else if (scanner.TryGetDelimiter("{"))
     {
         return(CodeParser.ParseBlock(scanner.ScanAcross("{", "}")));
     }
     else if (scanner.TryGetDelimiter(";"))
     {
         return(new NullStatement());
     }
     else
     {
         Expression expression = ParseExpression(scanner);
         scanner.GetDelimiter(";");
         return(new ExpressionStatement(expression));
     }
 }