alphalang 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/alphalang +70 -15
- data/lib/alpha.rb +13 -12
- data/lib/lang_creator.rb +38 -48
- data/lib/locale_defaulter.rb +16 -0
- data/lib/locale_defaulter.rb~ +0 -0
- data/lib/locale_deleter.rb +32 -0
- data/lib/locale_deleter.rb~ +0 -0
- data/lib/locale_lister.rb +56 -0
- data/lib/locale_lister.rb~ +0 -0
- data/lib/locales/default +17 -0
- data/lib/nodes/basenodes.rb +0 -5
- data/lib/nodes/scopemanager.rb +49 -0
- data/lib/nodes/scopemanager.rb~ +0 -0
- data/lib/nodes/stmtnodes.rb +37 -36
- data/lib/rdparse.rb +2 -3
- data/lib/rdparse_quiet.rb +2 -3
- metadata +10 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e3d320e5322d98b56f1cd6d5f9e51211de1c41ba6abe20800177bf83ae08133
|
4
|
+
data.tar.gz: fe85e5979279fbff45aa74ec9a09435bff67fd49dca35c5fbd332c1c8628c9e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ee5133266c1e9ebb85c13740d730efb69db1b4deb54853e53103e38275262edc789f425e1450ef5582bb4077590d33c21fbe24662920b473a6d05ba9de21676
|
7
|
+
data.tar.gz: 8e756c3aa10fa954cc06fbb299d5eff3ce1335d6c3130f76485280f09c08165fc1c62bd9b5160e83ea68dec7ead8368d7dc584b5915a62a14ca8ed28d2e954d8
|
data/bin/alphalang
CHANGED
@@ -1,7 +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.
|
4
|
+
ALPHA_VER = '0.1.4'.freeze
|
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
|
5
8
|
|
6
9
|
basic_error_msg = "Usage: alphalang [options] file.alpha\nUsage: alphalang -h for the help menu."
|
7
10
|
|
@@ -17,44 +20,96 @@ OptionParser.new do |opts|
|
|
17
20
|
options[:verify] = true
|
18
21
|
end
|
19
22
|
|
20
|
-
opts.on('--
|
21
|
-
options[:
|
23
|
+
opts.on('--locale=LOCALE', 'Set the syntax locale for current program keywords.') do |locale|
|
24
|
+
options[:locale] = locale
|
22
25
|
end
|
23
26
|
|
24
|
-
opts.on('--
|
27
|
+
opts.on('--setlocale=LOCALE', 'Set the default syntax locale for all program keywords.') do |new_locale|
|
28
|
+
options[:defaultlocale] = new_locale
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on('--createlocale', 'Creates a new locale file for all program keywords available.') do
|
25
32
|
options[:createlocale] = true
|
26
33
|
end
|
34
|
+
|
35
|
+
opts.on('--deletelocale', 'Deletes a locale file from your locales directory.') do
|
36
|
+
options[:deletelocale] = true
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on('--listlocales', 'Lists all available locale files from your locales directory.') do
|
40
|
+
options[:listlocales] = true
|
41
|
+
end
|
42
|
+
|
43
|
+
opts.on('--listlocale', 'Lists all keywords from a specific locale file from your locales directory.') do
|
44
|
+
options[:listlocale] = true
|
45
|
+
end
|
27
46
|
end.parse!
|
28
47
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
48
|
+
verbose_flag = options[:verbose]
|
49
|
+
verify_flag = options[:verify]
|
50
|
+
language_flag = options[:locale]
|
51
|
+
set_locale_flag = options[:defaultlocale]
|
52
|
+
create_locale_flag = options[:createlocale]
|
53
|
+
delete_locale_flag = options[:deletelocale]
|
54
|
+
list_locales_flag = options[:listlocales]
|
55
|
+
list_locale_flag = options[:listlocale]
|
33
56
|
|
34
|
-
if
|
57
|
+
if create_locale_flag
|
35
58
|
require_relative '../lib/lang_creator'
|
36
59
|
create_locale_file
|
37
60
|
return
|
38
61
|
end
|
39
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
|
+
|
77
|
+
if delete_locale_flag
|
78
|
+
require_relative '../lib/locale_deleter'
|
79
|
+
delete_locale_file
|
80
|
+
return
|
81
|
+
end
|
82
|
+
|
83
|
+
if list_locales_flag
|
84
|
+
require_relative '../lib/locale_lister'
|
85
|
+
list_locale_files
|
86
|
+
return
|
87
|
+
end
|
88
|
+
|
89
|
+
if list_locale_flag
|
90
|
+
require_relative '../lib/locale_lister'
|
91
|
+
list_specific_locale_file
|
92
|
+
return
|
93
|
+
end
|
94
|
+
|
40
95
|
raise OptionParser::MissingArgument, "No file provided.
|
41
|
-
Usage: alphalang my_file.alpha | alphalang -h" if ARGV.empty? unless
|
96
|
+
Usage: alphalang my_file.alpha | alphalang -h" if ARGV.empty? unless verify_flag
|
42
97
|
raise OptionParser::InvalidArgument, 'Too many files provided.
|
43
98
|
Did you mean to use an --[option]? | alphalang -h' if ARGV.length > 1
|
44
|
-
raise OptionParser::InvalidArgument, 'File must end with .alpha' unless
|
99
|
+
raise OptionParser::InvalidArgument, 'File must end with .alpha' unless verify_flag or ARGV[0].end_with?('.alpha')
|
45
100
|
|
46
|
-
if
|
101
|
+
if verbose_flag
|
47
102
|
require_relative '../lib/rdparse'
|
48
103
|
else
|
49
104
|
require_relative '../lib/rdparse_quiet'
|
50
105
|
end
|
51
106
|
|
52
|
-
if
|
53
|
-
|
107
|
+
if verify_flag
|
108
|
+
unless ARGV.empty?
|
54
109
|
puts 'Flag for verification found. Ignoring input file.'
|
55
110
|
sleep 1
|
56
111
|
end
|
57
112
|
require_relative '../lib/tester/test_unit'
|
58
113
|
else
|
59
|
-
LangParser.new(
|
114
|
+
LangParser.new(language_flag).parse_file(ARGV[0])
|
60
115
|
end
|
data/lib/alpha.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require_relative './nodes/
|
1
|
+
require_relative './nodes/scopemanager'
|
2
2
|
|
3
3
|
class LangParser
|
4
|
-
def initialize(locale='
|
4
|
+
def initialize(locale = 'default')
|
5
5
|
@langParser = Parser.new('lang parser', locale) do
|
6
6
|
token(/while/) { |_| :WHILE }
|
7
7
|
token(/print/) { |_| :PRINT }
|
@@ -81,11 +81,6 @@ class LangParser
|
|
81
81
|
match(:member, '=', :array_stmt) { |var, _, value| VariableDecNode.new(var, value) }
|
82
82
|
end
|
83
83
|
|
84
|
-
#array inte klar kan assigna till vaiabel men inte kolla upp än
|
85
|
-
rule :array_stmt do
|
86
|
-
match('[', :arg_list, ']') { |_, array, _| array }
|
87
|
-
end
|
88
|
-
|
89
84
|
rule :pause_stmt do
|
90
85
|
match(:PAUSE, :expr) { |_, a| PauseNode.new(a) }
|
91
86
|
end
|
@@ -109,6 +104,15 @@ class LangParser
|
|
109
104
|
match(:NOT, :expr_stmt) { |_, b| NotNode.new(b) }
|
110
105
|
end
|
111
106
|
|
107
|
+
rule :array_stmt do
|
108
|
+
match('[', :arg_list, ']') { |_, array, _| array }
|
109
|
+
end
|
110
|
+
|
111
|
+
rule :arg_list do
|
112
|
+
match(:expr, ',', :arg_list) { |a, _, b| ArrayNode.new(a, b) }
|
113
|
+
match(:expr) { |a| ArrayNode.new(a, NilClass) }
|
114
|
+
end
|
115
|
+
|
112
116
|
rule :expr do
|
113
117
|
match(:expr, /(<|>)/, :expr) { |a, op, b| CompareNode.new(a, op, b) }
|
114
118
|
match(/(<|>)/, :expr) { |op, b| CompareNode.new(nil, op, b) }
|
@@ -141,14 +145,11 @@ class LangParser
|
|
141
145
|
rule :member do
|
142
146
|
match(/[a-z]/, '(', :arg_list, ')') { |var, _, args, _| FuncCallNode.new(var, args) }
|
143
147
|
match(/[a-z]/, '(', ')') { |var, _, _| FuncCallNode.new(var, NilClass) }
|
148
|
+
match(/[a-z]/, '[', '-', /\d+/, ']') { |var, _, neg, index, _| ArrayCallNode.new(var, (neg+index)) }
|
149
|
+
match(/[a-z]/, '[', /\d+/, ']') { |var, _, index, _| ArrayCallNode.new(var, index) }
|
144
150
|
match(/[a-z]/) { |var| VariableCallNode.new(var) }
|
145
151
|
end
|
146
152
|
|
147
|
-
rule :arg_list do
|
148
|
-
match(:expr, ',', :arg_list) { |a, _, b| ArrayNode.new(a, b) }
|
149
|
-
match(:expr) { |a| ArrayNode.new(a, NilClass) }
|
150
|
-
end
|
151
|
-
|
152
153
|
rule :prio_stmt do
|
153
154
|
match('(', :stmt, ')') { |_, a, _| a }
|
154
155
|
end
|
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(
|
33
|
+
def prompt_user(file)
|
34
|
+
locale_template = File.readlines("#{LOCALES_PATH}/locale_template")
|
35
|
+
|
33
36
|
counter = 0
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
+
File.open(file, 'a') do |f|
|
38
|
+
locale_template.each do |line|
|
39
|
+
counter += 1
|
40
|
+
break if counter == 14
|
37
41
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
72
|
-
|
67
|
+
new_locale_name = gets.chomp
|
68
|
+
new_locale_file_path = "#{LOCALES_PATH}/#{new_locale_name}"
|
73
69
|
|
74
|
-
|
75
|
-
|
76
|
-
|
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(
|
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
|
-
|
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
|
-
|
90
|
+
puts "Locale is saved as #{new_locale_name} in #{LOCALES_PATH}"
|
101
91
|
else
|
102
92
|
puts 'Translation removed'
|
103
|
-
File.delete(
|
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
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
def prompt_user_for_deletion(locales)
|
4
|
+
puts 'Which locale would you like to delete?: RET or "none" to abort'
|
5
|
+
|
6
|
+
locales.each do |locale|
|
7
|
+
puts locale
|
8
|
+
end
|
9
|
+
|
10
|
+
locale_file = gets.chomp
|
11
|
+
|
12
|
+
return if ABORT_ANSWERS.include?(locale_file)
|
13
|
+
|
14
|
+
if PROTECTED_LOCALES.include?(locale_file)
|
15
|
+
puts 'You may not delete a default locale.'
|
16
|
+
return
|
17
|
+
else
|
18
|
+
File.delete("#{LOCALES_PATH}/#{locale_file}")
|
19
|
+
puts "Successfully deleted #{LOCALES_PATH}/#{locale_file}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete_locale_file()
|
24
|
+
imported_locales = Dir.entries(LOCALES_PATH).reject { |entry| PROTECTED_LOCALES.include?(entry) }
|
25
|
+
|
26
|
+
if imported_locales.empty?
|
27
|
+
puts '[alphalang] There are no locale files to delete. Default locale files are protected.'
|
28
|
+
return
|
29
|
+
else
|
30
|
+
prompt_user_for_deletion(imported_locales)
|
31
|
+
end
|
32
|
+
end
|
File without changes
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# extra_entries_array in case we wanna make a "protected locale" function later on
|
4
|
+
def get_locale_files(extra_entries_array = [])
|
5
|
+
protected_locales = ['.', '..', 'locale_template', 'default', 'default.old', extra_entries_array].flatten
|
6
|
+
Dir.entries(LOCALES_PATH).reject { |entry| protected_locales.include?(entry) }
|
7
|
+
end
|
8
|
+
|
9
|
+
def list_locale_files()
|
10
|
+
locales = get_locale_files
|
11
|
+
puts "[alphalang] These are the available locales.\ndefault"
|
12
|
+
locales.each do |locale|
|
13
|
+
puts locale
|
14
|
+
end
|
15
|
+
puts
|
16
|
+
end
|
17
|
+
|
18
|
+
def clean_locale_file_to_array(locale_name)
|
19
|
+
locale_file = File.readlines("#{LOCALES_PATH}/#{locale_name}")
|
20
|
+
|
21
|
+
clean_locale_file_array = []
|
22
|
+
locale_file.each do |line|
|
23
|
+
line.scan(/\b\p{Word}+\b/) do |word|
|
24
|
+
clean_locale_file_array << word if word.size > 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
clean_locale_file_array
|
28
|
+
end
|
29
|
+
|
30
|
+
def print_clean_locale_array(locale_name, clean_array)
|
31
|
+
header = "\n[alphalang] Syntax for locale <#{locale_name}>."
|
32
|
+
puts header
|
33
|
+
puts '+' * (header.size - 2)
|
34
|
+
clean_line = ''
|
35
|
+
clean_array.each_with_index do |word, index|
|
36
|
+
if index.even?
|
37
|
+
clean_line += "+ #{word}"
|
38
|
+
else
|
39
|
+
clean_line += (' ' * (20 - clean_line.size)) + "#{word}"
|
40
|
+
clean_line += (' ' * (header.size - clean_line.size - 3) + '+')
|
41
|
+
puts clean_line
|
42
|
+
clean_line = ''
|
43
|
+
end
|
44
|
+
end
|
45
|
+
puts '+' * (header.size - 2)
|
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
|
File without changes
|
data/lib/locales/default
ADDED
@@ -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
|
+
. .
|
data/lib/nodes/basenodes.rb
CHANGED
@@ -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
|
data/lib/nodes/stmtnodes.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative 'basenodes'
|
2
2
|
|
3
3
|
####################################################
|
4
|
+
|
4
5
|
class VariableCallNode < Node
|
5
6
|
attr_accessor :name
|
6
7
|
|
@@ -8,19 +9,9 @@ class VariableCallNode < Node
|
|
8
9
|
@name = name
|
9
10
|
end
|
10
11
|
|
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
12
|
def evaluate
|
23
|
-
|
13
|
+
@value = ScopeManager.lookup_var(@name)
|
14
|
+
return @value
|
24
15
|
end
|
25
16
|
end
|
26
17
|
|
@@ -33,9 +24,8 @@ class VariableDecNode < Node
|
|
33
24
|
end
|
34
25
|
|
35
26
|
def evaluate
|
36
|
-
|
27
|
+
ScopeManager.add_to_current_scope(name, @value)
|
37
28
|
self
|
38
|
-
# return nil
|
39
29
|
end
|
40
30
|
end
|
41
31
|
|
@@ -49,7 +39,7 @@ class FunctionDecNode < Node
|
|
49
39
|
end
|
50
40
|
|
51
41
|
def evaluate
|
52
|
-
|
42
|
+
ScopeManager.add_func_to_global_scope(@name, @value, @args)
|
53
43
|
return nil
|
54
44
|
end
|
55
45
|
end
|
@@ -62,19 +52,13 @@ class FuncCallNode < Node
|
|
62
52
|
@args = args
|
63
53
|
end
|
64
54
|
|
65
|
-
def lookup_var(name)
|
66
|
-
return $scopes[0][name]
|
67
|
-
end
|
68
|
-
|
69
55
|
def evaluate
|
70
|
-
|
71
|
-
|
56
|
+
func = ScopeManager.lookup_func(@name)
|
57
|
+
|
72
58
|
function_body = func[0]
|
73
59
|
function_param = func[1]
|
74
60
|
|
75
|
-
|
76
|
-
|
77
|
-
$scope_lvl += 1
|
61
|
+
ScopeManager.increment_scope_level
|
78
62
|
|
79
63
|
if function_param.is_a?(ArrayNode)
|
80
64
|
function_param.each do |val, index|
|
@@ -83,18 +67,18 @@ class FuncCallNode < Node
|
|
83
67
|
end
|
84
68
|
end
|
85
69
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
if func_result.is_a?(VariableDecNode)
|
90
|
-
$scope_lvl = 0
|
91
|
-
func_result.evaluate
|
92
|
-
$scope_lvl = old_scope_lvl
|
93
|
-
end
|
70
|
+
func_return_value = function_body.evaluate
|
71
|
+
|
72
|
+
ScopeManager.decrement_scope_level
|
94
73
|
|
95
|
-
|
96
|
-
|
97
|
-
|
74
|
+
# If function return value is an "Assign" then we declare that variable in the global scope.
|
75
|
+
old_scope_lvl = ScopeManager.scope_lvl
|
76
|
+
if func_return_value.is_a?(VariableDecNode)
|
77
|
+
ScopeManager.scope_lvl = 0
|
78
|
+
func_return_value.evaluate
|
79
|
+
ScopeManager.scope_lvl = old_scope_lvl
|
80
|
+
end
|
81
|
+
return func_return_value
|
98
82
|
end
|
99
83
|
end
|
100
84
|
|
@@ -176,6 +160,23 @@ end
|
|
176
160
|
|
177
161
|
####################################################
|
178
162
|
|
163
|
+
class ArrayCallNode < Node
|
164
|
+
def initialize(array, index)
|
165
|
+
super(array)
|
166
|
+
@index = index.to_i
|
167
|
+
end
|
168
|
+
|
169
|
+
def evaluate
|
170
|
+
arr = ScopeManager.lookup_var(@value)
|
171
|
+
if @index > arr.size - 1
|
172
|
+
raise ArgumentError, "You are trying to access an out of bounds index. Here -> #{@value}[#{@index}]"
|
173
|
+
end
|
174
|
+
@value = arr[@index]
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
####################################################
|
179
|
+
|
179
180
|
class PrintNode
|
180
181
|
attr_accessor :value
|
181
182
|
|
@@ -185,7 +186,7 @@ class PrintNode
|
|
185
186
|
|
186
187
|
def evaluate
|
187
188
|
puts @value.evaluate
|
188
|
-
self.class
|
189
|
+
self.class
|
189
190
|
end
|
190
191
|
end
|
191
192
|
|
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='
|
128
|
+
def initialize(language_name, locale = 'default', &block)
|
129
129
|
|
130
|
-
|
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='
|
129
|
+
def initialize(language_name, locale = 'default', &block)
|
130
130
|
|
131
|
-
|
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.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mattias
|
@@ -61,11 +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~
|
66
|
+
- lib/locale_deleter.rb
|
67
|
+
- lib/locale_deleter.rb~
|
68
|
+
- lib/locale_lister.rb
|
69
|
+
- lib/locale_lister.rb~
|
64
70
|
- lib/locales/de
|
71
|
+
- lib/locales/default
|
65
72
|
- lib/locales/en
|
66
73
|
- lib/locales/locale_template
|
67
74
|
- lib/locales/sv
|
68
75
|
- lib/nodes/basenodes.rb
|
76
|
+
- lib/nodes/scopemanager.rb
|
77
|
+
- lib/nodes/scopemanager.rb~
|
69
78
|
- lib/nodes/stmtnodes.rb
|
70
79
|
- lib/rdparse.rb
|
71
80
|
- lib/rdparse_quiet.rb
|