implement else if nesting
This commit is contained in:
@@ -22,7 +22,7 @@ print
|
|||||||
;
|
;
|
||||||
|
|
||||||
if_statement
|
if_statement
|
||||||
: IF OPAR expression CPAR braced_block (ELSE braced_block)?
|
: IF OPAR cond = expression CPAR then = braced_block (ELSE (alt = braced_block | elif = if_statement) )?
|
||||||
;
|
;
|
||||||
|
|
||||||
expression
|
expression
|
||||||
|
|||||||
@@ -42,14 +42,17 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node visitIf_statement(KlangParser.If_statementContext ctx) {
|
public Node visitIf_statement(KlangParser.If_statementContext ctx) {
|
||||||
Node condition = this.visit(ctx.expression());
|
Node condition = this.visit(ctx.cond);
|
||||||
Node thenBlock = this.visit(ctx.braced_block(0));
|
Node thenBlock = this.visit(ctx.then);
|
||||||
|
|
||||||
if (ctx.braced_block().size() > 1) {
|
if (ctx.alt != null) {
|
||||||
Node elseBlock = this.visit(ctx.braced_block(1));
|
Node elseBlock = this.visit(ctx.alt);
|
||||||
return new IfStatement((Expression) condition, (Block) thenBlock, (Block) elseBlock);
|
return new IfStatement((Expression) condition, (Block) thenBlock, (Block) elseBlock);
|
||||||
|
} else if(ctx.elif != null) {
|
||||||
|
Node elif = this.visit(ctx.elif);
|
||||||
|
return new IfStatement((Expression) condition, (Block) thenBlock, (IfStatement) elif);
|
||||||
} else {
|
} else {
|
||||||
return new IfStatement((Expression) condition, (Block) thenBlock, null);
|
return new IfStatement((Expression) condition, (Block) thenBlock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,25 @@ public class IfStatement extends Statement {
|
|||||||
public Expression cond;
|
public Expression cond;
|
||||||
public Block then;
|
public Block then;
|
||||||
public Block alt;
|
public Block alt;
|
||||||
|
public IfStatement elif;
|
||||||
|
|
||||||
public IfStatement(Expression cond, Block then, Block alt) {
|
public IfStatement(Expression cond, Block then, Block alt, IfStatement elif) {
|
||||||
this.cond = cond;
|
this.cond = cond;
|
||||||
this.then = then;
|
this.then = then;
|
||||||
this.alt = alt;
|
this.alt = alt;
|
||||||
|
this.elif = elif;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IfStatement(Expression cond, Block then, Block alt) {
|
||||||
|
this(cond, then, alt, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IfStatement(Expression cond, Block then, IfStatement elif) {
|
||||||
|
this(cond, then, null, elif);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IfStatement(Expression cond, Block then) {
|
||||||
|
this(cond, then, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
e.then.welcome(this);
|
e.then.welcome(this);
|
||||||
} else if (e.alt != null) {
|
} else if (e.alt != null) {
|
||||||
e.alt.welcome(this);
|
e.alt.welcome(this);
|
||||||
|
} else if (e.elif != null) {
|
||||||
|
e.elif.welcome(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -66,7 +68,9 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(Block e) {
|
public Value visit(Block e) {
|
||||||
// TODO Auto-generated method stub
|
for (var stmt: e.statements) {
|
||||||
|
stmt.welcome(this);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,9 @@ public class PrettyPrintVisitor implements Visitor<Void> {
|
|||||||
if (e.alt != null) {
|
if (e.alt != null) {
|
||||||
ex.write(" else ");
|
ex.write(" else ");
|
||||||
e.alt.welcome(this);
|
e.alt.welcome(this);
|
||||||
|
} else if (e.elif != null) {
|
||||||
|
ex.write(" else ");
|
||||||
|
e.elif.welcome(this);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user