Support table parameters and record types (#37) #38

Merged
Aire-One merged 7 commits from feat/#37 into master 2022-11-06 19:23:20 +01:00
3 changed files with 57 additions and 7 deletions
Showing only changes of commit 453c9f2949 - Show all commits

View File

@ -1,3 +1,4 @@
local List = require "pl.List"
local Map = require "pl.Map"
local record Type_Info
@ -9,7 +10,7 @@ local record Type_Info
-- Map : name -> type
-- We can't use Variable_Info here because it's a circular dependency.
record_entries: Map<string, string> | nil
record_entries: Map<string, List<Type_Info>> | nil
end
local __Type_Info: metatable<Type_Info> = {

View File

@ -2,7 +2,7 @@ local List = require "pl.List"
local Type_Info = require "entity.Type_Info"
local record Variable_Info
metamethod __call: function(Variable_Info): Variable_Info
metamethod __call: function(self: Variable_Info, name: string | nil, types: List<Type_Info.Type_Info> | nil): Variable_Info
Variable_Info: Variable_Info
@ -13,10 +13,12 @@ local record Variable_Info
end
local __Variable_Info: metatable<Variable_Info> = {
__call = function(_self: Variable_Info): Variable_Info
__call = function(_self: Variable_Info, name: string | nil, types: List<Type_Info.Type_Info> | nil): Variable_Info
name = name or ""
types = types or List()
return {
name = "",
types = List(),
name = name,
types = types,
}
end,
}

View File

@ -1,12 +1,24 @@
local Function_Info = require "entity.Function_Info"
local List = require "pl.List"
local Map = require "pl.Map"
local stringx = require "pl.stringx"
local template = require "pl.template"
local Type_Info = require "entity.Type_Info"
local utils = require "utils"
local Variable_Info = require "entity.Variable_Info"
local snippets = {}
local record Module
indent: function(str: string, level: number): string
render_typed_variable: function(name: string, types: List<Type_Info.Type_Info>): string
render_anonymous_function_signature: function(item: Function_Info.Function_Info): string
render_record_functions: function(items: List<Function_Info.Function_Info>): string
render_enum: function(name: string, values: List<string>): string
render_record_properties: function(items: List<Variable_Info.Variable_Info>): string
render_record: function(name: string, items: List<Variable_Info.Variable_Info>): string
end
local snippets: Module = {}
function snippets.indent(str: string, level: number): string
level = level or 1
@ -42,7 +54,26 @@ end
function snippets.render_record_functions(items: List<Function_Info.Function_Info>): string
return items:map(function(item: Function_Info.Function_Info): string
return snippets.render_anonymous_function_signature(item)
return string.format(
"%s%s",
item.parameters:map(function(param: Function_Info.Parameter): string
if #param.types == 0 then
return ""
end
return param.types:map(function(t: Type_Info.Type_Info): string
if t.record_entries and (t.record_entries as Map<string, List<Type_Info.Type_Info>>):len() > 0 then
local properties: List<Variable_Info.Variable_Info> = List()
for name, types in (t.record_entries as Map<string, List<Type_Info.Type_Info>>):iter() do
properties:append(Variable_Info(name, types))
end
return snippets.render_record(t.name, properties)
end
return ""
end):concat("\n")
end):concat("\n"),
snippets.render_anonymous_function_signature(item))
end):concat("\n")
end
@ -79,4 +110,20 @@ function snippets.render_record_properties(items: List<Variable_Info.Variable_In
end):concat("\n")
end
function snippets.render_record(name: string, items: List<Variable_Info.Variable_Info>): string
local tmpl = [[
record $(name)
$(indent(body))
end
]]
local tmpl_args = {
name = name,
body = snippets.render_record_properties(items),
indent = snippets.indent,
}
return utils.do_or_fail(template.substitute, tmpl, tmpl_args)
end
return snippets