Skip to content

Commit

Permalink
Add For-Each BAST structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cpurdy committed Sep 14, 2023
1 parent c96629a commit 1de25f0
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 19 deletions.
12 changes: 12 additions & 0 deletions javatools/src/main/java/org/xvm/asm/ast/AssignAST.java
Expand Up @@ -88,6 +88,18 @@ public AssignAST(ExprAST<C> lhs, Operator op, ExprAST<C> rhs) {
this.rhs = rhs;
}

public ExprAST<C> getLValue() {
return lhs;
}

public Operator getOperator() {
return op;
}

public ExprAST<C> getRValue() {
return rhs;
}

@Override
public int getCount() {
return lhs.getCount();
Expand Down
30 changes: 15 additions & 15 deletions javatools/src/main/java/org/xvm/asm/ast/BinaryAST.java
Expand Up @@ -193,20 +193,18 @@ public enum NodeType {
<C> BinaryAST<C> instantiate() {
return switch (this) {
case None -> null;
case Escape,
RegisterExpr -> throw new IllegalStateException();
case RegAlloc,
NamedRegAlloc,
AnnoRegAlloc,
AnnoNamedRegAlloc -> new RegAllocAST<>(this);
case Assign -> new AssignAST<>(false);
case BinOpAssign -> new AssignAST<>(true);

case Escape,
RegisterExpr -> throw new IllegalStateException();

case InvokeExpr,
InvokeAsyncExpr -> new InvokeExprAST<>(this);
InvokeAsyncExpr -> new InvokeExprAST<>(this);
case CallExpr,
CallAsyncExpr -> new CallExprAST<>(this);
CallAsyncExpr -> new CallExprAST<>(this);
case BindMethodExpr -> new BindMethodAST<>();
case BindFunctionExpr -> new BindFunctionAST<>();
case ConstantExpr -> new ConstantExprAST<>();
Expand All @@ -230,19 +228,23 @@ <C> BinaryAST<C> instantiate() {
case NotNullExpr -> new NotNullExprAST<>();
case TernaryExpr -> new TernaryExprAST<>();
case UnpackExpr -> new UnpackExprAST<>();
case SwitchExpr -> new SwitchAST<>(this);
case SwitchStmt,
SwitchExpr -> new SwitchAST<>(this);
case TemplateExpr -> new TemplateExprAST<>();
case ThrowExpr -> new ThrowExprAST<>();

case StmtBlock -> new StmtBlockAST<>(true);
case MultiStmt -> new StmtBlockAST<>(false);
case IfThenStmt -> new IfStmtAST<>(false);
case IfElseStmt -> new IfStmtAST<>(true);
case StmtBlock,
MultiStmt -> new StmtBlockAST<>(this);
case IfThenStmt,
IfElseStmt -> new IfStmtAST<>(this);
case LoopStmt -> new LoopStmtAST<>();
case WhileDoStmt -> new WhileStmtAST<>();
case DoWhileStmt -> new DoWhileStmtAST<>();
case ForStmt -> new ForStmtAST<>();
// case SwitchStmt -> new SwitchAST<>(this);
case ForIteratorStmt,
ForRangeStmt,
ForListStmt,
ForMapStmt,
ForIterableStmt -> new ForEachStmtAST<>(this);
case ContinueStmt -> new ContinueStmtAST<>();
case BreakStmt -> new BreakStmtAST<>();
case Return0Stmt,
Expand All @@ -252,9 +254,7 @@ <C> BinaryAST<C> instantiate() {
case TryCatchStmt -> new TryCatchStmtAST<>();
case TryFinallyStmt -> new TryFinallyStmtAST<>();
case AssertStmt -> new AssertStmtAST<>();

case NotImplYet -> new NotImplAST<>();

default -> throw new UnsupportedOperationException("nodeType: " + this);
};
}
Expand Down
118 changes: 118 additions & 0 deletions javatools/src/main/java/org/xvm/asm/ast/ForEachStmtAST.java
@@ -0,0 +1,118 @@
package org.xvm.asm.ast;


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.xvm.util.Handy;


/**
* A "for(lval : expr){...}" statement.
*/
public class ForEachStmtAST<C>
extends BinaryAST<C> {

private NodeType nodeType;
private ExprAST<C> lval;
private ExprAST<C> rval;
private BinaryAST<C> body;

ForEachStmtAST(NodeType nodeType) {
assert nodeTypeOk(nodeType);
this.nodeType = nodeType;
}

public ForEachStmtAST(NodeType nodeType,
ExprAST<C> lval,
ExprAST<C> rval,
BinaryAST<C> body) {
assert nodeTypeOk(nodeType) && lval != null && rval != null;

this.nodeType = nodeType;
this.lval = lval;
this.rval = rval;
this.body = body;
}

static private boolean nodeTypeOk(NodeType nodeType) {
return switch (nodeType) {
default -> false;
case ForIteratorStmt,
ForRangeStmt,
ForListStmt,
ForMapStmt,
ForIterableStmt -> true;
};
}

@Override
public NodeType nodeType() {
return nodeType;
}

public ExprAST<C> getLValue() {
return lval;
}

public ExprAST<C> getRValue() {
return rval;
}

public BinaryAST<C> getBody() {
return body;
}

@Override
protected void readBody(DataInput in, ConstantResolver<C> res)
throws IOException {
res.enter();
lval = readExprAST(in, res);
rval = readExprAST(in, res);
res.enter();
body = readAST(in, res);
res.exit();
res.exit();
}

@Override
public void prepareWrite(ConstantResolver<C> res) {
res.enter();
prepareAST(lval, res);
prepareAST(rval, res);
res.enter();
prepareAST(body, res);
res.exit();
res.exit();
}

@Override
protected void writeBody(DataOutput out, ConstantResolver<C> res)
throws IOException {
writeExprAST(lval, out, res);
writeExprAST(rval, out, res);
writeAST(body, out, res);
}

@Override
public String dump() {
StringBuilder buf = new StringBuilder("for (");
buf.append(lval.dump());
buf.append(": ");
buf.append(rval.dump());
buf.append(") ");
if (body == null) {
buf.append("{}");
} else {
buf.append('\n')
.append(Handy.indentLines(body.dump(), " "));
}
return buf.toString();
}

@Override
public String toString() {
return "for ( : ) {}";
}
}
8 changes: 6 additions & 2 deletions javatools/src/main/java/org/xvm/asm/ast/IfStmtAST.java
Expand Up @@ -22,8 +22,12 @@ public class IfStmtAST<C>
private boolean hasElse;
private BinaryAST<C> elseStmt;

IfStmtAST(boolean hasElse) {
this.hasElse = hasElse;
IfStmtAST(NodeType nodeType) {
this.hasElse = switch (nodeType) {
case IfThenStmt -> false;
case IfElseStmt -> true;
default -> throw new IllegalArgumentException("nodeType=" + nodeType);
};
}

public IfStmtAST(ExprAST<C> cond, BinaryAST<C> thenStmt) {
Expand Down
8 changes: 6 additions & 2 deletions javatools/src/main/java/org/xvm/asm/ast/StmtBlockAST.java
Expand Up @@ -25,8 +25,12 @@ public class StmtBlockAST<C>

private final boolean hasScope;

StmtBlockAST(boolean hasScope) {
this.hasScope = hasScope;
StmtBlockAST(NodeType nodeType) {
this.hasScope = switch (nodeType) {
case StmtBlock -> true;
case MultiStmt -> false;
default -> throw new IllegalArgumentException("nodeType=" + nodeType);
};
}

public StmtBlockAST(BinaryAST<C>[] stmts, Boolean hasScope) {
Expand Down

0 comments on commit 1de25f0

Please sign in to comment.