Start a Modula-2 experiment

This commit is contained in:
2022-06-05 23:43:45 +02:00
parent 5490f6ce1c
commit f29e68ec93
23 changed files with 994 additions and 1605 deletions

130
Rakefile
View File

@@ -2,68 +2,82 @@ require 'pathname'
require 'rake/clean'
require 'open3'
DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12']
BINARY = 'build/bin/elna'
TESTS = FileList['tests/*.elna']
.map { |test| (Pathname.new('build') + test).sub_ext('').to_path }
SOURCES = FileList['source/**/*.d']
directory 'build'
CLEAN.include 'build'
CLEAN.include '.dub'
rule(/build\/tests\/.+/ => ->(file) { test_for_out(file) }) do |t|
Pathname.new(t.name).dirname.mkpath
sh BINARY, t.source
sh 'gcc', '-o', t.name, "#{t.name}.o"
# Open3.pipeline [BINARY, t.source], ['gcc', '-x', 'assembler', '-o', t.name, '-']
end
file BINARY => SOURCES do |t|
sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc-12')
end
file 'build/tests/sample' => BINARY do |t|
sh t.source
sh 'gcc', '-o', t.name, 'build/tests/sample.o'
end
task default: BINARY
desc 'Run all tests and check the results'
task test: TESTS
task test: BINARY do
TESTS.each do |test|
expected = Pathname
.new(test)
.sub_ext('.txt')
.sub(/^build\/tests\//, 'tests/expectations/')
.read
.to_i
puts "Running #{test}"
system test
actual = $?.exitstatus
fail "#{test}: Expected #{expected}, got #{actual}" unless expected == actual
M2C = 'gm2' # Modula-2 compiler.
BOOT_OBJECTS = FileList['boot/*.mod']
.map do |source|
Pathname.new(source).basename.sub_ext('.o')
end
# system './build/tests/sample'
# actual = $?.exitstatus
# fail "./build/tests/sample: Expected 3, got #{actual}" unless 3 == actual
def source_for_object(out_file)
path = Pathname.new(out_file).relative_path_from('build')
result = ['build/boot']
definition = File.join('boot', path.basename.sub_ext('.def'))
result << definition if File.exist? definition
implementation = path.sub_ext('.mod').to_path
implementation = File.join 'build', implementation unless File.exist? implementation
result << implementation
end
desc 'Run unittest blocks'
task unittest: SOURCES do |t|
sh('dub', 'test', '--compiler=gdc-12')
directory 'build/boot'
directory 'build/self'
CLEAN.include 'build'
rule(/build\/.+\.o$/ => ->(file) { source_for_object(file) }) do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-c', '-I', 'boot', '-o', t.name, *sources
end
def test_for_out(out_file)
test_source = Pathname
.new(out_file)
.sub_ext('.elna')
.sub(/^build\//, '')
.to_path
[test_source, BINARY]
rule(/build\/self\/.+\.mod$/ => [
'build/self', 'build/boot/Compiler',
->(file) { File.join('boot', Pathname.new(file).basename) }
]) do |t|
sources, compiler = t.prerequisites
.reject { |f| File.directory? f }
.partition { |f| f.end_with? '.mod' }
File.open t.name, 'w' do |output|
puts
puts(compiler * ' ')
Open3.popen2(*compiler) do |cl_in, cl_out|
cl_in.write File.read(*sources)
cl_in.close
IO.copy_stream cl_out, output
cl_out.close
end
end
end
['boot', 'self'].each do |sub|
compiler_binary = Pathname.new('build') + sub + 'Compiler'
file compiler_binary.to_path => BOOT_OBJECTS.map { |file| File.join('build', sub, file) } do |t|
sh M2C, '-o', t.name, *t.prerequisites
end
compiler_object = compiler_binary.sub_ext('.o')
file compiler_object.to_path => source_for_object(compiler_object) do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-fscaffold-main', '-c', '-I', 'boot', '-o', t.name, *sources
end
end
task default: 'build/self/Compiler'
task default: 'build/self/Compiler.mod'
task default: 'boot/Compiler.mod'
task :default do |t|
exe, previous_output, source = t.prerequisites
cat_arguments = ['cat', source]
diff_arguments = ['diff', '-Nur', '--text', previous_output, '-']
puts [cat_arguments * ' ', exe, diff_arguments * ' '].join(' | ')
Open3.pipeline(cat_arguments, exe, diff_arguments)
end