Skip to content
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

--emit-lua: Don't "math.ln=math.log" at the start of every file #630

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 0 additions & 4 deletions spec/translator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ end)
local function assert_translation(pallene_code, expected)
assert(compile("__translation_test__.pln", pallene_code))
local contents = assert(util.get_file_contents("__translation_test__.lua"))
-- The introduction of math.ln in Pallene to workaround single param math.log requires emitted
-- Lua code to handle this as well. The current workaround injects "math.ln = math.log; " at
-- the beginning of the emited Lua. This function needs to account for this injection.
expected = "math.ln = math.log; " .. expected
assert.are.same(expected, contents)
end

Expand Down
17 changes: 6 additions & 11 deletions src/pallene/translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ function Translator:erase_region(start_index, stop_index)
self.last_index = stop_index + 1
end

-- This is a workaround to handle math.log built-in optimizations.
function Translator:prepend_compatibility_code()
-- Note: We do not add a newline after this code injection in order to
-- preserve line number parity with the original .pln file.
-- It looks ugly, but correct line numbers are more useful.
self.partials[1] = "math.ln = math.log; " .. self.partials[1]
end

function translator.translate(input, prog_ast)

-- Compatibility shim for math.ln
-- Technically, this is not perfect, because it can replace inside comments & strings.
-- But I think that's a fair tradeoff until implement optional arguments for math.log.
input = input:gsub("%f[%w_]math[.]ln%f[^%w_]", "math.log")

local instance = Translator.new(input)

-- Erase all type regions
Expand All @@ -77,9 +75,6 @@ function translator.translate(input, prog_ast)
-- Whatever characters that were not included in the partials should be added.
instance:add_previous(#input)

-- This prepends any compatibility shims we need.
instance:prepend_compatibility_code()

return table.concat(instance.partials)
end

Expand Down
Loading