mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Convert OwnLang value to Java object
This commit is contained in:
parent
5d00598e8c
commit
523c76dd38
@ -2,9 +2,7 @@ package com.annimon.ownlang.lib;
|
||||
|
||||
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||
import com.annimon.ownlang.exceptions.TypeException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Represents array type.
|
||||
@ -131,6 +129,11 @@ public class ArrayValue implements Value, Iterable<Value> {
|
||||
return elements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object asJavaObject() {
|
||||
return Arrays.stream(elements).map(Value::asJavaObject).toArray(Object[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int asInt() {
|
||||
throw new TypeException("Cannot cast array to integer");
|
||||
|
@ -69,6 +69,11 @@ public class ClassInstance implements Value {
|
||||
return thisMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object asJavaObject() {
|
||||
return thisMap.asJavaObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int asInt() {
|
||||
throw new TypeException("Cannot cast class " + className + " to integer");
|
||||
|
@ -1,10 +1,7 @@
|
||||
package com.annimon.ownlang.lib;
|
||||
|
||||
import com.annimon.ownlang.exceptions.TypeException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
@ -94,6 +91,13 @@ public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object asJavaObject() {
|
||||
Map<Object, Object> result = new HashMap<>(map.size());
|
||||
map.forEach((k, v) -> result.put(k.asJavaObject(), v.asJavaObject()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int asInt() {
|
||||
throw new TypeException("Cannot cast map to integer");
|
||||
|
@ -15,4 +15,8 @@ public interface Value extends Comparable<Value> {
|
||||
String asString();
|
||||
|
||||
int type();
|
||||
|
||||
default Object asJavaObject() {
|
||||
return raw();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user