rblox 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96c62a4c1631167e63a3271c808ea4444b9f5c6d2a55a4b62040b415aba91d84
4
- data.tar.gz: 8feb458e599976a076aebac98d50aa0ebeaff21997191326e7325430089a48e4
3
+ metadata.gz: 33fe0cfaebf1944ac7a1c6af6390f792fd74499a5249c23a7e14054e2b1003cd
4
+ data.tar.gz: 788651c6045c483c86dee52c2596f89e1d108b45c80f5f37598a1c578346867a
5
5
  SHA512:
6
- metadata.gz: f8f38b6eccc70997000a4a3976e1f821f6f602a10a5e068dfaabc90d1f454babd862d7c6666b32ed1a2374aa2f387359b368c1ea52f0bacccdba021a7eb367cb
7
- data.tar.gz: 1f658843f8588862150bb0226d389562a35e943992e4ffea256ab1fea75d9fd005e2bb160642867050ef97f702e5fc5d7d58f2c2c1984ed5a96132b32abaf9c4
6
+ metadata.gz: 074100db744494cc8b6900c283ff14e4ed6c392da3d264491fe746d2a3239550e82afad43c22e37abaef2866ff941b25cda48b06e676aafc94505592a50f52e1
7
+ data.tar.gz: c50ffc40e48779f5b2ec3a1dfd66de6db01964f94602af3d28bc32d36b2e868e43374f6c0a4902723476c9db3e60b195f9d92d778567a04d64434e8a6ab1522b
data/.rubocop.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  inherit_gem:
2
- dc-rubocop: default.yml
2
+ dc-rubocop: ruby26.yml
3
3
 
4
- AllCops:
5
- TargetRubyVersion: 2.6
4
+ Style/TrailingCommaInArrayLiteral:
5
+ EnforcedStyleForMultiline: comma
6
+
7
+ Style/TrailingCommaInHashLiteral:
8
+ EnforcedStyleForMultiline: comma
@@ -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 dave@dafyddcrosby.com. 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 CHANGED
@@ -5,5 +5,6 @@ source "https://rubygems.org"
5
5
  gemspec
6
6
 
7
7
  group :development, optional: true do
8
- gem "dc-devtools"
8
+ gem "dc-devtools", "~> 0.4"
9
+ gem "rdoc"
9
10
  end
data/Guardfile CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- directories(%w[. lib test].select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") })
3
+ directories(%w[. exe lib test].select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") })
4
4
 
5
5
  guard :rake, task: "default" do
6
6
  watch("Gemfile")
7
7
  watch("Rakefile")
8
8
  watch("Guardfile")
9
- watch(%r{^test/helper\.rb$})
10
9
  watch(%r{^test/test_(.*)\.rb$})
11
10
  watch(%r{^lib/rblox/(.*)\.rb$})
12
11
  watch(%r{^lib/(.*)\.rb$})
12
+ watch(%r{^exe/(.*)\.rb$})
13
13
  end
data/exe/rblox CHANGED
@@ -1,108 +1,12 @@
1
1
  #!/usr/bin/ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "strscan"
4
+ require "rblox"
4
5
 
5
- class RbLox
6
- def initialize
7
- @had_error = false
8
- end
9
-
10
- def self.read_source_file(file)
11
- # TODO: error handling
12
- f_contents = ::File.read(file)
13
- p f_contents
14
- end
15
-
16
- def run_prompt
17
- loop do
18
- # TODO: how to chomp newline?
19
- puts "> "
20
- line = gets
21
- next if line.strip == ""
22
-
23
- run(line)
24
- @had_error = false
25
- end
26
- end
27
-
28
- def run(line)
29
- tokens = Scanner.new(line)
30
- tokens.scantokens.each do |t|
31
- puts t
32
- end
33
- end
34
-
35
- def error(line, msg)
36
- report(line, "", msg)
37
- end
38
-
39
- module Tokens
40
- i = 1
41
- lookup = {}
42
- %w[ LEFT_PAREN RIGHT_PAREN LEFT_BRACE RIGHT_BRACE COMMA DOT MINUS
43
- PLUS SEMICOLON SLASH STAR BANG BANG_EQUAL EQUAL EQUAL_EQUAL
44
- GREATER GREATER_EQUAL LESS LESS_EQUAL IDENTIFIER STRING NUMBER
45
- AND CLASS ELSE FALSE FUN FOR IF NIL OR PRINT RETURN SUPER
46
- THIS TRUE VAR WHILE ].each do |token|
47
- val = 1 << i
48
- const_set(token, val)
49
- lookup[val] = token.to_sym
50
- i += 1
51
- end
52
- Lookup = lookup
53
- end
54
-
55
- class Token
56
- def initialize(type, lexeme = "", literal = "", line = nil)
57
- @type = type
58
- @lexeme = lexeme
59
- @literal = literal
60
- @line = line
61
- end
62
-
63
- def to_s
64
- "#{type} #{lexeme} #{literal}".strip
65
- end
66
- end
67
-
68
- class Scanner
69
- def initialize(source)
70
- @source = source
71
- @tokens = []
72
- end
73
-
74
- def scantokens
75
- s = ::StringScanner.new(@source)
76
- until s.eos?
77
- lexeme = s.scan(/\w+/)
78
- @tokens.append scan_token(lexeme)
79
-
80
- # Move scanner pointer forward
81
- s.scan(/\s+/)
82
- end
83
- puts "returning tok"
84
- @tokens
85
- end
86
-
87
- def scan_token(lexeme)
88
- case lexeme
89
- when "{"
90
- Tokens.new(Tokens::LEFT_BRACE)
91
- when "}"
92
- Tokens.new(Tokens::RIGHT_BRACE)
93
- end
94
- end
95
- end
96
-
97
- private
98
-
99
- def report(line, where, msg)
100
- puts "[line #{line}] Error #{where}: #{msg}"
101
- end
102
- end
6
+ # TODO: use optparse
103
7
 
104
8
  abort("usage blah") if ARGV.size > 1
105
- l = RbLox.new
9
+ l = RbLox::Interpreter.new
106
10
  if ARGV.size == 1
107
11
  l.read_source_file(ARGV[0])
108
12
  else
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strscan"
4
+
5
+ module RbLox
6
+ module Tokens
7
+ i = 0
8
+ lookup = {}
9
+ %w[ LEFT_PAREN RIGHT_PAREN LEFT_BRACE RIGHT_BRACE COMMA DOT MINUS
10
+ PLUS SEMICOLON SLASH STAR BANG BANG_EQUAL EQUAL EQUAL_EQUAL
11
+ GREATER GREATER_EQUAL LESS LESS_EQUAL IDENTIFIER STRING NUMBER
12
+ AND CLASS ELSE FALSE FUN FOR IF NIL OR PRINT RETURN SUPER
13
+ THIS TRUE VAR WHILE ].each do |token|
14
+ val = 1 << i
15
+ const_set(token, val)
16
+ lookup[val] = token.to_sym
17
+ i += 1
18
+ end
19
+ LOOKUP = lookup
20
+
21
+ # TODO: Could generate this from array and be clever ;-)
22
+ KEYWORD_MAP = {
23
+ "and" => RbLox::Tokens::AND,
24
+ "class" => RbLox::Tokens::CLASS,
25
+ "else" => RbLox::Tokens::ELSE,
26
+ "false" => RbLox::Tokens::FALSE,
27
+ "for" => RbLox::Tokens::FOR,
28
+ "fun" => RbLox::Tokens::FUN,
29
+ "if" => RbLox::Tokens::IF,
30
+ "nil" => RbLox::Tokens::NIL,
31
+ "or" => RbLox::Tokens::OR,
32
+ "print" => RbLox::Tokens::PRINT,
33
+ "return" => RbLox::Tokens::RETURN,
34
+ "super" => RbLox::Tokens::SUPER,
35
+ "this" => RbLox::Tokens::THIS,
36
+ "true" => RbLox::Tokens::TRUE,
37
+ "var" => RbLox::Tokens::VAR,
38
+ "while" => RbLox::Tokens::WHILE,
39
+ }.freeze
40
+ end
41
+
42
+ class Token
43
+ include Tokens
44
+ def initialize(type, lexeme = "", literal = "", line = nil)
45
+ @type = type
46
+ @lexeme = lexeme
47
+ @literal = literal
48
+ @line = line
49
+ end
50
+
51
+ def to_s
52
+ "#{@type} #{@lexeme} #{@literal}".strip
53
+ end
54
+
55
+ def type
56
+ LOOKUP[@type]
57
+ end
58
+ end
59
+
60
+ class Scanner
61
+ include Tokens
62
+ def initialize(source)
63
+ @source = source
64
+ @tokens = []
65
+ @has_errors = false
66
+ @scanner = nil
67
+ @line = 1
68
+ end
69
+
70
+ def scantokens
71
+ @scanner = ::StringScanner.new(@source)
72
+ until @scanner.eos?
73
+ t = case (c = @scanner.getch)
74
+ when "("
75
+ Token.new(LEFT_PAREN)
76
+ when ")"
77
+ Token.new(RIGHT_PAREN)
78
+ when "{"
79
+ Token.new(LEFT_BRACE)
80
+ when "}"
81
+ Token.new(RIGHT_BRACE)
82
+ when ","
83
+ Token.new(COMMA)
84
+ when "."
85
+ Token.new(DOT)
86
+ when "-"
87
+ Token.new(MINUS)
88
+ when "+"
89
+ Token.new(PLUS)
90
+ when ";"
91
+ Token.new(SEMICOLON)
92
+ when "*"
93
+ Token.new(STAR)
94
+ when "!"
95
+ Token.new(match("=") ? BANG_EQUAL : BANG)
96
+ when "="
97
+ Token.new(match("=") ? EQUAL_EQUAL : EQUAL)
98
+ when "<"
99
+ Token.new(match("=") ? LESS_EQUAL : LESS)
100
+ when ">"
101
+ Token.new(match("=") ? GREATER_EQUAL : GREATER)
102
+ when "\""
103
+ string
104
+ when "/"
105
+ if match("/")
106
+ @scanner.skip_until(/\n/)
107
+ next
108
+ else
109
+ Token.new(SLASH)
110
+ end
111
+ when "\r", "\t", " "
112
+ next
113
+ when "\n"
114
+ @line += 1
115
+ next
116
+ else
117
+ if digit?(c)
118
+ number(c)
119
+ elsif alpha?(c)
120
+ identifier(c)
121
+ else
122
+ # raise UnexpectedCharacter
123
+ @has_errors = true
124
+ next
125
+ end
126
+ end
127
+ @tokens.append t
128
+
129
+ # Move scanner pointer forward on
130
+ @scanner.scan(/\s*/)
131
+ end
132
+ @tokens.compact # compact to deal with nil returns (should check has_errors)
133
+ end
134
+
135
+ def digit?(char)
136
+ char >= "0" && char <= "9"
137
+ end
138
+
139
+ def alpha?(char)
140
+ (char >= "a" && char <= "z") ||
141
+ (char >= "A" && char <= "Z") ||
142
+ char == "_"
143
+ end
144
+
145
+ def alphanumeric?(char)
146
+ alpha?(char) || digit?(char)
147
+ end
148
+
149
+ def identifier(char)
150
+ # TODO: pull substring from scanner (save an allocate)
151
+ string = char
152
+ string << @scanner.getch while alphanumeric? @scanner.peek(1)
153
+
154
+ type = KEYWORD_MAP[string]
155
+ type ||= IDENTIFIER
156
+ Token.new(type)
157
+ end
158
+
159
+ def number(char)
160
+ # TODO: pull substring from scanner (save an allocate)
161
+ string = char
162
+ string << @scanner.getch while digit?(@scanner.peek(1))
163
+
164
+ if @scanner.peek(1) == "." && digit?(@scanner.peek(2)[1])
165
+ string << @scanner.getch
166
+ string << @scanner.getch while digit?(@scanner.peek(1))
167
+ end
168
+ Token.new(NUMBER, string.to_f)
169
+ end
170
+
171
+ def string
172
+ # TODO: pull substring from scanner (save an allocate)
173
+ string = String.new
174
+ while (@scanner.peek(1) != '"') && !@scanner.eos?
175
+ string << @scanner.getch
176
+ @lines += 1 if @scanner.peek(1) == "\n"
177
+ end
178
+
179
+ if @scanner.eos?
180
+ puts "unterminated line"
181
+ return
182
+ end
183
+
184
+ # get trailing "
185
+ @scanner.getch
186
+ Token.new(STRING, string)
187
+ end
188
+
189
+ def match(expected)
190
+ return false if @scanner.eos?
191
+ return false if @scanner.peek(1) != expected
192
+
193
+ @scanner.getch # move ahead a character
194
+ true
195
+ end
196
+ end
197
+ end
data/lib/rblox/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rblox
4
- VERSION = "0.0.1"
3
+ module RbLox
4
+ VERSION = "0.0.2"
5
5
  end
data/lib/rblox.rb CHANGED
@@ -1,7 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rblox/version"
4
+ require "rblox/scanner"
4
5
 
5
- module Rblox
6
- # TODO
6
+ module RbLox
7
+ class Interpreter
8
+ def initialize
9
+ @had_error = false
10
+ end
11
+
12
+ def read_source_file(file)
13
+ # TODO: error handling
14
+ f_contents = ::File.read(file)
15
+ p f_contents
16
+ end
17
+
18
+ def run_prompt
19
+ loop do
20
+ # TODO: how to chomp newline?
21
+ puts "> "
22
+ line = gets
23
+ next if line.strip == ""
24
+
25
+ run(line)
26
+ @had_error = false
27
+ end
28
+ end
29
+
30
+ def run(line)
31
+ tokens = Scanner.new(line)
32
+ tokens.scantokens.each do |t|
33
+ puts "tok: #{t}"
34
+ end
35
+ end
36
+
37
+ def error(line, msg)
38
+ report(line, "", msg)
39
+ end
40
+
41
+ def report(line, where, msg)
42
+ puts "[line #{line}] Error #{where}: #{msg}"
43
+ end
44
+ end
7
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Crosby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-27 00:00:00.000000000 Z
11
+ date: 2022-08-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Lox interpreter as seen in Crafting Interpreters
14
14
  email:
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - ".rubocop.yml"
20
+ - CODE_OF_CONDUCT.md
20
21
  - Gemfile
21
22
  - Guardfile
22
23
  - LICENSE
@@ -25,6 +26,7 @@ files:
25
26
  - examples/breakfast.lox
26
27
  - exe/rblox
27
28
  - lib/rblox.rb
29
+ - lib/rblox/scanner.rb
28
30
  - lib/rblox/version.rb
29
31
  homepage: https://daveops.net
30
32
  licenses: