Skip to content

Commit 039b46e

Browse files
committed
Add tokenizer
0 parents  commit 039b46e

23 files changed

+1395
-0
lines changed

.gitignore

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Maven
2+
target/
3+
pom.xml.tag
4+
pom.xml.releaseBackup
5+
pom.xml.versionsBackup
6+
pom.xml.next
7+
release.properties
8+
dependency-reduced-pom.xml
9+
buildNumber.properties
10+
.mvn/timing.properties
11+
.mvn/wrapper/maven-wrapper.jar
12+
13+
# Java
14+
*.class
15+
*.jar
16+
*.war
17+
*.ear
18+
*.log
19+
hs_err_pid*
20+
replay_pid*
21+
22+
# IDE - IntelliJ IDEA
23+
.idea/
24+
*.iml
25+
*.iws
26+
*.ipr
27+
out/
28+
29+
# IDE - Eclipse
30+
.project
31+
.classpath
32+
.settings/
33+
.metadata/
34+
bin/
35+
36+
# IDE - VS Code
37+
.vscode/
38+
*.code-workspace
39+
40+
# IDE - NetBeans
41+
nbproject/private/
42+
nbbuild/
43+
dist/
44+
nbdist/
45+
.nb-gradle/
46+
47+
# macOS
48+
.DS_Store
49+
.AppleDouble
50+
.LSOverride
51+
52+
# Build artifacts
53+
*.orig
54+
55+
# Test coverage
56+
*.exec
57+
jacoco.xml
58+
59+
# Logs
60+
logs/
61+
*.log
62+
63+
# Temporary files
64+
*.tmp
65+
*.bak
66+
*.swp
67+
*~

pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.forthix</groupId>
9+
<artifactId>forthic-java</artifactId>
10+
<version>0.1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<name>Forthic Java Runtime</name>
14+
<description>Java implementation of the Forthic language interpreter</description>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>15</maven.compiler.source>
19+
<maven.compiler.target>15</maven.compiler.target>
20+
<junit.version>5.10.0</junit.version>
21+
</properties>
22+
23+
<dependencies>
24+
<!-- JUnit 5 for testing -->
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-api</artifactId>
28+
<version>${junit.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
<version>${junit.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-params</artifactId>
40+
<version>${junit.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.11.0</version>
51+
<configuration>
52+
<source>15</source>
53+
<target>15</target>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>3.1.2</version>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.forthix.forthic.errors;
2+
3+
/**
4+
* Represents a location in Forthic source code.
5+
* Used for error reporting and debugging.
6+
*/
7+
public class CodeLocation {
8+
private final String screenName;
9+
private final int line;
10+
private final int column;
11+
private final int startPos;
12+
private final int endPos;
13+
14+
public CodeLocation(String screenName, int line, int column, int startPos, int endPos) {
15+
this.screenName = screenName;
16+
this.line = line;
17+
this.column = column;
18+
this.startPos = startPos;
19+
this.endPos = endPos;
20+
}
21+
22+
public CodeLocation() {
23+
this("<ad-hoc>", 1, 1, 0, 0);
24+
}
25+
26+
public String getScreenName() {
27+
return screenName;
28+
}
29+
30+
public int getLine() {
31+
return line;
32+
}
33+
34+
public int getColumn() {
35+
return column;
36+
}
37+
38+
public int getStartPos() {
39+
return startPos;
40+
}
41+
42+
public int getEndPos() {
43+
return endPos;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return String.format("%s:%d:%d", screenName, line, column);
49+
}
50+
51+
@Override
52+
public boolean equals(Object obj) {
53+
if (this == obj) return true;
54+
if (obj == null || getClass() != obj.getClass()) return false;
55+
CodeLocation that = (CodeLocation) obj;
56+
return line == that.line &&
57+
column == that.column &&
58+
startPos == that.startPos &&
59+
endPos == that.endPos &&
60+
screenName.equals(that.screenName);
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
int result = screenName.hashCode();
66+
result = 31 * result + line;
67+
result = 31 * result + column;
68+
result = 31 * result + startPos;
69+
result = 31 * result + endPos;
70+
return result;
71+
}
72+
73+
/**
74+
* Builder for constructing CodeLocation with optional parameters
75+
*/
76+
public static class Builder {
77+
private String screenName = "<ad-hoc>";
78+
private int line = 1;
79+
private int column = 1;
80+
private int startPos = 0;
81+
private int endPos = 0;
82+
83+
public Builder screenName(String screenName) {
84+
this.screenName = screenName;
85+
return this;
86+
}
87+
88+
public Builder line(int line) {
89+
this.line = line;
90+
return this;
91+
}
92+
93+
public Builder column(int column) {
94+
this.column = column;
95+
return this;
96+
}
97+
98+
public Builder startPos(int startPos) {
99+
this.startPos = startPos;
100+
return this;
101+
}
102+
103+
public Builder endPos(int endPos) {
104+
this.endPos = endPos;
105+
return this;
106+
}
107+
108+
public CodeLocation build() {
109+
return new CodeLocation(screenName, line, column, startPos, endPos);
110+
}
111+
}
112+
113+
public static Builder builder() {
114+
return new Builder();
115+
}
116+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.forthix.forthic.errors;
2+
3+
public class ExtraSemicolonError extends ForthicError {
4+
public ExtraSemicolonError(String forthic, CodeLocation location, Throwable cause) {
5+
super(forthic, "Extra semicolon", location, cause);
6+
}
7+
8+
public ExtraSemicolonError(String forthic, CodeLocation location) {
9+
this(forthic, location, null);
10+
}
11+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.forthix.forthic.errors;
2+
3+
/**
4+
* Base exception class for all Forthic interpreter errors.
5+
*/
6+
public class ForthicError extends Exception {
7+
private final String forthic;
8+
private final String note;
9+
private final CodeLocation location;
10+
11+
public ForthicError(String forthic, String note, CodeLocation location, Throwable cause) {
12+
super(note, cause);
13+
this.forthic = forthic;
14+
this.note = note;
15+
this.location = location;
16+
}
17+
18+
public ForthicError(String forthic, String note, CodeLocation location) {
19+
this(forthic, note, location, null);
20+
}
21+
22+
public ForthicError(String forthic, String note) {
23+
this(forthic, note, null, null);
24+
}
25+
26+
public String getForthic() {
27+
return forthic;
28+
}
29+
30+
public String getNote() {
31+
return note;
32+
}
33+
34+
public CodeLocation getLocation() {
35+
return location;
36+
}
37+
38+
public String getDescription() {
39+
StringBuilder sb = new StringBuilder();
40+
sb.append(note);
41+
if (location != null) {
42+
sb.append(" at ").append(location);
43+
}
44+
return sb.toString();
45+
}
46+
47+
@Override
48+
public String getMessage() {
49+
return getDescription();
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.forthix.forthic.errors;
2+
3+
/**
4+
* Thrown when execution is intentionally stopped (e.g., by .s or .S debug words)
5+
*/
6+
public class IntentionalStopError extends RuntimeException {
7+
public IntentionalStopError(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.forthix.forthic.errors;
2+
3+
public class InvalidInputPositionError extends RuntimeException {
4+
public InvalidInputPositionError(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.forthix.forthic.errors;
2+
3+
public class InvalidVariableNameError extends ForthicError {
4+
private final String varname;
5+
6+
public InvalidVariableNameError(String forthic, String varname, CodeLocation location, Throwable cause) {
7+
super(forthic, "Invalid variable name: " + varname, location, cause);
8+
this.varname = varname;
9+
}
10+
11+
public InvalidVariableNameError(String forthic, String varname, CodeLocation location) {
12+
this(forthic, varname, location, null);
13+
}
14+
15+
public String getVarname() {
16+
return varname;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.forthix.forthic.errors;
2+
3+
public class InvalidWordNameError extends RuntimeException {
4+
public InvalidWordNameError(String forthic, CodeLocation location, String note, Throwable cause) {
5+
super(note != null ? note : "Invalid word name", cause);
6+
}
7+
8+
public InvalidWordNameError(String forthic, CodeLocation location, String note) {
9+
this(forthic, location, note, null);
10+
}
11+
12+
public InvalidWordNameError(String forthic, CodeLocation location) {
13+
this(forthic, location, "Invalid word name", null);
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.forthix.forthic.errors;
2+
3+
public class MissingSemicolonError extends ForthicError {
4+
public MissingSemicolonError(String forthic, CodeLocation location, Throwable cause) {
5+
super(forthic, "Missing semicolon", location, cause);
6+
}
7+
8+
public MissingSemicolonError(String forthic, CodeLocation location) {
9+
this(forthic, location, null);
10+
}
11+
}

0 commit comments

Comments
 (0)