Skip to content

Commit

Permalink
1. [PROJECT]::Add test cases for semtic check and ast.
Browse files Browse the repository at this point in the history
  • Loading branch information
whtoo committed Oct 7, 2023
1 parent 3f01474 commit 9889baf
Show file tree
Hide file tree
Showing 22 changed files with 244 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ep20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ graph LR
- [x] 增加类型实体和相关类型处理内容(100%)
- [x] 将作用域和变量及函数的生命周期进行关联(100%)
- [x] 编译到[ep18](..%2Fep18)的VM
-
- [x] 扩展[ep18](..%2Fep18)的VM支持更丰富的[指令实现](../ep18/VM_Design.md)
## Removed
~~- [ ] 增加类和接口定义~~
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.teachfx.antlr4.ep20.symtab.type.OperatorType;
import org.teachfx.antlr4.ep20.symtab.type.Type;

import java.util.Objects;

public class BinaryExprNode extends ExprNode{

public BinaryExprNode(OperatorType.BinaryOpType opType, ExprNode lhs, ExprNode rhs, ParserRuleContext ctx) {
Expand Down Expand Up @@ -79,4 +81,16 @@ protected void _dump(Dumper d) {
d.printMember("rhs",rhs);

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BinaryExprNode that)) return false;
return getOpType() == that.getOpType() && Objects.equals(getLhs(), that.getLhs()) && Objects.equals(getRhs(), that.getRhs());
}

@Override
public int hashCode() {
return Objects.hash(getOpType(), getLhs(), getRhs());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.teachfx.antlr4.ep20.symtab.type.Type;

import java.util.List;
import java.util.Objects;

public class CallFuncNode extends ExprNode {
@Override
Expand Down Expand Up @@ -60,4 +61,16 @@ public Type getExprType() {
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CallFuncNode that)) return false;
return Objects.equals(getArgsNode(), that.getArgsNode()) && Objects.equals(getCallFuncSymbol(), that.getCallFuncSymbol()) && Objects.equals(getFuncName(), that.getFuncName());
}

@Override
public int hashCode() {
return Objects.hash(getArgsNode(), getCallFuncSymbol(), getFuncName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.teachfx.antlr4.ep20.debugger.ast.Dumper;
import org.teachfx.antlr4.ep20.symtab.type.TypeTable;

public class FloatExprNode extends LiteralNode<Float>{
public FloatExprNode(Float literalInt, ParserRuleContext ctx) {
public class FloatExprNode extends LiteralNode<Double>{
public FloatExprNode(Double literalInt, ParserRuleContext ctx) {
this.ctx = ctx;
this.rawValue = literalInt;
this.exprType = new TypeNode(TypeTable.FLOAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.teachfx.antlr4.ep20.debugger.ast.Dumper;
import org.teachfx.antlr4.ep20.symtab.symbol.Symbol;

import java.util.Objects;

public class IDExprNode extends ExprNode {

protected String image;
Expand Down Expand Up @@ -45,4 +47,16 @@ protected void _dump(Dumper d) {
d.printMember("id",image);
d.printMember("refSymbol",getRefSymbol());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IDExprNode that)) return false;
return Objects.equals(getImage(), that.getImage()) && Objects.equals(getRefSymbol(), that.getRefSymbol());
}

@Override
public int hashCode() {
return Objects.hash(getImage(), getRefSymbol());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.teachfx.antlr4.ep20.ast.expr;

import java.util.Objects;

abstract public class LiteralNode<T> extends ExprNode {

protected T rawValue;
Expand All @@ -13,4 +15,15 @@ public void setRawValue(T rawValue) {
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LiteralNode<?> that)) return false;
return Objects.equals(getRawValue(), that.getRawValue());
}

@Override
public int hashCode() {
return Objects.hash(getRawValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ protected void _dump(Dumper d) {
super._dump(d);
d.printMember("raw",rawValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.teachfx.antlr4.ep20.symtab.type.OperatorType.UnaryOpType;

import java.util.List;
import java.util.Objects;

public class UnaryExprNode extends ExprNode{
protected ExprNode valExpr;
Expand Down Expand Up @@ -40,4 +41,16 @@ protected void _dump(Dumper d) {
public UnaryOpType getOpType() {
return opType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UnaryExprNode that)) return false;
return Objects.equals(getValExpr(), that.getValExpr()) && getOpType() == that.getOpType();
}

@Override
public int hashCode() {
return Objects.hash(getValExpr(), getOpType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public ASTNode visitPrimaryINT(CymbolParser.PrimaryINTContext ctx) {

@Override
public ASTNode visitPrimaryFLOAT(CymbolParser.PrimaryFLOATContext ctx) {
return new FloatExprNode(Float.valueOf(ctx.getText()),ctx);
return new FloatExprNode(Double.valueOf(ctx.getText()),ctx);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.teachfx.antlr4.ep20.pass.sematic;

import org.teachfx.antlr4.ep20.pass.ast.ASTBaseVisitor;

public class TypeChecker extends ASTBaseVisitor
{
public TypeChecker()
{

}

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

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

public class MethodSymbol extends ScopedSymbol implements Type {
private int LABEL_SEQ = 0;
Expand Down Expand Up @@ -93,4 +94,20 @@ public void setArgs(int args) {
public String toString() {
return "%s<%s:%s>".formatted(getName(),getScopeName(),getType());
}
public Type getReturnType() {
return getType();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MethodSymbol that)) return false;
if (!super.equals(o)) return false;
return Objects.equals(getReturnType(),((MethodSymbol) o).getReturnType()) && Objects.equals(orderedArgs, that.orderedArgs);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), orderedArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ public void define(Symbol sym) {
public void setParentScope(Scope currentScope) {
this.enclosingScope = scope;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.teachfx.antlr4.ep20.symtab.type.Type;
import org.teachfx.antlr4.ep20.symtab.scope.Scope;

import java.util.Objects;

public class Symbol implements Dumpable {
static Type UNDEFINED;

Expand Down Expand Up @@ -62,4 +64,16 @@ public int getSlotIdx() {
public void setSlotIdx(int slotIdx) {
this.slotIdx = slotIdx;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Symbol symbol)) return false;
return Objects.equals(getType(), symbol.getType()) && Objects.equals(getName(), symbol.getName());
}

@Override
public int hashCode() {
return Objects.hash(getType(), getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public VariableSymbol(String name, Type type) {
super(name, type);
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface Type {

public Type getFuncType();
public Type getPrimitiveType();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.teachfx.antlr4.ep20.ast.expr;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class BoolExprNodeTest {

@Test
void testToString() {
var boolVal = new BoolExprNode(true,null);
assertEquals("true",boolVal.getRawValue().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.teachfx.antlr4.ep20.ast.expr;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class FloatExprNodeTest {
@Test
void floatRawExtract() {

var node = new FloatExprNode(1.2,null);
assertEquals((double) 1.2, node.getRawValue());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.teachfx.antlr4.ep20.ast.expr;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StringExprNodeTest {

@Test
void testRawVal() {
var strNode = new StringExprNode("Hello",null);
assertEquals("Hello",strNode.getRawValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.teachfx.antlr4.ep20.ast.expr;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.teachfx.antlr4.ep20.symtab.type.OperatorType;

class UnaryExprNodeTest {
@Test
public void testNegInt() {
var node = new UnaryExprNode(OperatorType.UnaryOpType.NEG, new IntExprNode(1, null),null);

assertEquals(node.getOpType(),OperatorType.UnaryOpType.NEG);
assertEquals(node.getValExpr(), new IntExprNode(1, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.teachfx.antlr4.ep20.pass.sematic;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.jupiter.api.Assertions.*;

class TypeCheckerTest {

@BeforeEach
void setUp() {
}

@AfterEach
void tearDown() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.teachfx.antlr4.ep20.pass.symtab;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class LocalDefineTest {
@BeforeEach
void setUp() {
}

@AfterEach
void tearDown() {
}

@Test
void stashScope() {
}

@Test
void popScope() {
}

@Test
void getTopScope() {
}

@Test
void setTopScope() {
}
}
Loading

0 comments on commit 9889baf

Please sign in to comment.