1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Added current path to includes.

This commit is contained in:
jespergravgaard 2018-09-26 21:13:54 +02:00
parent b777a6bd98
commit 44c32ba2e8
3 changed files with 38 additions and 27 deletions

View File

@ -1,11 +1,8 @@
<assembly>
<id>bin</id>
<!-- Specifies that our binary distribution is a zip package -->
<formats>
<format>zip</format>
</formats>
<!-- Adds the dependencies of our application to the lib directory -->
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
@ -13,7 +10,6 @@
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/kc/stdlib</directory>

View File

@ -13,6 +13,7 @@ import org.antlr.v4.runtime.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@ -27,12 +28,12 @@ public class Compiler {
this.program = new Program();
}
public static void loadAndParseFile(String fileName, Program program, Pass0GenerateStatementSequence pass0GenerateStatementSequence, boolean isImport) {
public static void loadAndParseFile(String fileName, Program program, Path currentPath) {
try {
if(!fileName.endsWith(".kc")) {
fileName += ".kc";
}
File file = loadFile(fileName, program);
File file = loadFile(fileName, currentPath, program);
List<String> imported = program.getImported();
if(imported.contains(file.getAbsolutePath())) {
return;
@ -58,15 +59,18 @@ public class Compiler {
throw new CompileError("Error parsing file " + fileStream.getSourceName() + "\n - Line: " + line + "\n - Message: " + msg);
}
});
pass0GenerateStatementSequence.generate(parser.file());
Pass0GenerateStatementSequence pass0GenerateStatementSequence = new Pass0GenerateStatementSequence(file, parser.file(), program);
pass0GenerateStatementSequence.generate();
} catch(IOException e) {
throw new CompileError("Error loading file " + fileName, e);
}
}
public static File loadFile(String fileName, Program program) {
List<String> importPaths = program.getImportPaths();
for(String importPath : importPaths) {
public static File loadFile(String fileName, Path currentPath, Program program) {
List<String> searchPaths = new ArrayList<>();
searchPaths.add(currentPath.toString());
searchPaths.addAll(program.getImportPaths());
for(String importPath : searchPaths) {
if(!importPath.endsWith("/")) {
importPath += "/";
}
@ -87,12 +91,14 @@ public class Compiler {
program.getImportPaths().add(path);
}
public Program compile(String fileName) throws IOException {
public Program compile(String fileName) {
program.setFileName(fileName);
program.setStatementSequence(new StatementSequence());
try {
Pass0GenerateStatementSequence pass0GenerateStatementSequence = new Pass0GenerateStatementSequence(program);
loadAndParseFile(fileName, program, pass0GenerateStatementSequence, false);
StatementSequence sequence = pass0GenerateStatementSequence.getSequence();
File currentPath = new File(".");
loadAndParseFile(fileName, program, currentPath.toPath());
StatementSequence sequence = program.getStatementSequence();
sequence.addStatement(new StatementCall(null, "main", new ArrayList<>(), new StatementSource(RuleContext.EMPTY)));
program.setStatementSequence(sequence);

View File

@ -21,6 +21,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@ -32,15 +33,25 @@ import java.util.regex.Pattern;
*/
public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
private Program program;
private Stack<Scope> scopeStack;
private StatementSequence sequence;
/** The source file currently being parsed. */
private File file;
/** The source ANTLR parse tree of the source file. */
private KickCParser.FileContext fileCtx;
public Pass0GenerateStatementSequence(Program program) {
/** The program containing all compile structures. */
private Program program;
/** Used to build the statements of the source file. */
private StatementSequence sequence;
/** Used to build the scopes of the source file. */
private Stack<Scope> scopeStack;
public Pass0GenerateStatementSequence(File file, KickCParser.FileContext fileCtx, Program program) {
this.file = file;
this.fileCtx = fileCtx;
this.program = program;
this.sequence = program.getStatementSequence();
this.scopeStack = new Stack<>();
scopeStack.push(program.getScope());
this.sequence = new StatementSequence();
}
private Scope getCurrentSymbols() {
@ -56,8 +67,8 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
return null;
}
public void generate(KickCParser.FileContext file) {
this.visit(file);
public void generate() {
this.visit(fileCtx);
}
@Override
@ -82,7 +93,8 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
if(program.getLog().isVerboseParse()) {
program.getLog().append("Importing " + importFileName);
}
Compiler.loadAndParseFile(importFileName, program, this, true);
Path currentPath = file.toPath().getParent();
Compiler.loadAndParseFile(importFileName, program, currentPath);
return null;
}
@ -268,7 +280,8 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
TerminalNode resource = ctx.STRING();
String resourceName = resource.getText();
resourceName = resourceName.substring(1, resourceName.length() - 1);
File resourceFile = Compiler.loadFile(resourceName, program);
Path currentPath = file.toPath().getParent();
File resourceFile = Compiler.loadFile(resourceName, currentPath, program);
program.addAsmResourceFile(resourceFile.toPath());
if(program.getLog().isVerboseParse()) {
program.getLog().append("Added resource " + resourceFile.getPath().replace('\\', '/'));
@ -885,10 +898,6 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
throw new CompileError("Error! Unhandled symbol " + symbol.toString(program));
}
public StatementSequence getSequence() {
return sequence;
}
/** A declaration directive. */
private interface Directive {
}