Skip to content

Commit

Permalink
Add String variables, real ones this time.
Browse files Browse the repository at this point in the history
  • Loading branch information
NGSpace committed Aug 8, 2024
1 parent 457bda3 commit b2ccfe1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,39 @@ protected V2Value() {}
public boolean isSet = false;
public String setKey;
public V2Value setValue;
public V2Value(String value, AVarTextCompiler compiler) {

public boolean isMath = false;
public V2Value[] values;
public char[] operations;

protected V2Value(String value, AVarTextCompiler compiler) {
super(value.toLowerCase(), compiler);
isStatic = compiler.isStaticVariable(value.toLowerCase());
if (isStatic) return;
isDynamic = compiler.isDynamicVariable(value.toLowerCase());
String[] values = value.split("=",2);
isSet = values.length==2&&!compiler.isFirstEqualsCondition(value);
if (isDynamic) return;
String[] conditionValues = value.split("=",2);
isSet = conditionValues.length==2&&!compiler.isFirstEqualsCondition(value);
if (isSet) {
setKey = values[0];
setValue = new V2Value(values[1], compiler);
setKey = conditionValues[0];
setValue = of(conditionValues[1], compiler);
return;
}
int i = 0;
char c;
//TODO mathematical optimizations
System.out.println(value.toLowerCase() + " " + isStatic + " " + isDynamic + " " + isSet);
}
public Object toObject() throws CompileException {

//Should ideally be overwritten by anyone extending this class
/**
* Process the value and return it as an Object.
* @return an Object of any kind.
* @throws CompileException
*/
public Object get() throws CompileException {
if (isStatic) return compiler.getStaticVariable(value);
if (isDynamic) return compiler.getStaticVariable(value);
if (isSet) compiler.put(setKey, setValue.toObject());
if (isSet) compiler.put(setKey, setValue.get());
return compiler.getVariable(value);
}

Expand All @@ -41,4 +55,30 @@ public static <T> T[] addToArray(T[] arr, T t) {
newarr[arr.length] = t;
return newarr;
}

public static class V2StringValue extends V2Value {
public V2StringValue(String value, AVarTextCompiler compiler) {
this.value=value;
this.compiler=compiler;
}
@Override public Object get() throws CompileException {
return value;
}
}

public static V2Value of(String valuee, AVarTextCompiler compiler) {
String value = valuee.trim();
if (!value.startsWith("\"")||!value.endsWith("\"")) return new V2Value(valuee, compiler);
value = value.substring(1,value.length()-1);
char c;
boolean safe = false;
for (int i = 0;i<value.length();i++) {
c = value.charAt(i);
if (c=='\\') safe = !safe; else {
if (c=='"'&&!safe) return new V2Value(valuee, compiler);
safe = false;
}
}
return new V2StringValue(value, compiler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public VariableV2RuntimeElement(String value, AVarTextCompiler compiler) {
}

@Override public void execute(CompileState meta, StringBuilder builder) throws CompileException {
Object val = value.toObject();
Object val = value.get();
if (val instanceof Number num&&num.doubleValue()%1==0) val = num.longValue();
builder.append(val);
}

public V2Value getValue(String s) {return new V2Value(s, compiler);}
public V2Value getValue(String s) {return V2Value.of(s, compiler);}
}
3 changes: 1 addition & 2 deletions src/main/java/io/github/ngspace/hudder/meta/MethodValue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.ngspace.hudder.meta;

import io.github.ngspace.hudder.compilers.ATextCompiler;
import io.github.ngspace.hudder.compilers.AVarTextCompiler;
import io.github.ngspace.hudder.compilers.CompileException;
import io.github.ngspace.hudder.util.MathUtils;
Expand All @@ -15,7 +14,7 @@ public MethodValue(String value, AVarTextCompiler compiler) {
}
public String getAbsoluteValue() {return value;}
@Override public String toString() {return getAbsoluteValue();}
public ATextCompiler getCompiler() {return compiler;}
public AVarTextCompiler getCompiler() {return compiler;}

public String asString() throws CompileException {return String.valueOf(compiler.getVariable(value.trim()));}
public int asInt() throws CompileException {return tryParseInt(compiler.getVariable(value.trim()));}
Expand Down

0 comments on commit b2ccfe1

Please sign in to comment.