Top Level Namespace

Defined Under Namespace

Classes: AndNode, ArrayCallNode, ArrayNode, BinaryOperationNode, BoolNode, CompStmtNode, CompareNode, ElseNode, ElseifNode, FuncCallNode, FunctionDecNode, IfCompStmtNode, IfNode, LangParser, Node, NotNode, NumberNode, OrNode, Parser, PauseNode, PrintNode, Rule, ScopeManager, TesCompactIf, TestArithmetic, TestComparisons, TestFunction, TestIf, TestLogic, TestLoop, TestPrograms, TestVariables, VariableCallNode, VariableDecNode, WhileLoopNode

Instance Method Summary collapse

Instance Method Details

#clean_locale_file_to_array(locale_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/locale_lister.rb', line 18

def clean_locale_file_to_array(locale_name)
  locale_file = File.readlines("#{LOCALES_PATH}/#{locale_name}")

  clean_locale_file_array = []
  locale_file.each do |line|
    line.scan(/[\p{Word}\p{Emoji}]+[|][\p{Word}\p{Emoji}]+|[\p{Word}\p{Emoji}]+/) do |word|
      clean_locale_file_array << word if word.size > 1 or word.match?(/\p{Emoji}/)
    end
  end

  clean_locale_file_array
end

#create_locale_fileObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/locale_creator.rb', line 65

def create_locale_file()
  puts 'Choose a filename for your locale/syntax:'
  new_locale_name = gets.chomp
  new_locale_file_path = "#{LOCALES_PATH}/#{new_locale_name}"

  if File.exist?(new_locale_file_path)
    puts "#{new_locale_name} already exists."
    return
  end

  prompt_user(new_locale_file_path)
  system('clear')

  locale_as_array = clean_locale_file_to_array(new_locale_name)
  print_clean_locale_array(new_locale_name, locale_as_array)

  puts 'Is this correct? [Y/n]'
  answer = gets.chomp

  if answer.downcase.match? /y|Y/ or answer.empty?
    puts "Locale is saved as #{new_locale_name} in #{LOCALES_PATH}"
  else
    puts 'Translation removed'
    File.delete(new_locale_file_path)
  end
end

#delete_locale_fileObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/locale_deleter.rb', line 23

def delete_locale_file()
  imported_locales = Dir.entries(LOCALES_PATH).reject { |entry| PROTECTED_LOCALES.include?(entry) }

  if imported_locales.empty?
    puts '[alphalang] There are no locale files to delete. Default locale files are protected.'
    return
  else
    prompt_user_for_deletion(imported_locales)
  end
end

#get_locale_files(extra_entries_array = []) ⇒ Object

extra_entries_array in case we wanna make a “protected locale” function later on



4
5
6
7
# File 'lib/locale_lister.rb', line 4

def get_locale_files(extra_entries_array = [])
  protected_locales = ['.', '..', 'locale_template', 'default', 'default.old', extra_entries_array].flatten
  Dir.entries(LOCALES_PATH).reject { |entry| protected_locales.include?(entry) }
end

#list_locale_filesObject



9
10
11
12
13
14
15
16
# File 'lib/locale_lister.rb', line 9

def list_locale_files()
  locales = get_locale_files
  puts "[alphalang] These are the available locales.\ndefault"
  locales.each do |locale|
    puts locale
  end
  puts
end

#list_specific_locale_fileObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/locale_lister.rb', line 54

def list_specific_locale_file()
  list_locale_files
  specific_locale = gets.chomp

  specific_locale = File.read("#{LOCALES_PATH}/#{specific_locale}") if specific_locale == 'default'

  return if ABORT_ANSWERS.include?(specific_locale)

  specific_locale_array = clean_locale_file_to_array(specific_locale)
  print_clean_locale_array(specific_locale, specific_locale_array)
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locale_lister.rb', line 31

def print_clean_locale_array(locale_name, clean_array)
  longest_word = clean_array.max_by(&:length).size
  padding = ' ' * (longest_word / 2 - 5)

  header = "#{padding}[alphalang] Syntax for locale <#{locale_name}>.#{padding}"
  puts header
  puts '+' * (header.size - 2)

  clean_line = ''
  clean_array.each_with_index do |word, index|
    if index.even?
      clean_line += "+ #{word}"
    else
      clean_line += (' ' * (20 - clean_line.size)) + "#{word}"
      clean_line += (' ' * (header.size - clean_line.size - 3) + '+')
      puts clean_line
      clean_line = ''
    end
  end

  puts '+' * (header.size - 2)
end

#prompt_user(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/locale_creator.rb', line 32

def prompt_user(file)
  locale_template = File.readlines("#{LOCALES_PATH}/locale_template")

  counter = 0
  File.open(file, 'a') do |f|
    f.puts ';;.*$ ;;.*$'
    locale_template.each do |line|
      counter += 1
      break if counter == 15

      if counter > 1 && counter < 10
        translation = read_translation(line.chomp)
        f.puts "#{line.chomp} #{translation}"
      end
      if counter == 10
        translation = read_translation_true_false(line.chomp)
        f.puts "#{line.chomp} #{translation}"
      end
      if counter == 11
        f.puts "#{line.chomp} (==|<=|>=)"
      end
      if counter > 11
        translation = read_translation_not_and_or(line.chomp)
        f.puts "#{line.chomp} #{translation}"
      end
    end
    f.puts '\s+ \s+'
    f.puts '\d+ \d+'
    f.puts '\w+ \w+'
    f.puts '. .'
  end
end

#prompt_user_for_deletion(locales) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/locale_deleter.rb', line 3

def prompt_user_for_deletion(locales)
  puts 'Which locale would you like to delete?:                    RET or "none" to abort'

  locales.each do |locale|
    puts locale
  end

  locale_file = gets.chomp

  return if ABORT_ANSWERS.include?(locale_file)

  if PROTECTED_LOCALES.include?(locale_file)
    puts 'You may not delete a default locale.'
    return
  else
    File.delete("#{LOCALES_PATH}/#{locale_file}")
    puts "Successfully deleted #{LOCALES_PATH}/#{locale_file}"
  end
end

#read_translation(line) ⇒ Object



4
5
6
7
8
# File 'lib/locale_creator.rb', line 4

def read_translation(line)
  puts "Enter the translation for: '#{line}'        <RET> to accept '#{line}'"
  translation = gets.chomp
  translation.empty? ? line : translation
end

#read_translation_not_and_or(line) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/locale_creator.rb', line 24

def read_translation_not_and_or(line)
  word = line.match(/\w+/)[0]
  postfix = line.match(/\|.+/)[0]
  puts "Enter the translation for: '#{word}'        <RET> to accept '#{word}'"
  input = gets.chomp
  return "(#{input.empty? ? word : input}#{postfix}"
end

#read_translation_true_false(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/locale_creator.rb', line 10

def read_translation_true_false(line)
  words = line.scan(/true|false/)
  translation = '('
  words.each do |word|
    puts "Enter the translation for: '#{word}'        <RET> to accept '#{word}'"
    input = gets.chomp
    translation += input.empty? ? word : input
    translation += '|'
  end
  translation.chop!
  translation += ')'
  translation
end

#set_default_locale(new_locale) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/locale_defaulter.rb', line 5

def set_default_locale(new_locale)
  available_locales = get_locale_files

  available_locales.each do |available_locale|
    if available_locale == new_locale
      begin
        File.open("#{LOCALES_PATH}/default", 'w') { |f| f.write(new_locale) }
        puts "[alphalang] Default syntax locale is now set to #{new_locale}."
      rescue Errno::ENOENT
        puts '[alphalang] Failed to change default locale. Likely permissions error on your machine.'
        puts "Could not open #{LOCALES_PATH}/default}"
      end
    end
  end
end

#set_up_scope_headerObject



3
4
5
6
7
# File 'lib/nodes/stmtnodes.rb', line 3

def set_up_scope_header
  result = "<#{ScopeManager.scope_lvl}>   "
  result += '++--++--' * (ScopeManager.scope_lvl + 1) + ' '
  result
end