Ejemplo 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)));
            }
        }
Ejemplo 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));
  }
Ejemplo 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));
  }
Ejemplo 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);
        }
Ejemplo 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);
  }
Ejemplo n.º 6
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)
    );
  }
Ejemplo n.º 7
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);
        }