Skip to content

Commit

Permalink
Only adding modules that are really in use
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoKrupitza committed Aug 7, 2021
1 parent 20000e3 commit bf81c8f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/sampleProject/BoProject.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Sample Project",
"description": "A description you can choose",
"main": "Mainaa.bo",
"main": "Main.bo",
"modules": {
"module1": "Module1.bo",
"module2": "Module2.bo",
Expand Down
1 change: 1 addition & 0 deletions examples/sampleProject/Main.bo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import module1;
import module2;
import module3;

import trainprinter;

Expand Down
7 changes: 7 additions & 0 deletions examples/sampleProject/Module3.bo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module module3;

// empty module never used!

function shouldNotExist() {
return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.function.Predicate.not;
Expand All @@ -26,6 +28,7 @@
*/
public class BuildAstVisitor extends BoBaseVisitor<ExpressionNode> {

private final Set<String> usedModules = new HashSet<>();
private BoSymbolTable symbolTable = null;
private List<String> importedModules = new ArrayList<>();
private String currentModuleName = "this";
Expand All @@ -40,7 +43,7 @@ public ExpressionNode visitNormalCode(BoParser.NormalCodeContext ctx) {
.map(this::visit)
.collect(Collectors.toList());

return new BoNode(processesStats);
return new BoNode(processesStats, usedModules);
}

@Override
Expand All @@ -67,7 +70,7 @@ public ExpressionNode visitModuleDef(BoParser.ModuleDefContext ctx) {

ModuleNode moduleNode = new ModuleNode(moduleName, importsInModule, functions);

return new BoNode(List.of(moduleNode));
return new BoNode(List.of(moduleNode), usedModules);
}

@Override
Expand Down Expand Up @@ -150,6 +153,10 @@ public ExpressionNode visitFuncVal(BoParser.FuncValContext ctx) {
.collect(Collectors.toList());
}

if (!FunctionFactory.getAllPredefinedModules().contains(moduleName)) {
usedModules.add(moduleName);
}

return new CallFunctionNode(funcName, moduleName, params);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.diegokrupitza.bolang.syntaxtree.nodes;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
* @author Diego Krupitza
* @version 1.0
* @date 08.07.21
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BoNode extends ExpressionNode {

private List<ExpressionNode> stats;
private Set<String> usedModules;

public BoNode() {
}

public BoNode(List<ExpressionNode> stats) {
this.stats = stats;
}

public BoNode(List<ExpressionNode> stats, Set<String> usedModules) {
this.stats = stats;
this.usedModules = usedModules;
}
}
22 changes: 8 additions & 14 deletions src/main/java/com/diegokrupitza/bolang/vm/ModulesImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ public static HashMap<String, List<FunctionNode>> importModules(BoProject boProj
}

// getting the names of the modules we want to import
Set<String> namesOfModuleToImport = importModules.stream()
.map(ImportNode::getModuleName)
.collect(Collectors.toUnmodifiableSet());
Set<String> namesOfModuleToImport = head.getUsedModules();

// populating the map with the list of all implemented functions in a module
for (String nameOfModule : namesOfModuleToImport) {

moduleFunctionMap = performImportForModule(boProject, moduleFunctionMap, nameOfModule);

}

} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -104,22 +100,20 @@ private static HashMap<String, List<FunctionNode>> performImportForModule(BoProj
// adding the current imported module to the map
moduleFunctionMap.put(nameOfModule, functionsOfModule);

// import module that the module imported
List<ImportNode> importsOfModule = moduleHead.getImports();
// getting the names of the modules we want to import
Set<String> namesOfModuleToImport = moduleBoNode.getUsedModules();

if (boProject.isExternalModule(nameOfModule)) {
if (CollectionUtils.isNotEmpty(importsOfModule)) {
throw new BoProjectException("External dependencies are not allowed to have imports! Please properly export the module you want to use!");
}
if (boProject.isExternalModule(nameOfModule) && CollectionUtils.isNotEmpty(namesOfModuleToImport)) {
throw new BoProjectException("External dependencies are not allowed to have imports! Please properly export the module you want to use!");
}

if (CollectionUtils.isNotEmpty(importsOfModule)) {
if (CollectionUtils.isNotEmpty(namesOfModuleToImport)) {
// we have imports to do!
// circular dependencies are not a problem since as soon as its once seen its get added before
// see few lines above

for (ImportNode importNodeOfModule : importsOfModule) {
moduleFunctionMap = performImportForModule(boProject, moduleFunctionMap, importNodeOfModule.getModuleName());
for (String nameOfModuleIteration : namesOfModuleToImport) {
moduleFunctionMap = performImportForModule(boProject, moduleFunctionMap, nameOfModuleIteration);
}
}

Expand Down

0 comments on commit bf81c8f

Please sign in to comment.