-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: code gen is now dumber than it was before.
This should make adding new features / indenting / language specific things easier.
- Loading branch information
1 parent
86c7a66
commit 4f5d25b
Showing
12 changed files
with
116 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
local utils = require("refactoring.code_generation.utils") | ||
|
||
local go = { | ||
extract_function = function(opts) | ||
return { | ||
create = string.format( | ||
[[ | ||
constant = function(opts) | ||
return string.format("%s := %s\n", opts.name, opts.value) | ||
end, | ||
["return"] = function(code) | ||
return string.format("return %s", utils.stringify_code(code)) | ||
end, | ||
["function"] = function(opts) | ||
return string.format( | ||
[[ | ||
func %s(%s) { | ||
%s | ||
return %s | ||
} | ||
]], | ||
|
||
opts.name, | ||
table.concat(opts.args, ", "), | ||
type(opts.body) == "table" | ||
and table.concat(opts.body, "\n") | ||
or opts.body, | ||
opts.ret | ||
), | ||
call = string.format( | ||
"%s := %s(%s)", | ||
opts.ret, | ||
opts.name, | ||
table.concat(opts.args, ", ") | ||
), | ||
} | ||
opts.name, | ||
table.concat(opts.args, ", "), | ||
utils.stringify_code(opts.body) | ||
) | ||
end, | ||
call_function = function(opts) | ||
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", ")) | ||
end, | ||
} | ||
return go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,28 @@ | ||
local utils = require("refactoring.code_generation.utils") | ||
|
||
local lua = { | ||
create_constant = function(opts) | ||
constant = function(opts) | ||
return string.format("local %s = %s\n", opts.name, opts.value) | ||
end, | ||
extract_function = function(opts) | ||
return { | ||
create = string.format( | ||
[[ | ||
["function"] = function(opts) | ||
return string.format( | ||
[[ | ||
local function %s(%s) | ||
%s | ||
return %s | ||
end | ||
]], | ||
opts.name, | ||
table.concat(opts.args, ", "), | ||
type(opts.body) == "table" | ||
and table.concat(opts.body, "\n") | ||
or opts.body, | ||
opts.ret | ||
), | ||
opts.name, | ||
table.concat(opts.args, ", "), | ||
utils.stringify_code(opts.body) | ||
) | ||
end, | ||
["return"] = function(code) | ||
return string.format("return %s", utils.stringify_code(code)) | ||
end, | ||
|
||
call = string.format( | ||
"local %s = %s(%s)", | ||
opts.ret, | ||
opts.name, | ||
table.concat(opts.args, ", ") | ||
), | ||
} | ||
call_function = function(opts) | ||
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", ")) | ||
end, | ||
} | ||
return lua |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
local utils = require("refactoring.code_generation.utils") | ||
|
||
local python = { | ||
extract_function = function(opts) | ||
return { | ||
create = string.format( | ||
[[ | ||
constant = function(opts) | ||
return string.format("%s = %s\n", opts.name, opts.value) | ||
end, | ||
["return"] = function(code) | ||
return string.format("return %s", utils.stringify_code(code)) | ||
end, | ||
|
||
["function"] = function(opts) | ||
return string.format( | ||
[[ | ||
def %s(%s): | ||
%s | ||
return %s | ||
]], | ||
opts.name, | ||
table.concat(opts.args, ", "), | ||
type(opts.body) == "table" | ||
and table.concat(opts.body, "\n") | ||
or opts.body, | ||
opts.ret | ||
), | ||
call = string.format( | ||
"%s = %s(%s)", | ||
opts.ret, | ||
opts.name, | ||
table.concat(opts.args, ", ") | ||
), | ||
} | ||
opts.name, | ||
table.concat(opts.args, ", "), | ||
utils.stringify_code(opts.body) | ||
) | ||
end, | ||
call_function = function(opts) | ||
return string.format("%s(%s)", opts.name, table.concat(opts.args, ", ")) | ||
end, | ||
} | ||
return python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
local M = {} | ||
|
||
function M.stringify_code(code) | ||
return type(code) == "table" and table.concat(code, "\n") or code | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.