toylang 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbe7ffd79835180620bc081934f946ad233de20d142c06cf88258e8ed961bdda
4
+ data.tar.gz: '08672691c40f5ccf2593eb1c1a821fc9a6f8feb01546e20044e26f844a392747'
5
+ SHA512:
6
+ metadata.gz: 2078057fca5129a6585a56547e7a2cbae22524eb89874e8210a77cfff781192b31047bbf701c3954e57d6ad87734ecbf1ded30d6fbcd04fdf263d3315569fb8e
7
+ data.tar.gz: 503dd28b803df08405869ac03688d9264233c95b43bf1012307171af0ece354024b33620ff54da294e6e4657e7d3d1443ab7ac55268e8deca757de99a91b6db1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,106 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.5
7
+ NewCops: enable
8
+ Exclude:
9
+ - 'spec/dummy/bin/*'
10
+ - '*.md'
11
+ - lib/toylang/parser/*
12
+ - vendor/bundle/**/*
13
+
14
+ Layout/SpaceInsideHashLiteralBraces:
15
+ EnforcedStyle: no_space
16
+
17
+ Gemspec/RequireMFA:
18
+ Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ Max: 100
22
+ Exclude:
23
+ - spec/**/*.rb
24
+ - toylang.gemspec
25
+
26
+ Metrics/BlockNesting:
27
+ Max: 4
28
+
29
+ Layout/LineLength:
30
+ Max: 120
31
+ AllowURI: true
32
+
33
+ Metrics/MethodLength:
34
+ CountComments: false
35
+ Max: 80
36
+
37
+ Metrics/ModuleLength:
38
+ Max: 250
39
+
40
+ Metrics/ClassLength:
41
+ Max: 350
42
+
43
+ Metrics/ParameterLists:
44
+ Max: 5
45
+ CountKeywordArgs: true
46
+
47
+ Metrics/AbcSize:
48
+ Enabled: false
49
+
50
+ Metrics/CyclomaticComplexity:
51
+ Enabled: false
52
+
53
+ Metrics/PerceivedComplexity:
54
+ Enabled: false
55
+
56
+ Style/CollectionMethods:
57
+ Enabled: true
58
+ PreferredMethods:
59
+ collect: 'map'
60
+ collect!: 'map!'
61
+ inject: 'reduce'
62
+ find: 'detect'
63
+ find_all: 'select'
64
+ delete: 'gsub'
65
+
66
+ Style/Documentation:
67
+ Enabled: false
68
+
69
+ Layout/DotPosition:
70
+ EnforcedStyle: trailing
71
+
72
+ Naming/FileName:
73
+ Enabled: false
74
+
75
+ Layout/AccessModifierIndentation:
76
+ Enabled: false
77
+
78
+ Style/TrailingCommaInArrayLiteral:
79
+ EnforcedStyleForMultiline: 'no_comma'
80
+
81
+ Style/TrailingCommaInHashLiteral:
82
+ EnforcedStyleForMultiline: 'no_comma'
83
+
84
+ RSpec/MultipleExpectations:
85
+ Max: 30
86
+
87
+ RSpec/ExampleLength:
88
+ Max: 35
89
+
90
+ RSpec/FilePath:
91
+ Enabled: false
92
+
93
+ RSpec/BeforeAfterAll:
94
+ Enabled: false
95
+
96
+ RSpec/NamedSubject:
97
+ Enabled: false
98
+
99
+ RSpec/MultipleMemoizedHelpers:
100
+ Max: 10
101
+
102
+ Style/StringLiterals:
103
+ EnforcedStyle: single_quotes
104
+
105
+ Style/StringLiteralsInInterpolation:
106
+ EnforcedStyle: single_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+
4
+ ## [0.2.0] - 2022-02-01
5
+
6
+ - Create lexer, parser and interpreter
7
+
8
+ ## [0.1.0] - 2022-01-01
9
+
10
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at maximbarsukov@bk.ru. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 maxbarsukov
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.
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # ToyLang
2
+
3
+ [![Build Status](https://github.com/maxbarsukov/toylang/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/maxbarsukov/toylang/actions/workflows/main.yml)
4
+ [![Codecov](https://codecov.io/gh/maxbarsukov/toylang/branch/master/graph/badge.svg?token=9L8Y4N4KKW)](https://codecov.io/gh/maxbarsukov/toylang)
5
+
6
+ ***ToyLang*** is interpreted high-level fully OOP language with *Python-like* syntax and dynamic typing.
7
+
8
+ > Complex is better than complicated.
9
+
10
+ As a multi-paradigm language, ***ToyLang*** supports object-oriented and imperative programming styles both.
11
+
12
+ ## Installation
13
+
14
+ $ gem install toylang
15
+
16
+ ## Usage
17
+
18
+ Run REPL:
19
+ ```bash
20
+ toylang
21
+ ```
22
+
23
+ or run file:
24
+ ```bash
25
+ toylang your_code.tlg
26
+ ```
27
+
28
+ ## Examples of code
29
+
30
+ ```python
31
+ # comment
32
+ println("Hello World")
33
+ "Hello World".println # Hello World
34
+
35
+ println(2 + 2 ** 5) # 34
36
+
37
+ def hello(name):
38
+ "Hi, " + name + "!"
39
+
40
+ hello("Max").println # => Hi, Max!
41
+
42
+ a = 23
43
+ b = a + 2
44
+ println(b) # => 25
45
+
46
+ Hey = 5 # constant
47
+
48
+ if (2 > 1):
49
+ println("Yeah!")
50
+ else:
51
+ println("Something goes bad!")
52
+
53
+ if (false):
54
+ print("Won't be printed")
55
+ ```
56
+
57
+ Cycles:
58
+ ```python
59
+ a = 0
60
+ while (a != 3):
61
+ println(a)
62
+ a = a + 1
63
+ # => 0 1 2
64
+ ```
65
+
66
+ OOP:
67
+ ```python
68
+ # class defination
69
+ class Klass:
70
+ def my_method:
71
+ true
72
+
73
+ # object creating
74
+ k = Klass.new
75
+ k.my_method.print # => true
76
+
77
+ # monkey patching
78
+ class Number:
79
+ def ten:
80
+ 10
81
+
82
+ 1.ten.println # => 10
83
+
84
+ class Number:
85
+ def ten:
86
+ 11
87
+
88
+ 1.ten.println # => 11
89
+
90
+ # inheritance
91
+ class Animal:
92
+ def is_a_human:
93
+ false
94
+ def say:
95
+ "..."
96
+
97
+ class Dog < Animal:
98
+ def say:
99
+ "Bark!"
100
+
101
+ dog = Dog.new
102
+ animal = Animal.new
103
+
104
+ println(animal.say) # ...
105
+ println(dog.say) # Bark!
106
+ println(dog.is_a_human) # false
107
+ ```
108
+
109
+ ## Building
110
+
111
+ ### Pre-reqs
112
+
113
+ To build and run this app locally you will need a few things:
114
+
115
+ - Install [Ruby](https://www.ruby-lang.org/en/) *(tested on **2.6**)*;
116
+ - Using [Racc](https://github.com/ruby/racc) as parser
117
+
118
+ ### Getting start
119
+
120
+ - Clone the repository
121
+ ```bash
122
+ git clone --depth=1 https://github.com/maxbarsukov/toylang.git
123
+ ```
124
+ - **Install dependencies**
125
+ ```bash
126
+ cd toylang
127
+ bundle install
128
+ ```
129
+ - **Run**
130
+ ```bash
131
+ ./bin/toylang input.tlg
132
+ # or
133
+ ./bin/toylang
134
+ ````
135
+ - **RSpec**
136
+ ```bash
137
+ bundle exec rspec
138
+ ```
139
+ - **Rubocop**
140
+ ```bash
141
+ bundle exec rubocop
142
+ ```
143
+
144
+ ## Contributing
145
+
146
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maxbarsukov/toylang. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/maxbarsukov/toylang/blob/master/CODE_OF_CONDUCT.md).
147
+
148
+ ## License
149
+
150
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
151
+
152
+ ## Code of Conduct
153
+
154
+ Everyone interacting in the Toylang project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/maxbarsukov/toylang/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ task :generate_parser do
15
+ puts 'Generating a parser (lib/toylang/parser/parser.rb)...'
16
+ `bundle exec racc -o lib/toylang/parser/parser.rb lib/toylang/grammar/grammar.y`
17
+ puts 'Complete!'
18
+ end
data/bin/toylang ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'readline'
5
+ require_relative '../lib/toylang'
6
+
7
+ interpreter = Interpreter.new
8
+
9
+ if ARGV.first
10
+ interpreter.eval File.read(ARGV.first)
11
+ else
12
+ lines = ''
13
+ puts 'ToyLang REPL, CTRL+C to quit'
14
+ loop do
15
+ greet = lines == '' ? '>> ' : '| '
16
+ line = Readline.readline(greet)
17
+ Readline::HISTORY.push(line)
18
+ lines += if lines.chomp[-1] == ':'
19
+ "\n#{line}"
20
+ else
21
+ line
22
+ end
23
+
24
+ begin
25
+ value = interpreter.eval(lines)
26
+ out = (value.ruby_value.inspect if value.respond_to? :ruby_value)
27
+ out ||= value.inspect
28
+ puts "=> #{out}"
29
+ lines = ''
30
+ rescue Racc::ParseError # rubocop:disable Lint/SuppressedException
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ Nodes = Struct.new(:nodes) do
4
+ def <<(node)
5
+ nodes << node
6
+ self
7
+ end
8
+ end
9
+
10
+ LiteralNode = Struct.new(:value)
11
+
12
+ class NumberNode < LiteralNode; end
13
+ class StringNode < LiteralNode; end
14
+
15
+ class TrueNode < LiteralNode
16
+ def initialize
17
+ super(true)
18
+ end
19
+ end
20
+
21
+ class FalseNode < LiteralNode
22
+ def initialize
23
+ super(false)
24
+ end
25
+ end
26
+
27
+ class NilNode < LiteralNode
28
+ def initialize
29
+ super(nil)
30
+ end
31
+ end
32
+
33
+ CallNode = Struct.new(:receiver, :call_method, :arguments)
34
+
35
+ GetConstantNode = Struct.new(:name)
36
+ SetConstantNode = Struct.new(:name, :value)
37
+
38
+ GetLocalNode = Struct.new(:name)
39
+ SetLocalNode = Struct.new(:name, :value)
40
+
41
+ DefNode = Struct.new(:name, :params, :body)
42
+
43
+ ClassNode = Struct.new(:name, :body, :superclass)
44
+ IfElseNode = Struct.new(:condition, :body, :else_body)
45
+ WhileNode = Struct.new(:condition, :body)
@@ -0,0 +1,171 @@
1
+ class Parser
2
+ token IF ELSE
3
+ token WHILE
4
+ token DEF
5
+ token CLASS
6
+ token NEWLINE
7
+ token NUMBER
8
+ token STRING
9
+ token TRUE FALSE NIL
10
+ token IDENTIFIER
11
+ token CONSTANT
12
+ token INDENT DEDENT
13
+
14
+ # Precedence Table
15
+ prechigh
16
+ left '.'
17
+ right '~'
18
+ right '!' '-'
19
+ left '**'
20
+ left '*' '/' '%'
21
+ left '+' '-'
22
+ left '<<' '>>'
23
+ left '>' '>=' '<' '<='
24
+ left '==' '!='
25
+ left '&'
26
+ left '^'
27
+ left '|'
28
+ left '&&'
29
+ left '||'
30
+ right '='
31
+ left ','
32
+ preclow
33
+
34
+ rule
35
+
36
+ Root:
37
+ /* nothing */ { result = Nodes.new([]) }
38
+ | Expressions { result = val[0] }
39
+ ;
40
+
41
+ Expressions:
42
+ Expression { result = Nodes.new(val) }
43
+ | Expressions Terminator Expression { result = val[0] << val[2] }
44
+ | Expressions Terminator { result = val[0] }
45
+ | Terminator { result = Nodes.new([]) }
46
+ ;
47
+
48
+ Expression:
49
+ Literal
50
+ | Call
51
+ | Operator
52
+ | Constant
53
+ | Assign
54
+ | Def
55
+ | Class
56
+ | IfElse
57
+ | While
58
+ | '(' Expression ')' { result = val[1] }
59
+ ;
60
+
61
+ Terminator:
62
+ NEWLINE
63
+ | ";"
64
+ ;
65
+
66
+ Literal:
67
+ NUMBER { result = NumberNode.new(val[0]) }
68
+ | STRING { result = StringNode.new(val[0]) }
69
+ | TRUE { result = TrueNode.new }
70
+ | FALSE { result = FalseNode.new }
71
+ | NIL { result = NilNode.new }
72
+ ;
73
+
74
+ Call:
75
+ # method
76
+ IDENTIFIER { result = CallNode.new(nil, val[0], []) }
77
+ # method(arguments)
78
+ | IDENTIFIER "(" ArgList ")" { result = CallNode.new(nil, val[0], val[2]) }
79
+ # receiver.method
80
+ | Expression "." IDENTIFIER { result = CallNode.new(val[0], val[2], []) }
81
+ # receiver.method(arguments)
82
+ | Expression "."
83
+ IDENTIFIER "(" ArgList ")" { result = CallNode.new(val[0], val[2], val[4]) }
84
+ ;
85
+
86
+ ArgList:
87
+ /* nothing */ { result = [] }
88
+ | Expression { result = val }
89
+ | ArgList "," Expression { result = val[0] << val[2] }
90
+ ;
91
+
92
+ Operator:
93
+ Expression '||' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
94
+ | Expression '&&' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
95
+ | Expression '==' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
96
+ | Expression '!=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
97
+ | Expression '>' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
98
+ | Expression '>=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
99
+ | Expression '<' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
100
+ | Expression '<=' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
101
+ | Expression '+' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
102
+ | Expression '-' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
103
+ | Expression '*' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
104
+ | Expression '/' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
105
+ | Expression '%' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
106
+ | Expression '<<' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
107
+ | Expression '>>' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
108
+ | Expression '**' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
109
+ | Expression '&' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
110
+ | Expression '|' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
111
+ | Expression '^' Expression { result = CallNode.new(val[0], val[1], [val[2]]) }
112
+ | '!' Expression { result = CallNode.new(val[1], val[0], []) }
113
+ | '-' Expression { result = CallNode.new(val[1], val[0], []) }
114
+ | '~' Expression { result = CallNode.new(val[1], val[0], []) }
115
+ ;
116
+
117
+ Constant:
118
+ CONSTANT { result = GetConstantNode.new(val[0]) }
119
+ ;
120
+
121
+ Assign:
122
+ IDENTIFIER "=" Expression { result = SetLocalNode.new(val[0], val[2]) }
123
+ | CONSTANT "=" Expression { result = SetConstantNode.new(val[0], val[2]) }
124
+ ;
125
+
126
+ Def:
127
+ DEF IDENTIFIER Block { result = DefNode.new(val[1], [], val[2]) }
128
+ | DEF IDENTIFIER
129
+ "(" ParamList ")" Block { result = DefNode.new(val[1], val[3], val[5]) }
130
+ ;
131
+
132
+ ParamList:
133
+ /* nothing */ { result = [] }
134
+ | IDENTIFIER { result = val }
135
+ | ParamList "," IDENTIFIER { result = val[0] << val[2] }
136
+ ;
137
+
138
+ While:
139
+ WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
140
+ ;
141
+
142
+ Class:
143
+ CLASS CONSTANT Block { result = ClassNode.new(val[1], val[2], nil) }
144
+ | CLASS CONSTANT "<" CONSTANT Block { result = ClassNode.new(val[1], val[4], val[3]) }
145
+ ;
146
+
147
+ IfElse:
148
+ IF Expression Block { result = IfElseNode.new(val[1], val[2], nil) }
149
+ | IF Expression Block NEWLINE ELSE Block { result = IfElseNode.new(val[1], val[2], val[5]) }
150
+ ;
151
+
152
+ Block:
153
+ INDENT Expressions DEDENT { result = val[1] }
154
+ ;
155
+ end
156
+
157
+ # The final code at the bottom of this Racc file will be put as-is in the generated `Parser` class.
158
+ ---- header
159
+ require_relative '../lexer/lexer'
160
+ require_relative '../ast/nodes'
161
+
162
+ ---- inner
163
+ def parse(code, show_tokens=false)
164
+ @tokens = Lexer.new.tokenize(code)
165
+ puts @tokens.inspect if show_tokens
166
+ do_parse
167
+ end
168
+
169
+ def next_token
170
+ @tokens.shift
171
+ end