2021-12-07 16:09:17 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-11-24 13:33:44 +01:00
|
|
|
module Rpl
|
2021-12-07 15:50:58 +01:00
|
|
|
module Lang
|
|
|
|
module Core
|
|
|
|
module_function
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# convert an object into a string
|
2021-12-07 16:46:33 +01:00
|
|
|
def to_string( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [:any] )
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :string,
|
2022-02-09 13:38:32 +01:00
|
|
|
value: Rpl::Lang.stringify( args[0] ) }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# convert a string into an object
|
2021-12-07 16:46:33 +01:00
|
|
|
def from_string( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string]] )
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2022-02-09 16:33:47 +01:00
|
|
|
stack += Rpl::Interpreter.parse( args[0][:value] )
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# convert ASCII character code in stack level 1 into a string
|
2021-12-07 16:46:33 +01:00
|
|
|
def chr( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :string,
|
2022-02-09 10:53:00 +01:00
|
|
|
value: args[0][:value].to_i.chr }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# return ASCII code of the first character of the string in stack level 1 as a real number
|
2021-12-07 16:46:33 +01:00
|
|
|
def num( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string]] )
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :numeric,
|
|
|
|
base: 10,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: args[0][:value].ord }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# return the length of the string
|
2021-12-07 16:46:33 +01:00
|
|
|
def size( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string]] )
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :numeric,
|
|
|
|
base: 10,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: args[0][:value].length }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# search for the string in level 1 within the string in level 2
|
2021-12-07 16:46:33 +01:00
|
|
|
def pos( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string], %i[string]] )
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :numeric,
|
|
|
|
base: 10,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: args[1][:value].index( args[0][:value] ) }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-11-24 15:04:56 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
# return a substring of the string in level 3
|
2021-12-07 16:46:33 +01:00
|
|
|
def sub( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric], %i[numeric], %i[string]] )
|
2021-11-24 13:33:44 +01:00
|
|
|
|
2021-12-07 15:50:58 +01:00
|
|
|
stack << { type: :string,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: args[2][:value][ (args[1][:value] - 1)..(args[0][:value] - 1) ] }
|
2021-12-07 16:46:33 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
2021-12-07 15:50:58 +01:00
|
|
|
end
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2021-12-16 15:21:23 +01:00
|
|
|
# reverse string or list
|
2021-12-15 13:32:48 +01:00
|
|
|
def rev( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string list]] )
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2021-12-16 15:21:23 +01:00
|
|
|
result = args[0]
|
|
|
|
|
|
|
|
case args[0][:type]
|
|
|
|
when :string
|
|
|
|
result = { type: :string,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: args[0][:value].reverse }
|
2021-12-16 15:21:23 +01:00
|
|
|
when :list
|
|
|
|
result[:value].reverse!
|
|
|
|
end
|
|
|
|
|
|
|
|
stack << result
|
2021-12-15 13:32:48 +01:00
|
|
|
|
|
|
|
[stack, dictionary]
|
|
|
|
end
|
|
|
|
|
|
|
|
# split string
|
|
|
|
def split( stack, dictionary )
|
2022-02-08 15:45:36 +01:00
|
|
|
stack, args = Rpl::Lang.stack_extract( stack, [%i[string], %i[string]] )
|
2021-12-15 13:32:48 +01:00
|
|
|
|
2022-01-26 18:30:51 +01:00
|
|
|
args[1][:value].split( args[0][:value] ).each do |elt|
|
2021-12-15 13:32:48 +01:00
|
|
|
stack << { type: :string,
|
2022-01-26 18:30:51 +01:00
|
|
|
value: elt }
|
2021-12-15 13:32:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
[stack, dictionary]
|
|
|
|
end
|
2021-11-24 13:33:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|