Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
modified tests to check fucntion def and call correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
nils bohland committed May 29, 2024
1 parent 11d018f commit 177c9f1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public ASTTypeNode getType(){
public ASTParamLstNode getParams(){
return getChild(ASTParamLstNode.class,0);
}
public ASTStmtLstNode getBody(){
return getChild(ASTStmtLstNode.class,0);
public ASTLogicNode getBody(){
return getChild(ASTLogicNode.class,0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Getter
@Setter
public class ASTParamLstNode extends ASTNode {

List<String> paramNames;
List<String> paramNames = new ArrayList<>();

@Override
public <T> T accept(ASTVisitor<T> visitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,12 @@ public ASTParamLstNode parseParamLst() {
enterNode(node);

parseType();
node.addParamName(lexer.getToken().getText());
lexer.expect(TokenType.TOK_IDENTIFIER);
while (lexer.getToken().getType() == TokenType.TOK_COMMA) {
lexer.expect(TokenType.TOK_COMMA);
parseType();
node.addParamName(lexer.getToken().getText());
lexer.expect(TokenType.TOK_IDENTIFIER);
}
exitNode(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ public Void visitFctDef(ASTFctDefNode node) {

currentScopes.push(functionScope);

//visit(node.getBody());

visit(node.getParams());
visit(node.getBody());

currentScopes.pop();

Expand Down Expand Up @@ -194,8 +195,8 @@ public Void visitFctCall(ASTFctCallNode node) {

visitChildren(node);

if (currentScopes.peek().lookupSymbolStrict(node.getName(), node) == null) {
throw new SemaError(node, "Function not defined");
if (currentScopes.peek().lookupSymbol(node.getName(), node) == null) {
throw new SemaError(node, "Function with name " + node.getName() + " not defined");
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.auberer.compilerdesignlectureproject.parser;

import com.auberer.compilerdesignlectureproject.ast.ASTCallParamsNode;
import com.auberer.compilerdesignlectureproject.ast.ASTFctCallNode;
import com.auberer.compilerdesignlectureproject.ast.*;
import com.auberer.compilerdesignlectureproject.lexer.Lexer;
import com.auberer.compilerdesignlectureproject.lexer.Token;
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableEntry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,14 +61,35 @@ void testFunctionCall() {
@Test
@DisplayName("Integration test for function call")
void testIntegrationTestForFunctionCall() {
String fctDef = "call myFunc(7);";
String fctDef = "func int myFunc(int x) int i = 17; return x; cnuf func int fn2() call myFunc(7); return 2; cnuf";

Reader reader = new Reader(fctDef);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);

ASTFctCallNode astFctCallNode = parser.parseFctCall();
ASTEntryNode parseNode = parser.parse();

ASTFctDefNode defNode = parseNode.getChild(ASTFctDefNode.class, 0);
ASTFctDefNode defWithCallNode = parseNode.getChild(ASTFctDefNode.class, 1);

ASTFctCallNode callNode = defWithCallNode.getChild(ASTLogicNode.class, 0).getChild(ASTStmtLstNode.class, 0).getChild(ASTStmtNode.class, 0).getChild(ASTAssignStmtNode.class, 0).getChild(ASTLogicalExprNode.class, 0).getChild(ASTCompareExprNode.class, 0).getChild(ASTAdditiveExprNode.class, 0).getChild(ASTMultiplicativeExprNode.class, 0).getChild(ASTPrefixExprNode.class, 0).getChild(ASTAtomicExprNode.class, 0).getFctCall();

SymbolTableBuilder symboltablebuilder = new SymbolTableBuilder();
symboltablebuilder.visitFctDef(defNode);
symboltablebuilder.visitFctCall(callNode);
}

symboltablebuilder.visitFctCall(astFctCallNode);
private static void searchNodeForFctCallNode(ASTNode node, String path) {
if(node.getChildren().isEmpty()){
System.out.println("no node found for path: " + path);
}
node.getChildren().forEach((child) -> {
if (child instanceof ASTFctCallNode) {
System.out.println(path + "ASTFctCallNode");
}
else {
searchNodeForFctCallNode(child, path + child.getClass().toString()+ " -> ");
}
});
}
}

0 comments on commit 177c9f1

Please sign in to comment.