Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
issy committed Nov 25, 2024
1 parent eeaa3e5 commit b19c41c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/issy/Walker.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.issy;

import com.issy.compiler.FileMatcher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class Walker {

public List<String> walk(String startPath, FileMatcher fileMatcher) {
public List<String> walk(String startPath, Predicate<String> fileMatcher) {
try (Stream<Path> pathStream = Files.walk(Path.of(startPath))) {
return pathStream.map(Path::toString).filter(fileMatcher::matches).toList();
return pathStream.map(Path::toString).filter(fileMatcher).toList();
} catch (IOException e) {
throw new RuntimeException("Error reading file: " + e.getMessage());
} catch (SecurityException e) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/issy/compiler/CompileErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum CompileErrorMessage {
TOKEN_NOT_ALLOWED("Token not allowed"),
INVALID_FUNCTION_NAME("Invalid function name"),
FUNCTION_NOT_CALLED("Function must be called"),
FUNCTION_REQUIRES_ARGUMENT("Function requires argument");
FUNCTION_REQUIRES_ARGUMENT("Function requires argument"),
EMPTY_EXPRESSION("Empty expression not allowed");

private final String humanReadableMessage;

Expand Down
60 changes: 56 additions & 4 deletions src/main/java/com/issy/compiler/Compiler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.issy.compiler;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.function.Predicate;

import static com.issy.compiler.CompileErrorMessage.*;
import static com.issy.compiler.Token.*;
Expand All @@ -26,6 +29,7 @@ public CompilerResult compile() {
for (int index = 0; index < tokens.size(); index++) {
final TokenContext currentContext = tokens.get(index);
final Optional<TokenContext> nextContextMaybe = getContextAtIndex(index + 1);
// Must end in closing parenthesis
if (nextContextMaybe.isEmpty()) { // Final token
if (!currentContext.token().equals(CLOSE_PAREN)) {
return CompilerResult.error(MUST_END_WITH_CLOSING_PAREN);
Expand All @@ -34,22 +38,33 @@ public CompilerResult compile() {
}

final TokenContext nextContext = nextContextMaybe.get();

if (!currentContext.token().nextTokenIsAllowed(nextContext.token())) {
return CompilerResult.error(TOKEN_NOT_ALLOWED); // TODO: Get error message from somewhere
}

// Function call is valid
if (currentContext.token().equals(Token.IDENTIFIER)) {
final Optional<CompileErrorMessage> validatedFunctionCall = validateFunctionCall(currentContext, index);
if (validatedFunctionCall.isPresent()) {
return CompilerResult.error(validatedFunctionCall.get());
}
}

// If () is not function call
if (getContextAtIndex(index - 1)
.filter(_ -> currentContext.token().equals(OPEN_PAREN))
.filter(_ -> nextContext.token().equals(CLOSE_PAREN))
.filter(prevContext -> !prevContext.token().equals(IDENTIFIER))
.isPresent()) {
return CompilerResult.error(EMPTY_EXPRESSION);
}
}
// TODO: Return success result here
return CompilerResult.success((s) -> true);

return CompilerResult.success(compileGroup().compile());
}

FileMatcher implementMatcher(int index) {
Predicate<String> implementMatcher(int index) {
TokenContext currentContext = getContextAtIndex(index).orElseThrow();
Identifier identifier = Identifier.getIdentifier(currentContext.value()).orElseThrow();
return identifier.getTakesArgument() ? identifier.implement(getContextAtIndex(index + 2).orElseThrow().value()) : identifier.implement();
Expand Down Expand Up @@ -87,7 +102,44 @@ Optional<CompileErrorMessage> validateFunctionCall(TokenContext context, int ind
}

Optional<TokenContext> getContextAtIndex(int index) {
return Optional.ofNullable(index < tokens.size() ? tokens.get(index) : null);
return Optional.ofNullable(index >= 0 && index < tokens.size() ? tokens.get(index) : null);
}

FileMatcherGroup compileGroup(int startIndex, int endIndexExclusive) {
List<TokenContext> localTokens = new ArrayList<>();
FileMatcherGroup.GroupMode groupMode = null;
boolean ignoreParen = false;
for (int subIndex = startIndex; subIndex < endIndexExclusive; subIndex++) {
TokenContext currentContext = getContextAtIndex(subIndex).orElseThrow();
if (currentContext.token().equals(IDENTIFIER)) {
ignoreParen = true;
}
if (currentContext.token().equals(CLOSE_PAREN)) {
if (ignoreParen) {
ignoreParen = false;
} else {
break;
}
}
if (currentContext.token().equals(OPEN_PAREN) && !ignoreParen) {
int depth = 0;
for (int innerIndex = subIndex + 1; innerIndex < endIndexExclusive; innerIndex++) {
TokenContext innerContext = getContextAtIndex(innerIndex).orElseThrow();
if (innerContext.token().equals(OPEN_PAREN)) {
depth++;
} else if (innerContext.token().equals(CLOSE_PAREN)) {
depth--;
if (depth == 0) {

}
}
}
}
}
}

FileMatcherGroup compileGroup() {
return compileGroup(0, tokens.size() - 1);
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/issy/compiler/CompilerResult.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.issy.compiler;

import java.util.Optional;
import java.util.function.Predicate;

public record CompilerResult(Optional<FileMatcher> fileMatcher, Optional<CompileErrorMessage> compileError) {
public record CompilerResult(Optional<Predicate<String>> fileMatcher, Optional<CompileErrorMessage> compileError) {

public static CompilerResult error(CompileErrorMessage errorMessage) {
return new CompilerResult(Optional.empty(), Optional.of(errorMessage));
}

public static CompilerResult success(FileMatcher fileMatcher) {
public static CompilerResult success(Predicate<String> fileMatcher) {
return new CompilerResult(Optional.of(fileMatcher), Optional.empty());
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/issy/compiler/FileMatcher.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.issy.compiler;

@FunctionalInterface
public interface FileMatcher {

boolean matches(String filepath);
import java.util.function.Predicate;

public interface FileMatcher extends Predicate<String> {
}
9 changes: 6 additions & 3 deletions src/main/java/com/issy/compiler/FileMatcherGroup.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.issy.compiler;

import java.util.List;
import java.util.function.Predicate;

public record FileMatcherGroup(GroupMode groupMode, List<FileMatcher> fileMatchers) {

Expand All @@ -9,12 +10,14 @@ public enum GroupMode {
OR
}

public FileMatcher compile() {
public Predicate<String> compile() {
var predicate = fileMatchers.stream().reduce((a, b) -> (FileMatcher) a.and(b));

return switch (groupMode) {
case AND:
yield filepath -> fileMatchers.stream().allMatch(fileMatcher -> fileMatcher.matches(filepath));
yield filepath -> fileMatchers.stream().reduce(fileMatcher -> fileMatcher.test(filepath));
case OR:
yield filepath -> fileMatchers.stream().anyMatch(fileMatcher -> fileMatcher.matches(filepath));
yield filepath -> fileMatchers.stream().anyMatch(fileMatcher -> fileMatcher.test(filepath));
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/issy/compiler/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

public enum Identifier {
HAS_FILE_EXTENSION("hasFileExtension", true),
Expand Down Expand Up @@ -30,7 +31,7 @@ public boolean getTakesArgument() {
return takesArgument;
}

public FileMatcher implement() {
public Predicate<String> implement() {
if (takesArgument) {
throw new RuntimeException("This function should take an argument");
}
Expand All @@ -40,7 +41,7 @@ public FileMatcher implement() {
};
}

public FileMatcher implement(String arg) {
public Predicate<String> implement(String arg) {
if (!takesArgument) {
throw new RuntimeException("This function does not take an argument");
}
Expand Down

0 comments on commit b19c41c

Please sign in to comment.