Skip to content

CST: Handle normal + compound assignment #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion batteries/syntax/ast_types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export type AstStatLocal = {
export type AstStatFor = {
tag: "for",
["for"]: Token<"for">,
variable: Token<string>,
variable: AstLocal,
equals: Token<"=">,
from: AstExpr,
toComma: Token<",">,
Expand All @@ -251,6 +251,21 @@ export type AstStatForIn = {
body: AstStatBlock,
["end"]: Token<"end">,
}

export type AstStatAssign = {
tag: "assign",
variables: Punctuated<AstExpr>,
equals: Token<"=">,
values: Punctuated<AstExpr>,
}

export type AstStatCompoundAssign = {
tag: "compoundassign",
variable: AstExpr,
operand: Token, -- TODO: enforce token type,
value: AstExpr,
}

export type AstStat =
| AstStatBlock
| AstStatIf
Expand All @@ -263,5 +278,7 @@ export type AstStat =
| AstStatLocal
| AstStatFor
| AstStatForIn
| AstStatAssign
| AstStatCompoundAssign

return {}
24 changes: 24 additions & 0 deletions batteries/syntax/visitor.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Visitor = {
visitLocalDeclaration: (T.AstStatLocal) -> boolean,
visitFor: (T.AstStatFor) -> boolean,
visitForIn: (T.AstStatForIn) -> boolean,
visitAssign: (T.AstStatAssign) -> boolean,
visitCompoundAssign: (T.AstStatCompoundAssign) -> boolean,

visitLocalReference: (T.AstExprLocal) -> boolean,
visitGlobal: (T.AstExprGlobal) -> boolean,
Expand Down Expand Up @@ -46,6 +48,8 @@ local defaultVisitor: Visitor = {
visitLocalDeclaration = alwaysVisit :: any,
visitFor = alwaysVisit :: any,
visitForIn = alwaysVisit :: any,
visitAssign = alwaysVisit :: any,
visitCompoundAssign = alwaysVisit :: any,

visitLocalReference = alwaysVisit :: any,
visitGlobal = alwaysVisit :: any,
Expand Down Expand Up @@ -185,6 +189,22 @@ local function visitForIn(node: T.AstStatForIn, visitor: Visitor)
end
end

local function visitAssign(node: T.AstStatAssign, visitor: Visitor)
if visitor.visitAssign(node) then
visitPunctuated(node.variables, visitor, visitExpression)
visitToken(node.equals, visitor)
visitPunctuated(node.values, visitor, visitExpression)
end
end

local function visitCompoundAssign(node: T.AstStatCompoundAssign, visitor: Visitor)
if visitor.visitCompoundAssign(node) then
visitExpression(node.variable, visitor)
visitToken(node.operand, visitor)
visitExpression(node.value, visitor)
end
end

local function visitString(node: T.AstExprConstantString, visitor: Visitor)
if visitor.visitString(node) then
visitor.visitToken(node)
Expand Down Expand Up @@ -388,6 +408,10 @@ function visitStatement(statement: T.AstStat, visitor: Visitor)
visitFor(statement, visitor)
elseif statement.tag == "forin" then
visitForIn(statement, visitor)
elseif statement.tag == "assign" then
visitAssign(statement, visitor)
elseif statement.tag == "compoundassign" then
visitCompoundAssign(statement, visitor)
else
exhaustiveMatch(statement.tag)
end
Expand Down
23 changes: 17 additions & 6 deletions luau/src/luau.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,14 +1241,22 @@ struct AstSerialize : public Luau::AstVisitor
void serializeStat(Luau::AstStatAssign* node)
{
lua_rawcheckstack(L, 2);
lua_createtable(L, 0, preambleSize + 2);
lua_createtable(L, 0, preambleSize + 3);

const auto cstNode = lookupCstNode<Luau::CstStatAssign>(node);

serializeNodePreamble(node, "assign");

serializeExprs(node->vars);
serializePunctuated(node->vars, cstNode ? cstNode->varsCommaPositions : Luau::AstArray<Luau::Position>{}, ",");
lua_setfield(L, -2, "variables");

serializeExprs(node->values);
if (cstNode)
{
serializeToken(cstNode->equalsPosition, "=");
lua_setfield(L, -2, "equals");
}

serializePunctuated(node->values, cstNode ? cstNode->valuesCommaPositions : Luau::AstArray<Luau::Position>{}, ",");
lua_setfield(L, -2, "values");
}

Expand All @@ -1259,12 +1267,15 @@ struct AstSerialize : public Luau::AstVisitor

serializeNodePreamble(node, "compoundassign");

serialize(node->op);
lua_setfield(L, -2, "operand");

node->var->visit(this);
lua_setfield(L, -2, "variable");

if (const auto cstNode = lookupCstNode<Luau::CstStatCompoundAssign>(node))
serializeToken(cstNode->opPosition, (Luau::toString(node->op) + "=").data());
else
serialize(node->op);
lua_setfield(L, -2, "operand");

node->value->visit(this);
lua_setfield(L, -2, "value");
}
Expand Down
5 changes: 5 additions & 0 deletions tests/astSerializerTests/assignment-1.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = 1

a, b = 1, true

a, b, c.d.e[f][g][1], h:i().j[k]:l()[m] = true, false, 1, 4
24 changes: 24 additions & 0 deletions tests/astSerializerTests/compound-assignment-1.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local x = 1
local y = 2

x += 5
x -= 5
x *= 5
x /= 5
x //= 5
x %= 5
x ^= 5

x += y
x -= y
x *= y
x /= y
x //= y
x %= y
x ^= y

local str1 = "Hello, "
local str2 = "world!"

str1 ..= "world!"
str1 ..= str2
2 changes: 2 additions & 0 deletions tests/testAstSerializer.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ local function test_roundtrippableAst()
"examples/parsing.luau",
"examples/time_example.luau",
"examples/writeFile.luau",
"tests/astSerializerTests/assignment-1.luau",
"tests/astSerializerTests/break-continue-1.luau",
"tests/astSerializerTests/compound-assignment-1.luau",
"tests/astSerializerTests/generic-for-loop-1.luau",
"tests/astSerializerTests/numeric-for-loop-1.luau",
"tests/astSerializerTests/while-1.luau",
Expand Down