use of dyvilx.tools.compiler.ast.statement.IfStatement in project Dyvil by Dyvil.
the class IfStatementParser method parse.
@Override
public void parse(IParserManager pm, IToken token) {
final int type = token.type();
switch(this.mode) {
case IF:
if (type != DyvilKeywords.IF) {
pm.report(token, "if.if_keyword");
return;
}
this.mode = OPEN_PAREN;
this.statement = new IfStatement(token.raw());
return;
case OPEN_PAREN:
if (type == BaseSymbols.OPEN_PARENTHESIS) {
this.parentheses = true;
this.mode = CONDITION_PART;
return;
}
// Fallthrough
case CONDITION_PART:
if (type == DyvilKeywords.LET) {
pm.pushParser(new DataMemberParser<>(this), true);
this.mode = VARIABLE_VALUE;
return;
}
this.mode = SEPARATOR;
pm.pushParser(this.expressionParser(this.statement::setCondition), true);
return;
case VARIABLE_VALUE:
if (type == BaseSymbols.EQUALS) {
pm.pushParser(this.expressionParser(this.lastVariable));
this.mode = SEPARATOR;
return;
}
pm.report(token, "if.binding.assignment");
// Fallthrough
case SEPARATOR:
switch(type) {
case BaseSymbols.COMMA:
case BaseSymbols.SEMICOLON:
if (this.parentheses && !this.hasCondition()) {
this.mode = CONDITION_PART;
return;
}
// then
break;
case BaseSymbols.CLOSE_PARENTHESIS:
if (this.parentheses) {
this.mode = THEN;
return;
}
// then
break;
}
if (this.parentheses) {
pm.report(token, "if.close_paren");
}
// Fallthrough
case THEN:
switch(type) {
case Tokens.EOF:
case BaseSymbols.SEMICOLON:
this.end(pm);
return;
}
this.mode = ELSE;
pm.pushParser(new ExpressionParser(this.statement::setThen), true);
return;
case ELSE:
if (type == DyvilKeywords.ELSE) {
pm.pushParser(new ExpressionParser(this.statement::setElse));
this.mode = END;
return;
}
if (token.isInferred() && token.next().type() == DyvilKeywords.ELSE) {
// ... inferred_semicolon else
pm.skip();
pm.pushParser(new ExpressionParser(this.statement::setElse));
this.mode = END;
return;
}
// Fallthrough
case END:
this.end(pm);
return;
}
}
use of dyvilx.tools.compiler.ast.statement.IfStatement in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyAnyMethod.
private CodeMethod createUnapplyAnyMethod() {
// static final func unapply<TypeParams...>(value: any) -> (T...)?
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = NullableType.apply(this.getUnapplyReturnType());
final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
unapply.setPosition(position);
unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, Types.NULLABLE_ANY);
unapply.getParameters().add(parameter);
// = (param is This) ? unapply(param as This) : null
final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
unapply.setValue(ifStatement);
return unapply;
}
Aggregations