summaryrefslogtreecommitdiff
path: root/Rakefile
blob: 4cb08d020ae2ff9933a37a06d8096ba340d810b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
# frozen_string_literal: true

require 'open3'
require 'rake/clean'

STAGES = Dir.glob('boot/stage*')
  .collect { |stage| File.basename stage }
  .sort { |a, b| a.delete_prefix('stage').to_i <=> b.delete_prefix('stage').to_i }
  .drop(1) # First assembly stage does not count.

CLOBBER.include 'build'

def run(exe)
  ENV.fetch('QEMU', '').split << exe
end

directory 'build'

task default: :boot

desc 'Final stage'
task boot: "build/valid/#{STAGES.last}/cl"
task boot: "boot/#{STAGES.last}/cl.elna" do |t|
  groupped = t.prerequisites.group_by { |stage| File.extname stage }.transform_values(&:first)
  exe = groupped['']
  expected = groupped[''] + '.s'
  source = groupped['.elna']

  cat_arguments = ['cat', source]
  diff_arguments = ['diff', '-Nur', '--text', expected, '-']
  Open3.pipeline(cat_arguments, run(exe), diff_arguments)
end

desc 'Convert previous stage language into the current stage language'
task :convert do
  File.open('boot/stage22/cl.elna', 'w') do |current_stage|
    seen_proc = false
    seen_global_var = false
    File.readlines('boot/stage21/cl.elna').each do |line|
      seen_proc = true if line.start_with? 'proc'
      seen_proc = false if line.start_with? 'end'
      if line.start_with?('begin') && !seen_proc
        current_stage << <<~FUN
          proc f();
          var
          	x: ElnaRtlObjectInfo;
          begin
          	x.allocated := true
          end;

          begin
          	f();
        FUN
      elsif line.start_with?('var') && !seen_global_var
        current_stage << <<~FUN
          var
        FUN
      elsif line.end_with?("allocated: Word\n")
        current_stage << "\t\tallocated: Bool\n"
      else
        current_stage << line
      end
    end
  end
end