mt-lang 0.3.27 → 0.3.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/milk_tea/base.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require "pathname"
4
4
 
5
5
  module MilkTea
6
- VERSION = "0.3.27"
6
+ VERSION = "0.3.28"
7
7
 
8
8
  def self.root
9
9
  @root ||= Pathname.new(File.expand_path("../..", __dir__))
@@ -369,7 +369,7 @@ module MilkTea
369
369
  AST::AttributeDecl.new(name: name_token.lexeme, targets:, params:, visibility:, line:, column: name_token.column)
370
370
  end
371
371
 
372
- def parse_struct_decl(packed: false, alignment: nil, visibility: :private, attributes: [])
372
+ def parse_struct_decl(packed: false, alignment: nil, visibility: :private, attributes: [], inline_methods: true)
373
373
  line = previous.line
374
374
  name_token = consume_name("expected struct name")
375
375
  name = name_token.lexeme
@@ -377,13 +377,32 @@ module MilkTea
377
377
  implements = parse_implements_clause
378
378
  c_name = parse_optional_c_name
379
379
  packed, alignment = parse_struct_layout_attributes(attributes) if attributes.any?
380
- members = parse_recoverable_block do
381
- parse_struct_member
380
+ receiver_type_param_names = type_params.map(&:name)
381
+ members = with_type_param_names(receiver_type_param_names) do
382
+ parse_recoverable_block do
383
+ parse_struct_member
384
+ end
382
385
  end
383
386
  fields = members.filter_map { |kind, member| member if kind == :field }
384
387
  events = members.filter_map { |kind, member| member if kind == :event }
385
388
  nested_types = members.filter_map { |kind, member| member if kind == :nested_type }
386
- AST::StructDecl.new(name:, type_params:, implements:, c_name:, fields:, events:, nested_types:, attributes:, packed:, alignment:, visibility:, lifetime_params:, line:, column: name_token.column)
389
+ methods = members.filter_map { |kind, member| member if kind == :method }
390
+ struct_decl = AST::StructDecl.new(name:, type_params:, implements:, c_name:, fields:, events:, nested_types:, attributes:, packed:, alignment:, visibility:, lifetime_params:, line:, column: name_token.column)
391
+
392
+ if inline_methods && methods.any?
393
+ type_ref_args = type_params.map do |tp|
394
+ AST::TypeArgument.new(
395
+ value: AST::TypeRef.new(name: AST::QualifiedName.new(parts: [tp.name]), arguments: [], nullable: false, line: tp.line, column: tp.column),
396
+ line: tp.line,
397
+ column: tp.column,
398
+ )
399
+ end
400
+ type_ref = AST::TypeRef.new(name: AST::QualifiedName.new(parts: [name]), arguments: type_ref_args, nullable: false, line: name_token.line, column: name_token.column)
401
+ extending_block = AST::ExtendingBlock.new(type_name: type_ref, methods:, line: name_token.line, column: name_token.column)
402
+ [struct_decl, extending_block]
403
+ else
404
+ struct_decl
405
+ end
387
406
  end
388
407
 
389
408
  def parse_struct_decl_params
@@ -429,8 +448,24 @@ module MilkTea
429
448
  [lifetime_params, type_params]
430
449
  end
431
450
 
451
+ def check_method_start?
452
+ saved = @current
453
+ match(:public)
454
+ match(:async)
455
+ match(:editable) if check(:editable)
456
+ match(:static) if check(:static)
457
+ result = check(:function)
458
+ @current = saved
459
+ result
460
+ end
461
+
432
462
  def parse_struct_member
433
463
  field_attributes = parse_attribute_applications
464
+
465
+ if check_method_start?
466
+ return [:method, parse_method_def(attributes: field_attributes)]
467
+ end
468
+
434
469
  visibility, visibility_token = parse_visibility
435
470
 
436
471
  if match(:event)
@@ -438,7 +473,7 @@ module MilkTea
438
473
  end
439
474
 
440
475
  if match(:struct)
441
- return [:nested_type, parse_struct_decl(visibility:, attributes: field_attributes)]
476
+ return [:nested_type, parse_struct_decl(visibility:, attributes: field_attributes, inline_methods: false)]
442
477
  end
443
478
 
444
479
  raise error(visibility_token, "public is only allowed on struct events") if visibility == :public
@@ -154,13 +154,23 @@ module MilkTea
154
154
  until eof?
155
155
  if errors
156
156
  begin
157
- declarations << parse_declaration
157
+ result = parse_declaration
158
+ if result.is_a?(Array)
159
+ declarations.concat(result)
160
+ else
161
+ declarations << result
162
+ end
158
163
  rescue ParseError => e
159
164
  errors << e
160
165
  synchronize_to_top_level_boundary
161
166
  end
162
167
  else
163
- declarations << parse_declaration
168
+ result = parse_declaration
169
+ if result.is_a?(Array)
170
+ declarations.concat(result)
171
+ else
172
+ declarations << result
173
+ end
164
174
  end
165
175
  skip_newlines
166
176
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mt-lang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.27
4
+ version: 0.3.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Long (Teefan) Tran
@@ -149,9 +149,7 @@ files:
149
149
  - docs/index.html
150
150
  - docs/language-design.md
151
151
  - docs/language-manual.md
152
- - docs/self-host-contract.md
153
- - docs/self-host-plan.md
154
- - docs/self-host-progress.md
152
+ - docs/self-hosted-compiler-plan.md
155
153
  - lib/milk_tea.rb
156
154
  - lib/milk_tea/base.rb
157
155
  - lib/milk_tea/bindings.rb
@@ -627,7 +625,7 @@ metadata:
627
625
  homepage_uri: https://teefan.github.io/mt-lang/
628
626
  source_code_uri: https://github.com/teefan/mt-lang
629
627
  post_install_message: |
630
- Milk Tea 0.3.27 installed!
628
+ Milk Tea 0.3.28 installed!
631
629
 
632
630
  System requirements:
633
631
  - A C compiler (gcc or clang) must be available on PATH