Skip to content

Commit c7dd50c

Browse files
committed
Значительные исправления
1 parent be9bdb0 commit c7dd50c

40 files changed

+249
-192
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ task generateJavaSources() {
3636
def source = """
3737
package com.annimon.ownlang;
3838
class Gen {
39+
private Gen() {}
3940
public static final String BUILD_DATE = "${new Date().format('YYMMdd')}";
4041
}
4142
"""

src/main/java/com/annimon/ownlang/Console.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public static void error(CharSequence value) {
5050
public static void handleException(Thread thread, Throwable throwable) {
5151
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
5252
try(final PrintStream ps = new PrintStream(baos)) {
53-
ps.printf("%s in %s\n", throwable.getMessage(), thread.getName());
53+
ps.printf("%s in %s%n", throwable.getMessage(), thread.getName());
5454
for (CallStack.CallInfo call : CallStack.getCalls()) {
55-
ps.printf("\tat %s\n", call);
55+
ps.printf("\tat %s%n", call);
5656
}
5757
ps.println();
5858
throwable.printStackTrace(ps);
@@ -74,4 +74,6 @@ public static File fileInstance(String path) {
7474
}
7575
return new File(filepath);
7676
}
77+
78+
7779
}

src/main/java/com/annimon/ownlang/Main.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,9 @@ public static String[] getOwnlangArgs() {
3333
public static void main(String[] args) throws IOException {
3434
if (args.length == 0) {
3535
try {
36-
final Options options = new Options();
37-
options.showAst = false;
38-
options.showTokens = false;
39-
options.showMeasurements = false;
40-
options.lintMode = false;
41-
options.optimizationLevel = 0;
42-
run(SourceLoader.readSource("program.own"), options);
36+
runDefault();
4337
} catch (IOException ioe) {
44-
System.out.println("OwnLang version " + VERSION + "\n\n" +
45-
"Usage: ownlang [options]\n" +
46-
" options:\n" +
47-
" -f, --file [input] Run program file. Required.\n" +
48-
" -r, --repl Enter to a REPL mode\n" +
49-
" -l, --lint Find bugs in code\n" +
50-
" -o N, --optimize N Perform optimization with N passes\n" +
51-
" -b, --beautify Beautify source code\n" +
52-
" -a, --showast Show AST of program\n" +
53-
" -t, --showtokens Show lexical tokens\n" +
54-
" -m, --showtime Show elapsed time of parsing and execution");
38+
printUsage();
5539
}
5640
return;
5741
}
@@ -139,6 +123,30 @@ public static void main(String[] args) throws IOException {
139123
run(input, options);
140124
}
141125

126+
private static void runDefault() throws IOException {
127+
final Options options = new Options();
128+
options.showAst = false;
129+
options.showTokens = false;
130+
options.showMeasurements = false;
131+
options.lintMode = false;
132+
options.optimizationLevel = 0;
133+
run(SourceLoader.readSource("program.own"), options);
134+
}
135+
136+
private static void printUsage() {
137+
System.out.println("OwnLang version " + VERSION + "\n\n" +
138+
"Usage: ownlang [options]\n" +
139+
" options:\n" +
140+
" -f, --file [input] Run program file. Required.\n" +
141+
" -r, --repl Enter to a REPL mode\n" +
142+
" -l, --lint Find bugs in code\n" +
143+
" -o N, --optimize N Perform optimization with N passes\n" +
144+
" -b, --beautify Beautify source code\n" +
145+
" -a, --showast Show AST of program\n" +
146+
" -t, --showtokens Show lexical tokens\n" +
147+
" -m, --showtime Show elapsed time of parsing and execution");
148+
}
149+
142150
private static void createOwnLangArgs(String[] javaArgs, int index) {
143151
if (index >= javaArgs.length) return;
144152
ownlangArgs = new String[javaArgs.length - index];
@@ -152,7 +160,8 @@ private static void run(String input, Options options) {
152160
final List<Token> tokens = Lexer.tokenize(input);
153161
measurement.stop("Tokenize time");
154162
if (options.showTokens) {
155-
for (int i = 0; i < tokens.size(); i++) {
163+
final int tokensCount = tokens.size();
164+
for (int i = 0; i < tokensCount; i++) {
156165
System.out.println(i + " " + tokens.get(i));
157166
}
158167
}
@@ -205,16 +214,16 @@ private static class Options {
205214
boolean lintMode;
206215
int optimizationLevel;
207216

208-
public Options() {
217+
Options() {
209218
showTokens = false;
210219
showAst = false;
211220
showMeasurements = false;
212221
lintMode = false;
213222
optimizationLevel = 0;
214223
}
215224

216-
public void validate() {
217-
if (lintMode == true) {
225+
void validate() {
226+
if (lintMode) {
218227
showTokens = false;
219228
showAst = false;
220229
showMeasurements = false;

src/main/java/com/annimon/ownlang/lib/Arguments.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public final class Arguments {
66

7+
private Arguments() { }
8+
79
public static void check(int expected, int got) {
810
if (got != expected) throw new ArgumentsMismatchException(String.format(
911
"%d %s expected, got %d", expected, pluralize(expected), got));

src/main/java/com/annimon/ownlang/lib/CallStack.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
public final class CallStack {
77

8-
private static final Deque<CallInfo> calls = new ConcurrentLinkedDeque<CallInfo>();;
8+
private static final Deque<CallInfo> calls = new ConcurrentLinkedDeque<>();
9+
10+
private CallStack() { }
911

1012
public static synchronized void clear() {
1113
calls.clear();

src/main/java/com/annimon/ownlang/lib/Functions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public final class Functions {
1515
functions = new HashMap<>();
1616
}
1717

18+
private Functions() { }
19+
1820
public static void clear() {
1921
functions.clear();
2022
}

src/main/java/com/annimon/ownlang/lib/NumberValue.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
*/
77
public final class NumberValue implements Value {
88

9-
public static final NumberValue MINUS_ONE, ZERO, ONE;
9+
private static final int CACHE_MIN = -128;
10+
private static final int CACHE_MAX = 127;
11+
12+
public static final NumberValue MINUS_ONE;
13+
public static final NumberValue ZERO;
14+
public static final NumberValue ONE;
1015

11-
private static final int CACHE_MIN = -128, CACHE_MAX = 127;
1216
private static final NumberValue[] NUMBER_CACHE;
17+
1318
static {
1419
final int length = CACHE_MAX - CACHE_MIN + 1;
1520
NUMBER_CACHE = new NumberValue[length];

src/main/java/com/annimon/ownlang/lib/Types.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public final class Types {
1010
MAP = 4,
1111
FUNCTION = 5;
1212

13-
private static final int FIRST = OBJECT, LAST = FUNCTION;
13+
private static final int FIRST = OBJECT;
14+
private static final int LAST = FUNCTION;
1415
private static final String[] NAMES = {"object", "number", "string", "array", "map", "function"};
1516

1617
public static String typeToString(int type) {
@@ -19,4 +20,6 @@ public static String typeToString(int type) {
1920
}
2021
return "unknown (" + type + ")";
2122
}
23+
24+
private Types() { }
2225
}

src/main/java/com/annimon/ownlang/lib/ValueUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
public final class ValueUtils {
1010

11+
private ValueUtils() { }
12+
1113
public static Object toObject(Value val) {
1214
switch (val.type()) {
1315
case Types.ARRAY:
@@ -101,7 +103,7 @@ public static byte[] toByteArray(ArrayValue array) {
101103
return result;
102104
}
103105

104-
public static Function consumeFunction(Value value, int argumentNumber) throws TypeException {
106+
public static Function consumeFunction(Value value, int argumentNumber) {
105107
final int type = value.type();
106108
if (type != Types.FUNCTION) {
107109
throw new TypeException("Function expected at argument " + (argumentNumber + 1)

src/main/java/com/annimon/ownlang/lib/Variables.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private static class ScopeFindData {
3535
Variables.clear();
3636
}
3737

38+
private Variables() { }
39+
3840
public static Map<String, Value> variables() {
3941
return scope.variables;
4042
}

0 commit comments

Comments
 (0)