Package com.stuffwithstuff.magpie.parser

Source Code of com.stuffwithstuff.magpie.parser.OrParser

package com.stuffwithstuff.magpie.parser;

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

import com.stuffwithstuff.magpie.ast.Expr;
import com.stuffwithstuff.magpie.ast.pattern.MatchCase;
import com.stuffwithstuff.magpie.ast.pattern.Pattern;
import com.stuffwithstuff.magpie.interpreter.Name;

/**
* 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));
   
    return Expr.match(left.getPosition().union(right.getPosition()),
        truthCheck, cases);
  }
 
  @Override
  public int getPrecedence() { return Precedence.LOGICAL; }
}
TOP

Related Classes of com.stuffwithstuff.magpie.parser.OrParser

TOP
Copyright © 2018 www.massapi.com. 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.