Class: OpenC3::ScriptEngine

Inherits:
Object show all
Defined in:
lib/openc3/script_engines/script_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(running_script) ⇒ ScriptEngine

Returns a new instance of ScriptEngine.



23
24
25
# File 'lib/openc3/script_engines/script_engine.rb', line 23

def initialize(running_script)
  @running_script = running_script
end

Instance Attribute Details

#running_scriptObject

Returns the value of attribute running_script.



21
22
23
# File 'lib/openc3/script_engines/script_engine.rb', line 21

def running_script
  @running_script
end

Instance Method Details

#debug(text) ⇒ Object



53
54
55
# File 'lib/openc3/script_engines/script_engine.rb', line 53

def debug(text)
  run_line(text, [text], "DEBUG", 1)
end

#mnemonic_check(text, filename: nil) ⇒ Object



62
63
64
65
# File 'lib/openc3/script_engines/script_engine.rb', line 62

def mnemonic_check(text, filename: nil)
  puts "Not Implemented"
  return 1
end

#run_line(line, lines, filename, line_no) ⇒ Object

Override this method in the subclass to implement the script engine



28
29
30
31
# File 'lib/openc3/script_engines/script_engine.rb', line 28

def run_line(line, lines, filename, line_no)
  puts line
  return line_no + 1
end

#run_text(text, filename: nil, line_no: 1, end_line_no: nil, bind_variables: false) ⇒ Object



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

def run_text(text, filename: nil, line_no: 1, end_line_no: nil, bind_variables: false)
  lines = text.lines
  loop do
    line = lines[line_no - 1]
    return if line.nil?

    begin
      next_line_no = line_no + 1
      running_script.pre_line_instrumentation(filename, line_no)
      next_line_no = run_line(line, lines, filename, line_no)
      running_script.post_line_instrumentation(filename, line_no)
    rescue Exception => e
      retry if running_script.exception_instrumentation(e, filename, line_no)
    end

    line_no = next_line_no
    return if end_line_no and line_no > end_line_no
  end
end

#syntax_check(text, filename: nil) ⇒ Object



57
58
59
60
# File 'lib/openc3/script_engines/script_engine.rb', line 57

def syntax_check(text, filename: nil)
  puts "Not Implemented"
  return 1
end

#tokenizer(s, special_chars = '()><+-*/=;,') ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openc3/script_engines/script_engine.rb', line 67

def tokenizer(s, special_chars = '()><+-*/=;,')
  result = []
  i = 0
  while i < s.length
    # Skip whitespace
    if s[i].match?(/\s/)
      i += 1
      next
    end

    # Handle quoted strings (single or double quotes)
    if ['"', "'"].include?(s[i])
      quote_char = s[i]
      quote_start = i
      i += 1
      # Find the closing quote
      while i < s.length
        if s[i] == '\\' && i + 1 < s.length  # Handle escaped characters
          i += 2
        elsif s[i] == quote_char  # Found closing quote
          i += 1
          break
        else
          i += 1
        end
      end
      # Include the quotes in the token
      result << s[quote_start...i]
      next
    end

    # Handle special characters
    if special_chars.include?(s[i])
      result << s[i]
      i += 1
      next
    end

    # Handle regular tokens
    token_start = i
    while i < s.length && !s[i].match?(/\s/) && !special_chars.include?(s[i]) && !['"', "'"].include?(s[i])
      i += 1
    end
    if i > token_start
      result << s[token_start...i]
    end
  end

  return result
end