Skip to content

Commit

Permalink
feat(parser): Support implicit function calls with string and table l…
Browse files Browse the repository at this point in the history
…iterals
  • Loading branch information
bytexenon committed Jun 26, 2024
1 parent bbfe964 commit 21a6f57
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions the-tiny-lua-compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,19 @@ function Parser.parse(tokens)
consume() -- Consume the last token of the expression
return { TYPE = "FunctionCall", Expression = currentExpression, Arguments = arguments, ReturnValueAmount = 1 }
end
local function consumeImplicitFunctionCall(lvalue)
local currentTokenType = currentToken.TYPE

-- <string>?
if currentTokenType == "String" then
local arguments = { currentToken }
return { TYPE = "FunctionCall", Expression = lvalue, Arguments = arguments, ReturnValueAmount = 1 }
end

-- <table>?
local arguments = { consumeTable() }
return { TYPE = "FunctionCall", Expression = lvalue, Arguments = arguments, ReturnValueAmount = 1 }
end
local function consumeMethodCall(currentExpression)
local methodIdentifier = consume().Value -- Consume the ":" character, and get the method identifier
consume() -- Consume the method identifier
Expand Down Expand Up @@ -897,6 +910,15 @@ function Parser.parse(tokens)
consume()
-- <expression> \[ <expression> \]
return consumeBracketTableIndex(primaryExpression)
elseif nextToken then
-- In some edge cases, a user may call a function using only string,
-- example: `print "Hello, World!"`. This is a valid Lua syntax.
-- Let's handle both strings and tables here for that case.
local nextTokenType = nextToken.TYPE
if nextTokenType == "String" or (nextTokenValue == "{" and nextTokenType == "Character") then
consume()
return consumeImplicitFunctionCall(primaryExpression)
end
end
return nil
end
Expand Down

0 comments on commit 21a6f57

Please sign in to comment.