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;
}
public Assign assign(DynamicExpr value) {
return new Assign(chStart, chEnd, name, value);
}
@Override
public Dynamic<?> get(Scope dataRoot) {
if (dataRoot.asObject().has(name)) return dataRoot.asObject().get(name);

View File

@ -64,7 +64,7 @@ public class Parser {
// Expressions
private Expr<?> expression() {
try {
return assignment();
return conditional();
} catch (RuntimeException e) {
if (e instanceof ParseException) throw e;
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() {
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.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.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)) {
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 org.junit.jupiter.api.*;
import static io.gitlab.jfronny.muscript.test.util.MuTestUtil.number;
import static org.junit.jupiter.api.Assertions.*;
class AssignTest {
@ -19,4 +20,11 @@ class AssignTest {
assertEquals("test", expr.get(scope));
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}()"));
}
}