turlang 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94fe16176f43af1785a946ef95038aab03b9076e
4
+ data.tar.gz: 7b2217a4262f742c824c3666c04265fc7bd269b4
5
+ SHA512:
6
+ metadata.gz: eec72677d7ee2b37e9b07b5c93ce44498d213fae6134a9135a9d169cb3e6efdba8d3facd2dbe5ca5b580b1db0efe766d9f7eb52f0d260259b2dc39a2186b5ee4
7
+ data.tar.gz: b951263bcfeaeb33b0660c6e38f82822930c3cdb5c34348179f37ead87d114e561570aa3acb86e7fd6e6c3048f909795986c967aa4ad315fa78fbdf8da8d6b68
@@ -0,0 +1,2 @@
1
+ *.sw?
2
+ *.gem
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'turlang'
4
+
5
+ contents = File.read(ARGV[0])
6
+ inter = Turlang::Interpeter.interpet(contents)
7
+ inter.run
@@ -0,0 +1,7 @@
1
+ require 'turlang/stack'
2
+ require 'turlang/state_machine'
3
+ require 'turlang/io'
4
+ require 'turlang/interpeter'
5
+
6
+ module Turlang
7
+ end
@@ -0,0 +1,93 @@
1
+ module Turlang
2
+ class Interpeter
3
+ STACK_OPERATIONS = [:>>, :<<]
4
+ IO_OPERATIONS = [:PRINT, :EXIT]
5
+
6
+ def self.interpet program
7
+ inter = Turlang::Interpeter.new
8
+ program.split("\n").each do |line|
9
+ next if line == ''
10
+ if line =~ /\A\[(.*)\]\Z/
11
+ line.strip!
12
+ inter.set_stack(line[1..-2].split(', '))
13
+ next
14
+ end
15
+
16
+ if line =~ /\A\:(.*)\:\Z/
17
+ line.strip!
18
+ inter.new_label(line)
19
+ next
20
+ end
21
+
22
+ if line =~ /\A\ \ [\*_A-Za-z\d].*\:\Z/
23
+ line.strip!
24
+ inter.new_condition(line[0..-2])
25
+ next
26
+ end
27
+
28
+ line.strip!
29
+ next if line == ''
30
+ inter.add_command line
31
+ break if line == "END"
32
+ end
33
+ inter
34
+ end
35
+
36
+ def initialize
37
+ @stack = Stack.new
38
+ @state_machine = StateMachine.new
39
+ @io = IO.new
40
+ @current_label = nil
41
+ @current_conditional = nil
42
+ end
43
+
44
+ def set_stack values
45
+ @stack.values = values.map{|v| v.to_sym}
46
+ end
47
+
48
+ def new_label label
49
+ @current_label = label
50
+ @state_machine.add_label @current_label
51
+ end
52
+
53
+ def new_condition conditional
54
+ raise "Syntax" if @current_label.nil?
55
+ @current_conditional = conditional.to_sym
56
+ @state_machine.add_condition @current_label, @current_conditional
57
+ end
58
+
59
+ def add_command command
60
+ raise "Syntax" if @current_label.nil?
61
+ raise "Syntax" if @current_conditional.nil?
62
+ command = command.split(' ')
63
+ real_command = [command[0].to_sym]
64
+ real_command << command[1..-1] if command.count > 1
65
+ @state_machine.add_command @current_label, @current_conditional, real_command
66
+ end
67
+
68
+ def is_label? label
69
+ label = label.to_s
70
+ label.start_with?(':') && label.end_with?(':')
71
+ end
72
+
73
+ def run
74
+ loop do
75
+ commands = @state_machine.commands(@stack.read)
76
+ commands.each do |command|
77
+ args = command[1]
78
+ command = command[0]
79
+ case
80
+ when is_label?(command)
81
+ @state_machine.next_label = command
82
+ when STACK_OPERATIONS.include?(command)
83
+ @stack.send(command)
84
+ when IO_OPERATIONS.include?(command)
85
+ @io.send(command.downcase, *args)
86
+ else
87
+ @stack.write(command)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,11 @@
1
+ module Turlang
2
+ class IO
3
+ def print message
4
+ puts message.match(/\"(.*)\"/)[1]
5
+ end
6
+
7
+ def exit code
8
+ Kernel.exit code.to_i
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module Turlang
2
+ class Stack
3
+ attr_writer :values
4
+ BLANK = :"_"
5
+
6
+ def initialize
7
+ @pointer = 0
8
+ @values = []
9
+ end
10
+
11
+ def read
12
+ return BLANK if @pointer < 0 || @pointer >= @values.count
13
+ @values[@pointer].to_sym
14
+ end
15
+
16
+ def write(symbol)
17
+ @values[@pointer] = symbol
18
+ end
19
+
20
+ def <<
21
+ @pointer -= 1
22
+ end
23
+
24
+ def >>
25
+ @pointer += 1
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ module Turlang
2
+ class StateMachine
3
+ attr_writer :next_label
4
+
5
+ def initialize
6
+ @labels = {}
7
+ @next_label = nil
8
+ end
9
+
10
+ def add_label label
11
+ @labels[label] = []
12
+ @next_label = label if @next_label.nil?
13
+ end
14
+
15
+ def add_condition label, condition
16
+ @labels[label] << {
17
+ conditional: condition,
18
+ commands: []
19
+ }
20
+ end
21
+
22
+ def add_command label, condition, command
23
+ get_condition(label, condition)[:commands] << command
24
+ end
25
+
26
+ def match_condition label, condition
27
+ @labels[label.to_s].detect do |conditional|
28
+ conditional[:conditional] == :* || conditional[:conditional] == condition
29
+ end.tap do |conditional|
30
+ raise "Condition #{condition} not found for label #{label}" if conditional.nil?
31
+ end
32
+ end
33
+
34
+ def get_condition label, condition
35
+ @labels[label].detect do |conditional|
36
+ conditional[:conditional] == condition
37
+ end.tap do |conditional|
38
+ raise "Condition #{condition} not found for label #{label}." if conditional.nil?
39
+ end
40
+ end
41
+
42
+ def commands symbol
43
+ match_condition(@next_label, symbol)[:commands]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Turlang
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'turlang/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "turlang"
8
+ spec.version = Turlang::VERSION
9
+ spec.authors = ["Dean"]
10
+ spec.email = ["deangalvin3@gmail.com"]
11
+
12
+ spec.summary = %q{Turlang is an interpeted language based off a turing machine.}
13
+ spec.homepage = "https://www.github.com/FreekingDean/turlang"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = "turlang"
19
+ spec.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turlang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - deangalvin3@gmail.com
16
+ executables:
17
+ - turlang
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - bin/turlang
23
+ - lib/turlang.rb
24
+ - lib/turlang/interpeter.rb
25
+ - lib/turlang/io.rb
26
+ - lib/turlang/stack.rb
27
+ - lib/turlang/state_machine.rb
28
+ - lib/turlang/version.rb
29
+ - turlang.gemspec
30
+ homepage: https://www.github.com/FreekingDean/turlang
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.5.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Turlang is an interpeted language based off a turing machine.
54
+ test_files: []