alphalang 0.1.3 → 0.1.4

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: 1c31a662b58f22db4c35e2e784739c0c0def7b9ccef89ad3f06292581dfdf28d
4
- data.tar.gz: bc02c5bfcdac5985367a5c722b88b01a4d95d35c973dcb6e61d1a05c66de23e2
3
+ metadata.gz: 9e3d320e5322d98b56f1cd6d5f9e51211de1c41ba6abe20800177bf83ae08133
4
+ data.tar.gz: fe85e5979279fbff45aa74ec9a09435bff67fd49dca35c5fbd332c1c8628c9e7
5
5
  SHA512:
6
- metadata.gz: 7c104ae659e875f33f9b3d9ac8f358613feab8dfe56b2b166b580626994a9089c8e6ecdb307cee7b8d64e80ba0e1b63f7be7380360cf427bc18e0eb34469d966
7
- data.tar.gz: 30f09cb6a7a6c914b0be2d84a50cd88495e482fdec29c06c49fc8d2d9bd306795a86cf26d08065b6d0da8b0b0a8458fa06cc664694c72fe546abd6f1e3adbb41
6
+ metadata.gz: 0ee5133266c1e9ebb85c13740d730efb69db1b4deb54853e53103e38275262edc789f425e1450ef5582bb4077590d33c21fbe24662920b473a6d05ba9de21676
7
+ data.tar.gz: 8e756c3aa10fa954cc06fbb299d5eff3ce1335d6c3130f76485280f09c08165fc1c62bd9b5160e83ea68dec7ead8368d7dc584b5915a62a14ca8ed28d2e954d8
data/bin/alphalang CHANGED
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'optparse'
3
3
  require File.expand_path('../lib/alpha', __dir__)
4
- ALPHA_VER = '0.1.3'.freeze
4
+ ALPHA_VER = '0.1.4'.freeze
5
5
  ABORT_ANSWERS = [' ', '', 'none', 'abort'].freeze # something here? Some cases empty means yes tho..
6
+ LOCALES_PATH = File.join(__dir__, '../lib/locales')
7
+ PROTECTED_LOCALES = ['.', '..', 'locale_template', 'default', 'default.old', 'en', 'sv', 'de'].freeze
6
8
 
7
9
  basic_error_msg = "Usage: alphalang [options] file.alpha\nUsage: alphalang -h for the help menu."
8
10
 
@@ -18,8 +20,12 @@ OptionParser.new do |opts|
18
20
  options[:verify] = true
19
21
  end
20
22
 
21
- opts.on('--lang=LOCALE', 'Set the locale language for all program keywords. Available: en, sv, de') do |lang|
22
- options[:lang] = lang
23
+ opts.on('--locale=LOCALE', 'Set the syntax locale for current program keywords.') do |locale|
24
+ options[:locale] = locale
25
+ end
26
+
27
+ opts.on('--setlocale=LOCALE', 'Set the default syntax locale for all program keywords.') do |new_locale|
28
+ options[:defaultlocale] = new_locale
23
29
  end
24
30
 
25
31
  opts.on('--createlocale', 'Creates a new locale file for all program keywords available.') do
@@ -39,9 +45,10 @@ OptionParser.new do |opts|
39
45
  end
40
46
  end.parse!
41
47
 
42
- verbose = options[:verbose]
43
- verify = options[:verify]
44
- language = options[:lang] || 'en'
48
+ verbose_flag = options[:verbose]
49
+ verify_flag = options[:verify]
50
+ language_flag = options[:locale]
51
+ set_locale_flag = options[:defaultlocale]
45
52
  create_locale_flag = options[:createlocale]
46
53
  delete_locale_flag = options[:deletelocale]
47
54
  list_locales_flag = options[:listlocales]
@@ -53,6 +60,20 @@ if create_locale_flag
53
60
  return
54
61
  end
55
62
 
63
+ if language_flag.is_a?(NilClass)
64
+ language_flag = 'default'
65
+ end
66
+
67
+ if !set_locale_flag.is_a?(NilClass)
68
+ unless ARGV.empty?
69
+ puts 'Flag for verification found. Ignoring input file.'
70
+ sleep 0.5
71
+ end
72
+ require_relative '../lib/locale_defaulter'
73
+ set_default_locale(set_locale_flag)
74
+ return
75
+ end
76
+
56
77
  if delete_locale_flag
57
78
  require_relative '../lib/locale_deleter'
58
79
  delete_locale_file
@@ -72,23 +93,23 @@ if list_locale_flag
72
93
  end
73
94
 
74
95
  raise OptionParser::MissingArgument, "No file provided.
75
- Usage: alphalang my_file.alpha | alphalang -h" if ARGV.empty? unless verify
96
+ Usage: alphalang my_file.alpha | alphalang -h" if ARGV.empty? unless verify_flag
76
97
  raise OptionParser::InvalidArgument, 'Too many files provided.
77
98
  Did you mean to use an --[option]? | alphalang -h' if ARGV.length > 1
78
- raise OptionParser::InvalidArgument, 'File must end with .alpha' unless verify or ARGV[0].end_with?('.alpha')
99
+ raise OptionParser::InvalidArgument, 'File must end with .alpha' unless verify_flag or ARGV[0].end_with?('.alpha')
79
100
 
80
- if verbose
101
+ if verbose_flag
81
102
  require_relative '../lib/rdparse'
82
103
  else
83
104
  require_relative '../lib/rdparse_quiet'
84
105
  end
85
106
 
86
- if verify
107
+ if verify_flag
87
108
  unless ARGV.empty?
88
109
  puts 'Flag for verification found. Ignoring input file.'
89
110
  sleep 1
90
111
  end
91
112
  require_relative '../lib/tester/test_unit'
92
113
  else
93
- LangParser.new(language).parse_file(ARGV[0])
114
+ LangParser.new(language_flag).parse_file(ARGV[0])
94
115
  end
data/lib/alpha.rb CHANGED
@@ -1,8 +1,7 @@
1
- require_relative './nodes/stmtnodes'
1
+ require_relative './nodes/scopemanager'
2
2
 
3
3
  class LangParser
4
- def initialize(locale = 'en')
5
- ScopeManager.new
4
+ def initialize(locale = 'default')
6
5
  @langParser = Parser.new('lang parser', locale) do
7
6
  token(/while/) { |_| :WHILE }
8
7
  token(/print/) { |_| :PRINT }
data/lib/lang_creator.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require_relative 'locale_lister'
2
3
 
3
4
  # Function to read user input for translation
4
5
  def read_translation(line)
@@ -29,77 +30,66 @@ def read_translation_not_and_or(line)
29
30
  return "(#{input.empty? ? word : input}#{postfix}"
30
31
  end
31
32
 
32
- def prompt_user(locale_template, file)
33
+ def prompt_user(file)
34
+ locale_template = File.readlines("#{LOCALES_PATH}/locale_template")
35
+
33
36
  counter = 0
34
- locale_template.each do |line|
35
- counter += 1
36
- break if counter == 14
37
+ File.open(file, 'a') do |f|
38
+ locale_template.each do |line|
39
+ counter += 1
40
+ break if counter == 14
37
41
 
38
- if counter < 9
39
- translation = read_translation(line.chomp)
40
- File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
41
- end
42
- if counter == 9
43
- translation = read_translation_true_false(line.chomp)
44
- File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
45
- end
46
- if counter == 10
47
- File.open(file, 'a') { |f| f.puts "#{line.chomp} (==|<=|>=)" }
42
+ if counter < 9
43
+ translation = read_translation(line.chomp)
44
+ f.puts "#{line.chomp} #{translation}"
45
+ end
46
+ if counter == 9
47
+ translation = read_translation_true_false(line.chomp)
48
+ f.puts "#{line.chomp} #{translation}"
49
+ end
50
+ if counter == 10
51
+ f.puts "#{line.chomp} (==|<=|>=)"
52
+ end
53
+ if counter > 10
54
+ translation = read_translation_not_and_or(line.chomp)
55
+ f.puts "#{line.chomp} #{translation}"
56
+ end
48
57
  end
49
- if counter > 10
50
- translation = read_translation_not_and_or(line.chomp)
51
- File.open(file, 'a') { |f| f.puts "#{line.chomp} #{translation}" }
52
- end
53
- end
54
- end
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>"
58
+ f.puts '\s+ \s+'
59
+ f.puts '\d+ \d+'
60
+ f.puts '\w+ \w+'
61
+ f.puts '. .'
66
62
  end
67
63
  end
68
64
 
69
65
  def create_locale_file()
70
66
  puts 'Choose a filename for your locale/syntax:'
71
- filename = gets.chomp
72
- File.open(filename, 'w') {}
67
+ new_locale_name = gets.chomp
68
+ new_locale_file_path = "#{LOCALES_PATH}/#{new_locale_name}"
73
69
 
74
- # import locale template
75
- locale_path = File.join(__dir__, 'locales', 'locale_template')
76
- token_template = File.readlines(locale_path)
70
+ if File.exist?(new_locale_file_path)
71
+ puts "#{new_locale_name} already exists."
72
+ return
73
+ end
77
74
 
78
75
  # prompt user for translations
79
- prompt_user(token_template, filename)
80
-
81
- # Append additional translations
82
- File.open(filename, 'a') do |f|
83
- f.puts '\s+ \s+'
84
- f.puts '\d+ \d+'
85
- f.puts '\w+ \w+'
86
- f.puts '. .'
87
- end
76
+ prompt_user(new_locale_file_path)
88
77
 
89
78
  # Clear the screen
90
79
  system('clear')
91
80
 
92
81
  # Display the contents of the translation file
93
- puts File.read(filename)
82
+ locale_as_array = clean_locale_file_to_array(new_locale_name)
83
+ print_clean_locale_array(new_locale_name, locale_as_array)
94
84
 
95
85
  # Ask user if the translation is correct
96
86
  puts 'Is this correct? [Y/n]'
97
87
  answer = gets.chomp
98
88
 
99
89
  if answer.downcase == /y|Y/ or answer.empty?
100
- save_file(filename)
90
+ puts "Locale is saved as #{new_locale_name} in #{LOCALES_PATH}"
101
91
  else
102
92
  puts 'Translation removed'
103
- File.delete(filename)
93
+ File.delete(new_locale_file)
104
94
  end
105
95
  end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'locale_lister'
4
+
5
+ def set_default_locale(new_locale)
6
+ available_locales = get_locale_files
7
+
8
+ available_locales.each do |locale|
9
+ if locale == new_locale
10
+ File.rename("#{LOCALES_PATH}/default", "#{LOCALES_PATH}/default.old")
11
+ File.copy_stream("#{LOCALES_PATH}/#{new_locale}", "#{LOCALES_PATH}/default")
12
+ puts "[alphalang] Default syntax locale is now set to #{locale}."
13
+ return
14
+ end
15
+ end
16
+ end
File without changes
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- def prompt_user_for_deletion(locales, install_path, protected_paths)
3
+ def prompt_user_for_deletion(locales)
4
4
  puts 'Which locale would you like to delete?: RET or "none" to abort'
5
5
 
6
6
  locales.each do |locale|
@@ -11,25 +11,22 @@ def prompt_user_for_deletion(locales, install_path, protected_paths)
11
11
 
12
12
  return if ABORT_ANSWERS.include?(locale_file)
13
13
 
14
- if protected_paths.include?(locale_file)
14
+ if PROTECTED_LOCALES.include?(locale_file)
15
15
  puts 'You may not delete a default locale.'
16
16
  return
17
17
  else
18
- File.delete("#{install_path}/#{locale_file}")
19
- puts "Successfully deleted #{install_path}/#{locale_file}"
18
+ File.delete("#{LOCALES_PATH}/#{locale_file}")
19
+ puts "Successfully deleted #{LOCALES_PATH}/#{locale_file}"
20
20
  end
21
21
  end
22
22
 
23
23
  def delete_locale_file()
24
- locale_path = File.join(__dir__, 'locales')
25
- protected_locales = ['.', '..', 'locale_template', 'en', 'sv', 'de']
26
-
27
- imported_locales = Dir.entries(locale_path).reject { |entry| protected_locales.include?(entry) }
24
+ imported_locales = Dir.entries(LOCALES_PATH).reject { |entry| PROTECTED_LOCALES.include?(entry) }
28
25
 
29
26
  if imported_locales.empty?
30
27
  puts '[alphalang] There are no locale files to delete. Default locale files are protected.'
31
28
  return
32
29
  else
33
- prompt_user_for_deletion(imported_locales, locale_path, protected_locales)
30
+ prompt_user_for_deletion(imported_locales)
34
31
  end
35
32
  end
data/lib/locale_lister.rb CHANGED
@@ -2,41 +2,37 @@
2
2
 
3
3
  # extra_entries_array in case we wanna make a "protected locale" function later on
4
4
  def get_locale_files(extra_entries_array = [])
5
- locale_path = File.join(__dir__, 'locales')
6
- protected_locales = ['.', '..', 'locale_template', extra_entries_array].flatten
7
- Dir.entries(locale_path).reject { |entry| protected_locales.include?(entry) }
5
+ protected_locales = ['.', '..', 'locale_template', 'default', 'default.old', extra_entries_array].flatten
6
+ Dir.entries(LOCALES_PATH).reject { |entry| protected_locales.include?(entry) }
8
7
  end
9
8
 
10
9
  def list_locale_files()
11
10
  locales = get_locale_files
12
- puts '[alphalang] These are the available locales.'
11
+ puts "[alphalang] These are the available locales.\ndefault"
13
12
  locales.each do |locale|
14
13
  puts locale
15
14
  end
16
15
  puts
17
16
  end
18
17
 
19
- def list_specific_locale_file()
20
- list_locale_files
21
- specific_locale = gets.chomp
22
-
23
- return if ABORT_ANSWERS.include?(specific_locale)
24
-
25
- locale_path = File.join(__dir__, 'locales')
26
- locale_file = File.readlines("#{locale_path}/#{specific_locale}")
18
+ def clean_locale_file_to_array(locale_name)
19
+ locale_file = File.readlines("#{LOCALES_PATH}/#{locale_name}")
27
20
 
28
21
  clean_locale_file_array = []
29
22
  locale_file.each do |line|
30
- line.scan(/\b\p{L}+\b/) do |word|
23
+ line.scan(/\b\p{Word}+\b/) do |word|
31
24
  clean_locale_file_array << word if word.size > 1
32
25
  end
33
26
  end
27
+ clean_locale_file_array
28
+ end
34
29
 
35
- header = "\n[alphalang] Syntax for locale <#{specific_locale}>."
30
+ def print_clean_locale_array(locale_name, clean_array)
31
+ header = "\n[alphalang] Syntax for locale <#{locale_name}>."
36
32
  puts header
37
33
  puts '+' * (header.size - 2)
38
34
  clean_line = ''
39
- clean_locale_file_array.each_with_index do |word, index|
35
+ clean_array.each_with_index do |word, index|
40
36
  if index.even?
41
37
  clean_line += "+ #{word}"
42
38
  else
@@ -48,3 +44,13 @@ def list_specific_locale_file()
48
44
  end
49
45
  puts '+' * (header.size - 2)
50
46
  end
47
+
48
+ def list_specific_locale_file()
49
+ list_locale_files
50
+ specific_locale = gets.chomp
51
+
52
+ return if ABORT_ANSWERS.include?(specific_locale)
53
+
54
+ specific_locale_array = clean_locale_file_to_array(specific_locale)
55
+ print_clean_locale_array(specific_locale, specific_locale_array)
56
+ end
@@ -0,0 +1,17 @@
1
+ while while
2
+ print print
3
+ pause pause
4
+ def def
5
+ end end
6
+ if if
7
+ elseif elseif
8
+ else else
9
+ (false|true) (false|true)
10
+ (==|<=|>=) (==|<=|>=)
11
+ (not|!) (not|!)
12
+ (and|&&) (and|&&)
13
+ (or|\|\|) (or|\|\|)
14
+ \s+ \s+
15
+ \d+ \d+
16
+ \w+ \w+
17
+ . .
@@ -0,0 +1,49 @@
1
+ require_relative 'stmtnodes'
2
+
3
+ ####################################################
4
+
5
+ $scopes = [{}]
6
+ $scope_lvl = 0
7
+ $test_nodes = false
8
+
9
+ # Need to figure out how to move the scopes in here as class members in a sane way
10
+ class ScopeManager
11
+ def self.scope_lvl
12
+ $scope_lvl
13
+ end
14
+
15
+ def self.lookup_var(name)
16
+ temp_scope_lvl = $scope_lvl
17
+ while temp_scope_lvl >= 0
18
+ if !$scopes[temp_scope_lvl].key?(name)
19
+ temp_scope_lvl -= 1
20
+ else
21
+ return $scopes[temp_scope_lvl][name]
22
+ end
23
+ end
24
+ raise ArgumentError, "Variable '#{name}' is not defined" unless @value
25
+ end
26
+
27
+ def self.increment_scope_level
28
+ $scope_lvl += 1
29
+ $scopes.push({})
30
+ end
31
+
32
+ def self.decrement_scope_level
33
+ $scope_lvl -= 1
34
+ $scopes.pop
35
+ end
36
+
37
+ def self.lookup_func(name)
38
+ raise ArgumentError, "Function '#{name}' is not defined" if $scopes[0][name].is_a?(NilClass)
39
+ return $scopes[0][name]
40
+ end
41
+
42
+ def self.add_to_current_scope(name, value)
43
+ $scopes[$scope_lvl][name.name] = value.evaluate
44
+ end
45
+
46
+ def self.add_func_to_global_scope(name, value, args)
47
+ $scopes[0][name.name] = [value, args]
48
+ end
49
+ end
File without changes
@@ -2,54 +2,6 @@ require_relative 'basenodes'
2
2
 
3
3
  ####################################################
4
4
 
5
- $scopes = [{}]
6
- $scope_lvl = 0
7
- $test_nodes = false
8
-
9
- # Need to expand this to actually handle scopes, not just lookups
10
- class ScopeManager
11
- def self.scope_lvl
12
- $scope_lvl
13
- end
14
-
15
- def self.lookup_var(name)
16
- temp_scope_lvl = $scope_lvl
17
- while temp_scope_lvl >= 0
18
- if !$scopes[temp_scope_lvl].key?(name)
19
- temp_scope_lvl -= 1
20
- else
21
- return $scopes[temp_scope_lvl][name]
22
- end
23
- end
24
- raise ArgumentError, "Variable '#{name}' is not defined" unless @value
25
- end
26
-
27
- def self.increment_scope_level
28
- $scope_lvl += 1
29
- $scopes.push({})
30
- end
31
-
32
- def self.decrement_scope_level
33
- $scope_lvl -= 1
34
- $scopes.pop
35
- end
36
-
37
- def self.lookup_func(name)
38
- raise ArgumentError, "Function '#{name}' is not defined" if $scopes[0][name].is_a?(NilClass)
39
- return $scopes[0][name]
40
- end
41
-
42
- def self.add_to_current_scope(name, value)
43
- $scopes[$scope_lvl][name.name] = value.evaluate
44
- end
45
-
46
- def self.add_func_to_global_scope(name, value, args)
47
- $scopes[0][name.name] = [value, args]
48
- end
49
- end
50
-
51
- ####################################################
52
-
53
5
  class VariableCallNode < Node
54
6
  attr_accessor :name
55
7
 
data/lib/rdparse.rb CHANGED
@@ -125,10 +125,9 @@ class Parser
125
125
  class ParseError < RuntimeError
126
126
  end
127
127
 
128
- def initialize(language_name, locale='en', &block)
128
+ def initialize(language_name, locale = 'default', &block)
129
129
 
130
- locales_path = File.join(__dir__, "locales", locale)
131
- token_pairs = File.readlines(locales_path)
130
+ token_pairs = File.readlines("#{LOCALES_PATH}/#{locale}")
132
131
  @token_list = Hash.new
133
132
  token_pairs.each do |pair|
134
133
  default_value, locale_value = pair.split(' ')
data/lib/rdparse_quiet.rb CHANGED
@@ -126,10 +126,9 @@ class Parser
126
126
  class ParseError < RuntimeError
127
127
  end
128
128
 
129
- def initialize(language_name, locale='en', &block)
129
+ def initialize(language_name, locale = 'default', &block)
130
130
 
131
- locales_path = File.join(__dir__, "locales", locale)
132
- token_pairs = File.readlines(locales_path)
131
+ token_pairs = File.readlines("#{LOCALES_PATH}/#{locale}")
133
132
  @token_list = Hash.new
134
133
  token_pairs.each do |pair|
135
134
  default_value, locale_value = pair.split(' ')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphalang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mattias
@@ -61,15 +61,20 @@ files:
61
61
  - bin/alphalang
62
62
  - lib/alpha.rb
63
63
  - lib/lang_creator.rb
64
+ - lib/locale_defaulter.rb
65
+ - lib/locale_defaulter.rb~
64
66
  - lib/locale_deleter.rb
65
67
  - lib/locale_deleter.rb~
66
68
  - lib/locale_lister.rb
67
69
  - lib/locale_lister.rb~
68
70
  - lib/locales/de
71
+ - lib/locales/default
69
72
  - lib/locales/en
70
73
  - lib/locales/locale_template
71
74
  - lib/locales/sv
72
75
  - lib/nodes/basenodes.rb
76
+ - lib/nodes/scopemanager.rb
77
+ - lib/nodes/scopemanager.rb~
73
78
  - lib/nodes/stmtnodes.rb
74
79
  - lib/rdparse.rb
75
80
  - lib/rdparse_quiet.rb