. operator as lvalue

This commit is contained in:
gnembon committed 2019-10-11 11:11:54 -03:00
1 parent 985b179cb2
commit a84b06306a
5 files changed
+67 -27

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ public class Context
static final int ITERATOR = 6;
static final int SIGNATURE = 7;
static final int LOCALIZATION = 8;
static final int LVALUE = 9;
private Map<String, LazyValue> variables = new HashMap<>();
+18 -5
View File
@@ -16,6 +16,7 @@ import carpet.script.value.AbstractListValue;
import carpet.script.value.ContainerValueInterface;
import carpet.script.value.FunctionSignatureValue;
import carpet.script.value.GlobalValue;
import carpet.script.value.LContainerValue;
import carpet.script.value.LazyListValue;
import carpet.script.value.ListValue;
import carpet.script.value.MapValue;
@@ -1242,11 +1243,18 @@ public class Expression implements Cloneable
*/
public void Operators()
{
addBinaryOperator(".", precedence.get("attribute~."),true, (container, key) ->
addLazyBinaryOperator(".", precedence.get("attribute~."),true, (c, t, container_lv, key_lv) ->
{
if (container instanceof ContainerValueInterface)
return ((ContainerValueInterface) container).get(key);
throw new InternalExpressionException("Cannot access elements of a non-container");
Value container = container_lv.evalValue(c);
if (!(container instanceof ContainerValueInterface))
throw new InternalExpressionException("Cannot access elements of a non-container");
Value address = key_lv.evalValue(c);
if (t != Context.LVALUE)
{
Value retVal = ((ContainerValueInterface) container).get(address);
return (cc, ct) -> retVal;
}
return (cc, ct) -> new LContainerValue((ContainerValueInterface) container, address);
});
addBinaryOperator("+", precedence.get("addition+-"), true, Value::add);
addBinaryOperator("-", precedence.get("addition+-"), true, Value::subtract);
@@ -1290,7 +1298,7 @@ public class Expression implements Cloneable
addLazyBinaryOperator("=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) ->
{
Value v1 = lv1.evalValue(c);
Value v1 = lv1.evalValue(c, Context.LVALUE);
Value v2 = lv2.evalValue(c);
if (v1 instanceof ListValue.ListConstructorValue && v2 instanceof ListValue)
{
@@ -1309,6 +1317,11 @@ public class Expression implements Cloneable
}
return (cc, tt) -> Value.TRUE;
}
if (v1 instanceof LContainerValue)
{
Value ret = ((LContainerValue) v1).getContainer().put(((LContainerValue) v1).getAddress(), v2);
return (cc, tt) -> ret;
}
v1.assertAssignable();
String varname = v1.getVariable();
Value copy = v2.reboundedTo(varname);
@@ -0,0 +1,30 @@
package carpet.script.value;
import carpet.script.exception.ExpressionException;
public abstract class FrameworkValue extends Value
{
@Override
public String getString()
{
throw new ExpressionException("Scarpet language component cannot be used");
}
@Override
public boolean getBoolean()
{
throw new ExpressionException("Scarpet language component cannot be used");
}
@Override
public Value clone()
{
throw new ExpressionException("Scarpet language component cannot be used");
}
@Override
public int hashCode()
{
throw new UnsupportedOperationException("Scarpet language component cannot be used as map key");
}
}
@@ -4,29 +4,13 @@ import carpet.script.exception.ExpressionException;
import java.util.List;
public class FunctionSignatureValue extends Value
public class FunctionSignatureValue extends FrameworkValue
{
private String identifier;
private List<String> arguments;
private List<String> globals;
@Override
public String getString()
{
throw new ExpressionException("Function "+identifier+" is not defined yet");
}
@Override
public boolean getBoolean()
{
throw new ExpressionException("Function "+identifier+" is not defined yet");
}
@Override
public Value clone()
{
throw new ExpressionException("Function "+identifier+" is not defined yet");
}
public FunctionSignatureValue(String name, List<String> args, List<String> globals)
{
this.identifier = name;
@@ -43,11 +27,7 @@ public class FunctionSignatureValue extends Value
}
public List<String> getGlobals() {return globals;}
@Override
public int hashCode()
{
throw new UnsupportedOperationException("function signature value cannot be used as map key");
}
}
@@ -0,0 +1,16 @@
package carpet.script.value;
public class LContainerValue extends FrameworkValue
{
private ContainerValueInterface container;
private Value address;
public LContainerValue(ContainerValueInterface c, Value v)
{
container = c;
address = v;
}
public ContainerValueInterface getContainer() {return container; }
public Value getAddress() {return address; }
}