Android Open Source - bad Expression Factory






From Project

Back to project page bad.

License

The source code is released under:

Copyright (C) 2013 Madis Pink Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...

If you think the Android project bad listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.madisp.bad.expr;
/*  w w  w  .  j  a v a  2s .  com*/
import com.madisp.bad.parse.ExprBaseVisitor;
import com.madisp.bad.parse.ExprLexer;
import com.madisp.bad.parse.ExprParser;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: madis
 * Date: 3/23/13
 * Time: 5:41 PM
 */
public class ExpressionFactory extends ExprBaseVisitor<Expression> {

  public Expression buildExpression(String expr) {
    ExprLexer lexer = new ExprLexer(new ANTLRInputStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    return visitLine(parser.line());
  }

  @Override
  public Expression visitLine(ExprParser.LineContext ctx) {
    return visitProg(ctx.prog());
  }

  @Override
  public Expression visitProg(ExprParser.ProgContext ctx) {
    List<Expression> statements = new ArrayList<Expression>();
    statements.add(visitExpr(ctx.expr()));

    ExprParser.ProgContext prog = ctx.prog();
    while (prog != null) {
      statements.add(visitExpr(prog.expr()));
      prog = prog.prog();
    }
    return new StatementList(statements.toArray(new Expression[statements.size()]));
  }

  @Override
  public Expression visitExpr(ExprParser.ExprContext ctx) {
    if (ctx.NOT() != null) {
      return new NotExpression(visitExpr(ctx.center));
    } else if (ctx.MINUS() != null && ctx.center != null) {
      return new UnaryMinusExpression(visitExpr(ctx.center));
    } else if (ctx.PLUS() != null) {
      return new PlusExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.MINUS() != null) {
      return new MinusExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.STAR() != null) {
      return new MultiplyExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.SLASH() != null) {
      return new DivisionExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.AND() != null) {
      return new AndExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.OR() != null) {
      return new OrExpression(visitExpr(ctx.left), visitExpr(ctx.right));
    } else if (ctx.ASSIGN() != null) {
      BasableExpression left = visitValue(ctx.leftVar);
      if (left instanceof VarExpression) {
        return new AssignExpression((VarExpression)left, visitExpr(ctx.right));
      }
      throw new RuntimeException("Cannot assign values to " + left.getClass().getSimpleName());
    } else if (ctx.value() != null) {
      return visitValue(ctx.value());
    } else if (ctx.constant() != null) {
      return visitConstant(ctx.constant());
    } else if (ctx.prog() != null) {
      // meta, this will return an expression containing an expression object :)
      List<String> varList = new ArrayList<String>();
      ExprParser.VarlistContext vars = ctx.varlist();
      while (vars != null && vars.IDENTIFIER() != null) {
        varList.add(vars.IDENTIFIER().toString());
        vars = vars.varlist();
      }
      return new BlockExpression(visitProg(ctx.prog()), varList);
    } else {
      return visitExpr(ctx.center);
    }
  }

  @Override
  public MethodExpression visitCall(ExprParser.CallContext ctx) {
    List<Expression> argsList = new ArrayList<Expression>();

    ExprParser.ArgslistContext args = ctx.argslist();
    while (args != null && args.expr() != null) {
      argsList.add(visitExpr(args.expr()));
      args = args.argslist();
    }

    return new MethodExpression(ctx.IDENTIFIER().getText(), argsList.toArray(new Expression[argsList.size()]));
  }

  @Override
  public VarExpression visitVariable(ExprParser.VariableContext ctx) {
    return new VarExpression(null, ctx.NULL() != null ? null : ctx.IDENTIFIER().getText());
  }

  @Override
  public BasableExpression visitValue(ExprParser.ValueContext ctx) {
    if (ctx.DOT() != null) {
      BasableExpression left = visitValue(ctx.left);
      BasableExpression right = visitValue(ctx.right);
      right.setBase(left);
      return right;
    }
    if (ctx.call() != null) {
      return visitCall(ctx.call());
    } else if (ctx.variable() != null) {
      return visitVariable(ctx.variable());
    }
    return null;
  }

  @Override
  public Expression visitResource(ExprParser.ResourceContext ctx) {
    if (ctx.NULL() != null) {
      return new ConstantExpression(0);
    } else {
      return new MethodExpression("getResourceId", new ConstantExpression(ctx.getText().substring(1).trim()));
    }
  }

  @Override
  public Expression visitConstant(ExprParser.ConstantContext ctx) {
    if (ctx.STRING() != null) {
      return new ConstantExpression(ctx.STRING().getText());
    } else if (ctx.INT() != null) {
      return new ConstantExpression(Integer.valueOf(ctx.INT().getText().trim()));
    } else if (ctx.bool() != null) {
      return visitBool(ctx.bool());
    } else if (ctx.resource() != null) {
      return visitResource(ctx.resource());
    } else {
      return null;
    }
  }

  @Override
  public ConstantExpression visitBool(ExprParser.BoolContext ctx) {
    return new ConstantExpression(ctx.TRUE() != null);
  }
}




Java Source Code List

com.madisp.bad.ConsoleRepl.java
com.madisp.bad.Freezer.java
com.madisp.bad.Persist.java
com.madisp.bad.decor.BadDecorator.java
com.madisp.bad.decor.BaseDecorator.java
com.madisp.bad.decor.CheckableDecorator.java
com.madisp.bad.decor.EditTextDecorator.java
com.madisp.bad.decor.ListViewDecorator.java
com.madisp.bad.decor.TextViewDecorator.java
com.madisp.bad.decor.ViewDecorator.java
com.madisp.bad.decor.WebViewDecorator.java
com.madisp.bad.demo.ListFragment.java
com.madisp.bad.demo.LoginFragment.java
com.madisp.bad.demo.MainActivity.java
com.madisp.bad.demo.PersistFragment.java
com.madisp.bad.demo.ReplFragment.java
com.madisp.bad.demo.ShopFragment.java
com.madisp.bad.demo.TwoWayFragment.java
com.madisp.bad.eval.BadCollections.java
com.madisp.bad.eval.BadConverter.java
com.madisp.bad.eval.BadScope.java
com.madisp.bad.eval.BadVar.java
com.madisp.bad.eval.ScopeWrapper.java
com.madisp.bad.eval.Scope.java
com.madisp.bad.eval.Watcher.java
com.madisp.bad.expr.AndExpression.java
com.madisp.bad.expr.AssignExpression.java
com.madisp.bad.expr.BasableExpression.java
com.madisp.bad.expr.BlockExpression.java
com.madisp.bad.expr.ConstantExpression.java
com.madisp.bad.expr.DivisionExpression.java
com.madisp.bad.expr.ExpressionFactory.java
com.madisp.bad.expr.Expression.java
com.madisp.bad.expr.MethodExpression.java
com.madisp.bad.expr.MinusExpression.java
com.madisp.bad.expr.MultiplyExpression.java
com.madisp.bad.expr.NotExpression.java
com.madisp.bad.expr.OrExpression.java
com.madisp.bad.expr.PlusExpression.java
com.madisp.bad.expr.StatementList.java
com.madisp.bad.expr.UnaryMinusExpression.java
com.madisp.bad.expr.VarExpression.java
com.madisp.bad.lib.BadFragment.java
com.madisp.bad.lib.BadLayoutFactory.java
com.madisp.bad.lib.BadStdLib.java
com.madisp.bad.lib.ui.BadAdapter.java
com.madisp.bad.lib.ui.BadTextWatcher.java
com.madisp.bad.parse.ExprBaseVisitor.java
com.madisp.bad.parse.ExprLexer.java
com.madisp.bad.parse.ExprParser.java
com.madisp.bad.parse.ExprVisitor.java