Skip to content

Commit

Permalink
User
Browse files Browse the repository at this point in the history
How do I use DevChat?
DevChat

Do you want to write some code or have a question about the project? Simply right-click on your chosen files or code snippets and add them to DevChat. Feel free to ask me anything or let me help you with coding.

Don't forget to check out the "+" button on the left of the input to add more context. To see a list of workflows you can run in the context, just type "/". Happy prompting!

To get started, here are some of the things that I can do for you:

/code: write code based on your prompt

/commit_message: compose a commit message based on your code changes

/release_note: draft a release note based on your latest commits

/ask-code: ask anything about your codebase and get answers from our AI agent

You can configure DevChat from Settings.

User

git diff HEAD
/commit_message
DevChat

Here is a commit message for the diff output:

commitmsg

Refactor operand to temp and add Operand base class

- Rename operand to temp in parser rules and visitor
- Add Operand base class
  - Temp and ImmValue extend Operand instead of Expr
- Change method signatures to use Operand instead of Temp
  - pushEvalOperand
  - Assign constructor and with method
- Update callers of renamed methods
  • Loading branch information
whtoo committed Nov 15, 2023
1 parent 031a2eb commit 9d36f7d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public class VMAssemblerBaseListener implements VMAssemblerListener {
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterOperand(VMAssemblerParser.OperandContext ctx) { }
@Override public void enterTemp(VMAssemblerParser.TempContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitOperand(VMAssemblerParser.OperandContext ctx) { }
@Override public void exitTemp(VMAssemblerParser.TempContext ctx) { }
/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class VMAssemblerBaseVisitor<T> extends AbstractParseTreeVisitor<T> imple
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitOperand(VMAssemblerParser.OperandContext ctx) { return visitChildren(ctx); }
@Override public T visitTemp(VMAssemblerParser.TempContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public interface VMAssemblerListener extends ParseTreeListener {
*/
void exitInstr(VMAssemblerParser.InstrContext ctx);
/**
* Enter a parse tree produced by {@link VMAssemblerParser#operand}.
* Enter a parse tree produced by {@link VMAssemblerParser#temp}.
* @param ctx the parse tree
*/
void enterOperand(VMAssemblerParser.OperandContext ctx);
void enterTemp(VMAssemblerParser.TempContext ctx);
/**
* Exit a parse tree produced by {@link VMAssemblerParser#operand}.
* Exit a parse tree produced by {@link VMAssemblerParser#temp}.
* @param ctx the parse tree
*/
void exitOperand(VMAssemblerParser.OperandContext ctx);
void exitTemp(VMAssemblerParser.TempContext ctx);
/**
* Enter a parse tree produced by {@link VMAssemblerParser#label}.
* @param ctx the parse tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class VMAssemblerParser extends Parser {
INT=11, CHAR=12, BOOL=13, STRING=14, FLOAT=15, WS=16, NEWLINE=17;
public static final int
RULE_program = 0, RULE_globals = 1, RULE_functionDeclaration = 2, RULE_instr = 3,
RULE_operand = 4, RULE_label = 5;
RULE_temp = 4, RULE_label = 5;
private static String[] makeRuleNames() {
return new String[] {
"program", "globals", "functionDeclaration", "instr", "operand", "label"
"program", "globals", "functionDeclaration", "instr", "temp", "label"
};
}
public static final String[] ruleNames = makeRuleNames();
Expand Down Expand Up @@ -349,16 +349,16 @@ public final FunctionDeclarationContext functionDeclaration() throws Recognition
@SuppressWarnings("CheckReturnValue")
public static class InstrContext extends ParserRuleContext {
public Token op;
public OperandContext a;
public OperandContext b;
public OperandContext c;
public TempContext a;
public TempContext b;
public TempContext c;
public TerminalNode NEWLINE() { return getToken(VMAssemblerParser.NEWLINE, 0); }
public TerminalNode ID() { return getToken(VMAssemblerParser.ID, 0); }
public List<OperandContext> operand() {
return getRuleContexts(OperandContext.class);
public List<TempContext> temp() {
return getRuleContexts(TempContext.class);
}
public OperandContext operand(int i) {
return getRuleContext(OperandContext.class,i);
public TempContext temp(int i) {
return getRuleContext(TempContext.class,i);
}
public InstrContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
Expand Down Expand Up @@ -401,7 +401,7 @@ public final InstrContext instr() throws RecognitionException {
setState(47);
((InstrContext)_localctx).op = match(ID);
setState(48);
((InstrContext)_localctx).a = operand();
((InstrContext)_localctx).a = temp();
setState(49);
match(NEWLINE);
}
Expand All @@ -412,11 +412,11 @@ public final InstrContext instr() throws RecognitionException {
setState(51);
((InstrContext)_localctx).op = match(ID);
setState(52);
((InstrContext)_localctx).a = operand();
((InstrContext)_localctx).a = temp();
setState(53);
match(T__5);
setState(54);
((InstrContext)_localctx).b = operand();
((InstrContext)_localctx).b = temp();
setState(55);
match(NEWLINE);
}
Expand All @@ -427,15 +427,15 @@ public final InstrContext instr() throws RecognitionException {
setState(57);
((InstrContext)_localctx).op = match(ID);
setState(58);
((InstrContext)_localctx).a = operand();
((InstrContext)_localctx).a = temp();
setState(59);
match(T__5);
setState(60);
((InstrContext)_localctx).b = operand();
((InstrContext)_localctx).b = temp();
setState(61);
match(T__5);
setState(62);
((InstrContext)_localctx).c = operand();
((InstrContext)_localctx).c = temp();
setState(63);
match(NEWLINE);
}
Expand All @@ -454,7 +454,7 @@ public final InstrContext instr() throws RecognitionException {
}

@SuppressWarnings("CheckReturnValue")
public static class OperandContext extends ParserRuleContext {
public static class TempContext extends ParserRuleContext {
public TerminalNode ID() { return getToken(VMAssemblerParser.ID, 0); }
public TerminalNode REG() { return getToken(VMAssemblerParser.REG, 0); }
public TerminalNode FUNC() { return getToken(VMAssemblerParser.FUNC, 0); }
Expand All @@ -463,28 +463,28 @@ public static class OperandContext extends ParserRuleContext {
public TerminalNode CHAR() { return getToken(VMAssemblerParser.CHAR, 0); }
public TerminalNode STRING() { return getToken(VMAssemblerParser.STRING, 0); }
public TerminalNode FLOAT() { return getToken(VMAssemblerParser.FLOAT, 0); }
public OperandContext(ParserRuleContext parent, int invokingState) {
public TempContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_operand; }
@Override public int getRuleIndex() { return RULE_temp; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof VMAssemblerListener ) ((VMAssemblerListener)listener).enterOperand(this);
if ( listener instanceof VMAssemblerListener ) ((VMAssemblerListener)listener).enterTemp(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof VMAssemblerListener ) ((VMAssemblerListener)listener).exitOperand(this);
if ( listener instanceof VMAssemblerListener ) ((VMAssemblerListener)listener).exitTemp(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof VMAssemblerVisitor ) return ((VMAssemblerVisitor<? extends T>)visitor).visitOperand(this);
if ( visitor instanceof VMAssemblerVisitor ) return ((VMAssemblerVisitor<? extends T>)visitor).visitTemp(this);
else return visitor.visitChildren(this);
}
}

public final OperandContext operand() throws RecognitionException {
OperandContext _localctx = new OperandContext(_ctx, getState());
enterRule(_localctx, 8, RULE_operand);
public final TempContext temp() throws RecognitionException {
TempContext _localctx = new TempContext(_ctx, getState());
enterRule(_localctx, 8, RULE_temp);
int _la;
try {
enterOuterAlt(_localctx, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public interface VMAssemblerVisitor<T> extends ParseTreeVisitor<T> {
*/
T visitInstr(VMAssemblerParser.InstrContext ctx);
/**
* Visit a parse tree produced by {@link VMAssemblerParser#operand}.
* Visit a parse tree produced by {@link VMAssemblerParser#temp}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitOperand(VMAssemblerParser.OperandContext ctx);
T visitTemp(VMAssemblerParser.TempContext ctx);
/**
* Visit a parse tree produced by {@link VMAssemblerParser#label}.
* @param ctx the parse tree
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.teachfx.antlr4.ep20.ir.expr;

public abstract class ImmValue extends Temp {
public abstract class ImmValue extends Operand {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import org.teachfx.antlr4.ep20.ir.IRVisitor;

public abstract class Temp extends Expr {
public abstract class Operand extends Expr {
abstract public <S,E> E accept(IRVisitor<S,E> visitor);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.teachfx.antlr4.ep20.ir.expr;

public abstract class VarSlot extends Temp {
public abstract class VarSlot extends Operand {

}
10 changes: 5 additions & 5 deletions ep20/src/main/java/org/teachfx/antlr4/ep20/ir/stmt/Assign.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.teachfx.antlr4.ep20.ir.stmt;

import org.teachfx.antlr4.ep20.ir.IRVisitor;
import org.teachfx.antlr4.ep20.ir.expr.Temp;
import org.teachfx.antlr4.ep20.ir.expr.Operand;
import org.teachfx.antlr4.ep20.ir.expr.VarSlot;

public class Assign extends Stmt {
protected VarSlot lhs;
protected Temp rhs;
protected Operand rhs;

/**
* Assign a value to a variable
Expand All @@ -24,10 +24,10 @@ public static Assign with(VarSlot lhs,VarSlot rhs) {
* @param rhs Value to assign
* @return Assign object
*/
public static Assign with(VarSlot lhs, Temp rhs) {
public static Assign with(VarSlot lhs, Operand rhs) {
return new Assign(lhs,rhs);
}
public Assign(VarSlot lhs, Temp rhs) {
public Assign(VarSlot lhs, Operand rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
Expand All @@ -36,7 +36,7 @@ public Assign(VarSlot lhs, Temp rhs) {
public VarSlot getLhs() {
return lhs;
}
public Temp getRhs() {
public Operand getRhs() {
return rhs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.teachfx.antlr4.ep20.ast.expr.*;
import org.teachfx.antlr4.ep20.ast.stmt.*;
import org.teachfx.antlr4.ep20.ast.type.TypeNode;
import org.teachfx.antlr4.ep20.ir.expr.Temp;
import org.teachfx.antlr4.ep20.ir.expr.Operand;
import org.teachfx.antlr4.ep20.pass.cfg.BasicBlock;
import org.teachfx.antlr4.ep20.ir.IRNode;
import org.teachfx.antlr4.ep20.ir.Prog;
Expand Down Expand Up @@ -382,24 +382,24 @@ public void popContinueStack() {
continueStack.pop();
}
static int cnt = 0;
protected VarSlot pushEvalOperand(Temp temp) {
protected VarSlot pushEvalOperand(Operand operand) {

if (curNode != null) {
logger.info(curNode.toString());
}

if (!(temp instanceof OperandSlot)){
if (!(operand instanceof OperandSlot)){
cnt++;
var assignee = OperandSlot.pushStack();
evalExprStack.push(assignee);
addInstr(Assign.with(assignee, temp));
addInstr(Assign.with(assignee, operand));
logger.info("-> eval stack %s%n", evalExprStack.toString());

return assignee;
} else {
logger.info("-> eval stack %s%n", evalExprStack.toString());
evalExprStack.push((VarSlot) temp);
return (VarSlot) temp;
evalExprStack.push((VarSlot) operand);
return (VarSlot) operand;
}

}
Expand Down

0 comments on commit 9d36f7d

Please sign in to comment.