Esempio n. 1
0
        public override Statement visit(VariableStatement variableStatement)
        {
            ArrayList statements = new ArrayList(0);

            for (int i = 0; i < variableStatement.declarations.Length; i++)
            {
                Expression expression = visitExpression(variableStatement.declarations[i]);

                if (expression != null)
                {
                    Statement statement = new ExpressionStatement(expression);
                    statement.setLineNumber(variableStatement.getLineNumber());
                    statements.Add(statement);
                }
            }

            if (statements.Count == 0)
            {
                return(new EmptyStatement());
            }
            else if (statements.Count == 1)
            {
                return((ExpressionStatement)statements[0]);
            }
            else
            {
                return(new BlockStatement(CompilerUtil.vectorToStatementArray(statements)));
            }
        }
Esempio n. 2
0
  //
  // program
  //

  public virtual ProgramNode parseProgram() {
    ArrayList statementVector = new ArrayList();

	statementVector.Add(parseSourceElement());

    while (nextToken != Token.EOF) {
	  statementVector.Add(parseSourceElement());
    }

    return new ProgramNode(CompilerUtil.vectorToStatementArray(statementVector));
  }
Esempio n. 3
0
  private Statement parseBlockStatement() {
    readToken(Token.OPERATOR_OPENBRACE);

	ArrayList vector = new ArrayList();
    while (nextToken != Token.OPERATOR_CLOSEBRACE) {
	  vector.Add(parseStatement());
    }

    readToken(Token.OPERATOR_CLOSEBRACE);

    return new BlockStatement(CompilerUtil.vectorToStatementArray(vector));
  }
Esempio n. 4
0
        public override ProgramNode visit(ProgramNode program)
        {
            ArrayList oldFunctionVector = functionVector;

            functionVector = new ArrayList();

            program = base.visit(program);

            program.functions = CompilerUtil.vectorToStatementArray(functionVector);

            functionVector = oldFunctionVector;

            return(program);
        }
Esempio n. 5
0
  private Statement parseSwitchStatement() {
	ArrayList caseClauseVector = new ArrayList();
	bool defaultSeen = false;

    readToken(Token.KEYWORD_SWITCH);
    readToken(Token.OPERATOR_OPENPAREN);
    Expression switchExpression = parseExpression(true);
    readToken(Token.OPERATOR_CLOSEPAREN);

    readToken(Token.OPERATOR_OPENBRACE);

    while (nextToken != Token.OPERATOR_CLOSEBRACE) {
      Expression caseExpression = null;
	  ArrayList caseStatements = new ArrayList();

      if (nextToken == Token.KEYWORD_CASE) {
        readToken(Token.KEYWORD_CASE);
        caseExpression = parseExpression(true);
        readToken(Token.OPERATOR_COLON);
      } else {
        if (defaultSeen == false) {
          defaultSeen = true;
        } else {
          throwCompilerException("duplication default clause in switch statement");
        }

        readToken(Token.KEYWORD_DEFAULT);
        caseExpression = null;
        readToken(Token.OPERATOR_COLON);
      }

      while (nextToken != Token.KEYWORD_CASE
          && nextToken != Token.KEYWORD_DEFAULT
          && nextToken != Token.OPERATOR_CLOSEBRACE) {
	    caseStatements.Add(parseStatement());
      }

	  caseClauseVector.Add(
          new CaseStatement(caseExpression, CompilerUtil.vectorToStatementArray(caseStatements)));
    }

    readToken(Token.OPERATOR_CLOSEBRACE);

	CaseStatement[] caseClauseArray = new CaseStatement[caseClauseVector.Count];

	caseClauseVector.CopyTo(caseClauseArray);

    return new SwitchStatement(switchExpression, caseClauseArray);
  }
Esempio n. 6
0
  private Expression[] parseArgumentList() {
    ArrayList argumentVector = new ArrayList();

    readToken(Token.OPERATOR_OPENPAREN);

    if (nextToken != Token.OPERATOR_CLOSEPAREN) {
	  argumentVector.Add(parseAssignmentExpression(true));
      while (nextToken == Token.OPERATOR_COMMA) {
        readToken(Token.OPERATOR_COMMA);
		argumentVector.Add(parseAssignmentExpression(true));
      }
    }

    readToken(Token.OPERATOR_CLOSEPAREN);

    return CompilerUtil.vectorToExpressionArray(argumentVector);
  }
Esempio n. 7
0
  private Statement parseVariableStatement() {
    ArrayList declarationVector = new ArrayList();

    readToken(Token.KEYWORD_VAR);

    // there must be at least one variable declaration

	declarationVector.Add(parseVariableDeclaration(true));
    while (nextToken == Token.OPERATOR_COMMA) {
      readToken(Token.OPERATOR_COMMA);
	  declarationVector.Add(parseVariableDeclaration(true));
    }

    readTokenSemicolon();

    return new VariableStatement(CompilerUtil.vectorToDeclarationArray(declarationVector));
  }
Esempio n. 8
0
  private FunctionLiteral parseFunctionLiteral(bool nameFlag) {
    Identifier name = null;
	ArrayList parameterVector = new ArrayList();
	ArrayList statementVector = new ArrayList();

    readToken(Token.KEYWORD_FUNCTION);

    if (nameFlag || nextToken != Token.OPERATOR_OPENPAREN) {
      name = parseIdentifier();
    }

    // function literals may have zero or more parameters.

    readToken(Token.OPERATOR_OPENPAREN);

    if (nextToken != Token.OPERATOR_CLOSEPAREN) {
	  parameterVector.Add(parseIdentifier());

      while (nextToken != Token.OPERATOR_CLOSEPAREN) {
        readToken(Token.OPERATOR_COMMA);
		parameterVector.Add(parseIdentifier());
      }
    }

    readToken(Token.OPERATOR_CLOSEPAREN);

    // function literals are required the have at least one SourceElement.

    readToken(Token.OPERATOR_OPENBRACE);

    // statementVector.addElement(parseStatement());

    while (nextToken != Token.OPERATOR_CLOSEBRACE) {
      statementVector.Add(parseSourceElement());
    }

    readToken(Token.OPERATOR_CLOSEBRACE);

    return new FunctionLiteral(name,
        CompilerUtil.vectorToIdentifierArray(parameterVector),
        CompilerUtil.vectorToStatementArray(statementVector)
    );
  }
Esempio n. 9
0
        public override Expression visit(FunctionLiteral literal)
        {
            ArrayList oldFunctionVector       = functionVector;
            ArrayList oldVariableVector       = variableVector;
            bool      oldHasWithStatement     = hasWithStatement;
            bool      oldHasArgumentsVariable = hasArgumentsVariable;

            functionVector       = new ArrayList();
            variableVector       = new ArrayList();
            hasWithStatement     = false;
            hasArgumentsVariable = false;
            hasFunctionLiteral   = false;

            Identifier[] parameters = literal.parameters;
            for (int i = 0; i < parameters.Length; i++)
            {
                addVariable(parameters[i]);
            }

            literal = (FunctionLiteral)base.visit(literal);

            literal.functions = CompilerUtil.vectorToStatementArray(functionVector);
            literal.variables = CompilerUtil.vectorToIdentifierArray(variableVector);

            // if this function literal:
            // * contains a function literal
            // * contains a 'with' statement
            // * contains a reference to 'arguments'
            //
            // then we need to disable the "access locals by index" optimisation for
            // this function literal.

            literal.enableLocalsOptimization =
                !(hasWithStatement | hasArgumentsVariable | hasFunctionLiteral);

            functionVector       = oldFunctionVector;
            variableVector       = oldVariableVector;
            hasWithStatement     = oldHasWithStatement;
            hasArgumentsVariable = oldHasArgumentsVariable;
            hasFunctionLiteral   = true;

            return(literal);
        }
Esempio n. 10
0
  private ArrayLiteral parseArrayLiteral() {
    ArrayList arrayElements = new ArrayList();

    readToken(Token.OPERATOR_OPENSQUARE);

    while (nextToken != Token.OPERATOR_CLOSESQUARE) {
      if (nextToken == Token.OPERATOR_COMMA) {
	    arrayElements.Add(null);
      } else {
	    arrayElements.Add(parseAssignmentExpression(true));
      }

      if (nextToken != Token.OPERATOR_CLOSESQUARE) {
        readToken(Token.OPERATOR_COMMA);
      }
    }

    readToken(Token.OPERATOR_CLOSESQUARE);

    return new ArrayLiteral(CompilerUtil.vectorToExpressionArray(arrayElements));
  }
Esempio n. 11
0
  private Statement parseForStatement() {
    Expression declaration = null;
    Expression initial = null;
    Expression condition = null;
    Expression increment = null;
    Expression variable = null;
    Expression expression = null;
    Statement statement = null;

    // 'for' statements can be one of the follow four productions:
    //
    // for ( ExpressionNoIn_opt; Expression_opt ; Expression_opt ) Statement
    // for ( var VariableDeclarationListNoIn; Expression_opt ; Expression_opt ) Statement
    // for ( LeftHandSideExpression in Expression ) Statement
    // for ( var VariableDeclarationNoIn in Expression ) Statement

    readToken(Token.KEYWORD_FOR);
    readToken(Token.OPERATOR_OPENPAREN);

    int state = 0;
    while (statement == null) {
      switch (state) {
        case 0:
          // initial state
          if (nextToken == Token.KEYWORD_VAR) {
            state = 1;
          } else if (nextToken != Token.OPERATOR_SEMICOLON) {
            state = 2;
          } else {
            state = 5;
          }
          break;

        case 1:
          // 'for' '(' 'var'
          readToken(Token.KEYWORD_VAR);
          declaration = parseVariableDeclaration(false);
          if (nextToken == Token.KEYWORD_IN) {
            variable = declaration;
            state = 3;
          } else {
            state = 4;
          }
          break;

        case 2:
          // 'for' '(' Expression
          initial = parseExpression(false);
          if (nextToken == Token.KEYWORD_IN) {
            variable = initial;
            state = 3;
          } else {
            state = 5;
          }
          break;

        case 3:
          // 'for' '(' ... 'in'
          readToken(Token.KEYWORD_IN);
          expression = parseExpression(true);
          readToken(Token.OPERATOR_CLOSEPAREN);

          // 'for' '(' ... 'in' ... ')' Statement
          statement = new ForInStatement(variable, expression, parseStatement());
          break;

        case 4:
          // 'for' '(' 'var' VariableDeclarationList
		  ArrayList declarationVector = new ArrayList();

		  declarationVector.Add(declaration);
          while (nextToken == Token.OPERATOR_COMMA) {
            readToken(Token.OPERATOR_COMMA);
			declarationVector.Add(parseVariableDeclaration(false));
          }
          initial = new VariableExpression(CompilerUtil.vectorToDeclarationArray(declarationVector));

          // fall through
		  goto case 5;

        case 5:
          // 'for' '(' ... ';'
          readToken(Token.OPERATOR_SEMICOLON);

          // 'for' '(' ... ';' ...
          if (nextToken != Token.OPERATOR_SEMICOLON) {
            condition = parseExpression(true);
          }

          // 'for' '(' ... ';' ... ';'
          readToken(Token.OPERATOR_SEMICOLON);

          // 'for' '(' ... ';' ... ';' ...
          if (nextToken != Token.OPERATOR_CLOSEPAREN) {
            increment = parseExpression(true);
          }

          // 'for' '(' ... ';' ... ';' ... ')'
          readToken(Token.OPERATOR_CLOSEPAREN);

          // 'for' '(' ... ';' ... ';' ... ')' Statement
          statement = new ForStatement(initial, condition, increment, parseStatement());
          break;
      }
    }

    return statement;
  }