Package loop.ast

Examples of loop.ast.Node


    // Not all parts of the sexpr are lists.
    if (child instanceof InlineListDef) {
      InlineListDef list = (InlineListDef) child;

      if (!list.children().isEmpty()) {
        Node first = list.children().get(0);
        if (first instanceof Variable) {
          Variable var = (Variable) first;

          if ("define".equals(var.name)) {
            return transformToFunctionDecl(list);
View Full Code Here


  private FunctionDecl transformToFunctionDecl(InlineListDef list) {
    if (list.children().size() != 4)
      throw new RuntimeException("Incorrect number of arguments: " + list);

    Node second = list.children().get(1);
    String name;
    if (second instanceof Variable) {
      Variable var = (Variable) second;

      name = var.name;
    } else
      throw new RuntimeException("Expected function name in (define)");

    Node third = list.children().get(2);
    if (!(third instanceof InlineListDef))
      throw new RuntimeException("Expected argument definition");

    ArgDeclList args = new ArgDeclList();
    List<Node> exprs = reduce(third).children();
    for (Node expr : exprs) {
      Variable argName = (Variable) expr;
      args.add(new ArgDeclList.Argument(argName.name, null));
    }

    FunctionDecl functionDecl = new FunctionDecl(name, args);

    Node fourth = list.children().get(3);
    functionDecl.children().add(reduceNode(fourth));
    return functionDecl;
  }
View Full Code Here

   */
  @Override
  public Node parse() {
    InlineListDef list = new InlineListDef(false);
    while (!endOfInput()) {
      Node sexpr = sexpr();
      if (sexpr != null)
        list.add(new SexprAstTransformer(sexpr).transform());
    }
    return list;
  }
View Full Code Here

    }
    return list;
  }

  private Node sexpr() {
    Node atom = atom();
    if (atom != null)
      return atom;

    List<Token> startParen = match(Token.Kind.LPAREN);
    if (startParen == null)
      return null;

    InlineListDef listDef = new InlineListDef(false).sourceLocation(startParen);
    Node inner;
    while ((inner = sexpr()) != null) {
      listDef.add(inner);
    }

    if (match(Token.Kind.RPAREN) == null)
View Full Code Here

  }

  @Override
  public Unit script(String file) {
    Unit unit = new Unit(file, ModuleDecl.DEFAULT);
    Node parse = parse();
    for (Node child : parse.children()) {
      if (child instanceof FunctionDecl)
        unit.declare((FunctionDecl) child);
      else
        unit.addToInitializer(child);
    }
View Full Code Here

    Parser parser = isLisp
        ? new SexprParser(tokens)
        : new LexprParser(tokens);
    FunctionDecl functionDecl = parser.functionDecl();
    ClassDecl classDecl = null;
    Node node;
    if (null == functionDecl) {
      classDecl = parser.classDecl();
      node = classDecl;
    } else
      node = functionDecl;
View Full Code Here

        if (line.startsWith(":f") || line.startsWith(":functions")) {
          for (FunctionDecl functionDecl : shellScope.functions()) {
            StringBuilder args = new StringBuilder();
            List<Node> children = functionDecl.arguments().children();
            for (int i = 0, childrenSize = children.size(); i < childrenSize; i++) {
              Node arg = children.get(i);
              args.append(arg.toSymbol());

              if (i < childrenSize - 1)
                args.append(", ");
            }
View Full Code Here

                                       FunctionDecl func,
                                       Unit shellScope,
                                       boolean addToWhereBlock) {
    rawLine = rawLine.trim() + '\n';
    Executable executable = new Executable(new StringReader(rawLine));
    Node parsedLine;
    try {
      Parser parser = new LexprParser(new Tokenizer(rawLine).tokenize(), shellScope);
      parsedLine = parser.line();
      if (parsedLine == null || !parser.getErrors().isEmpty()) {
        executable.printErrors(parser.getErrors());
        return "";
      }

      // If this is an assignment, just check the rhs portion of it.
      // This is a bit hacky but prevents verification from balking about new
      // vars declared in the lhs.
      if (parsedLine instanceof Assignment) {
        new Reducer(parsedLine).reduce();
        Assignment assignment = (Assignment) parsedLine;

        // Strip the lhs of the assignment if this is a simple variable setter
        // as that will happen after the fact in a where-block.
        // However we still do have Assignment nodes that assign "in-place", i.e.
        // mutate the state of existing variables (example: a.b = c), and these need
        // to continue untouched.
        if (assignment.lhs() instanceof Variable)
          func.children().add(assignment.rhs());
        else
          func.children().add(parsedLine);
      } else
        func.children().add(parsedLine);

      // Compress nodes and eliminate redundancies.
      new Reducer(func).reduce();

      shellScope.loadDeps("<shell>");
      executable.runMain(true);
      executable.compileExpression(shellScope);

      if (executable.hasErrors()) {
        executable.printStaticErrorsIfNecessary();

        return "";
      }
    } catch (Exception e) {
      e.printStackTrace();
      return new LoopError("malformed expression " + rawLine);
    }

    try {
      Object result = Loop.safeEval(executable, null);

      if (addToWhereBlock && parsedLine instanceof Assignment) {
        Assignment assignment = (Assignment) parsedLine;

        boolean shouldReplace = false, shouldAddToWhere = true;
        if (assignment.lhs() instanceof Variable) {
          String name = ((Variable) assignment.lhs()).name;
          shellContext.put(name, result);

          // Look up the value of the RHS of the variable from the shell context,
          // if this is the second reference to the same variable.
          assignment.setRhs(new LexprParser(new Tokenizer(
              "`loop.LoopShell`.shellObtain('" + name + "')").tokenize()).parse());
          shouldReplace = true;
        } else
          shouldAddToWhere = false;   // Do not add state-mutating assignments to where block.

        // If this assignment is already present in the current scope, we should replace it.
        if (shouldReplace)
          for (Iterator<Node> iterator = func.whereBlock().iterator(); iterator.hasNext(); ) {
            Node node = iterator.next();
            if (node instanceof Assignment && ((Assignment) node).lhs() instanceof Variable) {
              iterator.remove();
            }
          }
View Full Code Here

  public List<String> handledExceptions() {
    List<String> exceptions = new ArrayList<String>(children.size());
    for (Node child : children) {
      assert child instanceof PatternRule;

      Node pattern = ((PatternRule) child).patterns.get(0);
      if (pattern instanceof TypeLiteral)
        exceptions.add(((TypeLiteral) pattern).name);
      else if (pattern instanceof WildcardPattern)
        exceptions.add("java.lang.Exception");
    }
View Full Code Here

    parser.parse();

    // Need to stringify first coz the reducer mutates the original AST.
    String parsedSexpr = LexprParser.stringify(parser.ast());

    Node reduced = new Reducer(parser.ast()).reduce();
    Assert.assertNotNull("Reducer returned no output", reduced);

    System.out.println("\n------------------------");
    System.out.println("Reduced Parse Tree:\n" + reduced);
    System.out.println("Parsed S-Expr:   " + parsedSexpr);
View Full Code Here

TOP

Related Classes of loop.ast.Node

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.