Skip to content

Commit c74aa11

Browse files
committed
Update lang-interpreter and migrate to the new Lang Native Function system
1 parent 2bf6cca commit c74aa11

File tree

2 files changed

+64
-65
lines changed

2 files changed

+64
-65
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repositories {
1010
}
1111

1212
dependencies {
13-
compileOnly 'at.jddev0.lang:lang-interpreter:1.0.0-beta-04'
13+
compileOnly 'at.jddev0.lang:lang-interpreter:1.0.0-beta-07'
1414

1515
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
1616
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

src/main/java/me/jddev0/ExampleModule.java

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import at.jddev0.lang.DataObject.FunctionPointerObject;
77
import at.jddev0.lang.LangInterpreter.LangInterpreterInterface;
88

9+
import static at.jddev0.lang.LangFunction.*;
10+
import static at.jddev0.lang.LangFunction.LangParameter.*;
11+
912
public class ExampleModule extends LangNativeModule {
1013
@Override
1114
public DataObject load(List<DataObject> args, final int SCOPE_ID) {
@@ -35,13 +38,15 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
3538
callPredefinedFunction("println", Arrays.asList(
3639
createDataObject("Another print statement.") //createDataObject can be used instead of new DataObject().set...()
3740
), SCOPE_ID);
38-
39-
exportFunction("exampleFunction", (argumentList, INNER_SCOPE_ID) -> {
40-
List<DataObject> innerCombinedArgs = LangUtils.combineArgumentsWithoutArgumentSeparators(argumentList);
4141

42-
System.out.println("exampleFunction was called with " + innerCombinedArgs);
42+
exportNativeFunction(new Object() {
43+
@LangFunction("exampleFunction")
44+
public DataObject exampleFunctionFunction(int SCOPE_ID,
45+
@LangParameter("&args") @LangParameter.VarArgs List<DataObject> args) {
46+
System.out.println("exampleFunction was called with " + args);
4347

44-
return createDataObject(innerCombinedArgs.size());
48+
return createDataObject(args.size());
49+
}
4550
});
4651

4752
exportNormalVariable("testVar", createDataObject("This is a test variable provided by the \"" + lmc.getName() + "\" module!"));
@@ -56,30 +61,24 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
5661
exportCompositeVariableFinal("finalValues", new DataObject().setArray(new DataObject[] {
5762
createDataObject('a'), createDataObject(false)
5863
}));
59-
exportFunctionPointerVariable("calc", new DataObject().setFunctionPointer(new FunctionPointerObject((interpreter, argumentList, INNER_SCOPE_ID) -> {
60-
List<DataObject> combinedArgumentList = LangUtils.combineArgumentsWithoutArgumentSeparators(argumentList);
61-
if(combinedArgumentList.size() != 3)
62-
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARG_COUNT, "3 arguments are needed", INNER_SCOPE_ID);
63-
64-
DataObject aObj = combinedArgumentList.get(0);
65-
DataObject bObj = combinedArgumentList.get(1);
66-
DataObject cObj = combinedArgumentList.get(2);
67-
68-
Number aNum = aObj.toNumber();
69-
if(aNum == null)
70-
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARGUMENTS, "Argument 1 must be a number", INNER_SCOPE_ID);
71-
Number bNum = bObj.toNumber();
72-
if(bNum == null)
73-
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARGUMENTS, "Argument 2 must be a number", INNER_SCOPE_ID);
74-
Number cNum = cObj.toNumber();
75-
if(cNum == null)
76-
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARGUMENTS, "Argument 2 must be a number", INNER_SCOPE_ID);
77-
78-
return createDataObject(aNum.intValue() * bNum.intValue() + cNum.intValue() * cNum.intValue());
79-
})));
80-
exportFunctionPointerVariableFinal("finalFunc", new DataObject().setFunctionPointer(new FunctionPointerObject((interpreter, argumentList, INNER_SCOPE_ID) -> {
81-
return createDataObject(-42);
82-
})));
64+
exportFunctionPointerVariable("calc", new DataObject().setFunctionPointer(new FunctionPointerObject(
65+
LangNativeFunction.getSingleLangFunctionFromObject(interpreter, new Object() {
66+
@LangFunction("calc")
67+
public DataObject calcFunction(int SCOPE_ID,
68+
@LangParameter("$a") @NumberValue Number aNum,
69+
@LangParameter("$b") @NumberValue Number bNum,
70+
@LangParameter("$c") @NumberValue Number cNum) {
71+
return createDataObject(aNum.intValue() * bNum.intValue() + cNum.intValue() * cNum.intValue());
72+
}
73+
}))));
74+
exportFunctionPointerVariableFinal("finalFunc", new DataObject().setFunctionPointer(new FunctionPointerObject(
75+
LangNativeFunction.getSingleLangFunctionFromObject(interpreter, new Object() {
76+
@LangFunction("finalFunc")
77+
public DataObject finalFuncFunction(LangInterpreter interpreter, int SCOPE_ID,
78+
@LangParameter("&args") @RawVarArgs List<DataObject> ignore) {
79+
return createDataObject(-42);
80+
}
81+
}))));
8382

8483
DataObject ret = callPredefinedFunction("include", LangUtils.separateArgumentsWithArgumentSeparators(
8584
Arrays.asList(
@@ -146,46 +145,46 @@ public DataObject load(List<DataObject> args, final int SCOPE_ID) {
146145
}
147146
}
148147

149-
exportFunction("testConvertToDataObject", (argumentList, INNER_SCOPE_ID) -> {
150-
List<DataObject> combinedArgumentList = LangUtils.combineArgumentsWithoutArgumentSeparators(argumentList);
151-
if(combinedArgumentList.size() > 1)
152-
return new LangInterpreterInterface(interpreter).setErrnoErrorObject(LangInterpreter.InterpretingError.INVALID_ARG_COUNT, "0 ore 1 argument(s) are needed", INNER_SCOPE_ID);
153-
154-
if(combinedArgumentList.size() == 0) {
148+
exportNativeFunction(new Object() {
149+
@LangFunction(value="testConvertToDataObject", hasInfo=true)
150+
public DataObject testConvertToDataObjectFunction(int SCOPE_ID) {
155151
callPredefinedFunction("println", Arrays.asList(
156-
createDataObject("Call \"func.testConvertToDataObject()\" with 0 for normal output or with 1 for debug output using \"func.printDebug()\" [1 Will only work in the LangShell]")
157-
), INNER_SCOPE_ID);
152+
createDataObject("Call \"func.testConvertToDataObject()\" with 0 for normal output " +
153+
"or with 1 for debug output using \"func.printDebug()\" [1 Will only work in the LangShell]")
154+
), SCOPE_ID);
158155

159156
return null;
160157
}
161158

162-
boolean useLangShellsPrintDebug = combinedArgumentList.get(0).getBoolean();
163-
164-
printDataObjectInformation(convertToDataObject(null), useLangShellsPrintDebug, INNER_SCOPE_ID);
165-
printDataObjectInformation(convertToDataObject("text"), useLangShellsPrintDebug, INNER_SCOPE_ID);
166-
printDataObjectInformation(convertToDataObject(new Object[] {
167-
2, "test", null, Character.class
168-
}), useLangShellsPrintDebug, INNER_SCOPE_ID);
169-
printDataObjectInformation(convertToDataObject(new byte[] {
170-
2, -1, (byte)255, 127, -128, 0, (byte)'A', (byte)'B', (byte)'µ', (byte)1555
171-
}), useLangShellsPrintDebug, INNER_SCOPE_ID);
172-
printDataObjectInformation(convertToDataObject(42), useLangShellsPrintDebug, INNER_SCOPE_ID);
173-
printDataObjectInformation(convertToDataObject(true), useLangShellsPrintDebug, INNER_SCOPE_ID);
174-
printDataObjectInformation(convertToDataObject(42.2f), useLangShellsPrintDebug, INNER_SCOPE_ID);
175-
printDataObjectInformation(convertToDataObject(42.2), useLangShellsPrintDebug, INNER_SCOPE_ID);
176-
printDataObjectInformation(convertToDataObject('a'), useLangShellsPrintDebug, INNER_SCOPE_ID);
177-
printDataObjectInformation(convertToDataObject(new RuntimeException("A custom error")), useLangShellsPrintDebug, INNER_SCOPE_ID);
178-
printDataObjectInformation(convertToDataObject(new IllegalStateException("Another error")), useLangShellsPrintDebug, INNER_SCOPE_ID);
179-
printDataObjectInformation(convertToDataObject(Integer.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
180-
printDataObjectInformation(convertToDataObject(Boolean.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
181-
printDataObjectInformation(convertToDataObject(Long.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
182-
printDataObjectInformation(convertToDataObject(Class.class), useLangShellsPrintDebug, INNER_SCOPE_ID);
183-
printDataObjectInformation(convertToDataObject(Object[].class), useLangShellsPrintDebug, INNER_SCOPE_ID); //DataType.ARRAY
184-
printDataObjectInformation(convertToDataObject(DataObject[].class), useLangShellsPrintDebug, INNER_SCOPE_ID); //DataType.ARRAY
185-
printDataObjectInformation(convertToDataObject(Byte[].class), useLangShellsPrintDebug, INNER_SCOPE_ID); //DataType.ARRAY
186-
printDataObjectInformation(convertToDataObject(byte[].class), useLangShellsPrintDebug, INNER_SCOPE_ID); //ByteBuffer.ARRAY
187-
188-
return null;
159+
@LangFunction("testConvertToDataObject")
160+
public DataObject testConvertToDataObjectFunction(int SCOPE_ID,
161+
@LangParameter("usePrintDebug") @BooleanValue boolean usePrintDebug) {
162+
printDataObjectInformation(convertToDataObject(null), usePrintDebug, SCOPE_ID);
163+
printDataObjectInformation(convertToDataObject("text"), usePrintDebug, SCOPE_ID);
164+
printDataObjectInformation(convertToDataObject(new Object[] {
165+
2, "test", null, Character.class
166+
}), usePrintDebug, SCOPE_ID);
167+
printDataObjectInformation(convertToDataObject(new byte[] {
168+
2, -1, (byte)255, 127, -128, 0, (byte)'A', (byte)'B', (byte)'µ', (byte)1555
169+
}), usePrintDebug, SCOPE_ID);
170+
printDataObjectInformation(convertToDataObject(42), usePrintDebug, SCOPE_ID);
171+
printDataObjectInformation(convertToDataObject(true), usePrintDebug, SCOPE_ID);
172+
printDataObjectInformation(convertToDataObject(42.2f), usePrintDebug, SCOPE_ID);
173+
printDataObjectInformation(convertToDataObject(42.2), usePrintDebug, SCOPE_ID);
174+
printDataObjectInformation(convertToDataObject('a'), usePrintDebug, SCOPE_ID);
175+
printDataObjectInformation(convertToDataObject(new RuntimeException("A custom error")), usePrintDebug, SCOPE_ID);
176+
printDataObjectInformation(convertToDataObject(new IllegalStateException("Another error")), usePrintDebug, SCOPE_ID);
177+
printDataObjectInformation(convertToDataObject(Integer.class), usePrintDebug, SCOPE_ID);
178+
printDataObjectInformation(convertToDataObject(Boolean.class), usePrintDebug, SCOPE_ID);
179+
printDataObjectInformation(convertToDataObject(Long.class), usePrintDebug, SCOPE_ID);
180+
printDataObjectInformation(convertToDataObject(Class.class), usePrintDebug, SCOPE_ID);
181+
printDataObjectInformation(convertToDataObject(Object[].class), usePrintDebug, SCOPE_ID); //DataType.ARRAY
182+
printDataObjectInformation(convertToDataObject(DataObject[].class), usePrintDebug, SCOPE_ID); //DataType.ARRAY
183+
printDataObjectInformation(convertToDataObject(Byte[].class), usePrintDebug, SCOPE_ID); //DataType.ARRAY
184+
printDataObjectInformation(convertToDataObject(byte[].class), usePrintDebug, SCOPE_ID); //ByteBuffer.ARRAY
185+
186+
return null;
187+
}
189188
});
190189

191190
System.out.println("Test convertToDataObject() by calling \"func.testConvertToDataObject()\"");

0 commit comments

Comments
 (0)