Skip to content

Commit 8965174

Browse files
committed
Возможность импортировать несколько модулей use ["std", "types", "files"]
1 parent 52d7121 commit 8965174

File tree

9 files changed

+70
-29
lines changed

9 files changed

+70
-29
lines changed

src/main/java/com/annimon/ownlang/parser/ast/UseStatement.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.annimon.ownlang.parser.ast;
22

3+
import com.annimon.ownlang.exceptions.TypeException;
4+
import com.annimon.ownlang.lib.ArrayValue;
5+
import com.annimon.ownlang.lib.Types;
6+
import com.annimon.ownlang.lib.Value;
37
import com.annimon.ownlang.modules.Module;
48
import java.lang.reflect.Method;
59

@@ -21,24 +25,55 @@ public UseStatement(Expression expression) {
2125
@Override
2226
public void execute() {
2327
super.interruptionCheck();
28+
final Value value = expression.eval();
29+
switch (value.type()) {
30+
case Types.ARRAY:
31+
for (Value module : ((ArrayValue) value)) {
32+
loadModule(module.asString());
33+
}
34+
break;
35+
case Types.STRING:
36+
loadModule(value.asString());
37+
break;
38+
default:
39+
throw new TypeException("Array or string required");
40+
}
41+
}
42+
43+
private void loadModule(String name) {
2444
try {
25-
final String moduleName = expression.eval().asString();
26-
final Module module = (Module) Class.forName(String.format(PACKAGE, moduleName, moduleName)).newInstance();
45+
final Module module = (Module) Class.forName(String.format(PACKAGE, name, name)).newInstance();
2746
module.init();
2847
} catch (Exception ex) {
2948
throw new RuntimeException(ex);
3049
}
3150
}
3251

3352
public void loadConstants() {
53+
final Value value = expression.eval();
54+
switch (value.type()) {
55+
case Types.ARRAY:
56+
for (Value module : ((ArrayValue) value)) {
57+
loadConstants(module.asString());
58+
}
59+
break;
60+
case Types.STRING:
61+
loadConstants(value.asString());
62+
break;
63+
default:
64+
throw new TypeException("Array or string required");
65+
}
66+
}
67+
68+
private void loadConstants(String moduleName) {
3469
try {
35-
final String moduleName = expression.eval().asString();
3670
final Class<?> moduleClass = Class.forName(String.format(PACKAGE, moduleName, moduleName));
3771
final Method method = moduleClass.getMethod(INIT_CONSTANTS_METHOD);
3872
if (method != null) {
3973
method.invoke(this);
4074
}
4175
} catch (Exception ex) {
76+
// ignore
4277
}
4378
}
4479

src/main/java/com/annimon/ownlang/parser/linters/UseWithNonStringValueValidator.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.annimon.ownlang.parser.linters;
22

33
import com.annimon.ownlang.Console;
4+
import com.annimon.ownlang.lib.ArrayValue;
45
import com.annimon.ownlang.lib.Types;
56
import com.annimon.ownlang.lib.Value;
67
import com.annimon.ownlang.parser.ast.*;
@@ -23,9 +24,26 @@ public void visit(UseStatement st) {
2324
}
2425

2526
final Value value = ((ValueExpression) st.expression).value;
26-
if (value.type() != Types.STRING) {
27-
Console.error(String.format(
28-
"Warning: `use` with %s - %s, not string", Types.typeToString(value.type()), value.asString()));
27+
switch (value.type()) {
28+
case Types.STRING:
29+
// ok
30+
break;
31+
case Types.ARRAY:
32+
// ok, need additional check
33+
for (Value module : ((ArrayValue) value)) {
34+
if (module.type() != Types.STRING) {
35+
warnWrongType(module);
36+
}
37+
}
38+
break;
39+
default:
40+
warnWrongType(value);
2941
}
3042
}
43+
44+
private void warnWrongType(Value value) {
45+
Console.error(String.format(
46+
"Warning: `use` with %s - %s, not string",
47+
Types.typeToString(value.type()), value.asString()));
48+
}
3149
}

src/test/resources/modules/base64/base64.own

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use "base64"
2-
use "functional"
3-
use "types"
1+
use ["base64", "functional", "types"]
42

53
base64Example = [0x42, 0x61, 0x73, 0x65, 0x36, 0x34, 0x20, 0x45, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65]
64
base64Example_enc = [0x51, 0x6D, 0x46, 0x7A, 0x5A, 0x54, 0x59, 0x30,

src/test/resources/modules/files/files.own

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use "files"
2-
use "types"
1+
use ["files", "types"]
32

43
def testFiles() {
54
// writeLong

src/test/resources/modules/functional/stream.own

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use "std"
2-
use "functional"
3-
use "math"
1+
use ["std", "functional", "math"]
42

53
def testStream() {
64
data = [1,2,3,4,5,6,7]

src/test/resources/modules/java/classes.own

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use "std"
2-
use "java"
1+
use ["std", "java"]
32

43
def testCheckNull() {
54
assertTrue(isNull(null))

src/test/resources/modules/std/range.own

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use "std"
2-
use "types"
3-
use "functional"
1+
use ["std", "types", "functional"]
42

53
def testRangeParams() {
64
x = range(10)
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
use "std"
2-
use "yaml"
3-
use "ounit"
1+
use ["std", "yaml", "ounit"]
42

53
x = yamldecode("
64
name: \"std\"
75
scope: \"both\"
86
desc: \"Contains common functions\"
9-
desc_ru: \"�������� ��������������� ������� ������ ����������\"
7+
desc_ru: \"Содержит вспомогательные функции общего назначения\"
108
constants: []
119
functions:
1210
-
1311
name: \"arrayCombine\"
1412
args: \"keys, values\"
1513
desc: \"creates map by combining two arrays\"
16-
desc_ru: \"������ ������ �� ������ ���� ��������\"
14+
desc_ru: \"создаёт объект на основе двух массивов\"
1715
-
1816
name: \"typeof\"
1917
args: \"value\"
2018
desc: \"returns the type of value\"
21-
desc_ru: \"���������� ��� ����������� ��������\"
19+
desc_ru: \"возвращает тип переданного значения\"
2220
example: |-
2321
print typeof(1) // 1 (NUMBER)
2422
print typeof(\"text\") // 2 (STRING)
@@ -30,4 +28,4 @@ assertEquals("both", x.scope)
3028
assertEquals(0, length(x.constants))
3129
assertEquals(2, length(x.functions))
3230
assertEquals("arrayCombine", x.functions[0].name)
33-
assertEquals("���������� ��� ����������� ��������", x.functions[1].desc_ru)
31+
assertEquals("возвращает тип переданного значения", x.functions[1].desc_ru)

src/test/resources/modules/yaml/yamlencode.own

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use "std"
2-
use "yaml"
3-
use "ounit"
1+
use ["std", "yaml", "ounit"]
42

53
yml = yamlencode({
64
"name": "Yaml Example",

0 commit comments

Comments
 (0)