Package org.hisrc.jscm.codemodel

Examples of org.hisrc.jscm.codemodel.JSCodeModel


public class FactorialExampleTest {

  @Test
  public void programsFactorial() throws IOException {
    // Instantiate the code model
    JSCodeModel codeModel = new CodeModelImpl();
    // Create the program
    JSProgram program = codeModel.program();
    // Add a function declaration
    JSFunctionDeclaration factorial = program
        .functionDeclaration("factorial");
    // Add a function parameter
    JSVariable x = factorial.parameter("x");
    // Create an integer literal
    JSDecimalIntegerLiteral one = codeModel.integer(1);
    // Add a return statement to the function body
    factorial.getBody()._return(
        x.le(one).cond(
            one,
            x.mul(factorial.getFunctionExpression().i()
View Full Code Here


public class CodeWriterTest {
  @Test
  public void allExpressions() throws IOException {

    final CodeWriter out = new CodeWriter(System.out);
    JSCodeModel codeModel = new CodeModelImpl();

    JSProgram program = codeModel.program();
   
    program._if(codeModel._boolean(true))._then().block()._return();
    program._if(codeModel._boolean(false))._then().block()._return();
   
    out.program(program);
  }
View Full Code Here

  @Test
  public void allExpressions() throws IOException {

    final CodeWriter out = new CodeWriter(System.out);
    JSCodeModel codeModel = new CodeModelImpl();

    JSProgram program = codeModel.program();
    JSFunctionDeclaration f = program.functionDeclaration("f");

    JSVariable x = f.parameter("x");
    JSVariable y = f.parameter("y");

    JSFunctionBody body = f.getBody();

    JSBlock assignmentExpressions = body.block();

    assignmentExpressions.expression(x.assign(y));
    assignmentExpressions.expression(x.mulAssign(y));
    assignmentExpressions.expression(x.divAssign(y));
    assignmentExpressions.expression(x.modAssign(y));
    assignmentExpressions.expression(x.plusAssign(y));
    assignmentExpressions.expression(x.minusAssign(y));
    assignmentExpressions.expression(x.shlAssign(y));
    assignmentExpressions.expression(x.shrAssign(y));
    assignmentExpressions.expression(x.shrzAssign(y));
    assignmentExpressions.expression(x.bandAssign(y));
    assignmentExpressions.expression(x.borAssign(y));
    assignmentExpressions.expression(x.xorAssign(y));

    JSBlock conditionalExpressions = assignmentExpressions.block();
    conditionalExpressions.expression(x.cond(y, y.not()));

    JSBlock logicalORExpressions = conditionalExpressions.block();
    logicalORExpressions.expression(x.or(y));

    JSBlock logicalANDExpressions = logicalORExpressions.block();
    logicalANDExpressions.expression(x.and(y));

    JSBlock bitwiseORExpressions = logicalANDExpressions.block();
    bitwiseORExpressions.expression(x.bor(y));

    JSBlock bitwiseXORExpressions = bitwiseORExpressions.block();
    bitwiseXORExpressions.expression(x.xor(y));

    JSBlock bitwiseANDExpressions = bitwiseXORExpressions.block();

    bitwiseANDExpressions.expression(x.band(y));

    JSBlock equalityExpressions = bitwiseANDExpressions.block();
    equalityExpressions.expression(x.eq(y));
    equalityExpressions.expression(x.ne(y));
    equalityExpressions.expression(x.eeq(y));
    equalityExpressions.expression(x.nee(y));

    JSBlock relationalExpressions = equalityExpressions.block();

    relationalExpressions.expression(x.lt(y));
    relationalExpressions.expression(x.gt(y));
    relationalExpressions.expression(x.le(y));
    relationalExpressions.expression(x.ge(y));
    relationalExpressions.expression(x._instanceof(y));
    relationalExpressions.expression(x.in(y));

    JSBlock shiftExpressions = relationalExpressions.block();

    shiftExpressions.expression(x.shl(y));
    shiftExpressions.expression(x.shr(y));
    shiftExpressions.expression(x.shrz(y));

    JSBlock additiveExpressions = shiftExpressions.block();

    additiveExpressions.expression(x.plus(y));
    additiveExpressions.expression(x.minus(y));

    JSBlock multiplicativeExpressions = additiveExpressions.block();

    multiplicativeExpressions.expression(x);
    multiplicativeExpressions.expression(x.mul(y));
    multiplicativeExpressions.expression(x.div(y));
    multiplicativeExpressions.expression(x.mod(y));

    JSBlock unaryExpressions = multiplicativeExpressions.block();

    unaryExpressions.expression(x.delete());
    unaryExpressions.expression(x._void());
    unaryExpressions.expression(x.typeof());
    unaryExpressions.expression(x.preIncr());
    unaryExpressions.expression(x.preDecr());
    unaryExpressions.expression(x.positive());
    unaryExpressions.expression(x.negative());
    unaryExpressions.expression(x.complement());
    unaryExpressions.expression(x.not());

    JSBlock postfixExpressions = unaryExpressions.block();

    postfixExpressions.expression(x.postIncr());
    postfixExpressions.expression(x.postDecr());

    JSBlock leftHandSideExpressions = postfixExpressions.block();

    JSBlock callExpressions = leftHandSideExpressions.block();

    callExpressions.expression(x.i().args(y).args(y).args(y));
    callExpressions.expression(x.i().args(y));
    callExpressions.expression(x.i().args(y, y, y));
    callExpressions.expression(x.i().args(y).i()
        .args(codeModel.integer(0), codeModel.integer(1)));

    callExpressions.expression(x.i().args(y).e(codeModel.integer(0)));
    callExpressions.expression(x.i().args(y).p("zero"));
    callExpressions.expression(x.i().args(y).p(codeModel.integer(0)));
    callExpressions.expression(x.i().args(y).p(codeModel.string("two")));

    JSBlock newExpressions = leftHandSideExpressions.block();

    newExpressions.expression(x._new());

    JSBlock memberExpressions = newExpressions.block();

    memberExpressions.expression(codeModel.function());
    JSFunctionExpression.Function foo = codeModel.function("foo");
    JSVariable a = foo.parameter("a");
    JSVariable b = foo.parameter("b");
    JSVariable c = foo.parameter("c");
    foo.getBody().expression(a.plus(b));
    foo.getBody()._return(c);
    memberExpressions.expression(foo);
    memberExpressions.expression(x.e(codeModel.integer(0)));
    memberExpressions.expression(x.p("zero"));
    memberExpressions.expression(x.p(codeModel.integer(1)));
    memberExpressions.expression(x.p(codeModel.string("two")));
    memberExpressions.expression(x.instantiate().args(y));

    JSBlock primaryExpressions = memberExpressions.block();

    primaryExpressions.expression(codeModel._this());
    primaryExpressions.expression(x);

    JSBlock literals = primaryExpressions.block();
    literals.expression(codeModel._null());
    literals.expression(codeModel._boolean(true));
    literals.expression(codeModel._boolean(false));
    literals.expression(codeModel.integer(-1L));
    literals.expression(codeModel.integer(0L));
    literals.expression(codeModel.integer(1L));
    literals.expression(codeModel.decimal("-1.1"));
    literals.expression(codeModel.decimal("0.0"));
    literals.expression(codeModel.decimal("1.1"));
    literals.expression(codeModel.string("a"));

    primaryExpressions.expression(codeModel.array());
    primaryExpressions.expression(codeModel.array().append(x, y));
    primaryExpressions.expression(codeModel.array().append(
        codeModel.integer(0), codeModel.string("a")));
    primaryExpressions.expression(codeModel.object());
    primaryExpressions.expression(codeModel.object().append("zero", x)
        .append(codeModel.integer(1), x)
        .append(codeModel.string("two"), x));
    primaryExpressions.expression(codeModel._boolean(true).brackets());
    primaryExpressions.expression(codeModel._boolean(true)
        .comma(codeModel._boolean(false)).brackets());

    out.program(program);

  }
View Full Code Here

public class StatementsTest {
  @Test
  public void allExpressions() throws IOException {

    final CodeWriter out = new CodeWriter(System.out);
    JSCodeModel codeModel = new CodeModelImpl();

    JSProgram program = codeModel.program();
    program.empty();
    JSGlobalVariable window = codeModel.globalVariable("window");
    JSGlobalVariable window1 = codeModel.globalVariable("window");
    Assert.assertSame(window, window1);

    JSFunctionDeclaration f = program.functionDeclaration("f");

    JSVariable x = f.parameter("x");
    JSVariable y = f.parameter("y");

    JSFunctionBody body = f.getBody();

    {
      body.functionDeclaration("g").getBody().functionDeclaration("h")
          .getBody().functionDeclaration("i");
      body.expression(window.p("open").i()
          .args(codeModel._this()));
      body.block();
      body.block().block();
      body.block().block();
      body.block().block().block().block();
      body.block().block().block().block().debugger();
    }
    {
      body.var("a").getVariable();
      body.var("b", codeModel.string("b"));

      body.var("c", codeModel.string("c")).comma("d")
          .comma("e", codeModel.string("e"));
    }
    body.empty();
    {
      {
        body._if(x.gt(y))._then()._return();
      }
      {
        body._if(x.gt(y))._then().block()._return();
      }

      {
        JSIfStatement _if = body._if(x.le(y));
        _if._then()._return(x.mul(y));
        _if._else()._return(x.plus(x));
      }
      {
        JSIfStatement _if = body._if(x.le(y));
        _if._then().block()._return(x.mul(y));
        _if._else().block()._return(x.plus(x));
      }
      {
        JSIfStatement _if = body._if(x.le(y));
        _if._then().block()._return(x.mul(y));
        _if._else()._if(x.eq(y))._then()._return(x.plus(x));
      }
    }

    {

      body._for().test(x.lt(y)).update(x.postIncr())
          .expression(y.preIncr());
      body._for().update(x.postIncr()).expression(y.preIncr());
      body._for().test(x.lt(y)).expression(y.preIncr());
      body._for()._break();
      body._for(x.assign(codeModel.integer(0))).test(x.lt(y))
          .update(x.postIncr()).expression(y.preIncr());
      body.forIn(x, y).block().expression(x.postDecr());

      JSForVarStatement _forVar0 = body.forVar("x0");
      Assert.assertNull(_forVar0.getExpression());
      JSVariable x0 = _forVar0.getVariable();
      _forVar0.comma("x1").comma("x2", codeModel.integer(10));
      _forVar0.test(x0.lt(codeModel.integer(5))).update(x0.postIncr())
          .expression(y.plusAssign(x0));

      JSForVarStatement _forVar1 = body
          .forVar("x1", codeModel.integer(0));
      Assert.assertNotNull(_forVar1.getExpression());
      JSVariable x1 = _forVar1.getVariable();
      _forVar1.test(x1.lt(codeModel.integer(5))).update(x1.postIncr())
          .expression(y.plusAssign(x1));

      JSForVarInStatement _forVarIn = body.forVarIn("x3", y);
      JSVariable x3 = _forVarIn.getVariable();
      _forVarIn.expression(y.plusAssign(x3));

      body.doWhile(x.lt(y)).expression(x.postIncr());
      body.doWhile(x.lt(y)).block().expression(x.postIncr());

      body._while(x.lt(y)).expression(x.postIncr());
      body._while(x.lt(y)).block().expression(x.postIncr());
    }

    {
      JSLabelledStatement labelledStatement = body.label("label0");
      JSLabel label0 = labelledStatement.getLabel();
      labelledStatement.block()._break(label0);

      body._continue();
      body._continue(label0);

      body._break();
      body._break(label0);
    }
    {
      body._return();
      body._return(x);
      body.with(x).expression(y.postIncr());
      body.with(x).block().expression(y.postIncr());
    }
    {
      JSSwitchStatement _switch0 = body._switch(x);
      JSCaseClause _case00 = _switch0._case(codeModel.integer(0));
      _case00.empty();
      _case00._return(codeModel.integer(1));

      JSSwitchStatement _switch1 = body._switch(x);
      JSCaseClause _case10 = _switch1._case(codeModel.integer(0));
      _case10.empty();
      _case10._return(codeModel.integer(1));
      JSDefaultClause _default1 = _switch1._default();
      _default1.empty();
      _default1._return(codeModel.integer(2));

      JSSwitchStatement _switch2 = body._switch(x);
      JSCaseClause _case20 = _switch2._case(codeModel.integer(0));
      _case20.expression(x.postIncr());
      _case20._return(codeModel.integer(1));
      JSDefaultClause _default2 = _switch2._default();
      _default2.empty();
      _default2._return(codeModel.integer(2));
      JSCaseClause _case22 = _switch2._case(codeModel.integer(2));
      _case22.empty();
      _case22._return(codeModel.integer(3));

    }

    {
      body._throw(x);
    }

    {
      JSTryStatement tryCatch0 = body.tryCatch("e");
      tryCatch0.getBody().expression(y.preIncr());
      tryCatch0.getCatch()._throw(tryCatch0.getException());

      JSTryStatement tryFinally0 = body.tryFinally();
      tryFinally0.getBody().expression(y.preIncr());
      tryFinally0.getFinally()._return(codeModel._boolean(true));

      JSTryStatement tryCatchFinally = body.tryCatchFinally("e");
      tryCatchFinally.getBody().expression(y.preIncr());
      tryCatchFinally.getCatch()._throw(tryCatchFinally.getException());
      tryCatchFinally.getFinally()._return(codeModel._boolean(true));
    }

    {
      body.debugger();
    }
View Full Code Here

TOP

Related Classes of org.hisrc.jscm.codemodel.JSCodeModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.