#!/usr/bin/ruby def evaluate_line line, sub l = line.clone sub.each do |n, v| l.gsub!('$' + n.to_s, v.to_s) l.gsub!(/([0-9]+\+[0-9]+)/) { |m| eval m } end l end def evaluate_for block, cond ret = [] eval cond[0] while eval cond[1] local = Hash.new local_variables.each do |n| next if ['local', 'ret', 'cond', 'block'].index n local[n] = eval n end ret += [evaluate_line(block, $constants.merge(local))] eval cond[2] end ret end unless filename = ARGV.pop puts "Usage:" puts "./precompiler.rb [CONST1=VAL1 [CONST2=VAL2 [CONST3=VAL3...]]] filename" end $constants = Hash.new ARGV.each do |l| c = l.split "=" next unless c.length == 2 $constants[c[0]] = c[1] end f = File.open filename c = 0 while !f.eof? l = f.gets.chop l = evaluate_line l, $constants if l =~ /^\/\/ ?for\(([^;]*);([^;]*);([^;]*)\)$/ cond = [$1, $2, $3] l = f.gets.chop unless l =~ /^\/\/ ?\{$/ puts "Syntax error after \"for\" instruction" exit end block = "" while !f.eof? l=f.gets break if l =~ /^\/\/ ?\}/ block += l end (evaluate_for block, cond).each { |l| puts l} else puts l end end