spitewaste 0.1.005 → 0.1.010

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,7 @@ module Spitewaste
11
11
  INSTRUCTIONS = /(\S+):|(\b(#{OPERATORS_M2T.keys * ?|})\s+(-?\d\S*)?)/
12
12
  SPECIAL_INSN = /(call|jump|jz|jn)\s+(\S+)/
13
13
 
14
- attr_reader :src, :instructions, :error
14
+ attr_reader :src, :instructions, :error, :symbol_table
15
15
 
16
16
  def initialize program, **options
17
17
  @src = program.dup
@@ -127,8 +127,22 @@ module Spitewaste
127
127
  def propagate_macros
128
128
  # Macros are write-once, allowing user code to customize the special
129
129
  # values that get used to drive certain behavior in the standard library.
130
+
131
+ parse = -> s { s.split(?,).map &:strip }
132
+
133
+ # Macro "functions" get handled first.
134
+ @src.gsub!(/(\$\S+?)\(([^)]+)\)\s*{(.+?)}/m) {
135
+ @macros[$1] ||= [$2, $3]; ''
136
+ }
137
+ @src.gsub!(/(\$\S+?)\(([^)]+)\)/) {
138
+ params, body = @macros[$1]
139
+ raise "no macro function '#$1'" unless body
140
+ map = parse[params].zip(parse[$2]).to_h
141
+ body.gsub(/`(.+?)`/) { map[$1] }
142
+ }
143
+
130
144
  @src.gsub!(/(\$\S+)\s*=\s*(.+)/) { @macros[$1] ||= $2; '' }
131
- @src.gsub!(/(\$\S+)/) { @macros[$1] || raise("no macro '#{$1}'") }
145
+ @src.gsub!(/(\$\S+)/) { @macros[$1] || raise("no macro '#$1'") }
132
146
  end
133
147
 
134
148
  def eliminate_dead_code!
@@ -146,7 +160,7 @@ module Spitewaste
146
160
  subroutines = {}
147
161
  while label = tokens.shift
148
162
  sublen = tokens.index { |t| t[/:$/] } || tokens.size
149
- subroutines[label.chop] = tokens.shift sublen
163
+ subroutines[label.chop] ||= tokens.shift sublen
150
164
  end
151
165
 
152
166
  # A subroutine may indirectly depend on the one immediately after by
@@ -1,3 +1,3 @@
1
1
  module Spitewaste
2
- VERSION = '0.1.005'
2
+ VERSION = '0.1.010'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spitewaste
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.005
4
+ version: 0.1.010
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collided Scope (collidedscope)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-15 00:00:00.000000000 Z
11
+ date: 2020-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake