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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 656ef67db905ee5ced18a75d79bc9b85abcc9c3c
4
+ data.tar.gz: 1a0915c9d60d49304a6e06422cf5e7c73828295a
5
+ SHA512:
6
+ metadata.gz: a22c9f3557f76e5d4cad816da6f71ca404bb09dcea62a6093e0ee55e4513eb0e1c97e3a7ca80faf71b5fff2f97da3019a574644bd2c5d30f9aef0174cee68e58
7
+ data.tar.gz: b036680aebb5314bc61a37a74acecc556f535539a4690e3495d0b97c72c99397b46437fe8aaae49c6902698ba88b86cc0971ca37e7057a3a5bf60787cdc19fdc
@@ -0,0 +1,17 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+
4
+ # Custom for Visual Studio
5
+ *.cs diff=csharp
6
+
7
+ # Standard to msysgit
8
+ *.doc diff=astextplain
9
+ *.DOC diff=astextplain
10
+ *.docx diff=astextplain
11
+ *.DOCX diff=astextplain
12
+ *.dot diff=astextplain
13
+ *.DOT diff=astextplain
14
+ *.pdf diff=astextplain
15
+ *.PDF diff=astextplain
16
+ *.rtf diff=astextplain
17
+ *.RTF diff=astextplain
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.vagrant/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - "lib/carbon/compiler/parser/**/*.rb"
5
+ - "lib/carbon/compiler/visitor/generation/**/*.rb"
6
+ Style/AlignParameters:
7
+ EnforcedStyle: with_fixed_indentation
8
+ Style/SignalException:
9
+ EnforcedStyle: only_fail
10
+ Style/AccessModifierIndentation:
11
+ EnforcedStyle: outdent
12
+ Style/ConditionalAssignment:
13
+ Enabled: false
14
+ Style/Encoding:
15
+ Enabled: true
16
+ EnforcedStyle: always
17
+ AutoCorrectEncodingComment: "# encoding: utf-8\n"
18
+ Style/MultilineBlockChain:
19
+ Enabled: false
20
+ Style/MultilineMethodCallIndentation:
21
+ Enabled: false
22
+ Style/ParallelAssignment:
23
+ Enabled: false
24
+ Style/RedundantFreeze:
25
+ Enabled: false
26
+ Style/RegexpLiteral:
27
+ EnforcedStyle: mixed
28
+ Style/StringLiterals:
29
+ EnforcedStyle: double_quotes
30
+ Style/AndOr:
31
+ EnforcedStyle: conditionals
32
+ Style/AlignHash:
33
+ EnforcedLastArgumentHashStyle: ignore_implicit
34
+ Style/AlignArray:
35
+ Enabled: false
36
+ Style/IndentArray:
37
+ Enabled: false
38
+ Style/Alias:
39
+ EnforcedStyle: prefer_alias_method
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at me@medcat.me. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ source "https://rubygems.org"
5
+
6
+ # Specify your gem's dependencies in carbon-compiler.gemspec
7
+ gemspec
8
+ gem "pry"
9
+ gem "pry-byebug"
10
+ gem "pry-rescue"
11
+ gem "pry-stack_explorer"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jeremy Rodi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Carbon::Compiler
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/carbon/compiler`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'carbon-compiler'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install carbon-compiler
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/carbon-compiler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/gem_tasks"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ # -*- mode: ruby -*-
4
+ # vi: set ft=ruby :
5
+
6
+ # All Vagrant configuration is done below. The "2" in Vagrant.configure
7
+ # configures the configuration version (we support older styles for
8
+ # backwards compatibility). Please don't change it unless you know what
9
+ # you're doing.
10
+ Vagrant.configure(2) do |config|
11
+ # The most common configuration options are documented and commented below.
12
+ # For a complete reference, please see the online documentation at
13
+ # https://docs.vagrantup.com.
14
+
15
+ # Every Vagrant development environment requires a box. You can search for
16
+ # boxes at https://atlas.hashicorp.com/search.
17
+ config.vm.box = "terrywang/archlinux"
18
+
19
+ # Disable automatic box update checking. If you disable this, then
20
+ # boxes will only be checked for updates when the user runs
21
+ # `vagrant box outdated`. This is not recommended.
22
+ # config.vm.box_check_update = false
23
+
24
+ # Create a forwarded port mapping which allows access to a specific port
25
+ # within the machine from a port on the host machine. In the example below,
26
+ # accessing "localhost:8080" will access port 80 on the guest machine.
27
+ # config.vm.network "forwarded_port", guest: 3000, host: 3000
28
+
29
+ # Create a private network, which allows host-only access to the machine
30
+ # using a specific IP.
31
+ # config.vm.network "private_network", ip: "192.168.33.10"
32
+
33
+ # Create a public network, which generally matched to bridged network.
34
+ # Bridged networks make the machine appear as another physical device on
35
+ # your network.
36
+ # config.vm.network "public_network"
37
+
38
+ # Share an additional folder to the guest VM. The first argument is
39
+ # the path on the host to the actual folder. The second argument is
40
+ # the path on the guest to mount the folder. And the optional third
41
+ # argument is a set of non-required options.
42
+ # config.vm.synced_folder "../data", "/vagrant_data"
43
+ config.vm.synced_folder "../core", "/vagrant_core"
44
+
45
+ # Provider-specific configuration so you can fine-tune various
46
+ # backing providers for Vagrant. These expose provider-specific options.
47
+ # Example for VirtualBox:
48
+ #
49
+ config.vm.provider "virtualbox" do |vb|
50
+ # Display the VirtualBox GUI when booting the machine
51
+ # vb.gui = true
52
+
53
+ # Customize the amount of memory on the VM:
54
+ vb.memory = "1024"
55
+ end
56
+ #
57
+ # View the documentation for the provider you are using for more
58
+ # information on available options.
59
+
60
+ # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
61
+ # such as FTP and Heroku are also available. See the documentation at
62
+ # https://docs.vagrantup.com/v2/push/atlas.html for more information.
63
+ # config.push.define "atlas" do |push|
64
+ # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
65
+ # end
66
+
67
+ # Enable provisioning with a shell script. Additional provisioners such as
68
+ # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
69
+ # documentation for more information about their specific syntax and use.
70
+ # config.vm.provision "shell", inline: <<-SHELL
71
+ # sudo apt-get update
72
+ # sudo apt-get install -y apache2
73
+ # SHELL
74
+
75
+ config.vm.provision "shell", inline: <<-SHELL
76
+ sudo pacman --noconfirm -Syyu git fish base-devel libyaml openssl zlib llvm35
77
+ sudo chsh vagrant -s /usr/bin/fish
78
+ SHELL
79
+
80
+ config.vm.provision "shell", privileged: false, name: "ruby", inline: <<-SHELL
81
+ mkdir -p ~/.config/fish
82
+ echo "set -x PATH ~/.gem/ruby/2.3.0/bin \\\$PATH" >> ~/.config/fish/config.fish
83
+ SHELL
84
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "carbon/compiler/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "carbon-compiler"
9
+ spec.version = Carbon::Compiler::VERSION
10
+ spec.authors = ["Jeremy Rodi"]
11
+ spec.email = ["me@medcat.me"]
12
+
13
+ spec.summary = ""
14
+ spec.description = ""
15
+ spec.homepage = "https://github.com/carbon-lang/compiler"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_dependency "carbon-core", "~> 0.2"
27
+ spec.add_dependency "rainbow", "~> 2.1"
28
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require "carbon"
5
+ require "carbon/compiler/errors"
6
+ require "carbon/compiler/version"
7
+ require "carbon/compiler/directive"
8
+ require "carbon/compiler/location"
9
+ require "carbon/compiler/metanostic"
10
+ require "carbon/compiler/node"
11
+ require "carbon/compiler/parser"
12
+ require "carbon/compiler/project"
13
+ require "carbon/compiler/scanner"
14
+ require "carbon/compiler/visitor"
15
+
16
+ module Carbon
17
+ module Compiler
18
+ # Your code goes here...
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ class Directive
7
+ ATTACHING = %w(doc extern).freeze
8
+ def self.handlers
9
+ @_handlers ||= {}
10
+ end
11
+
12
+ def self.handle(name, &block)
13
+ handlers[name.to_s] = block
14
+ end
15
+
16
+ require "carbon/compiler/directive/import"
17
+
18
+ def initialize(node, file, index)
19
+ @node = node
20
+ @file = file
21
+ @index = index
22
+ end
23
+
24
+ def call
25
+ instance_exec(&self.class.handlers.fetch(name.to_s))
26
+ end
27
+
28
+ def name
29
+ @node.value
30
+ end
31
+
32
+ def attach?
33
+ ATTACHING.include?(name)
34
+ end
35
+
36
+ private
37
+
38
+ def error_excessive_parameters(node, expected)
39
+ @file.emit("Directive/Parameter/Excessive", node.location,
40
+ [expected, node.parameters.size])
41
+ end
42
+
43
+ def error_invalid_parameter(parameter)
44
+ @file.emit("Directive/Parameter/Invalid", parameter.location)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ class Directive
7
+ module Import
8
+ Directive.handle :import do
9
+ error_excessive_parameters(node, 1) if @node.parameters.size != 1
10
+ etype = @node.parameters.first
11
+ error_invalid_parameter(etype) unless etype.is_a?(Node::EType)
12
+ @file.aliases.merge!(etype.expand(@file))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ class DiagnosticError < Error
6
+ end
7
+ end
@@ -0,0 +1,136 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Compiler
6
+ # Contains location information for tokens and nodes. This is commonly used
7
+ # in error reporting.
8
+ class Location
9
+ include Comparable
10
+ # The lines that the location covers. Despite its name, this is actually
11
+ # a range of lines. Tokens may only be on one line; however, nodes can
12
+ # be on multiple lines.
13
+ #
14
+ # @return [Range<Integer>]
15
+ attr_reader :line
16
+ # The columns that the location covers. Despite its name, this is
17
+ # actually a range of columns.
18
+ #
19
+ # @return [Range<Integer>]
20
+ attr_reader :column
21
+ # The file that the location is in.
22
+ #
23
+ # @return [String]
24
+ attr_reader :file
25
+
26
+ include Comparable
27
+
28
+ # Returns a default location, representing no location information.
29
+ # This is used when no location information can be obtained, or none
30
+ # exists; for example, a compiler error unrelated to the parsing of the
31
+ # file.
32
+ #
33
+ # @return [Location]
34
+ def self.default(file = "(null)")
35
+ new(0..0, 0..0, file)
36
+ end
37
+
38
+ def self.internal
39
+ default("(internal)")
40
+ end
41
+
42
+ # Initializes the location. The location is frozen after initialization.
43
+ #
44
+ # @param lines [Range<Integer>]
45
+ # @param columns [Range<Integer>]
46
+ # @param files [String]
47
+ def initialize(lines, columns, files)
48
+ @line = lines
49
+ @column = columns
50
+ @file = files
51
+ to_a
52
+ freeze
53
+ end
54
+
55
+ # Compares this location to another location. It does so by converting
56
+ # both to arrays and comparing said arrays.
57
+ #
58
+ # @return [Numeric]
59
+ def <=>(other)
60
+ fail ArgumentError, "Expected a Location" unless other.is_a?(Location)
61
+ to_a <=> other.to_a
62
+ end
63
+
64
+ # Converts the location to an array. This array has no meaning outside
65
+ # of comparisons and equality. The array is frozen.
66
+ #
67
+ # @return [(Class, Range<Integer>, Range<Integer>, String)]
68
+ def to_a
69
+ @array ||= [self.class, @line, @column, @file.to_s].freeze
70
+ end
71
+
72
+ # Returns a numeric representation of this class for use of hashing.
73
+ #
74
+ # @return [Numeric]
75
+ def hash
76
+ to_a.hash
77
+ end
78
+
79
+ # A union operation. Performing a union on two locations expands both
80
+ # of them; the range of lines is expanded so that the start of the new
81
+ # range is the lowest of the start of the ranges from either location,
82
+ # and the end of the range is the highest of the end of the ranges from
83
+ # either location; the same is done with the column. This sets the size
84
+ # of the new location so that both originating locations are contained
85
+ # within the new location.
86
+ #
87
+ # @param other [Location] The location to union with.
88
+ # @return [Location] A new location containing the result of the union.
89
+ def |(other)
90
+ fail ArgumentError, "Cannot union #{other.class}" unless
91
+ other.is_a?(Location)
92
+
93
+ lines = merge_ranges(line, other.line)
94
+ columns = merge_ranges(column, other.column)
95
+ Location.new(lines, columns, file)
96
+ end
97
+
98
+ # Creates a duplicate. Since the location is frozen, this returns self.
99
+ #
100
+ # @return [self]
101
+ def dup
102
+ self
103
+ end
104
+
105
+ # Pretty inspect.
106
+ #
107
+ # @return [String]
108
+ def inspect
109
+ "#(Location #{self})"
110
+ end
111
+
112
+ # Creates a string representation of the location.
113
+ #
114
+ # @return [String]
115
+ def to_s
116
+ "#{file}:#{line_to_s}:#{column_to_s}"
117
+ end
118
+
119
+ private
120
+
121
+ def merge_ranges(*ranges)
122
+ ranges.map(&:begin).min..ranges.map(&:end).max
123
+ end
124
+
125
+ def line_to_s
126
+ return @line.end.to_s if @line.size == 1
127
+ @line.to_s
128
+ end
129
+
130
+ def column_to_s
131
+ return @column.end.to_s if @column.size == 1
132
+ @column.to_s
133
+ end
134
+ end
135
+ end
136
+ end