rblox 0.0.1

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: 96c62a4c1631167e63a3271c808ea4444b9f5c6d2a55a4b62040b415aba91d84
4
+ data.tar.gz: 8feb458e599976a076aebac98d50aa0ebeaff21997191326e7325430089a48e4
5
+ SHA512:
6
+ metadata.gz: f8f38b6eccc70997000a4a3976e1f821f6f602a10a5e068dfaabc90d1f454babd862d7c6666b32ed1a2374aa2f387359b368c1ea52f0bacccdba021a7eb367cb
7
+ data.tar.gz: 1f658843f8588862150bb0226d389562a35e943992e4ffea256ab1fea75d9fd005e2bb160642867050ef97f702e5fc5d7d58f2c2c1984ed5a96132b32abaf9c4
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ inherit_gem:
2
+ dc-rubocop: default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ group :development, optional: true do
8
+ gem "dc-devtools"
9
+ end
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ directories(%w[. lib test].select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") })
4
+
5
+ guard :rake, task: "default" do
6
+ watch("Gemfile")
7
+ watch("Rakefile")
8
+ watch("Guardfile")
9
+ watch(%r{^test/helper\.rb$})
10
+ watch(%r{^test/test_(.*)\.rb$})
11
+ watch(%r{^lib/rblox/(.*)\.rb$})
12
+ watch(%r{^lib/(.*)\.rb$})
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 David Crosby
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # rblox
2
+
3
+ rblox is a Lox interpreter written in Ruby.
4
+
5
+ You can find more out about Lox in the (fantastic!) [Crafting Interpreters book](https://craftinginterpreters.com)
6
+
7
+ ## Usage
8
+
9
+ TODO!
10
+
11
+ ## License
12
+
13
+ See LICENSE file
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/test_*.rb"]
11
+ end
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: %i[test rubocop]
@@ -0,0 +1,9 @@
1
+ class Breakfast {
2
+ cook() {
3
+ print "Eggs a-fryin'!";
4
+ }
5
+
6
+ serve(who) {
7
+ print "Enjoy your breakfast, " + who + ".";
8
+ }
9
+ }
data/exe/rblox ADDED
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "strscan"
4
+
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
103
+
104
+ abort("usage blah") if ARGV.size > 1
105
+ l = RbLox.new
106
+ if ARGV.size == 1
107
+ l.read_source_file(ARGV[0])
108
+ else
109
+ l.run_prompt
110
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rblox
4
+ VERSION = "0.0.1"
5
+ end
data/lib/rblox.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rblox/version"
4
+
5
+ module Rblox
6
+ # TODO
7
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rblox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Crosby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Lox interpreter as seen in Crafting Interpreters
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rubocop.yml"
20
+ - Gemfile
21
+ - Guardfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - examples/breakfast.lox
26
+ - exe/rblox
27
+ - lib/rblox.rb
28
+ - lib/rblox/version.rb
29
+ homepage: https://daveops.net
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://daveops.net
34
+ rubygems_mfa_required: 'true'
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.3.7
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Lox interpreter
54
+ test_files: []