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

Named Arguments #625

Merged
merged 32 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8ac5b1d
Initial commit for semantic tokenizer.
gdotdesign Jun 12, 2023
60f1bb5
Simplify semantic tokenizer a bit.
gdotdesign Jun 13, 2023
0e865c6
Working language server implementation.
gdotdesign Jun 13, 2023
1c14a5a
Cleanup semantic tokenizer class.
gdotdesign Jun 13, 2023
ca01ad6
Save keywords automatically instead of manually.
gdotdesign Jun 13, 2023
6926d57
Use an array derived from the actual token types.
gdotdesign Jun 13, 2023
29fb79b
Implement suggestions from code review.
gdotdesign Jun 14, 2023
6b25fc8
Update src/ls/semantic_tokens.cr
gdotdesign Jun 14, 2023
0eea575
Implement HTML highlighting.
gdotdesign Jun 14, 2023
1cd96da
Implement highlight directive.
gdotdesign Jun 14, 2023
529117d
Avoid unnecessary interations.
gdotdesign Jun 14, 2023
79c3f11
Implement suggestions from code review.
gdotdesign Jun 14, 2023
6faec26
Use the ast from the workspace semantic tokens.
gdotdesign Jun 25, 2023
37db9b6
Implementation of localization language structures.
gdotdesign Jun 26, 2023
6e51ae0
Update operation.cr
gdotdesign Jul 18, 2023
673b361
Merge branch 'master' into locales
gdotdesign Jul 18, 2023
92833ca
Update test.
gdotdesign Jul 21, 2023
5e9eef7
Merge branch 'master' into locales
gdotdesign Aug 8, 2023
44ab595
Revert change to the operation formatting.
gdotdesign Aug 8, 2023
a8ecd78
Update Locale.md
gdotdesign Aug 8, 2023
cb99c78
Implement labelled calls.
gdotdesign Jul 21, 2023
dd254f8
Don't reorder arguments in the formatter.
gdotdesign Jul 22, 2023
630c080
Minor fixes.
gdotdesign Jul 26, 2023
d28c7a3
Merge branch 'master' into locales
gdotdesign Sep 6, 2023
095ab66
Merge branch 'locales' into labelled-calls
gdotdesign Sep 6, 2023
d171155
Merge branch 'master' into locales
gdotdesign Sep 7, 2023
88bd4cc
Merge branch 'locales' into labelled-calls
gdotdesign Sep 7, 2023
36c9d98
Apply suggestions from code review
gdotdesign Sep 7, 2023
a2a0c04
Update src/compilers/call.cr
gdotdesign Sep 7, 2023
687550d
Finish renaming an error from code review.
gdotdesign Sep 7, 2023
99a1bac
Merge branch 'master' into labelled-calls
gdotdesign Sep 17, 2023
99acd9f
Merge branch 'master' into labelled-calls
gdotdesign Sep 17, 2023
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
Prev Previous commit
Next Next commit
Don't reorder arguments in the formatter.
  • Loading branch information
gdotdesign committed Aug 8, 2023
commit dd254f8bed6f1077c88346a1eccb59a55f7d1447
2 changes: 1 addition & 1 deletion src/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mint

delegate lookups, checked, cache, component_records, to: @artifacts
delegate ast, types, variables, resolve_order, to: @artifacts
delegate record_field_lookup, locales, to: @artifacts
delegate record_field_lookup, locales, argument_order, to: @artifacts

getter js, style_builder, static_components, static_components_pool
getter build, relative
Expand Down
2 changes: 1 addition & 1 deletion src/compilers/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mint
compile node.expression

arguments =
compile node.arguments, ", "
compile node.arguments.sort_by! { |item| argument_order.index(item) || -1 }, ", "
gdotdesign marked this conversation as resolved.
Show resolved Hide resolved

case
when node.expression.is_a?(Ast::InlineFunction)
Expand Down
4 changes: 0 additions & 4 deletions src/formatters/call.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module Mint
class Formatter
def format(node : Ast::CallExpression)
format node.expression
end

def format(node : Ast::Call) : String
expression =
format node.expression
Expand Down
14 changes: 14 additions & 0 deletions src/formatters/call_expression.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mint
class Formatter
def format(node : Ast::CallExpression)
expression =
format node.expression

if name = node.name
"#{name.value}: #{expression}"
else
expression
end
end
end
end
4 changes: 3 additions & 1 deletion src/messages/call_not_found_argument.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ message CallNotFoundArgument do
title "Type Error"

block do
text "I was looking for the name argument:"
text "I was looking for the argument:"
bold name
text "but it's not there."
end

type_with_text function_type, "The type of the function is:"

snippet node, "The call is here:"
end
2 changes: 1 addition & 1 deletion src/type_checker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Mint

delegate checked, record_field_lookup, component_records, to: artifacts
delegate types, variables, ast, lookups, cache, to: artifacts
delegate assets, resolve_order, locales, to: artifacts
delegate assets, resolve_order, locales, argument_order, to: artifacts

delegate component?, component, stateful?, current_top_level_entity?, to: scope
delegate format, to: formatter
Expand Down
2 changes: 2 additions & 0 deletions src/type_checker/artifacts.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Mint
class Artifacts
getter ast, lookups, cache, checked, record_field_lookup, assets
getter types, variables, component_records, resolve_order, locales
getter argument_order

def initialize(@ast : Ast,
@component_records = {} of Ast::Component => Record,
Expand All @@ -13,6 +14,7 @@ module Mint
@types = {} of Ast::Node => Checkable,
@cache = {} of Ast::Node => Checkable,
@locales = {} of String => Hash(String, Ast::Node),
@argument_order = [] of Ast::Node,
@resolve_order = [] of Ast::Node,
@checked = Set(Ast::Node).new)
end
Expand Down
1 change: 1 addition & 0 deletions src/type_checker/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Mint
parameters.map do |param|
pretty =
param.to_pretty.as(String)

if param.label
"#{param.label}: #{pretty}"
else
Expand Down
50 changes: 27 additions & 23 deletions src/type_checkers/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Mint
type_error CallNotAFunction

def check(node : Ast::CallExpression)
resolve(node.expression).tap do |item|
resolve(node.expression).dup.tap do |item|
item.label = node.name.try(&.value)
end
end
Expand Down Expand Up @@ -46,29 +46,33 @@ module Mint
} if node.arguments.size > argument_size || # If it's more than the maximum
node.arguments.size < required_argument_size # If it's less then the minimum

if node.arguments.all?(&.name.nil?)
node.arguments
elsif node.arguments.all?(&.name.!=(nil))
node.arguments.sort_by! do |argument|
index =
function_type
.parameters
.index { |param| param.label == argument.name.try(&.value) }

raise CallNotFoundArgument, {
"name" => argument.name.try(&.value).to_s,
args =
if node.arguments.all?(&.name.nil?)
node.arguments
elsif node.arguments.all?(&.name.!=(nil))
gdotdesign marked this conversation as resolved.
Show resolved Hide resolved
node.arguments.sort_by do |argument|
index =
function_type
.parameters
.index { |param| param.label == argument.name.try(&.value) }

raise CallNotFoundArgument, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise CallNotFoundArgument, {
raise ArgumentNameMismatch, {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with change, it's not a mismatch (although it could be) but a not found parameter.

If we really need to change this, then It could be CallArgumentNameMismatch.

"function_type" => function_type,
"name" => argument.name.try(&.value).to_s,
"node" => node,
} unless index

index
end
else
raise CallMixedArguments, {
gdotdesign marked this conversation as resolved.
Show resolved Hide resolved
"node" => node,
} unless index

index
}
end
else
raise CallMixedArguments, {
"node" => node,
}
end

node.arguments.each_with_index do |argument, index|
argument_order.concat args

args.each_with_index do |argument, index|
argument_type =
resolve argument

Expand All @@ -86,8 +90,8 @@ module Mint
parameters << argument_type
end

if (optional_param_count = argument_size - node.arguments.size) > 0
parameters.concat(function_type.parameters[node.arguments.size, optional_param_count])
if (optional_param_count = argument_size - args.size) > 0
parameters.concat(function_type.parameters[args.size, optional_param_count])
end

call_type =
Expand Down