Package com.stuffwithstuff.magpie.ast

Examples of com.stuffwithstuff.magpie.ast.Expr


    return Pattern.record(fields);
  }
 
  private static Pattern primary(MagpieParser parser) {
    if (parser.match(TokenType.IS)) {
      Expr type = parser.parsePrecedence(Precedence.COMPARISON);
      return Pattern.type(type);
    } else if (parser.match(TokenType.EQEQ)) {
      Expr value = parser.parsePrecedence(Precedence.COMPARISON);
      return Pattern.value(value);
    } if (parser.match(TokenType.BOOL)) {
      return Pattern.value(Expr.bool(parser.last(1).getBool()));
    } else if (parser.match(TokenType.INT)) {
      return Pattern.value(Expr.int_(parser.last(1).getInt()));
View Full Code Here


* Parses an "or" operator.
*/
public class OrParser implements InfixParser {
  @Override
  public Expr parse(MagpieParser parser, Expr left, Token token) {
    Expr right = parser.parsePrecedence(Precedence.LOGICAL);
   
    // Desugar to a pattern match:
    //
    // match (val temp__ = `left) isTrue
    //     case true then temp__
    //     else `right
    // end
    // TODO(bob): This desugaring should be done in a later pass.
    String temp = parser.generateName();
    Expr value = Expr.var(left.getPosition(), false, temp, left);
    Expr truthCheck = Expr.call(left.getPosition(), value, Name.IS_TRUE);
   
    List<MatchCase> cases = new ArrayList<MatchCase>();
    cases.add(new MatchCase(Pattern.value(Expr.bool(true)), Expr.name(temp)));
    cases.add(new MatchCase(right));
   
View Full Code Here

public class EqualsParser implements InfixParser {
  @Override
  public Expr parse(MagpieParser parser, Expr left, Token token) {
    // Parse the value being assigned.
    Expr value = parser.parseStatement();
    return ConvertAssignmentExpr.convert(left, value);
  }
View Full Code Here

* Parses an "and" operator.
*/
public class AndParser implements InfixParser {
  @Override
  public Expr parse(MagpieParser parser, Expr left, Token token) {
    Expr right = parser.parsePrecedence(Precedence.LOGICAL);
   
    // Desugar to a pattern match:
    //
    // match (val temp__ = `left) isTrue
    //     case true then `right
    //     else temp__
    // end
    // TODO(bob): This desugaring should be done in a later pass.
    String temp = parser.generateName();
    Expr value = Expr.var(left.getPosition(), false, temp, left);
    Expr truthCheck = Expr.call(left.getPosition(), value, Name.IS_TRUE);
   
    List<MatchCase> cases = new ArrayList<MatchCase>();
    cases.add(new MatchCase(Pattern.value(Expr.bool(true)), right));
    cases.add(new MatchCase(Expr.name(temp)));
   
View Full Code Here

          getExpr(expr, "body"));
    } else if (exprClass == getClass("RecordExpression")) {
      List<Pair<String, Expr>> fields = new ArrayList<Pair<String, Expr>>();
      for (Obj field : getList(expr, "fields")) {
        String name = field.getField(0).asString();
        Expr value = convertExpr(field.getField(1));
        fields.add(new Pair<String, Expr>(name, value));
      }
      return Expr.record(
          getPosition(expr),
          fields);
View Full Code Here

      throw new ParseException(token.getPosition(),
          "Cannot unquote outside of a quotation.");
    }
   
    Position position = token.getPosition();
    Expr body;
    if (parser.match(TokenType.NAME)) {
      body = Expr.name(parser.last(1).getPosition(),
          parser.last(1).getString());
    } else {
      parser.consume(TokenType.LEFT_PAREN);
View Full Code Here

   
    int index = 1;
    String name = token.getString();
    Position namePosition = token.getPosition();
    while (true) {
      Expr value = parser.parsePrecedence(Precedence.RECORD);
      fields.add(new Pair<String, Expr>(name, value));
     
      if (usedNames.contains(name)) {
        throw new ParseException(namePosition,
            "A record may only use a field name once.");
View Full Code Here

  }
 
  @Override
  public Expr parse(MagpieParser parser, Expr left, Token token) {
    // Parse the argument, if any.
    Expr arg = null;
    if (parser.match(TokenType.LEFT_PAREN)) {
      arg = parser.groupExpression(TokenType.RIGHT_PAREN);
    }
   
    // Pass the block, if any.
    if (parser.match(TokenType.WITH)) {
      // Parse the parameter list if given.
      Pattern blockType;
      if (parser.lookAhead(TokenType.LEFT_PAREN)) {
        blockType = parser.parseFunctionType();
      } else {
        // Else just assume a single "it" parameter.
        blockType = Pattern.variable(Name.IT);
      }

      // Parse the block and wrap it in a function.
      Expr block = parser.parseExpressionOrBlock();
      // TODO(bob): Parse doc comment.
      block = Expr.fn(block.getPosition(), "", blockType, block);
     
      // Add it to the argument list.
      arg = appendField(arg, block);
    }
   
View Full Code Here

import com.stuffwithstuff.magpie.ast.Expr;

public class ParenthesisPrefixParser implements PrefixParser {
  @Override
  public Expr parse(MagpieParser parser, Token token) {
    Expr expr = parser.parseStatement();
    parser.consume(TokenType.RIGHT_PAREN);
   
    return expr;
  }
View Full Code Here

    }
   
    // TODO(bob): Parse doc.
   
    // Parse the body.
    Expr expr = parser.parseExpressionOrBlock();
   
    return Expr.fn(span.end(), "", pattern, expr);
  }
View Full Code Here

TOP

Related Classes of com.stuffwithstuff.magpie.ast.Expr

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.