carbon-compiler 0.2.0

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.
Files changed (122) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +17 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +39 -0
  6. data/.travis.yml +5 -0
  7. data/CODE_OF_CONDUCT.md +49 -0
  8. data/Gemfile +11 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +41 -0
  11. data/Rakefile +9 -0
  12. data/Vagrantfile +84 -0
  13. data/carbon-compiler.gemspec +28 -0
  14. data/lib/carbon/compiler.rb +20 -0
  15. data/lib/carbon/compiler/directive.rb +48 -0
  16. data/lib/carbon/compiler/directive/import.rb +17 -0
  17. data/lib/carbon/compiler/errors.rb +7 -0
  18. data/lib/carbon/compiler/location.rb +136 -0
  19. data/lib/carbon/compiler/metanostic.rb +123 -0
  20. data/lib/carbon/compiler/metanostic/defaults.rb +41 -0
  21. data/lib/carbon/compiler/metanostic/defaults.yml +138 -0
  22. data/lib/carbon/compiler/metanostic/diagnostic.rb +112 -0
  23. data/lib/carbon/compiler/metanostic/list.rb +109 -0
  24. data/lib/carbon/compiler/metanostic/mode.rb +162 -0
  25. data/lib/carbon/compiler/metanostic/state.rb +174 -0
  26. data/lib/carbon/compiler/metanostic/template.erb +11 -0
  27. data/lib/carbon/compiler/node.rb +18 -0
  28. data/lib/carbon/compiler/node/base.rb +213 -0
  29. data/lib/carbon/compiler/node/definition.rb +19 -0
  30. data/lib/carbon/compiler/node/definition/class.rb +24 -0
  31. data/lib/carbon/compiler/node/definition/class/element.rb +18 -0
  32. data/lib/carbon/compiler/node/definition/directive.rb +22 -0
  33. data/lib/carbon/compiler/node/definition/directive/function.rb +18 -0
  34. data/lib/carbon/compiler/node/definition/enum.rb +20 -0
  35. data/lib/carbon/compiler/node/definition/enum/element.rb +17 -0
  36. data/lib/carbon/compiler/node/definition/function.rb +44 -0
  37. data/lib/carbon/compiler/node/definition/function/body.rb +17 -0
  38. data/lib/carbon/compiler/node/definition/function/name.rb +18 -0
  39. data/lib/carbon/compiler/node/definition/function/parameter.rb +18 -0
  40. data/lib/carbon/compiler/node/definition/function/parameters.rb +17 -0
  41. data/lib/carbon/compiler/node/definition/module.rb +23 -0
  42. data/lib/carbon/compiler/node/definition/struct.rb +24 -0
  43. data/lib/carbon/compiler/node/definition/struct/element.rb +18 -0
  44. data/lib/carbon/compiler/node/etype.rb +66 -0
  45. data/lib/carbon/compiler/node/etype/option.rb +25 -0
  46. data/lib/carbon/compiler/node/etype/star.rb +13 -0
  47. data/lib/carbon/compiler/node/expression.rb +18 -0
  48. data/lib/carbon/compiler/node/expression/assignment.rb +22 -0
  49. data/lib/carbon/compiler/node/expression/call.rb +22 -0
  50. data/lib/carbon/compiler/node/expression/call/access.rb +24 -0
  51. data/lib/carbon/compiler/node/expression/call/attribute.rb +23 -0
  52. data/lib/carbon/compiler/node/expression/call/enum.rb +25 -0
  53. data/lib/carbon/compiler/node/expression/call/module.rb +24 -0
  54. data/lib/carbon/compiler/node/expression/call/parameters.rb +17 -0
  55. data/lib/carbon/compiler/node/expression/call/self.rb +17 -0
  56. data/lib/carbon/compiler/node/expression/call/unified.rb +27 -0
  57. data/lib/carbon/compiler/node/expression/literal.rb +83 -0
  58. data/lib/carbon/compiler/node/expression/operation.rb +20 -0
  59. data/lib/carbon/compiler/node/expression/operation/and.rb +20 -0
  60. data/lib/carbon/compiler/node/expression/operation/neq.rb +21 -0
  61. data/lib/carbon/compiler/node/expression/operation/normal.rb +20 -0
  62. data/lib/carbon/compiler/node/expression/operation/or.rb +20 -0
  63. data/lib/carbon/compiler/node/expression/unit.rb +16 -0
  64. data/lib/carbon/compiler/node/name.rb +27 -0
  65. data/lib/carbon/compiler/node/root.rb +23 -0
  66. data/lib/carbon/compiler/node/statement.rb +24 -0
  67. data/lib/carbon/compiler/node/statement/catch.rb +20 -0
  68. data/lib/carbon/compiler/node/statement/condition.rb +14 -0
  69. data/lib/carbon/compiler/node/statement/else.rb +17 -0
  70. data/lib/carbon/compiler/node/statement/elsif.rb +18 -0
  71. data/lib/carbon/compiler/node/statement/finally.rb +17 -0
  72. data/lib/carbon/compiler/node/statement/for.rb +18 -0
  73. data/lib/carbon/compiler/node/statement/if.rb +18 -0
  74. data/lib/carbon/compiler/node/statement/let.rb +18 -0
  75. data/lib/carbon/compiler/node/statement/match.rb +14 -0
  76. data/lib/carbon/compiler/node/statement/return.rb +17 -0
  77. data/lib/carbon/compiler/node/statement/try.rb +19 -0
  78. data/lib/carbon/compiler/node/statement/while.rb +19 -0
  79. data/lib/carbon/compiler/parser.rb +63 -0
  80. data/lib/carbon/compiler/parser/common.rb +79 -0
  81. data/lib/carbon/compiler/parser/expressions.rb +39 -0
  82. data/lib/carbon/compiler/parser/expressions/precedence.rb +134 -0
  83. data/lib/carbon/compiler/parser/expressions/primary.rb +120 -0
  84. data/lib/carbon/compiler/parser/firsts.rb +74 -0
  85. data/lib/carbon/compiler/parser/helpers.rb +61 -0
  86. data/lib/carbon/compiler/parser/root.rb +57 -0
  87. data/lib/carbon/compiler/parser/root/class.rb +34 -0
  88. data/lib/carbon/compiler/parser/root/directive.rb +87 -0
  89. data/lib/carbon/compiler/parser/root/enum.rb +45 -0
  90. data/lib/carbon/compiler/parser/root/function.rb +90 -0
  91. data/lib/carbon/compiler/parser/root/struct.rb +34 -0
  92. data/lib/carbon/compiler/parser/root/trait.rb +44 -0
  93. data/lib/carbon/compiler/parser/statements.rb +86 -0
  94. data/lib/carbon/compiler/parser/statements/if.rb +50 -0
  95. data/lib/carbon/compiler/parser/statements/match.rb +39 -0
  96. data/lib/carbon/compiler/parser/statements/try.rb +49 -0
  97. data/lib/carbon/compiler/project.rb +37 -0
  98. data/lib/carbon/compiler/project/file.rb +64 -0
  99. data/lib/carbon/compiler/scanner.rb +82 -0
  100. data/lib/carbon/compiler/scanner/main.rb +76 -0
  101. data/lib/carbon/compiler/scanner/token.rb +58 -0
  102. data/lib/carbon/compiler/version.rb +8 -0
  103. data/lib/carbon/compiler/visitor.rb +13 -0
  104. data/lib/carbon/compiler/visitor/base.rb +52 -0
  105. data/lib/carbon/compiler/visitor/generation.rb +45 -0
  106. data/lib/carbon/compiler/visitor/generation/asserts.rb +30 -0
  107. data/lib/carbon/compiler/visitor/generation/class.rb +93 -0
  108. data/lib/carbon/compiler/visitor/generation/context.rb +75 -0
  109. data/lib/carbon/compiler/visitor/generation/expressions.rb +82 -0
  110. data/lib/carbon/compiler/visitor/generation/expressions/assignment.rb +105 -0
  111. data/lib/carbon/compiler/visitor/generation/expressions/calls.rb +89 -0
  112. data/lib/carbon/compiler/visitor/generation/function.rb +68 -0
  113. data/lib/carbon/compiler/visitor/generation/statements.rb +131 -0
  114. data/lib/carbon/compiler/visitor/generation/struct.rb +115 -0
  115. data/lib/carbon/compiler/visitor/preparation.rb +86 -0
  116. data/lib/carbon/compiler/visitor/preparation/expressions.rb +26 -0
  117. data/lib/carbon/compiler/visitor/preparation/function.rb +55 -0
  118. data/lib/carbon/compiler/visitor/preparation/statements.rb +73 -0
  119. data/lib/carbon/compiler/visitor/preparation/struct.rb +37 -0
  120. data/program.ca +16 -0
  121. data/test.rb +21 -0
  122. metadata +234 -0
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/directive"
5
+ require "carbon/compiler/node/definition/enum"
6
+ require "carbon/compiler/node/definition/function"
7
+ require "carbon/compiler/node/definition/module"
8
+ require "carbon/compiler/node/definition/struct"
9
+
10
+ module Carbon
11
+ module Compiler
12
+ module Node
13
+ # A Definition. This is in most cases a module, function, struct, enum,
14
+ # trait, or directive.
15
+ module Definition
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/struct/element"
5
+
6
+ module Carbon
7
+ module Compiler
8
+ module Node
9
+ module Definition
10
+ # A class definition. This sets up a structured block of memory
11
+ # for various purposes. A Class's children are its elements.
12
+ class Class < Base
13
+ # If this definition is a data definition. A Structure is one of
14
+ # many possible data definitions.
15
+ #
16
+ # @return [true]
17
+ def data?
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Class < Base
9
+ # An element of a class. This contains name and type information for
10
+ # a class element.
11
+ class Element < Base
12
+ attributes name: 0, type: 1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/directive/function"
5
+
6
+ module Carbon
7
+ module Compiler
8
+ module Node
9
+ module Definition
10
+ # A directive. This is normally anything that starts with a colon
11
+ # in the source file. This contains information about said directive.
12
+ class Directive < Base
13
+ attributes name: 0, parameters: 1
14
+
15
+ def value
16
+ name.value
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Directive < Base
9
+ # A directive "function." Mainly, this is a key-value store, with
10
+ # the keys being the names of the parameters and the values being
11
+ # the types of the parameters.
12
+ class Function < Base
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/enum/element"
5
+
6
+ module Carbon
7
+ module Compiler
8
+ module Node
9
+ module Definition
10
+ # An Enum definition. Enums contain a set of elements that have
11
+ # associated type or value information.
12
+ class Enum < Base
13
+ def data?
14
+ true
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Enum < Base
9
+ # An element of an Enumerable. This contains either type or value
10
+ # information.
11
+ class Element < Base
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/function/parameter"
5
+ require "carbon/compiler/node/definition/function/parameters"
6
+ require "carbon/compiler/node/definition/function/body"
7
+ require "carbon/compiler/node/definition/function/name"
8
+
9
+ module Carbon
10
+ module Compiler
11
+ module Node
12
+ module Definition
13
+ # A function definition. This contains information about the arguments
14
+ # to be passed to the function, the "klass" (type) or return type of
15
+ # the function, the body of a function (if an extern function, this
16
+ # is null), and the associated {Project::Function} information, when
17
+ # tied.
18
+ class Function < Base
19
+ attributes _name: 0, parameters: 1, type: 2, body: 3
20
+
21
+ # If this definition is a behavior definition. Functions are the
22
+ # only behavior definitions in a module.
23
+ #
24
+ # @return [true]
25
+ def behavior?
26
+ true
27
+ end
28
+
29
+ # The name of the function. This is used in place of an actual
30
+ # node for various reasons.
31
+ #
32
+ # @return [String]
33
+ def name
34
+ @children[0].name.value
35
+ end
36
+
37
+ def generics
38
+ @children[0].generics
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Function < Base
9
+ # The body of a function. This acts an array containing all of the
10
+ # statements that make up a function.
11
+ class Body < Base
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Function < Base
9
+ # The name of the function. Note that this is mostly unused right
10
+ # now due to various reasons.
11
+ class Name < Base
12
+ attributes name: 0, generics: 1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Function < Base
9
+ # An argument to a function. This contains name and "klass" (type)
10
+ # information for a specific argument.
11
+ class Parameter < Base
12
+ attributes name: 0, type: 1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Function < Base
9
+ # Arguments to a function. This will eventually be turned directly
10
+ # into an array passed to functions.
11
+ class Parameters < Base
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ # A module definition. This has one child, a {ModuleName} node.
9
+ class Module < Base
10
+ attributes name: 0
11
+
12
+ # If this definition is an identity definition. Module definitions
13
+ # are the only identity definitions in a module.
14
+ #
15
+ # @return [true]
16
+ def identity?
17
+ true
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/definition/struct/element"
5
+
6
+ module Carbon
7
+ module Compiler
8
+ module Node
9
+ module Definition
10
+ # A struct definition. This sets up a structured block of memory
11
+ # for various purposes. A Struct's children are its elements.
12
+ class Struct < Base
13
+ # If this definition is a data definition. A Structure is one of
14
+ # many possible data definitions.
15
+ #
16
+ # @return [true]
17
+ def data?
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Definition
8
+ class Struct < Base
9
+ # An element of a struct. This contains name and "klass" (type)
10
+ # information for a structure element.
11
+ class Element < Base
12
+ attributes name: 0, type: 1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/etype/option"
5
+ require "carbon/compiler/node/etype/star"
6
+
7
+ module Carbon
8
+ module Compiler
9
+ module Node
10
+ class EType < Base
11
+ def expand(file, typify = true)
12
+ catch(:invalid) { check_valid_parts(file) } or return {}
13
+ return blank_mapping if clean?
14
+
15
+ # now we know that children[0..-2] are all parts and children[-1] is
16
+ # an option... we just have to expand the option now.
17
+ children[-1].expand(file).map do |key, value|
18
+ value = children[0..-2].concat(value)
19
+
20
+ if typify
21
+ { Concrete::Type.new(Concrete::Type::Name.new([key], nil)) =>
22
+ Concrete::Type.new(Concrete::Type::Name.new(value, nil)) }
23
+ else
24
+ { key => value }
25
+ end
26
+ end.inject(:merge)
27
+ end
28
+
29
+ def clean?
30
+ children.none? { |c| c.is_a?(Base) }
31
+ end
32
+
33
+ def to_type
34
+ return unless clean?
35
+ Concrete::Type.new(Concrete::Type::Name.new(children, nil),
36
+ location: location)
37
+ end
38
+
39
+ private
40
+
41
+ def blank_mapping
42
+ type = Concrete::Type.new(Concrete::Type::Name.new([children.last],
43
+ nil))
44
+ { type => to_type }
45
+ end
46
+
47
+ def check_valid_parts(file)
48
+ error_misplaced_option(file) && throw(:invalid) if children[0..-2]\
49
+ .any? { |c| c.is_a?(Base) }
50
+ error_included_star(file) && throw(:invalid) if children[-1]\
51
+ .is_a?(EType::Star)
52
+ true
53
+ end
54
+
55
+ def error_misplaced_option(file)
56
+ file.emit("Module/Name/InvalidExtended", node.location)
57
+ end
58
+
59
+ def error_included_star(file)
60
+ file.emit("System/Error", node.location,
61
+ "Cannot handle star extended types")
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ class EType < Base
8
+ class Option < Base
9
+ def expand(file)
10
+ children.map do |child|
11
+ # child.is_a?(Node::EType) # => true
12
+ if child.clean?
13
+ type = child.to_type
14
+ last = type.name.parts.last
15
+ { last => type.name.parts }
16
+ else
17
+ child.expand(file, false)
18
+ end
19
+ end.inject(:merge)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ class EType < Base
8
+ class Star < Base
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end