tp_plus 0.0.73 → 0.0.74

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +74 -65
  3. data/Rakefile +35 -21
  4. data/bin/tpp +73 -73
  5. data/lib/tp_plus/interpreter.rb +152 -129
  6. data/lib/tp_plus/namespace.rb +66 -66
  7. data/lib/tp_plus/nodes/abort_node.rb +9 -9
  8. data/lib/tp_plus/nodes/address_node.rb +22 -0
  9. data/lib/tp_plus/nodes/argument_node.rb +20 -19
  10. data/lib/tp_plus/nodes/assignment_node.rb +45 -45
  11. data/lib/tp_plus/nodes/call_node.rb +34 -34
  12. data/lib/tp_plus/nodes/case_condition_node.rb +26 -26
  13. data/lib/tp_plus/nodes/case_node.rb +33 -33
  14. data/lib/tp_plus/nodes/comment_node.rb +22 -22
  15. data/lib/tp_plus/nodes/conditional_node.rb +60 -54
  16. data/lib/tp_plus/nodes/definition_node.rb +22 -22
  17. data/lib/tp_plus/nodes/digit_node.rb +22 -21
  18. data/lib/tp_plus/nodes/empty_stmt_node.rb +9 -0
  19. data/lib/tp_plus/nodes/eval_node.rb +13 -13
  20. data/lib/tp_plus/nodes/expression_node.rb +68 -65
  21. data/lib/tp_plus/nodes/for_node.rb +20 -20
  22. data/lib/tp_plus/nodes/header_node.rb +27 -27
  23. data/lib/tp_plus/nodes/indirect_node.rb +51 -49
  24. data/lib/tp_plus/nodes/inline_conditional_node.rb +40 -40
  25. data/lib/tp_plus/nodes/io_method_node.rb +55 -55
  26. data/lib/tp_plus/nodes/io_node.rb +31 -30
  27. data/lib/tp_plus/nodes/jump_node.rb +23 -23
  28. data/lib/tp_plus/nodes/label_definition_node.rb +21 -21
  29. data/lib/tp_plus/nodes/motion_node.rb +62 -62
  30. data/lib/tp_plus/nodes/namespace_node.rb +16 -16
  31. data/lib/tp_plus/nodes/namespaced_var_node.rb +38 -38
  32. data/lib/tp_plus/nodes/numreg_node.rb +26 -25
  33. data/lib/tp_plus/nodes/offset_node.rb +27 -27
  34. data/lib/tp_plus/nodes/operator_node.rb +78 -72
  35. data/lib/tp_plus/nodes/paren_expression_node.rb +17 -0
  36. data/lib/tp_plus/nodes/pause_node.rb +9 -9
  37. data/lib/tp_plus/nodes/position_data_node.rb +64 -50
  38. data/lib/tp_plus/nodes/position_node.rb +26 -25
  39. data/lib/tp_plus/nodes/posreg_node.rb +64 -48
  40. data/lib/tp_plus/nodes/raise_node.rb +13 -13
  41. data/lib/tp_plus/nodes/real_node.rb +27 -27
  42. data/lib/tp_plus/nodes/set_skip_node.rb +14 -0
  43. data/lib/tp_plus/nodes/skip_node.rb +22 -22
  44. data/lib/tp_plus/nodes/speed_node.rb +29 -29
  45. data/lib/tp_plus/nodes/string_node.rb +13 -13
  46. data/lib/tp_plus/nodes/string_register_node.rb +26 -25
  47. data/lib/tp_plus/nodes/termination_node.rb +23 -18
  48. data/lib/tp_plus/nodes/terminator_node.rb +16 -16
  49. data/lib/tp_plus/nodes/time_node.rb +24 -24
  50. data/lib/tp_plus/nodes/timer_method_node.rb +37 -37
  51. data/lib/tp_plus/nodes/timer_node.rb +16 -21
  52. data/lib/tp_plus/nodes/units_node.rb +20 -20
  53. data/lib/tp_plus/nodes/use_node.rb +21 -21
  54. data/lib/tp_plus/nodes/user_alarm_node.rb +16 -15
  55. data/lib/tp_plus/nodes/var_method_node.rb +23 -23
  56. data/lib/tp_plus/nodes/var_node.rb +39 -39
  57. data/lib/tp_plus/nodes/vision_register_node.rb +22 -21
  58. data/lib/tp_plus/nodes/wait_for_node.rb +54 -54
  59. data/lib/tp_plus/nodes/wait_until_node.rb +65 -65
  60. data/lib/tp_plus/nodes/while_node.rb +42 -36
  61. data/lib/tp_plus/parser.rb +1682 -1592
  62. data/lib/tp_plus/scanner.rb +283 -383
  63. data/lib/tp_plus/token.rb +97 -0
  64. data/lib/tp_plus/version.rb +3 -3
  65. data/lib/tp_plus.rb +66 -62
  66. data/test/test_helper.rb +5 -5
  67. data/test/tp_plus/test_interpreter.rb +1309 -1168
  68. data/test/tp_plus/test_parser.rb +496 -489
  69. data/test/tp_plus/test_scanner.rb +577 -522
  70. data/tp_plus.gemspec +31 -28
  71. metadata +61 -14
  72. data/lib/tp_plus/nodes/set_node.rb +0 -22
@@ -1,22 +1,22 @@
1
- module TPPlus
2
- module Nodes
3
- class DefinitionNode
4
- attr_reader :identifier, :assignable
5
- def initialize(identifier,assignable)
6
- @identifier = identifier
7
- @assignable = assignable
8
- end
9
-
10
- def eval(context)
11
- if @assignable.is_a?(DigitNode) || @assignable.is_a?(RealNode)
12
- raise "Constants must be defined with all CAPS" unless @identifier.upcase == @identifier
13
-
14
- context.add_constant(@identifier, @assignable)
15
- else
16
- context.add_var(@identifier, @assignable)
17
- end
18
- nil
19
- end
20
- end
21
- end
22
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class DefinitionNode
4
+ attr_reader :identifier, :assignable
5
+ def initialize(identifier,assignable)
6
+ @identifier = identifier
7
+ @assignable = assignable
8
+ end
9
+
10
+ def eval(context)
11
+ if @assignable.is_a?(DigitNode) || @assignable.is_a?(RealNode)
12
+ raise "Constants must be defined with all CAPS" unless @identifier.upcase == @identifier
13
+
14
+ context.add_constant(@identifier, @assignable)
15
+ else
16
+ context.add_var(@identifier, @assignable)
17
+ end
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,21 +1,22 @@
1
- module TPPlus
2
- module Nodes
3
- class DigitNode
4
- def initialize(value)
5
- @value = value
6
- end
7
-
8
- def requires_mixed_logic?(context)
9
- false
10
- end
11
-
12
- def eval(context, options={})
13
- if @value < 0
14
- "(#{@value})"
15
- else
16
- @value
17
- end
18
- end
19
- end
20
- end
21
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class DigitNode
4
+ attr_reader :value
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def requires_mixed_logic?(context)
10
+ false
11
+ end
12
+
13
+ def eval(context, options={})
14
+ if @value < 0
15
+ "(#{@value})"
16
+ else
17
+ @value
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module TPPlus
2
+ module Nodes
3
+ class EmptyStmtNode
4
+ def eval(context, options={})
5
+ ""
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,13 +1,13 @@
1
- module TPPlus
2
- module Nodes
3
- class EvalNode
4
- def initialize(string)
5
- @string = string
6
- end
7
-
8
- def eval(context)
9
- @string
10
- end
11
- end
12
- end
13
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class EvalNode
4
+ def initialize(string)
5
+ @string = string
6
+ end
7
+
8
+ def eval(context)
9
+ @string
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,65 +1,68 @@
1
- module TPPlus
2
- module Nodes
3
- class ExpressionNode
4
- attr_reader :left_op, :op, :right_op
5
- attr_accessor :grouped
6
- def initialize(left_op, op_string, right_op)
7
- @left_op = left_op
8
- @op = OperatorNode.new(op_string)
9
- @right_op = right_op
10
- end
11
-
12
- def requires_mixed_logic?(context)
13
- contains_expression? ||
14
- @grouped ||
15
- [@op, @left_op, @right_op].map { |op|
16
- next if op.nil?
17
- op.requires_mixed_logic?(context)
18
- }.any?
19
- end
20
-
21
- def contains_expression?
22
- [@left_op, @right_op].map {|op| op.is_a? ExpressionNode }.any?
23
- end
24
-
25
- def boolean_result?
26
- case @op.string
27
- when "&&","||","!","==","<>",">",">=","<","<="
28
- true
29
- else
30
- false
31
- end
32
- end
33
-
34
- def with_parens(string, context, options={})
35
- return string unless options[:force_parens] || options[:as_condition] && requires_mixed_logic?(context)
36
-
37
- "(#{string})"
38
- end
39
-
40
- def string_val(context, options={})
41
- if @op.bang?
42
- # this is for skip conditions, which do not
43
- # support mixed logic
44
- if options[:disable_mixed_logic]
45
- "#{@left_op.eval(context)}=OFF"
46
- else
47
- "#{@op.eval(context,options)}#{@left_op.eval(context)}"
48
- end
49
- else
50
- if @op.boolean? && options[:opposite]
51
- "!#{@left_op.eval(context)}#{@op.eval(context,options)}!#{@right_op.eval(context)}"
52
- else
53
- "#{@left_op.eval(context)}#{@op.eval(context,options)}#{@right_op.eval(context)}"
54
- end
55
- end
56
- end
57
-
58
- def eval(context,options={})
59
- options[:force_parens] = true if @grouped
60
-
61
- with_parens(string_val(context, options), context, options)
62
- end
63
- end
64
- end
65
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class ExpressionNode
4
+ attr_reader :left_op, :op, :right_op
5
+ def initialize(left_op, op_string, right_op)
6
+ @left_op = left_op
7
+ @op = OperatorNode.new(op_string)
8
+ @right_op = right_op
9
+ end
10
+
11
+ def grouped?
12
+ return false if @right_op.nil? # this is for NOT (!) operator
13
+
14
+ @left_op.is_a?(ParenExpressionNode) || @right_op.is_a?(ParenExpressionNode)
15
+ end
16
+
17
+ def requires_mixed_logic?(context)
18
+ contains_expression? ||
19
+ grouped? ||
20
+ [@op, @left_op, @right_op].map { |op|
21
+ next if op.nil?
22
+ op.requires_mixed_logic?(context)
23
+ }.any?
24
+ end
25
+
26
+ def contains_expression?
27
+ [@left_op, @right_op].map {|op| op.is_a? ExpressionNode }.any? ||
28
+ [@left_op, @right_op].map { |op| op.is_a? ParenExpressionNode }.any?
29
+ end
30
+
31
+ def boolean_result?
32
+ case @op.string
33
+ when "&&","||","!","==","<>",">",">=","<","<="
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ def string_val(context, options={})
41
+ if @op.bang?
42
+ # this is for skip conditions, which do not
43
+ # support mixed logic
44
+ if options[:disable_mixed_logic]
45
+ "#{@left_op.eval(context)}=OFF"
46
+ else
47
+ "#{@op.eval(context,options)}#{@left_op.eval(context)}"
48
+ end
49
+ else
50
+ if @op.boolean?
51
+ # if operator is &&, || or !, we flip the operator and the operands
52
+ "#{@left_op.eval(context,options)}#{@op.eval(context, options)}#{@right_op.eval(context, options)}"
53
+ else
54
+ # flip the operator if options[:opposite]
55
+ # flip the operands only if opposite and the operand is an expression
56
+ "#{@left_op.eval(context, opposite: (@left_op.is_a?(ExpressionNode) && options[:opposite]))}#{@op.eval(context, options)}#{@right_op.eval(context, opposite: (@right_op.is_a?(ExpressionNode) && options[:opposite]))}"
57
+ end
58
+ end
59
+ end
60
+
61
+ def eval(context,options={})
62
+ options[:force_parens] = true if grouped?
63
+
64
+ string_val(context, options)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,20 +1,20 @@
1
- module TPPlus
2
- module Nodes
3
- class ForNode
4
- def initialize(var_node, initial_value_node, final_value_node, block)
5
- @var_node = var_node
6
- @initial_value_node = initial_value_node
7
- @final_value_node = final_value_node
8
- @block = block.flatten.reject {|n| n.is_a?(TerminatorNode) }
9
- end
10
-
11
- def block(context)
12
- @s ||= @block.inject("") {|s,n| s << "#{n.eval(context)} ;\n" }
13
- end
14
-
15
- def eval(context)
16
- "FOR #{@var_node.eval(context)}=#{@initial_value_node.eval(context)} TO #{@final_value_node.eval(context)} ;\n#{block(context)}ENDFOR"
17
- end
18
- end
19
- end
20
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class ForNode
4
+ def initialize(var_node, initial_value_node, final_value_node, block)
5
+ @var_node = var_node
6
+ @initial_value_node = initial_value_node
7
+ @final_value_node = final_value_node
8
+ @block = block.flatten.reject {|n| n.is_a?(TerminatorNode) }
9
+ end
10
+
11
+ def block(context)
12
+ @s ||= @block.inject("") {|s,n| s << "#{n.eval(context)} ;\n" }
13
+ end
14
+
15
+ def eval(context)
16
+ "FOR #{@var_node.eval(context)}=#{@initial_value_node.eval(context)} TO #{@final_value_node.eval(context)} ;\n#{block(context)}ENDFOR"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,27 +1,27 @@
1
- module TPPlus
2
- module Nodes
3
- class HeaderNode
4
- def initialize(type, value)
5
- @type = type
6
- @value = value
7
- end
8
-
9
- def eval(context, options={})
10
- case @type
11
- when "TP_IGNORE_PAUSE"
12
- context.header_data[:ignore_pause] = @value
13
- when "TP_COMMENT"
14
- context.header_data[:comment] = @value
15
- when "TP_GROUPMASK"
16
- context.header_data[:group_mask] = @value
17
- when "TP_SUBTYPE"
18
- context.header_data[:subtype] = @value.to_sym
19
- else
20
- raise "Unsupported TP Header value (#{@type})"
21
- end
22
-
23
- nil
24
- end
25
- end
26
- end
27
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class HeaderNode
4
+ def initialize(type, value)
5
+ @type = type
6
+ @value = value
7
+ end
8
+
9
+ def eval(context, options={})
10
+ case @type
11
+ when "TP_IGNORE_PAUSE"
12
+ context.header_data[:ignore_pause] = @value
13
+ when "TP_COMMENT"
14
+ context.header_data[:comment] = @value
15
+ when "TP_GROUPMASK"
16
+ context.header_data[:group_mask] = @value
17
+ when "TP_SUBTYPE"
18
+ context.header_data[:subtype] = @value.to_sym
19
+ else
20
+ raise "Unsupported TP Header value (#{@type})"
21
+ end
22
+
23
+ nil
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,49 +1,51 @@
1
- module TPPlus
2
- module Nodes
3
- class IndirectNode
4
- def initialize(type, target)
5
- @type = type
6
- @target = target
7
- end
8
-
9
- def string
10
- case @type
11
- when :position
12
- "P"
13
- when :position_register
14
- "PR"
15
- when :flag
16
- "F"
17
- when :timer
18
- "TIMER"
19
- when :register
20
- "R"
21
- when :user_alarm
22
- "UALM"
23
- else
24
- raise "Invalid indirect type"
25
- end
26
- end
27
-
28
- def requires_mixed_logic?(context)
29
- case @type
30
- when :flag#, :timer
31
- true
32
- else
33
- false
34
- end
35
- end
36
-
37
- def eval(context,options={})
38
- s = "#{string}[#{@target.eval(context)}]"
39
- if options[:opposite]
40
- s = "!#{s}"
41
- end
42
- if options[:as_condition]
43
- s = "(#{s})"
44
- end
45
- s
46
- end
47
- end
48
- end
49
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class IndirectNode
4
+ def initialize(type, target)
5
+ @type = type
6
+ @target = target
7
+ end
8
+
9
+ def string
10
+ case @type
11
+ when :position
12
+ "P"
13
+ when :position_register
14
+ "PR"
15
+ when :flag
16
+ "F"
17
+ when :timer
18
+ "TIMER"
19
+ when :register
20
+ "R"
21
+ when :user_alarm
22
+ "UALM"
23
+ when :user_frame
24
+ "UFRAME"
25
+ else
26
+ raise "Invalid indirect type"
27
+ end
28
+ end
29
+
30
+ def requires_mixed_logic?(context)
31
+ case @type
32
+ when :flag#, :timer
33
+ true
34
+ else
35
+ false
36
+ end
37
+ end
38
+
39
+ def eval(context,options={})
40
+ s = "#{string}[#{@target.eval(context)}]"
41
+ if options[:opposite]
42
+ s = "!#{s}"
43
+ end
44
+ if options[:as_condition]
45
+ s = "(#{s})"
46
+ end
47
+ s
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,40 +1,40 @@
1
- module TPPlus
2
- module Nodes
3
- class InlineConditionalNode
4
- attr_reader :condition_node
5
- def initialize(type, condition, block)
6
- @type = type
7
- @condition_node = condition
8
- @block = block
9
- end
10
-
11
- def condition_requires_mixed_logic?(context)
12
- @condition_node.is_a?(VarNode) ||
13
- @condition_node.is_a?(NamespacedVarNode) ||
14
- @condition_node.requires_mixed_logic?(context)
15
- end
16
-
17
- def block_requires_mixed_logic?(context)
18
- @block.requires_mixed_logic?(context)
19
- end
20
-
21
- def can_be_inlined?
22
- false
23
- end
24
-
25
- def condition(context,options={})
26
- options[:opposite] ||= @type == "unless"
27
-
28
- if condition_requires_mixed_logic?(context) || block_requires_mixed_logic?(context)
29
- "(#{@condition_node.eval(context, options)})"
30
- else
31
- @condition_node.eval(context, options)
32
- end
33
- end
34
-
35
- def eval(context)
36
- "IF #{condition(context)},#{@block.eval(context,mixed_logic:true)}"
37
- end
38
- end
39
- end
40
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class InlineConditionalNode
4
+ attr_reader :condition_node
5
+ def initialize(type, condition, block)
6
+ @type = type
7
+ @condition_node = condition
8
+ @block = block
9
+ end
10
+
11
+ def condition_requires_mixed_logic?(context)
12
+ @condition_node.is_a?(VarNode) ||
13
+ @condition_node.is_a?(NamespacedVarNode) ||
14
+ @condition_node.requires_mixed_logic?(context)
15
+ end
16
+
17
+ def block_requires_mixed_logic?(context)
18
+ @block.requires_mixed_logic?(context)
19
+ end
20
+
21
+ def can_be_inlined?
22
+ false
23
+ end
24
+
25
+ def condition(context,options={})
26
+ options[:opposite] ||= @type == "unless"
27
+
28
+ if condition_requires_mixed_logic?(context) || block_requires_mixed_logic?(context)
29
+ "(#{@condition_node.eval(context, options)})"
30
+ else
31
+ @condition_node.eval(context, options)
32
+ end
33
+ end
34
+
35
+ def eval(context)
36
+ "IF #{condition(context)},#{@block.eval(context,mixed_logic:true)}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,55 +1,55 @@
1
- module TPPlus
2
- module Nodes
3
- class IOMethodNode
4
- def initialize(method, target,options={})
5
- @method = method
6
- @target = target
7
- @init_options = options
8
- end
9
-
10
- def requires_mixed_logic?(context)
11
- true
12
- end
13
-
14
- def can_be_inlined?
15
- true
16
- end
17
-
18
- def on_off(value,options={})
19
- options[:mixed_logic] ? "(#{value})" : value
20
- end
21
-
22
- def pulse_time(context)
23
- "%.1f" % case @init_options[:pulse_units]
24
- when "s"
25
- @init_options[:pulse_time].eval(context)
26
- when "ms"
27
- @init_options[:pulse_time].eval(context).to_f / 1000
28
- else
29
- raise "Invalid pulse units"
30
- end
31
- end
32
-
33
- def pulse_extra(context)
34
- return "" if @init_options[:pulse_time].nil?
35
-
36
- ",#{pulse_time(context)}sec"
37
- end
38
-
39
- def eval(context,options={})
40
- options[:mixed_logic] = true if @target.requires_mixed_logic?(context)
41
-
42
- case @method
43
- when "turn_on"
44
- "#{@target.eval(context)}=#{on_off("ON",options)}"
45
- when "turn_off"
46
- "#{@target.eval(context)}=#{on_off("OFF",options)}"
47
- when "toggle"
48
- "#{@target.eval(context)}=(!#{@target.eval(context)})"
49
- when "pulse"
50
- "#{@target.eval(context)}=PULSE#{pulse_extra(context)}"
51
- end
52
- end
53
- end
54
- end
55
- end
1
+ module TPPlus
2
+ module Nodes
3
+ class IOMethodNode
4
+ def initialize(method, target,options={})
5
+ @method = method
6
+ @target = target
7
+ @init_options = options
8
+ end
9
+
10
+ def requires_mixed_logic?(context)
11
+ true
12
+ end
13
+
14
+ def can_be_inlined?
15
+ true
16
+ end
17
+
18
+ def on_off(value,options={})
19
+ options[:mixed_logic] ? "(#{value})" : value
20
+ end
21
+
22
+ def pulse_time(context)
23
+ "%.1f" % case @init_options[:pulse_units]
24
+ when "s"
25
+ @init_options[:pulse_time].eval(context)
26
+ when "ms"
27
+ @init_options[:pulse_time].eval(context).to_f / 1000
28
+ else
29
+ raise "Invalid pulse units"
30
+ end
31
+ end
32
+
33
+ def pulse_extra(context)
34
+ return "" if @init_options[:pulse_time].nil?
35
+
36
+ ",#{pulse_time(context)}sec"
37
+ end
38
+
39
+ def eval(context,options={})
40
+ options[:mixed_logic] = true if @target.requires_mixed_logic?(context)
41
+
42
+ case @method
43
+ when "turn_on"
44
+ "#{@target.eval(context)}=#{on_off("ON",options)}"
45
+ when "turn_off"
46
+ "#{@target.eval(context)}=#{on_off("OFF",options)}"
47
+ when "toggle"
48
+ "#{@target.eval(context)}=(!#{@target.eval(context)})"
49
+ when "pulse"
50
+ "#{@target.eval(context)}=PULSE#{pulse_extra(context)}"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end