Skip to content

Commit f9938a3

Browse files
committed
Compiler: First Implementation. Tests are failing.
1 parent e908f3f commit f9938a3

312 files changed

Lines changed: 9065 additions & 1613 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.bladecoder.ink.compiler;
2+
3+
import java.util.Collection;
4+
import java.util.HashSet;
5+
6+
public final class CharacterRange {
7+
public static CharacterRange define(char start, char end) {
8+
return new CharacterRange(start, end, null);
9+
}
10+
11+
public static CharacterRange define(char start, char end, Collection<Character> excludes) {
12+
return new CharacterRange(start, end, excludes);
13+
}
14+
15+
public CharacterSet toCharacterSet() {
16+
if (_correspondingCharSet.isEmpty()) {
17+
for (char c = _start; c <= _end; c++) {
18+
if (!_excludes.contains(c)) {
19+
_correspondingCharSet.add(c);
20+
}
21+
}
22+
}
23+
return _correspondingCharSet;
24+
}
25+
26+
public char getStart() {
27+
return _start;
28+
}
29+
30+
public char getEnd() {
31+
return _end;
32+
}
33+
34+
private CharacterRange(char start, char end, Collection<Character> excludes) {
35+
_start = start;
36+
_end = end;
37+
_excludes = excludes == null ? new HashSet<>() : new HashSet<>(excludes);
38+
}
39+
40+
private final char _start;
41+
private final char _end;
42+
private final Collection<Character> _excludes;
43+
private final CharacterSet _correspondingCharSet = new CharacterSet();
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.bladecoder.ink.compiler;
2+
3+
import java.util.Collection;
4+
import java.util.HashSet;
5+
6+
public class CharacterSet extends HashSet<Character> {
7+
public static CharacterSet fromRange(char start, char end) {
8+
return new CharacterSet().addRange(start, end);
9+
}
10+
11+
public CharacterSet() {
12+
}
13+
14+
public CharacterSet(String str) {
15+
addCharacters(str);
16+
}
17+
18+
public CharacterSet(CharacterSet charSetToCopy) {
19+
addCharacters(charSetToCopy);
20+
}
21+
22+
public CharacterSet addRange(char start, char end) {
23+
for (char c = start; c <= end; ++c) {
24+
add(c);
25+
}
26+
return this;
27+
}
28+
29+
public CharacterSet addCharacters(Collection<Character> chars) {
30+
for (char c : chars) {
31+
add(c);
32+
}
33+
return this;
34+
}
35+
36+
public CharacterSet addCharacters(String chars) {
37+
for (char c : chars.toCharArray()) {
38+
add(c);
39+
}
40+
return this;
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.bladecoder.ink.compiler;
2+
3+
public class CommandLineInput {
4+
public boolean isHelp;
5+
public boolean isExit;
6+
public Integer choiceInput;
7+
public Integer debugSource;
8+
public String debugPathLookup;
9+
public Object userImmediateModeStatement;
10+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.bladecoder.ink.compiler;
2+
3+
import com.bladecoder.ink.compiler.StringParser.StringParser;
4+
import com.bladecoder.ink.compiler.StringParser.StringParser.ParseRule;
5+
import java.util.List;
6+
7+
public class CommentEliminator extends StringParser {
8+
public CommentEliminator(String input) {
9+
super(input);
10+
}
11+
12+
public String process() {
13+
List<String> stringList = interleave(optional(this::commentsAndNewlines), optional(this::mainInk));
14+
15+
if (stringList != null) {
16+
return String.join("", stringList);
17+
}
18+
19+
return null;
20+
}
21+
22+
private String mainInk() {
23+
return parseUntil(this::commentsAndNewlines, commentOrNewlineStartCharacter, null);
24+
}
25+
26+
private String commentsAndNewlines() {
27+
List<String> newlines = interleave(optional(this::parseNewline), optional(this::parseSingleComment));
28+
29+
if (newlines != null) {
30+
return String.join("", newlines);
31+
}
32+
33+
return null;
34+
}
35+
36+
private Object parseSingleComment() {
37+
return oneOf(this::endOfLineComment, this::blockComment);
38+
}
39+
40+
private String endOfLineComment() {
41+
if (parseString("//") == null) {
42+
return null;
43+
}
44+
45+
parseUntilCharactersFromCharSet(newlineCharacters);
46+
47+
return "";
48+
}
49+
50+
private String blockComment() {
51+
if (parseString("/*") == null) {
52+
return null;
53+
}
54+
55+
int startLineIndex = getLineIndex();
56+
57+
String commentResult = parseUntil(stringRule("*/"), commentBlockEndCharacter, null);
58+
59+
if (!isEndOfInput()) {
60+
parseString("*/");
61+
}
62+
63+
if (commentResult != null) {
64+
return repeatChar('\n', getLineIndex() - startLineIndex);
65+
}
66+
67+
return null;
68+
}
69+
70+
private String repeatChar(char ch, int count) {
71+
StringBuilder sb = new StringBuilder();
72+
for (int i = 0; i < count; i++) {
73+
sb.append(ch);
74+
}
75+
return sb.toString();
76+
}
77+
78+
private final CharacterSet commentOrNewlineStartCharacter = new CharacterSet("/\r\n");
79+
private final CharacterSet commentBlockEndCharacter = new CharacterSet("*");
80+
private final CharacterSet newlineCharacters = new CharacterSet("\n\r");
81+
}

0 commit comments

Comments
 (0)