Skip to content

Commit 8eb25ef

Browse files
committed
Оператор include
1 parent 3e16e49 commit 8eb25ef

File tree

10 files changed

+96
-11
lines changed

10 files changed

+96
-11
lines changed

program.own

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,6 @@ println `extended word variable`
240240
def `::`(v1, v2) = string(v1) + string(v2)
241241
println 1 :: 2 :: 3
242242

243-
println "\u042a"
243+
println "\u042a"
244+
245+
include "visitor.own"

src/com/annimon/ownlang/Main.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import com.annimon.ownlang.lib.CallStack;
44
import com.annimon.ownlang.parser.Lexer;
55
import com.annimon.ownlang.parser.Parser;
6+
import com.annimon.ownlang.parser.SourceLoader;
67
import com.annimon.ownlang.parser.Token;
78
import com.annimon.ownlang.parser.ast.Statement;
89
import com.annimon.ownlang.parser.visitors.AssignValidator;
910
import com.annimon.ownlang.parser.visitors.FunctionAdder;
1011
import com.annimon.ownlang.parser.visitors.VariablePrinter;
1112
import java.io.IOException;
12-
import java.nio.file.Files;
13-
import java.nio.file.Paths;
1413
import java.util.List;
1514
import java.util.concurrent.TimeUnit;
1615

@@ -21,7 +20,7 @@ public final class Main {
2120

2221
public static void main(String[] args) throws IOException {
2322
if (args.length == 0) {
24-
run(readFile("program.own"), true, true, true);
23+
run(SourceLoader.readSource("program.own"), true, true, true);
2524
return;
2625
}
2726

@@ -47,7 +46,7 @@ public static void main(String[] args) throws IOException {
4746
case "-f":
4847
case "--file":
4948
if (i + 1 < args.length) {
50-
input = readFile(args[i + 1]);
49+
input = SourceLoader.readSource(args[i + 1]);
5150
i++;
5251
}
5352
break;
@@ -62,10 +61,6 @@ public static void main(String[] args) throws IOException {
6261
run(input, showTokens, showAst, showMeasurements);
6362
}
6463

65-
private static String readFile(String file) throws IOException {
66-
return new String( Files.readAllBytes(Paths.get(file)), "UTF-8");
67-
}
68-
6964
private static void run(String input, boolean showTokens, boolean showAst, boolean showMeasurements) {
7065
final TimeMeasurement measurement = new TimeMeasurement();
7166
measurement.start("Tokenize time");

src/com/annimon/ownlang/parser/Lexer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public final class Lexer {
9191
KEYWORDS.put("match", TokenType.MATCH);
9292
KEYWORDS.put("case", TokenType.CASE);
9393
KEYWORDS.put("extract", TokenType.EXTRACT);
94+
KEYWORDS.put("include", TokenType.INCLUDE);
9495
}
9596

9697
private final String input;

src/com/annimon/ownlang/parser/Parser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ private Statement statement() {
129129
if (match(TokenType.USE)) {
130130
return new UseStatement(expression());
131131
}
132+
if (match(TokenType.INCLUDE)) {
133+
return new IncludeStatement(expression());
134+
}
132135
if (match(TokenType.FOR)) {
133136
return forStatement();
134137
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.annimon.ownlang.parser;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
9+
public final class SourceLoader {
10+
11+
public static String readSource(String name) throws IOException {
12+
InputStream is = SourceLoader.class.getResourceAsStream(name);
13+
if (is != null) return readStream(is);
14+
15+
is = new FileInputStream(name);
16+
return readStream(is);
17+
}
18+
19+
private static String readStream(InputStream is) throws IOException {
20+
final StringBuilder text = new StringBuilder();
21+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
22+
String line;
23+
while ((line = reader.readLine()) != null) {
24+
text.append(line);
25+
text.append(System.lineSeparator());
26+
}
27+
}
28+
return text.toString();
29+
}
30+
}

src/com/annimon/ownlang/parser/TokenType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public enum TokenType {
2727
MATCH,
2828
CASE,
2929
EXTRACT,
30+
INCLUDE,
3031

3132
PLUS, // +
3233
MINUS, // -
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.annimon.ownlang.parser.ast;
2+
3+
import com.annimon.ownlang.parser.Lexer;
4+
import com.annimon.ownlang.parser.Parser;
5+
import com.annimon.ownlang.parser.SourceLoader;
6+
import com.annimon.ownlang.parser.Token;
7+
import com.annimon.ownlang.parser.visitors.FunctionAdder;
8+
import java.util.List;
9+
10+
/**
11+
*
12+
* @author aNNiMON
13+
*/
14+
public final class IncludeStatement implements Statement {
15+
16+
public final Expression expression;
17+
18+
public IncludeStatement(Expression expression) {
19+
this.expression = expression;
20+
}
21+
22+
@Override
23+
public void execute() {
24+
try {
25+
final String input = SourceLoader.readSource(expression.eval().asString());
26+
final List<Token> tokens = new Lexer(input).tokenize();
27+
final Parser parser = new Parser(tokens);
28+
final Statement program = parser.parse();
29+
if (!parser.getParseErrors().hasErrors()) {
30+
program.accept(new FunctionAdder());
31+
program.execute();
32+
}
33+
} catch (Exception ex) {
34+
throw new RuntimeException(ex);
35+
}
36+
}
37+
38+
@Override
39+
public void accept(Visitor visitor) {
40+
visitor.visit(this);
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "include " + expression;
46+
}
47+
}

src/com/annimon/ownlang/parser/ast/Visitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface Visitor {
2424
void visit(ExprStatement s);
2525
void visit(FunctionalExpression s);
2626
void visit(IfStatement s);
27+
void visit(IncludeStatement s);
2728
void visit(MapExpression s);
2829
void visit(MatchExpression s);
2930
void visit(PrintStatement s);

src/com/annimon/ownlang/parser/visitors/AbstractVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public void visit(IfStatement s) {
118118
}
119119
}
120120

121+
@Override
122+
public void visit(IncludeStatement s) {
123+
s.expression.accept(this);
124+
}
125+
121126
@Override
122127
public void visit(MapExpression s) {
123128
for (Map.Entry<Expression, Expression> entry : s.elements.entrySet()) {

visitor.own

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
func()
2-
def func() print "function\n"
1+
function()
2+
def function() print "function\n"
33

44
a = 2 + 3 * 4
55
print a

0 commit comments

Comments
 (0)