Move assignment gen to primary
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2023-01-24 12:29:47 +01:00
parent abdbc27618
commit 7aa94c2e8e
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 16 additions and 20 deletions

View File

@ -17,10 +17,6 @@ public class Variable extends DynamicExpr {
this.name = name; this.name = name;
} }
public Assign assign(DynamicExpr value) {
return new Assign(chStart, chEnd, name, value);
}
@Override @Override
public Dynamic<?> get(Scope dataRoot) { public Dynamic<?> get(Scope dataRoot) {
if (dataRoot.asObject().has(name)) return dataRoot.asObject().get(name); if (dataRoot.asObject().has(name)) return dataRoot.asObject().get(name);

View File

@ -64,7 +64,7 @@ public class Parser {
// Expressions // Expressions
private Expr<?> expression() { private Expr<?> expression() {
try { try {
return assignment(); return conditional();
} catch (RuntimeException e) { } catch (RuntimeException e) {
if (e instanceof ParseException) throw e; if (e instanceof ParseException) throw e;
else if (e instanceof LocationalException le) { else if (e instanceof LocationalException le) {
@ -73,20 +73,6 @@ public class Parser {
} }
} }
private Expr<?> assignment() {
Expr<?> expr = conditional();
if (match(Token.Assign)) {
if (expr instanceof Variable variable) {
expr = variable.assign(expression().asDynamicExpr());
} else {
throw error("Attempted to assign non-variable. This is not supported!", expr);
}
}
return expr;
}
private Expr<?> conditional() { private Expr<?> conditional() {
Expr<?> expr = and(); Expr<?> expr = and();
@ -285,7 +271,13 @@ public class Parser {
if (match(Token.String)) return Expr.literal(previous.start, previous.current - 1, previous.lexeme); if (match(Token.String)) return Expr.literal(previous.start, previous.current - 1, previous.lexeme);
if (match(Token.True, Token.False)) return Expr.literal(previous.start, previous.current - 1, previous.lexeme.equals("true")); if (match(Token.True, Token.False)) return Expr.literal(previous.start, previous.current - 1, previous.lexeme.equals("true"));
if (match(Token.Number)) return Expr.literal(previous.start, previous.current - 1, Double.parseDouble(previous.lexeme)); if (match(Token.Number)) return Expr.literal(previous.start, previous.current - 1, Double.parseDouble(previous.lexeme));
if (match(Token.Identifier)) return new Variable(previous.start, previous.current - 1, previous.lexeme); if (match(Token.Identifier)) {
int start = previous.start;
int end = previous.current - 1;
String name = previous.lexeme;
if (match(Token.Assign)) return new Assign(start, end, name, expression().asDynamicExpr());
else return new Variable(start, end, name);
}
if (match(Token.LeftParen)) { if (match(Token.LeftParen)) {
Expr<?> expr = expression(); Expr<?> expr = expression();

View File

@ -8,6 +8,7 @@ import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.test.util.UnforkableScope; import io.gitlab.jfronny.muscript.test.util.UnforkableScope;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static io.gitlab.jfronny.muscript.test.util.MuTestUtil.number;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class AssignTest { class AssignTest {
@ -19,4 +20,11 @@ class AssignTest {
assertEquals("test", expr.get(scope)); assertEquals("test", expr.get(scope));
assertEquals("test", scope.getValue().get("someval").asString().getValue()); assertEquals("test", scope.getValue().get("someval").asString().getValue());
} }
@Test
void testAssignInner() {
assertEquals(2, number("{->some = other = 2; other}()"));
assertEquals(2, number("{->some = other = 2; some}()"));
assertEquals(2, number("{->some = 2 + 4 * (other = 4 / 2); other}()"));
}
} }