carbon-compiler 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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,18 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/expression/assignment"
5
+ require "carbon/compiler/node/expression/call"
6
+ require "carbon/compiler/node/expression/literal"
7
+ require "carbon/compiler/node/expression/operation"
8
+ require "carbon/compiler/node/expression/unit"
9
+
10
+ module Carbon
11
+ module Compiler
12
+ module Node
13
+ # An expression.
14
+ module Expression
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ # An assignment expression. This typically takes the form of
9
+ # `<Expression> = <Expression>`. There are three cases for assignments:
10
+ # a) an attribute assignment; b) an access assignment; and c) an
11
+ # identifier assignment. The first and second are turned into
12
+ # function calls using {Call::Dispatch}; the last is kept as-is,
13
+ # and turns into a store.
14
+ # This has three children: the left hand side, the operation, and
15
+ # the right hand side.
16
+ class Assignment < Base
17
+ attributes left: 0, op: 1, right: 2
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/expression/call/parameters"
5
+ require "carbon/compiler/node/expression/call/access"
6
+ require "carbon/compiler/node/expression/call/attribute"
7
+ require "carbon/compiler/node/expression/call/enum"
8
+ require "carbon/compiler/node/expression/call/module"
9
+ require "carbon/compiler/node/expression/call/self"
10
+ require "carbon/compiler/node/expression/call/unified"
11
+
12
+ module Carbon
13
+ module Compiler
14
+ module Node
15
+ module Expression
16
+ # Contains all of the ways a call can be made/handled.
17
+ module Call
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ module Call
9
+ # An "Access" call. An access call takes the form of
10
+ # `<Expression>[<Arguments>]`. This is used to index arrays.
11
+ # An access call has two children: an expression and the arguments
12
+ # for the access. In the example below, there is only one argument.
13
+ #
14
+ # @example
15
+ # # let a: Carbon::Pointer<Carbon::Integer>;
16
+ # a[0]; # => Carbon::Integer
17
+ class Access < Base
18
+ attributes expression: 0, parameters: 1, generics: 2
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ 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 Expression
8
+ module Call
9
+ # An attribute call. This is used for accessing an "attribute" of
10
+ # a module. This takes the form of `<Expression>.<Name>`. This
11
+ # has two children: an expression, and a name.
12
+ #
13
+ # @example
14
+ # # let a: Carbon::Pointer<Carbon::Integer>;
15
+ # a.value = 1;
16
+ class Attribute < Base
17
+ attributes expression: 0, name: 1, generics: 2
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ 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
+ module Expression
8
+ module Call
9
+ # An Enum call. This is a call used to create an "enum" value.
10
+ # This takes the form of
11
+ # `<ModuleName>.<ModuleNamePart>.(<Arguments>)`. This has three
12
+ # children: the module name, the enum element name, and the
13
+ # arguments.
14
+ #
15
+ # @example
16
+ # # let a: Some::Enum;
17
+ # a = Some::Enum.Red();
18
+ class Enum < Base
19
+ attributes module: 0, name: 1, parameters: 2, generics: 3
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ module Call
9
+ # A basic module call. This is the most basic version of a call.
10
+ # This takes the form `<ModuleName>.<Name>(<Arguments>)`.
11
+ # This has three children: the module name, the name of the
12
+ # function, and the arguments.
13
+ #
14
+ # @example
15
+ # let a: Carbon::Pointer<Carbon::Integer>;
16
+ # Carbon::Pointer<Carbon::Integer>.value=(a, 1);
17
+ class Module < Base
18
+ attributes module: 0, name: 1, parameters: 2, generics: 3
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ 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 Expression
8
+ module Call
9
+ # The arguments to the call. This may be turned into a pure array
10
+ # in later versions.
11
+ class Parameters < Base
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ 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 Expression
8
+ module Call
9
+ # Unused.
10
+ class Self < Base
11
+ attributes name: 0, parameters: 1, generics: 2
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ module Call
9
+ # A "unified" call. This is the extended version of the module
10
+ # call. This can be turned into a module call by taking the
11
+ # module type of the expression and using that as the base, and
12
+ # passing the expression as the first parameter. This takes the
13
+ # form `<Expression>.<Name>(<Arguments>)`. This has three
14
+ # children: an expression, a name, and the arguments.
15
+ #
16
+ # @example
17
+ # # let a: Carbon::Pointer<Carbon::Integer>;
18
+ # a.size(); # Same as the following:
19
+ # Carbon::Pointer<Carbon::Integer>.size(a);
20
+ class Unified < Base
21
+ attributes expression: 0, name: 1, parameters: 2, generics: 3
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ # A literal expression. Contains information about how the literal
9
+ # expression should be represented.
10
+ class Literal < Base
11
+ # Replacements for strings.
12
+ #
13
+ # @return [{String => String}]
14
+ REPLACE = {
15
+ '\"' => '"',
16
+ '\\\\' => '\\',
17
+ '\/' => "/",
18
+ '\b' => "\b",
19
+ '\f' => "\f",
20
+ '\n' => "\n",
21
+ '\r' => "\r",
22
+ '\t' => "\t"
23
+ }.freeze
24
+
25
+ # Yields a LLVM value representing the literal.
26
+ #
27
+ # @return [::Numeric, ::String]
28
+ def value
29
+ case @children.first.type
30
+ when :NUMBER then number_value
31
+ # LLVM.Int(@children.first.value.to_i)
32
+ when :FLOAT
33
+ LLVM.Float(@children.first.value.to_f)
34
+ when :STRING
35
+ fail
36
+ @children.first.value[1..-2]
37
+ .gsub(%r{\\["\\/bfnrt]}, REPLACE)
38
+ else fail
39
+ end
40
+ end
41
+
42
+ # Yields the Carbon type representing the literal.
43
+ #
44
+ # @return [Carbon::Concrete::Type]
45
+ def type
46
+ case @children.first.type
47
+ # when :NUMBER then Carbon::Type("Carbon::Int32")
48
+ when :NUMBER then number_type
49
+ when :FLOAT then Carbon::Type("Carbon::Float")
50
+ when :STRING then Carbon::Type("Carbon::String")
51
+ else fail
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def raw
58
+ @children.first.value
59
+ end
60
+
61
+ def postfix
62
+ raw.match(/(?:0x[a-f0-9]+|(?:0|[1-9][0-9]*))(?:_?([ui][0-9]+))?/)[1] || "i32"
63
+ end
64
+
65
+ def number
66
+ raw.match(/(0x[a-f0-9]+|(?:0|[1-9][0-9]*))(?:_?(?:[ui][0-9]+))?/)[1]
67
+ end
68
+
69
+ def number_value
70
+ LLVM.const_get("Int#{postfix[1..-1]}").from_i(number.to_i,
71
+ postfix[0] == "i")
72
+ end
73
+
74
+ def number_type
75
+ sign = postfix[0] == "i" ? :signed : :unsigned
76
+ size = postfix[1..-1].to_i
77
+ Core::Int.find(sign: sign, size: size).name
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon/compiler/node/expression/operation/and"
5
+ require "carbon/compiler/node/expression/operation/neq"
6
+ require "carbon/compiler/node/expression/operation/normal"
7
+ require "carbon/compiler/node/expression/operation/or"
8
+
9
+ module Carbon
10
+ module Compiler
11
+ module Node
12
+ module Expression
13
+ # An operation. This is just the superclass for the potential
14
+ # operations.
15
+ class Operation < Base
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ class Operation < Base
9
+ # An "And" operation. This is generated internally and cannot be
10
+ # redefined by defining an "And" function.
11
+ # This has three children: the left hand side, the operation, and
12
+ # the right hand side. This has the form `<Left> && <Right>`.
13
+ class And < Operation
14
+ attributes left: 0, op: 1, right: 2
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ class Operation < Base
9
+ # A "Neq" operation. This is generated internally and cannot be
10
+ # redefined by defining a "Neq" function.
11
+ # This has three children: the left hand side, the operation, and
12
+ # the right hand side. This has the form `<Left> != <Right>`.
13
+ # This is redefined in terms of the Eq operation (`==`).
14
+ class Neq < Operation
15
+ attributes left: 0, op: 1, right: 2, func: 3
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ class Operation < Base
9
+ # A normal operation. This is redefined into a {Call::Dispatch}
10
+ # in order to turn all operations into function calls.
11
+ # This has three children: the left hand side, the operation,
12
+ # and the right hand side. This takes the form `<Left> <Op> <Right>`.
13
+ class Normal < Operation
14
+ attributes left: 0, op: 1, right: 2
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ class Operation < Base
9
+ # An "Or" operation. This is generated internally and cannot be
10
+ # redefined by defining an "Or" function.
11
+ # This has three children: the left hand side, the operation, and
12
+ # the right hand side. This has the form `<Left> || <Right>`.
13
+ class Or < Operation
14
+ attributes left: 0, op: 1, right: 2
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ module Node
7
+ module Expression
8
+ # A "unit". This takes the form of `(<Expressions>)`. This is
9
+ # used as a sort of "mini-struct". Mostly unused at the moment.
10
+ class Unit < Base
11
+ attr_reader :type
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end