alphalang 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f7356e9bd0fa5e53d01733fa3266b7294f969037c665941daaac523e50e3b9f
4
- data.tar.gz: '060006929bc0e33a766c001622f0079f9f3c3c51163138c867cc5bb67f31755d'
3
+ metadata.gz: bb11af78d1a68ee1f2a5aaba719040996f435be0183e690bea2532a7fce80201
4
+ data.tar.gz: 0f5a33e46b3fa611bab83528e90c481e5ba4ec1a480808a1bc7551fc25b90e4e
5
5
  SHA512:
6
- metadata.gz: c1e5673759a21f75cc4d7fa14147b5a60fab5319e07ebce2349cdf35121e6a79bb1e78714c641c9061806c50ff1d9d60c2ca0c615c09f13c32f08a38eb812fb7
7
- data.tar.gz: 53a824dd4c27bd9cf3ffb35d9db46a7b98a67b2ffd095d365cd1cc569faa835fa2d41e4e0752f3d4ba2b0c31825fd8e1000e0aca4a3e62686187ef66916e35e8
6
+ metadata.gz: 7411738e5cd015019a137324144b4a735f3e195263cc23ddf1301ecc2a3803be9ede4d28cc4f84403cb6547bc8dc6c75cf9ee59e307ef22d692632daa1439b58
7
+ data.tar.gz: b9ebbb5a83f4a2fb5f547754e7458e0de1249ff0e15fa79a293c5d56727d68237b6ff2de9d9b9be08866440dd5195f1eeff777b11eda23606a05c6b62941c54f
data/bin/alphalang CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'optparse'
3
3
  require File.expand_path('../lib/alpha', __dir__)
4
+ ALPHA_VER = '0.1.2'.freeze
4
5
 
5
6
  basic_error_msg = "Usage: alphalang [options] file.alpha\nUsage: alphalang -h for the help menu."
6
7
 
@@ -31,8 +32,8 @@ language = options[:lang] || 'en'
31
32
  createlocale = options[:createlocale]
32
33
 
33
34
  if createlocale
34
- require_relative'../lib/lang_creator'
35
- create()
35
+ require_relative '../lib/lang_creator'
36
+ create_locale_file
36
37
  return
37
38
  end
38
39
 
data/lib/alpha.rb CHANGED
@@ -145,8 +145,8 @@ class LangParser
145
145
  end
146
146
 
147
147
  rule :arg_list do
148
- match(:expr, ',', :arg_list) { |a, _, b| ArgListNode.new(a, b) }
149
- match(:expr) { |a| ArgListNode.new(a, NilClass) }
148
+ match(:expr, ',', :arg_list) { |a, _, b| ArrayNode.new(a, b) }
149
+ match(:expr) { |a| ArrayNode.new(a, NilClass) }
150
150
  end
151
151
 
152
152
  rule :prio_stmt do
data/lib/lang_creator.rb CHANGED
@@ -4,86 +4,82 @@
4
4
  def read_translation(line)
5
5
  puts "Enter the translation for: '#{line}' <RET> to accept '#{line}'"
6
6
  translation = gets.chomp
7
- translation = line if translation.empty?
8
- translation
7
+ translation.empty? ? line : translation
9
8
  end
10
9
 
11
10
  def read_translation_true_false(line)
12
- word1 = /false/.match(line)
13
- word2 = /true/.match(line)
14
- puts "Enter the translation for: '#{word1}' <RET> to accept '#{word1}'"
11
+ words = line.scan(/true|false/)
15
12
  translation = '('
16
- input = gets.chomp
17
- if input.empty?
18
- translation += word1.to_s
19
- else
20
- translation += input
21
- end
22
- translation += '|'
23
- puts "Enter the translation for: '#{word2}' <RET> to accept '#{word2}'"
24
- input2 = gets.chomp
25
- if input2.empty?
26
- translation += word2.to_s
27
- else
28
- translation += input2
13
+ words.each do |word|
14
+ puts "Enter the translation for: '#{word}' <RET> to accept '#{word}'"
15
+ input = gets.chomp
16
+ translation += input.empty? ? word : input
17
+ translation += '|'
29
18
  end
19
+ translation.chop!
30
20
  translation += ')'
31
21
  translation
32
22
  end
33
23
 
34
24
  def read_translation_not_and_or(line)
35
- word = /\w+/.match(line)
25
+ word = line.match(/\w+/)[0]
26
+ postfix = line.match(/\|.+/)[0]
36
27
  puts "Enter the translation for: '#{word}' <RET> to accept '#{word}'"
37
- translation = '('
38
28
  input = gets.chomp
39
- if input.empty?
40
- translation += word.to_s
41
- else
42
- translation += input
43
- end
44
- translation += /\|.+/.match(line).to_s
45
- translation
29
+ return "(#{input.empty? ? word : input}#{postfix}"
46
30
  end
47
- def create()
48
- # Prompt the user to choose a name for the translation file
49
- puts "Choose a name for your translation file:"
50
- name = gets.chomp
51
-
52
- # Create an empty file to store translations
53
- File.open(name, 'w') {}
54
31
 
55
- # Read each line from the input file
56
- locale_path = File.join(__dir__, "locales", 'locale_template')
57
-
58
- # Prompt the user to translate the line
32
+ def prompt_user(locale_template, file)
59
33
  counter = 0
60
- locale_thingies = File.readlines(locale_path)
61
- locale_thingies.each do |line|
62
-
34
+ locale_template.each do |line|
63
35
  counter += 1
64
36
  break if counter == 14
65
- if counter < 9
37
+
38
+ if counter < 9
66
39
  translation = read_translation(line.chomp)
67
-
68
- # Save translation after the original line
69
- File.open(name, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
40
+ File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
70
41
  end
71
- if counter == 9
42
+ if counter == 9
72
43
  translation = read_translation_true_false(line.chomp)
73
- File.open(name, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
44
+ File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
74
45
  end
75
46
  if counter == 10
76
- File.open(name, 'a') { |f| f.puts "#{line.chomp} (==|<=|>=)" }
47
+ File.open(file, 'a') { |f| f.puts "#{line.chomp} (==|<=|>=)" }
77
48
  end
78
- if counter > 10
49
+ if counter > 10
79
50
  translation = read_translation_not_and_or(line.chomp)
80
- File.open(name, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
51
+ File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
81
52
  end
53
+ end
54
+ end
82
55
 
56
+ def save_file(file)
57
+ ruby_version = "#{RUBY_VERSION}"
58
+ ruby_version[-1] = '0'
59
+ install_path = "/home/#{ENV['USER']}/.local/share/gem/ruby/#{ruby_version}/gems/alphalang-#{ALPHA_VER}"
60
+
61
+ if File.exist?(install_path)
62
+ File.rename(file, "#{install_path}/lib/locales/#{file}")
63
+ puts "Locale/syntax saved to #{install_path}/lib/locales/#{file}"
64
+ else
65
+ raise ArgumentError, "Didn't find your #{install_path}.\nTried #{ruby_version} as <VERSION>"
83
66
  end
67
+ end
68
+
69
+ def create_locale_file()
70
+ puts 'Choose a filename for your locale/syntax:'
71
+ filename = gets.chomp
72
+ File.open(filename, 'w') {}
73
+
74
+ # import locale template
75
+ locale_path = File.join(__dir__, 'locales', 'locale_template')
76
+ token_template = File.readlines(locale_path)
77
+
78
+ # prompt user for translations
79
+ prompt_user(token_template, filename)
84
80
 
85
81
  # Append additional translations
86
- File.open(name, 'a') do |f|
82
+ File.open(filename, 'a') do |f|
87
83
  f.puts '\s+ \s+'
88
84
  f.puts '\d+ \d+'
89
85
  f.puts '\w+ \w+'
@@ -91,34 +87,19 @@ def create()
91
87
  end
92
88
 
93
89
  # Clear the screen
94
- system("clear")
90
+ system('clear')
95
91
 
96
92
  # Display the contents of the translation file
97
- puts File.read(name)
93
+ puts File.read(filename)
98
94
 
99
95
  # Ask user if the translation is correct
100
- puts "Is this correct? [Y/n]"
96
+ puts 'Is this correct? [Y/n]'
101
97
  answer = gets.chomp
102
98
 
103
99
  if answer.downcase == /y|Y/ or answer.empty?
104
- puts "Translation saved to #{name}"
105
-
106
- # Move the translation file to the desired directory
107
- ruby_version = "" + RUBY_VERSION.to_s
108
- ruby_version[-1] = '0'
109
-
110
- # Find the users installation directory and place the locale file there
111
- if File.exist?("/home/#{ENV['USER']}/.local/share/gem/ruby/#{ruby_version}/gems/alphalang-0.1.0/lib/locales/")
112
- File.rename(name, "/home/#{ENV['USER']}/.local/share/gem/ruby/#{ruby_version}/gems/alphalang-0.1.0/lib/locales/#{name}")
113
- # elsif File.exist?("~/var/lib/gems/#{ruby_version}/gems/alphalang-0.1.0/lib/locales/")
114
- # File.rename(name, "~/var/lib/gems/#{ruby_version}/gems/alphalang-0.1.0/lib/locales/#{name}")
115
- else
116
- raise ArgumentError, "Didn't find your .local/share/gem/ruby/<VERSION>/gems/alphalang-0.1.0
117
- Tried Ruby <VERSION> = #{ruby_version} "
118
- end
119
-
100
+ save_file(filename)
120
101
  else
121
102
  puts 'Translation removed'
122
- File.delete(name)
103
+ File.delete(filename)
123
104
  end
124
- end
105
+ end
@@ -38,6 +38,10 @@ class Node
38
38
  @value = value
39
39
  end
40
40
 
41
+ def to_s
42
+ @value
43
+ end
44
+
41
45
  def evaluate
42
46
  @value.evaluate
43
47
  end
@@ -143,7 +147,6 @@ end
143
147
  ####################################################
144
148
 
145
149
  class CompStmtNode < Node
146
-
147
150
  def initialize(stmt_compstmt)
148
151
  super
149
152
  @comp_statements = stmt_compstmt
@@ -155,10 +158,9 @@ class CompStmtNode < Node
155
158
  end
156
159
  end
157
160
 
158
-
159
161
  ####################################################
160
162
 
161
- class ArgListNode < Node
163
+ class ArrayNode < Node
162
164
  attr_accessor :lhs, :rhs
163
165
 
164
166
  def initialize(lhs, rhs)
@@ -76,7 +76,7 @@ class FuncCallNode < Node
76
76
 
77
77
  $scope_lvl += 1
78
78
 
79
- if function_param.is_a?(ArgListNode)
79
+ if function_param.is_a?(ArrayNode)
80
80
  function_param.each do |val, index|
81
81
  function_param[index] = VariableDecNode.new(function_param[index].name, @args[index])
82
82
  function_param[index].evaluate
@@ -0,0 +1,12 @@
1
+ x = 1
2
+
3
+ während x < 10
4
+ drucken x
5
+
6
+ wenn x > 5 und < 10
7
+ pause 1
8
+ ende
9
+ x = x + 1
10
+ ende
11
+
12
+ drucken x
@@ -0,0 +1,12 @@
1
+ x = 1
2
+
3
+ medan x < 10
4
+ skriv x
5
+
6
+ om x > 5 och < 10
7
+ vänta 1
8
+ slut
9
+ x = x + 1
10
+ slut
11
+
12
+ skriv x
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphalang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mattias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-03 00:00:00.000000000 Z
11
+ date: 2024-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: optparse
14
+ name: logger
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: '1.5'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.3.0
22
+ version: 1.5.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.3'
29
+ version: '1.5'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.3.0
32
+ version: 1.5.0
33
33
  - !ruby/object:Gem::Dependency
34
- name: logger
34
+ name: optparse
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '1.5'
39
+ version: '0.3'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 1.5.0
42
+ version: 0.3.0
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '1.5'
49
+ version: '0.3'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 1.5.0
52
+ version: 0.3.0
53
53
  description: Abstract Syntax Tree building language with a recursive descent parser
54
54
  email:
55
55
  - mattiasreuterskiold@gmail.com
@@ -65,11 +65,12 @@ files:
65
65
  - lib/locales/en
66
66
  - lib/locales/locale_template
67
67
  - lib/locales/sv
68
- - lib/nodes/#stmtnodes.rb#
69
68
  - lib/nodes/basenodes.rb
70
69
  - lib/nodes/stmtnodes.rb
71
70
  - lib/rdparse.rb
72
71
  - lib/rdparse_quiet.rb
72
+ - lib/tester/demo_de.alpha
73
+ - lib/tester/demo_sv.alpha
73
74
  - lib/tester/fibonacci.alpha
74
75
  - lib/tester/test_unit.rb
75
76
  homepage: https://portfolio.reuterskiold.dev
@@ -1,196 +0,0 @@
1
- require_relative 'basenodes'
2
-
3
- ####################################################
4
- class VariableCallNode < Node
5
- attr_accessor :name
6
-
7
- def initialize(name)
8
- @name = name
9
- end
10
-
11
- def lookup_var(name)
12
- temp_scope_lvl = $scope_lvl
13
- while temp_scope_lvl >= 0
14
- if not $scopes[temp_scope_lvl].has_key?(name)
15
- temp_scope_lvl -= 1
16
- else
17
- return $scopes[temp_scope_lvl][name]
18
- end
19
- end
20
- end
21
-
22
- def evaluate
23
- return @value = lookup_var(@name)
24
- end
25
- end
26
-
27
- class VariableDecNode < Node
28
- attr_accessor :name
29
-
30
- def initialize(name, value)
31
- super(value)
32
- @name = name
33
- end
34
-
35
- def evaluate
36
- $scopes[$scope_lvl][@name.name] = @value.evaluate
37
- self
38
- # return nil
39
- end
40
- end
41
-
42
- ####################################################
43
-
44
- class FunctionDecNode < Node
45
- def initialize(node, value)
46
- super(value)
47
- @name = node
48
- @args = node.args
49
- end
50
-
51
- def evaluate
52
- $scopes[0][@name.name] = [@value, @args]
53
- return nil
54
- end
55
- end
56
-
57
- class FuncCallNode < Node
58
- attr_accessor :name, :args
59
-
60
- def initialize(name, args)
61
- @name = name
62
- @args = args
63
- end
64
-
65
- def lookup_var(name)
66
- return $scopes[0][name]
67
- end
68
-
69
- def evaluate
70
- $scopes.push({})
71
- func = lookup_var(@name)
72
- function_body = func[0]
73
- function_param = func[1]
74
-
75
- return nil if func.is_a?(NilClass)
76
-
77
- $scope_lvl += 1
78
-
79
- if function_param.is_a?(ArgListNode)
80
- function_param.each do |val, index|
81
- function_param[index] = VariableDecNode.new(function_param[index].name, @args[index])
82
- function_param[index].evaluate
83
- end
84
- end
85
-
86
- func_result = function_body.evaluate
87
- old_scope_lvl = $scope_lvl
88
-
89
- if func_result.is_a?(VariableDecNode)
90
- $scope_lvl = 0
91
- func_result.evaluate
92
- $scope_lvl = old_scope_lvl
93
- end
94
-
95
- $scope_lvl -= 1
96
- $scopes.pop
97
- return func_result
98
- end
99
- end
100
-
101
- ####################################################
102
-
103
- class IfNode < Node
104
- attr_accessor :argument
105
-
106
- def initialize(argument, node)
107
- @argument, @node = argument, node
108
- end
109
-
110
- def evaluate
111
- if @argument.evaluate
112
- @value = @node.evaluate
113
- else
114
- @value = nil
115
- end
116
- @value
117
- end
118
- end
119
-
120
- class ElseifNode < Node
121
- def initialize(argument, node)
122
- @argument, @node = argument, node
123
- end
124
- def evaluate
125
- if @argument.evaluate
126
- @value = @node.evaluate
127
- end
128
- end
129
- end
130
-
131
- class ElseNode < Node
132
- def initialize(node)
133
- @node = node
134
- end
135
-
136
- def evaluate
137
- @value = @node.evaluate
138
- end
139
- end
140
-
141
- class IfCompStmtNode < Node
142
- def initialize(*nodes)
143
- @nodes = nodes
144
- end
145
- def evaluate
146
- if @nodes[0].argument.evaluate
147
- return @nodes[0].evaluate
148
- else
149
- return nil if @nodes[1] == nil # hmm??
150
- return @nodes[1].evaluate
151
- end
152
- end
153
- end
154
-
155
- ####################################################
156
-
157
- class WhileLoopNode < Node
158
- attr_accessor :condition
159
-
160
- def initialize(condition, statement)
161
- @condition = condition
162
- super(statement)
163
- end
164
-
165
- def evaluate
166
- while @condition.evaluate
167
- @value.evaluate
168
- end
169
- self.class
170
- end
171
- end
172
-
173
- ####################################################
174
-
175
- class PrintNode
176
- attr_accessor :value
177
-
178
- def initialize(value)
179
- @value = value
180
- end
181
-
182
- def evaluate
183
- puts @value.evaluate
184
- self.class # detta kanske är trevligare än nil, åter igen den diskussionen.
185
- end
186
- end
187
-
188
- class PauseNode < Node
189
- def initialize(value)
190
- super(value)
191
- end
192
-
193
- def evaluate
194
- sleep @value.evaluate
195
- end
196
- end