Skip to content

Commit

Permalink
Extending the compiler to support an arbitrary number of parents
Browse files Browse the repository at this point in the history
  • Loading branch information
blinry committed Apr 24, 2019
1 parent 5ccd2ce commit caa4d1d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,16 @@ def compile outfile
ir << " call void @exit(i32 0)\n"
ir << " unreachable\n"
when 1
ir << " br label %commit#{c.parents.first.oid[0..7]}\n"
when 2
ir << " %val#{u} = call i64 @pop()\n"
ir << " %cmp#{u} = icmp eq i64 %val#{u}, 0\n"
ir << " br i1 %cmp#{u}, label %commit#{c.parents[0].oid[0..7]}, label %commit#{c.parents[1].oid[0..7]}\n"
ir << " br label %commit#{c.parents.last.oid[0..7]}\n"
else
raise "More than 2 parents are not implemented yet\n"
ir << " %val#{u} = call i64 @pop()\n"
ir << " switch i64 %val#{u}, label %commit#{c.parents.last.oid[0..7]} ["

c.parents[0..-2].each_with_index do |p, i|
ir << "i64 #{i}, label %commit#{p.oid[0..7]} "
end

ir << "]\n"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion examples/brainfuck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ DECODE1=$(git commit-tree -m "dup \">\" sub" -p $READ_CAB -p $DECODE2 $EMPTY_TRE
GETCHAR=$(git commit-tree -m "getchar dup" -p $READ_BACK -p $DECODE1 $EMPTY_TREE)
git tag read-loop $GETCHAR

SETUP=$(git commit-tree -m "50 write 2 right 2 write 4 right" -p $GETCHAR $EMPTY_TREE)
SETUP=$(git commit-tree -m "500 write 2 right 2 write 4 right" -p $GETCHAR $EMPTY_TREE)
git reset $SETUP
2 changes: 1 addition & 1 deletion interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def pop

class Tape
def initialize
@tape = Array.new(10000, 0)
@tape = Array.new(500, 0)
@position = @tape.size/2
end
def write value
Expand Down

0 comments on commit caa4d1d

Please sign in to comment.