Search in sources :

Example 1 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class EvalFunctions method eval.

public static Memory eval(Environment env, TraceInfo trace, @Runtime.GetLocals ArrayMemory locals, String code) throws Throwable {
    Context context = new Context(code);
    try {
        Tokenizer tokenizer = new Tokenizer(context);
        SyntaxAnalyzer analyzer = syntaxAnalyzer.get();
        analyzer.reset(env, tokenizer);
        AbstractCompiler compiler = new JvmCompiler(env, context, analyzer);
        ModuleEntity module = compiler.compile();
        env.scope.loadModule(module);
        env.registerModule(module);
        return module.include(env, locals);
    } catch (ErrorException e) {
        if (e.getType() == ErrorType.E_PARSE) {
            if (env.isHandleErrors(ErrorType.E_PARSE))
                throw new ParseException(evalErrorMessage(e), trace);
        } else
            env.error(trace, e.getType(), evalErrorMessage(e));
    }
    return Memory.FALSE;
}
Also used : Context(php.runtime.env.Context) AbstractCompiler(php.runtime.common.AbstractCompiler) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) JvmCompiler(org.develnext.jphp.core.compiler.jvm.JvmCompiler) ModuleEntity(php.runtime.reflection.ModuleEntity) ParseException(php.runtime.exceptions.ParseException) ErrorException(php.runtime.exceptions.support.ErrorException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 2 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class TokenizerTest method testStmt.

@Test
public void testStmt() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("class function private public protected static final try catch for if foreach switch while " + "default return declare case do else elseif endif endfor endforeach endwhile endswitch " + "abstract use namespace finally extends implements global"));
    assertTrue(tokenizer.nextToken() instanceof ClassStmtToken);
    assertTrue(tokenizer.nextToken() instanceof FunctionStmtToken);
    assertTrue(tokenizer.nextToken() instanceof PrivateStmtToken);
    assertTrue(tokenizer.nextToken() instanceof PublicStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ProtectedStmtToken);
    assertTrue(tokenizer.nextToken() instanceof StaticExprToken);
    assertTrue(tokenizer.nextToken() instanceof FinalStmtToken);
    assertTrue(tokenizer.nextToken() instanceof TryStmtToken);
    assertTrue(tokenizer.nextToken() instanceof CatchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ForStmtToken);
    assertTrue(tokenizer.nextToken() instanceof IfStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ForeachStmtToken);
    assertTrue(tokenizer.nextToken() instanceof SwitchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof WhileStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DefaultStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ReturnStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DeclareStmtToken);
    assertTrue(tokenizer.nextToken() instanceof CaseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof DoStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ElseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ElseIfStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndifStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndforStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndforeachStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndwhileStmtToken);
    assertTrue(tokenizer.nextToken() instanceof EndswitchStmtToken);
    assertTrue(tokenizer.nextToken() instanceof AbstractStmtToken);
    assertTrue(tokenizer.nextToken() instanceof NamespaceUseStmtToken);
    assertTrue(tokenizer.nextToken() instanceof NamespaceStmtToken);
    assertTrue(tokenizer.nextToken() instanceof FinallyStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ExtendsStmtToken);
    assertTrue(tokenizer.nextToken() instanceof ImplementsStmtToken);
    assertTrue(tokenizer.nextToken() instanceof GlobalStmtToken);
}
Also used : Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Context(php.runtime.env.Context) Test(org.junit.Test)

Example 3 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class TokenizerTest method testVarVar.

@Test
public void testVarVar() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("$$foo $ $bar $$$foobar"));
    assertTrue(tokenizer.nextToken() instanceof DollarExprToken);
    assertTrue(tokenizer.nextToken() instanceof VariableExprToken);
    assertTrue(tokenizer.nextToken() instanceof DollarExprToken);
    assertTrue(tokenizer.nextToken() instanceof VariableExprToken);
    assertTrue(tokenizer.nextToken() instanceof DollarExprToken);
    assertTrue(tokenizer.nextToken() instanceof DollarExprToken);
    assertTrue(tokenizer.nextToken() instanceof VariableExprToken);
    tokenizer = new Tokenizer(new Context("${'foo;6bar'}"));
    assertTrue(tokenizer.nextToken() instanceof DollarExprToken);
    assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
    assertTrue(tokenizer.nextToken() instanceof StringExprToken);
    assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
}
Also used : Context(php.runtime.env.Context) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 4 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class ASMExpressionTest method getASMExpression.

private ASMExpression getASMExpression(String expr) {
    environment.scope.setLangMode(LangMode.DEFAULT);
    Tokenizer tokenizer = null;
    try {
        tokenizer = new Tokenizer(context = new Context(expr + ";"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    Assert.assertTrue(analyzer.getTree().size() == 1);
    Assert.assertTrue(analyzer.getTree().get(0) instanceof ExprStmtToken);
    return new ASMExpression(null, context, (ExprStmtToken) analyzer.getTree().get(0));
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) IOException(java.io.IOException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 5 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class TokenizerTest method testScalarTokens.

@Test
public void testScalarTokens() throws IOException {
    Token token;
    Tokenizer tokenizer = new Tokenizer(new Context("10 3.3 'foo' '' \"xyz\" 0xCC 0b0011 true false"));
    token = tokenizer.nextToken();
    assertTrue(token instanceof IntegerExprToken);
    assertEquals(10L, ((IntegerExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof DoubleExprToken);
    assertEquals(3.3, ((DoubleExprToken) token).getValue(), 0.01);
    token = tokenizer.nextToken();
    assertTrue(token instanceof StringExprToken);
    assertEquals(StringExprToken.Quote.SINGLE, ((StringExprToken) token).getQuote());
    assertEquals("foo", ((StringExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof StringExprToken);
    assertEquals(StringExprToken.Quote.SINGLE, ((StringExprToken) token).getQuote());
    assertEquals("", ((StringExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof StringExprToken);
    assertEquals(StringExprToken.Quote.DOUBLE, ((StringExprToken) token).getQuote());
    assertEquals("xyz", ((StringExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof IntegerExprToken);
    assertEquals(new BigInteger("CC", 16).longValue(), ((IntegerExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof IntegerExprToken);
    assertEquals(new BigInteger("0011", 2).longValue(), ((IntegerExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof BooleanExprToken);
    assertEquals(true, ((BooleanExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof BooleanExprToken);
    assertEquals(false, ((BooleanExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertNull(token);
}
Also used : Context(php.runtime.env.Context) BigInteger(java.math.BigInteger) Token(org.develnext.jphp.core.tokenizer.token.Token) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Aggregations

Context (php.runtime.env.Context)30 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)27 Test (org.junit.Test)23 Token (org.develnext.jphp.core.tokenizer.token.Token)10 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)8 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)8 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)4 File (java.io.File)3 IOException (java.io.IOException)3 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)3 FunctionStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.FunctionStmtToken)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 Environment (php.runtime.env.Environment)2 ParseException (php.runtime.exceptions.ParseException)2 Launcher (php.runtime.launcher.Launcher)2 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 JvmCompiler (org.develnext.jphp.core.compiler.jvm.JvmCompiler)1 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)1