public Expression expr() { | |
return or(); | |
} | |
public Expression or() { | |
Expression left = and(); | |
while (found(Token.Type.KEYWORD, "or")) { | |
String operator = token.text; | |
Expression right = and(); | |
left = new EOp(operator, left, right); | |
} | |
return left; | |
} | |
public Expression and() { | |
Expression left = comp(); | |
while (found(Token.Type.KEYWORD, "and")) { | |
String operator = token.text; | |
Expression right = comp(); | |
left = new EOp(operator, left, right); | |
} | |
return left; | |
} | |
public Expression comp() { | |
Expression left = add_rem(); | |
if (found(Token.Type.SYM, "<") || found(Token.Type.SYM, "<=") | |
|| found(Token.Type.SYM, ">") || found(Token.Type.SYM, ">=") | |
|| found(Token.Type.SYM, "=")) { | |
String operator = token.text; | |
Expression right = add_rem(); | |
return new EOp(operator, left, right); | |
} else { | |
return left; | |
} | |
} | |
public Expression add_rem() { | |
Expression left = mul_div(); | |
while (found(Token.Type.SYM, "+") || found(Token.Type.SYM, "-")) { | |
String operator = token.text; | |
Expression right = mul_div(); | |
left = new EOp(operator, left, right); | |
} | |
return left; | |
} | |
public Expression mul_div() { | |
Expression left = app(); | |
while (found(Token.Type.SYM, "*") || found(Token.Type.SYM, "/") || found(Token.Type.SYM, "%")) { | |
String operator = token.text; | |
Expression right = app(); | |
left = new EOp(operator, left, right); | |
} | |
return left; | |
} | |
public Expression app() { | |
Expression fn = atom(); | |
List<Expression> args = new ArrayList<Expression>(); | |
while (peek(Token.Type.NUM) || peek(Token.Type.BOOL) || peek(Token.Type.NAME) || peek(Token.Type.SYM, "(") | |
|| peek(Token.Type.KEYWORD, "not") || peek(Token.Type.KEYWORD, "if")) { | |
args.add(atom()); | |
} | |
if (args.isEmpty()) { | |
return fn; | |
} else { | |
return new EAp(fn, args); | |
} | |
} | |
public Expression atom() { | |
if (found(Token.Type.NUM)) { | |
return new ENum(Integer.parseInt(token.text)); | |
} else if (found(Token.Type.BOOL)) { | |
return new EBool(Boolean.parseBoolean(token.text)); | |
} else if (found(Token.Type.NAME)) { | |
return new EName(token.text); | |
} else if (found(Token.Type.SYM, "(")) { | |
Expression expr = expr(); | |
expect(Token.Type.SYM, ")"); | |
return expr; | |
} else if (found(Token.Type.SYM, "-")) { | |
Expression operand = atom(); | |
return new EOp("-", operand, null); | |
} else if (found(Token.Type.KEYWORD, "not")) { | |
Expression operand = atom(); | |
return new EOp("not", operand, null); | |
} else if (found(Token.Type.KEYWORD, "if")) { | |
Expression cond = expr(); | |
expect(Token.Type.KEYWORD, "then"); | |
Expression thenExpr = expr(); | |
expect(Token.Type.KEYWORD, "else"); | |
Expression elseExpr = expr(); | |
return new EIf(cond, thenExpr, elseExpr); | |
} else { | |
throw error("Unexpected input"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment