rpl 0.9.1 → 0.10.1
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 +4 -4
- data/bin/rpl +24 -10
- data/lib/rpl/interpreter.rb +13 -6
- data/lib/rpl/words/filesystem.rb +2 -2
- data/lib/rpl.rb +37 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f003b868db4dbda2168ccfa57e5ac82d01621af12b56d8c65f541a91e9f305ff
|
4
|
+
data.tar.gz: c3792a7db5c8f3e3a9ff3950c859db242bd2ddffa0dcc783b85e87143e68a88b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 004405ac78a320d83a9a7e24448c8e10ee916d999afc29201df8d66601d455286764f598fd6a8703560dcb3fda1da8976569e237d256faf3d42240ae4416b6ac
|
7
|
+
data.tar.gz: 7ed6ddfd403d45e120e3b5adb0c3c1657b8e1de9c4fe7e511af56f212c492f271e8ef5152aabc0bf9bbc344af1ce56f66e97b7e60d947840be479b5de62eb9ec
|
data/bin/rpl
CHANGED
@@ -8,9 +8,7 @@ require 'readline'
|
|
8
8
|
require 'rpl'
|
9
9
|
|
10
10
|
class RplRepl
|
11
|
-
def initialize( interpreter )
|
12
|
-
interpreter ||= Rpl.new
|
13
|
-
|
11
|
+
def initialize( interpreter: Rpl.new )
|
14
12
|
@interpreter = interpreter
|
15
13
|
end
|
16
14
|
|
@@ -51,11 +49,25 @@ class RplRepl
|
|
51
49
|
end
|
52
50
|
|
53
51
|
options = { run_REPL: ARGV.empty?,
|
52
|
+
persistence_filename: File.expand_path( '~/.local/state/rpl.rb/machine' ),
|
53
|
+
live_persistence: true,
|
54
54
|
files: [],
|
55
55
|
programs: [] }
|
56
56
|
|
57
57
|
OptionParser.new do |opts|
|
58
|
-
opts.on('-
|
58
|
+
opts.on('-s', "--state filename", "persist state in filename (default: #{options[:persistence_filename]}) (will be created if needed)") do |filename|
|
59
|
+
options[:persistence_filename] = File.expand_path( filename )
|
60
|
+
end
|
61
|
+
|
62
|
+
opts.on('-q', '--no-state', 'Do not load persisted state') do
|
63
|
+
options[:persistence_filename] = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
opts.on('-d', '--no-persist', 'Do not persist state') do
|
67
|
+
options[:live_persistence] = false
|
68
|
+
end
|
69
|
+
|
70
|
+
opts.on('-c', '--code "program"', 'run provided "program"') do |program|
|
59
71
|
options[:programs] << program
|
60
72
|
end
|
61
73
|
|
@@ -69,11 +81,12 @@ OptionParser.new do |opts|
|
|
69
81
|
end.parse!
|
70
82
|
|
71
83
|
# Instantiate interpreter
|
72
|
-
interpreter = Rpl.new
|
84
|
+
interpreter = Rpl.new( persistence_filename: options[:persistence_filename],
|
85
|
+
live_persistence: options[:live_persistence] )
|
73
86
|
|
74
87
|
# first run provided files if any
|
75
88
|
options[:files].each do |filename|
|
76
|
-
interpreter.run "\"#{filename}\" feval"
|
89
|
+
interpreter.run "\"#{File.expand_path( filename )}\" feval"
|
77
90
|
end
|
78
91
|
|
79
92
|
# second run provided code if any
|
@@ -82,7 +95,8 @@ options[:programs].each do |program|
|
|
82
95
|
end
|
83
96
|
|
84
97
|
# third launch REPL if (explicitely or implicitely) asked
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
98
|
+
if options[:run_REPL]
|
99
|
+
RplRepl.new( interpreter: interpreter ).run
|
100
|
+
else
|
101
|
+
interpreter.persist_state
|
102
|
+
end
|
data/lib/rpl/interpreter.rb
CHANGED
@@ -36,8 +36,8 @@ class Interpreter
|
|
36
36
|
|
37
37
|
attr_accessor :precision
|
38
38
|
|
39
|
-
def initialize( stack
|
40
|
-
@version = 0.
|
39
|
+
def initialize( stack: [], dictionary: Dictionary.new )
|
40
|
+
@version = 0.101
|
41
41
|
|
42
42
|
@dictionary = dictionary
|
43
43
|
@stack = stack
|
@@ -100,12 +100,19 @@ class Interpreter
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def export_vars
|
103
|
-
@
|
104
|
-
|
105
|
-
|
103
|
+
vars_as_string = "@ variables:\n"
|
104
|
+
vars_as_string += @dictionary.vars
|
105
|
+
.map { |name, value| "#{value}\n'#{name}' sto\n" }
|
106
|
+
.join("\n")
|
107
|
+
|
108
|
+
vars_as_string
|
106
109
|
end
|
107
110
|
|
108
111
|
def export_stack
|
109
|
-
@stack
|
112
|
+
stack_as_string = "@ stack:\n"
|
113
|
+
stack_as_string += @stack.map(&:to_s)
|
114
|
+
.join("\n")
|
115
|
+
|
116
|
+
stack_as_string
|
110
117
|
end
|
111
118
|
end
|
data/lib/rpl/words/filesystem.rb
CHANGED
@@ -16,7 +16,7 @@ module RplLang
|
|
16
16
|
proc do
|
17
17
|
args = stack_extract( [[RplString]] )
|
18
18
|
|
19
|
-
path = File.
|
19
|
+
path = File.expand_path( args[0].value )
|
20
20
|
|
21
21
|
@stack << Types.new_object( RplString, "\"#{File.read( path )}\"" )
|
22
22
|
end )
|
@@ -32,7 +32,7 @@ module RplLang
|
|
32
32
|
proc do
|
33
33
|
args = stack_extract( [[RplString], :any] )
|
34
34
|
|
35
|
-
File.write( File.
|
35
|
+
File.write( File.expand_path( args[0].value ),
|
36
36
|
args[1].value )
|
37
37
|
end )
|
38
38
|
end
|
data/lib/rpl.rb
CHANGED
@@ -7,10 +7,45 @@ require 'rpl/words'
|
|
7
7
|
class Rpl < Interpreter
|
8
8
|
include Types
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
attr_accessor :live_persistence
|
11
|
+
|
12
|
+
def initialize( stack: [],
|
13
|
+
dictionary: Dictionary.new,
|
14
|
+
persistence_filename: nil,
|
15
|
+
live_persistence: true )
|
16
|
+
super( stack: stack, dictionary: dictionary )
|
17
|
+
|
18
|
+
@persistence_filename = persistence_filename
|
19
|
+
@live_persistence = live_persistence
|
12
20
|
|
13
21
|
populate_dictionary if @dictionary.words.empty?
|
22
|
+
|
23
|
+
load_persisted_state
|
24
|
+
end
|
25
|
+
|
26
|
+
def load_persisted_state
|
27
|
+
return if @persistence_filename.nil?
|
28
|
+
|
29
|
+
FileUtils.mkdir_p( File.dirname( @persistence_filename ) )
|
30
|
+
FileUtils.touch( @persistence_filename )
|
31
|
+
|
32
|
+
run "\"#{@persistence_filename}\" feval"
|
33
|
+
end
|
34
|
+
|
35
|
+
def persist_state
|
36
|
+
return if @persistence_filename.nil?
|
37
|
+
|
38
|
+
File.open( @persistence_filename, 'w' ) do |persistence_file|
|
39
|
+
persistence_file.write "#{export_vars}\n#{export_stack}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def run( input )
|
44
|
+
stack = super
|
45
|
+
|
46
|
+
persist_state if @live_persistence
|
47
|
+
|
48
|
+
stack
|
14
49
|
end
|
15
50
|
|
16
51
|
prepend RplLang::Words::Branch
|