From 44c32ba2e83675a966e1650088e62c81c6d29cfc Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Wed, 26 Sep 2018 21:13:54 +0200 Subject: [PATCH] Added current path to includes. --- src/main/assembly/assembly.xml | 4 --- .../java/dk/camelot64/kickc/Compiler.java | 26 ++++++++------ .../Pass0GenerateStatementSequence.java | 35 ++++++++++++------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/main/assembly/assembly.xml b/src/main/assembly/assembly.xml index 985de778d..807dd1340 100644 --- a/src/main/assembly/assembly.xml +++ b/src/main/assembly/assembly.xml @@ -1,11 +1,8 @@ bin - zip - - false @@ -13,7 +10,6 @@ false - src/main/kc/stdlib diff --git a/src/main/java/dk/camelot64/kickc/Compiler.java b/src/main/java/dk/camelot64/kickc/Compiler.java index 64501b0d4..2e862c2dd 100644 --- a/src/main/java/dk/camelot64/kickc/Compiler.java +++ b/src/main/java/dk/camelot64/kickc/Compiler.java @@ -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 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 importPaths = program.getImportPaths(); - for(String importPath : importPaths) { + public static File loadFile(String fileName, Path currentPath, Program program) { + List 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); diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java index 2b5293ce9..72945e58e 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java @@ -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 { - private Program program; - private Stack 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 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 { 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 { 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 { 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 { throw new CompileError("Error! Unhandled symbol " + symbol.toString(program)); } - public StatementSequence getSequence() { - return sequence; - } - /** A declaration directive. */ private interface Directive { }