File tree Expand file tree Collapse file tree 3 files changed +42
-3
lines changed
Expand file tree Collapse file tree 3 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,8 @@ local AstKind = {
7676
7777 -- Misc
7878 NopStatement = " NopStatement" ;
79+
80+ IfElseExpression = " IfElseExpression" ;
7981}
8082
8183local astKindExpressionLookup = {
@@ -144,6 +146,15 @@ function Ast.NopStatement()
144146 }
145147end
146148
149+ function Ast .IfElseExpression (condition , true_value , false_value )
150+ return {
151+ kind = AstKind .IfElseExpression ,
152+ condition = condition ,
153+ true_value = true_value ,
154+ false_value = false_value
155+ }
156+ end
157+
147158-- Create Ast Top Node
148159function Ast .TopNode (body , globalScope )
149160 return {
789800
790801
791802
792- return Ast ;
803+ return Ast ;
Original file line number Diff line number Diff line change @@ -928,6 +928,19 @@ function Parser:expressionLiteral(scope)
928928 local scope , id = scope :resolve (name );
929929 return Ast .VariableExpression (scope , id );
930930 end
931+
932+ -- IfElse
933+ if (LuaVersion .LuaU ) then
934+ if (consume (self , TokenKind .Keyword , " if" )) then
935+ local condition = self :expression (scope );
936+ expect (self , TokenKind .Keyword , " then" );
937+ local true_value = self :expression (scope );
938+ expect (self , TokenKind .Keyword , " else" );
939+ local false_value = self :expression (scope );
940+
941+ return Ast .IfElseExpression (condition , true_value , false_value );
942+ end
943+ end
931944
932945 if (self .disableLog ) then error () end
933946 logger :error (generateError (self , " Unexpected Token \" " .. peek (self ).source .. " \" . Expected a Expression!" ))
@@ -966,4 +979,4 @@ function Parser:tableConstructor(scope)
966979 return Ast .TableConstructorExpression (entries );
967980end
968981
969- return Parser
982+ return Parser
Original file line number Diff line number Diff line change @@ -738,7 +738,7 @@ function Unparser:unparseExpression(expression, tabbing)
738738 k = AstKind .IndexExpression ;
739739 if (expression .kind == k or expression .kind == AstKind .AssignmentIndexing ) then
740740 local base = self :unparseExpression (expression .base , tabbing );
741- if (Ast .astKindExpressionToNumber (expression .base .kind ) > Ast .astKindExpressionToNumber (k )) then
741+ if (expression . base . kind == AstKind . VarargExpression or Ast .astKindExpressionToNumber (expression .base .kind ) > Ast .astKindExpressionToNumber (k )) then
742742 base = " (" .. base .. " )" ;
743743 end
744744
@@ -860,6 +860,21 @@ function Unparser:unparseExpression(expression, tabbing)
860860 return code .. self :optionalWhitespace ((p and " ," or " " ) .. self :newline () .. self :tabs (tabbing )) .. " }" ;
861861 end
862862
863+ if (self .luaVersion == LuaVersion .LuaU ) then
864+ k = AstKind .IfElseExpression
865+ if (expression .kind == k ) then
866+ code = " if " ;
867+
868+ code = code .. self :unparseExpression (expression .condition );
869+ code = code .. " then " ;
870+ code = code .. self :unparseExpression (expression .true_value );
871+ code = code .. " else " ;
872+ code = code .. self :unparseExpression (expression .false_value );
873+
874+ return code
875+ end
876+ end
877+
863878 logger :error (string.format (" \" %s\" is not a valid unparseable expression" , expression .kind ));
864879end
865880
You can’t perform that action at this time.
0 commit comments