Switch to c++

This commit is contained in:
2025-12-07 17:22:12 +00:00
parent 2927df7710
commit 7acf35c8ea
55 changed files with 3506 additions and 304 deletions

0
__old__/builds/.keep Normal file
View File

54
__old__/crib.rb Executable file
View File

@@ -0,0 +1,54 @@
#!/usr/bin/env ruby
require "ffi"
require "zlib"
require_relative "../src/ruby/utils"
require_relative "../src/ruby/mod"
require_relative "../src/ruby/ts_rb"
require_relative "../src/ruby/fm"
require_relative "../src/ruby/editor"
require_relative "../src/ruby/ide"
$rows, $cols = C.start_screen
$running = true
$event_queue = Queue.new
$folder = Dir.new File.dirname(ARGV[0] || Dir.pwd)
$threads = []
at_exit do
IDE.close
C.end_screen
puts "Exiting crib.rb"
end
IDE.start
$threads << Thread.new do
loop do
sleep 1.0 / 64
break unless $running
IDE.handle_event $event_queue.pop timeout: 0 until $event_queue.empty?
IDE.render
C.render
end
end
$threads << Thread.new do
loop do
sleep 1.0 / 64
break unless $running
IDE.work!
end
end
$threads << Thread.new do
loop do
break unless $running
event = C.read_key # read_key is blocking
$running = false if KEY_TYPE[event[:key_type]] == :char && event[:c] == ctrl_key('q')
$event_queue << event
end
end
$threads.each &:join

329
__old__/editor.rb Normal file
View File

@@ -0,0 +1,329 @@
class Editor
attr_accessor :text, :cursor, :selection_start, :filename
def initialize(filename, x, y, width, height)
contents = File.exist?(filename) ? File.read(filename) : "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
@text = contents.grapheme_clusters
@cursor = @text.size
@selection_start = 0
@scroll = 0
@x = x
@y = y
@width = width
@height = height
@filename = filename
@folds = []
@highlights = Array.new(@text.size)
@highlight_generation = 0
@tree = nil
@num_width = (@text.count("\n") + 1).to_s.size + 3
@parser = C.ts_parser_new
@lang_ts = C.tree_sitter_ruby
C.ts_parser_set_language @parser, @lang_ts
@cached_tree = nil
C.load_query "/home/syed/main/crib/grammar/ruby.scm"
highlight!
rebuild_folded_lines!
end
def save!
File.write @filename, @text.join
end
def highlight!
gen = @highlight_generation
src = @text.join
text_bytes = []
byte_offset = 0
@text.each do |cluster|
text_bytes << byte_offset
byte_offset += cluster.bytesize
end
text_bytes << byte_offset
line_map = []
line = 0
@text.each_with_index do |ch, i|
line_map[i] = line
line += 1 if ch == "\n"
end
# TODO: add ts_tree_edit calls in erase/insert
count_ptr = FFI::MemoryPointer.new :uint32
@cached_tree = C.ts_parser_parse_string @parser, nil, src, src.bytesize
root = C.ts_tree_root_node @cached_tree
ptr = C.ts_collect_tokens root, count_ptr, @lang_ts, src
count = count_ptr.read_uint32
ints = ptr.read_array_of_uint32 count
C.free_tokens ptr
count_ptr.free
C.ts_tree_delete @cached_tree
return if gen != @highlight_generation
@highlight_generation = 0
@highlights = Array.new(@text.size)
done_folds = []
ints.each_slice(3).map do |start_byte, end_byte, sym_i|
start_cluster = byte_to_cluster start_byte, text_bytes
end_cluster = byte_to_cluster end_byte - 1, text_bytes
next if start_cluster.nil? || end_cluster.nil?
sym = TS_SYMBOL_MAP[sym_i]
if sym == "@fold"
start_row = line_map[start_cluster]
end_row = line_map[end_cluster]
next if start_row == end_row
hash_value = Zlib.crc32 @text[start_cluster...end_cluster].join
done_folds << hash_value
if (f = @folds.find { |f| f.crc32 == hash_value })
f.start = start_row
f.end = end_row
else
@folds << Fold.new(start_row, end_row, false, hash_value)
end
next
end
hl = TS_RUBY[sym]
next unless hl
(start_cluster..end_cluster).each do |i|
@highlights[i] = hl if @highlights[i].nil? || @highlights[i].priority < hl.priority
end
end
@folds.select! { |f| done_folds.include? f.crc32 }
rebuild_folded_lines!
end
def byte_to_cluster(byte, byte_starts)
left = 0
right = byte_starts.size - 2
while left <= right
mid = (left + right) / 2
if byte_starts[mid] <= byte && byte < byte_starts[mid + 1]
return mid
elsif byte < byte_starts[mid]
right = mid - 1
else
left = mid + 1
end
end
nil
end
def erase(x0, x1)
return if x0 < 0 || x1 < x0
lines_deleted = @text[x0...x1].count "\n"
@text[x0...x1] = []
@highlights[x0...x1] = []
@highlight_generation += 1
@folds.map! do |f|
if f.start >= @text[...x0].count("\n")
f.start -= lines_deleted
f.end -= lines_deleted
end
f
end
rebuild_folded_lines!
@num_width = (@text.count("\n") + 1).to_s.size + 3
end
def insert(x, str)
@text.insert x, *str.grapheme_clusters
@highlights.insert x, *Array.new(str.grapheme_clusters.size)
lines_added = str.count "\n"
@folds.map! do |f|
if f.start >= @text[...x].count("\n")
f.start += lines_added
f.end += lines_added
end
f
end
rebuild_folded_lines!
@highlight_generation += 1
@num_width = (@text.count("\n") + 1).to_s.size + 3
end
def move_up!
precursor_text = @text[...@cursor]
last_newline = precursor_text.rindex "\n"
return unless last_newline
@preferred_col ||= @cursor - last_newline
prev_start = precursor_text[...last_newline].rindex("\n") || -1
@cursor = [
prev_start + @preferred_col,
@text[prev_start + 1..].index("\n")&.+(prev_start + 1) || @text.size
].min
end
def move_down!
next_newline = @text[@cursor..].index("\n")&.+(@cursor)
return unless next_newline
@preferred_col ||= (@cursor - (@text[...@cursor].rindex("\n") || -1)).abs
@cursor = [
next_newline + @preferred_col,
@text[next_newline + 1..].index("\n")&.+(next_newline + 1) || @text.size
].min
end
def move_left!
@cursor = [@cursor - 1, 0].max
@preferred_col = nil
end
def move_right!
@cursor = [@cursor + 1, @text.size].min
@preferred_col = nil
end
def cursor_valid?
!@folded_lines.include? @text[0...@cursor].count("\n")
end
def handle_event(event)
return if event.nil?
case KEY_TYPE[event[:key_type]]
when :char
ch = event[:c].chr
ch = "\n" if ch == "\r"
if ch == "'"
cr = @text[0...@cursor].count("\n")
@folds.find { |f| f.active = !f.active if f.start <= cr && f.end >= cr }
rebuild_folded_lines!
elsif ch == ctrl_key('s').chr
save!
elsif ch =~ /[[:print:]]|\n/
insert @cursor, ch
@cursor += 1
elsif ch == "\b" || ch == "\x7f"
erase @cursor - 1, @cursor
@cursor = [@cursor - 1, 0].max
end
@preferred_col = nil
when :special
return unless MODIFIER[event[:special_modifier]] == :none
case SPECIAL_KEY[event[:special_key]]
when :up
move_up!
move_up! until cursor_valid?
when :down
move_down!
move_down! until cursor_valid?
when :left
move_left!
move_left! until cursor_valid?
when :right
move_right!
move_right! until cursor_valid?
when :delete
erase @cursor, @cursor + 1
@cursor = [@cursor, @text.size].min
@preferred_col = nil
end
end
move_right! until cursor_valid?
adjust_scroll!
end
def adjust_scroll!
cr = cursor_row_visual
start = @scroll
last = @scroll + @height - 1
if cr < start
@scroll = cr
elsif cr > last
@scroll = cr - @height + 1
end
end
def cursor_row_visual
row = col = 0
nc = 0
@text.each_with_index do |ch, i|
return row if i == @cursor
if @folded_lines.include? nc
nc += 1 if ch == "\n"
next
end
if ch == "\n"
row += 1
nc += 1
col = 0
next
end
w = C.display_width(ch)
if col + w > @width - @num_width
row += 1
col = 0
end
col += w
end
row
end
def rebuild_folded_lines!
@folded_lines = Set.new
@folds.each do |f|
(f.start + 1..f.end).each { |line_num| @folded_lines.add(line_num) } if f.active
end
end
def render
(0...@height).each { |h| (0...@width).each { |w|
C.update @y + h, @x + w, ' ', 0x000000, 0x000000, 0
} }
row = col = nc = 0
cursor_row = cursor_col = nil
@text.each_with_index do |ch, i|
if @folded_lines.include? nc
nc += 1 if ch == "\n"
next
end
if i == @cursor
cursor_row = row
cursor_col = col
end
if col == 0
color = 0x666666
color = 0xFFFFFF if cursor_row == row
num = (nc + 1).to_s.rjust @num_width - 1, '👪👩‍💻👨🏿‍🚀🏳️‍🌈🔥🛠️🌍🥶✅❌💡⏰🚀'#ddasasdasda
num.each_char.with_index do |c, j|
C.update @y + row - @scroll, @x + j, c, color, 0x000000, 0
end
if (fold = @folds.find { |f| nc == f.start })
icon = fold.active ? "" : "" # "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : "" "" : ""
C.update @y + row - @scroll, @x, icon, 0xFF0000, 0x000000, 0
end
end
if ch == "\n"
row += 1
nc += 1
col = 0
next
end
w = C.display_width(ch)
if col + w > @width - @num_width
row += 1
col = 0
end
col += w
next if row < @scroll
break if row - @scroll >= @height
fg = 0xFFFFFF
bg = 0x000000
fl = 0
if (h = @highlights[i])
fg = h.color_fg
bg = h.color_bg
fl = h.flags
end
C.update @y + row - @scroll, @x + @num_width + col - w, ch, fg, bg, fl
end
if @text[-1] == "\n" || @text.size == 0
num = (nc + 1).to_s.rjust @num_width - 1, ' '
color = 0x666666
color = 0xFFFFFF if (cursor_row || row) == row
num.each_char.with_index do |c, j|
C.update @y + row - @scroll, @x + j, c, color, 0x000000, 0
end
end
C.set_cursor @y - @scroll + (cursor_row || row), @x + @num_width + (cursor_col || col), 1
end
end

30
__old__/fm.rb Normal file
View File

@@ -0,0 +1,30 @@
module FileManager
@hidden = true
module_function
def start(x, y, width, height)
@hidden = false
@x = x
@y = y
@width = width
@height = height
end
def toggle!
@hidden = !@hidden
end
def render
return if @hidden
(0...@height).each { |h| (0...@width).each { |w|
C.update @y + h, @x + w, ' ', 0x000000, 0x000000, 0
} }
files = $folder.children
files.each_with_index do |f, i|
f.each_char.with_index do |c, j|
C.update @y + i, @x + j, c, 0xFFFFFF, 0x000000, 0
end
end
end
end

36
__old__/ide.rb Normal file
View File

@@ -0,0 +1,36 @@
module IDE
FM_WIDTH = 20
module_function
def start
@editors = {}
@u_c = 0
editor = Editor.new (ARGV[0] || ""), FM_WIDTH, 0, $cols - FM_WIDTH, $rows
@editors[editor.filename || "untitled #{@u_c += 1}"] = editor
@selected_editor = @editors.keys.first
@focus = :editor
FileManager.start 0, 0, FM_WIDTH, $rows
end
def handle_event(event)
if @focus == :editor
@editors[@selected_editor].handle_event event if @editors.key? @selected_editor
elsif @focus == :file_manager
# TODO
end
end
def render
@editors[@selected_editor].render if @editors.key? @selected_editor
FileManager.render
end
def work!
@editors[@selected_editor].highlight! if @editors.key? @selected_editor
end
def close
# TODO
end
end

126
__old__/mod.rb Normal file
View File

@@ -0,0 +1,126 @@
module C
extend FFI::Library
ffi_lib File.join(__dir__, "../../libs/tree-sitter/libtree-sitter.so")
# TSTree and TSParser are opaque pointers
typedef :pointer, :tstree
typedef :pointer, :tsparser
# Define TSNode struct
class TSNode < FFI::Struct
layout :context, [:uint32, 4],
:id, :pointer,
:tree, :tstree
end
class TSPoint < FFI::Struct
layout :row, :uint32,
:column, :uint32
end
class TSInputEdit < FFI::Struct
layout :start_byte, :uint32,
:old_end_byte, :uint32,
:new_end_byte, :uint32,
:start_point, TSPoint.by_value,
:old_end_point, TSPoint.by_value,
:new_end_point, TSPoint.by_value
end
class TSTreeCursor < FFI::Struct
layout :tree, :pointer,
:id, :pointer,
:context, [:uint32, 3]
end
attach_function :ts_parser_new, [], :tsparser
attach_function :ts_parser_set_language, [:tsparser, :pointer], :bool
attach_function :ts_parser_parse_string, [:tsparser, :tstree, :string, :size_t], :tstree, blocking: true
attach_function :ts_tree_delete, [:tstree], :void
attach_function :ts_node_start_byte, [TSNode.by_value], :uint32
attach_function :ts_node_end_byte, [TSNode.by_value], :uint32
attach_function :ts_node_symbol, [TSNode.by_value], :uint16
attach_function :ts_tree_edit, [:tstree, TSInputEdit.by_ref], :void
attach_function :ts_tree_root_node, [:tstree], TSNode.by_value, blocking: true
attach_function :ts_node_child_count, [TSNode.by_value], :uint32
attach_function :ts_node_child, [TSNode.by_value, :uint32], TSNode.by_value
# Ruby grammar
ffi_lib File.join(__dir__, "../../libs/tree-sitter-ruby/libtree-sitter-ruby.so")
attach_function :tree_sitter_ruby, [], :pointer
ffi_lib File.join(__dir__, "../../builds/C-crib.so")
class KeyEvent < FFI::Struct
layout :key_type, :uint8,
:c, :char,
:special_key, :uint8,
:special_modifier, :uint8,
:mouse_x, :uint8,
:mouse_y, :uint8,
:mouse_button, :uint8,
:mouse_state, :uint8,
:mouse_direction, :uint8,
:mouse_modifier, :uint8
def to_s
case KEY_TYPE[self[:key_type]]
when :char
"#<KeyEvent char=#{self[:c].inspect}>"
when :special
"#<KeyEvent special key=#{SPECIAL_KEY[self[:special_key]]} mod=#{MODIFIER[self[:special_modifier]]}>"
when :mouse
"#<KeyEvent mouse x=#{self[:mouse_x]} y=#{self[:mouse_y]} " \
"btn=#{MOUSE_BUTTON[self[:mouse_button]]} state=#{MOUSE_STATE[self[:mouse_state]]} " \
"scroll_dir=#{SCROLL_DIR[self[:mouse_direction]]} mod=#{MODIFIER[self[:mouse_modifier]]}>"
else
"#<KeyEvent type=#{self[:key_type]} unknown=true>"
end
end
end
class Coords < FFI::Struct
layout :x, :int,
:y, :int
def to_ary
[self[:x], self[:y]]
end
def to_s
"#<Coords x=#{self[:x]} y=#{self[:y]}>"
end
end
class Size < FFI::Struct
layout :rows, :int,
:cols, :int
def to_ary
[self[:rows], self[:cols]]
end
def to_s
"#<Size rows=#{self[:rows]} cols=#{self[:cols]}>"
end
end
attach_function :load_query, [:string], :void
attach_function :ts_collect_tokens, [TSNode.by_value, :pointer, :pointer, :string], :pointer, blocking: true
attach_function :free_tokens, [:pointer], :void, blocking: true
attach_function :start_screen, [], Size.by_value
attach_function :end_screen, [], :void
attach_function :update, [:int, :int, :string, :uint32, :uint32, :uint8], :void
attach_function :render, [], :void, blocking: true
attach_function :set_cursor, [:int, :int, :int], :void
attach_function :read_key, [], KeyEvent.by_value, blocking: true
attach_function :get_size, [], Coords.by_value
attach_function :display_width, [:string], :int, blocking: true
end

57
__old__/ts_rb.rb Normal file
View File

@@ -0,0 +1,57 @@
query = File.read "/home/syed/main/crib/grammar/ruby.scm"
raw = query.scan(/@[a-zA-Z0-9_.]+/)
seen = {}
ordered = []
raw.each do |c|
next if seen[c]
seen[c] = true
ordered << c
end
TS_SYMBOL_MAP = ordered.freeze
spawn "echo \"#{TS_SYMBOL_MAP.sort.join "\n"}\" > /tmp/gg"
TS_RUBY = {
"@string.special.symbol" => Highlight.new(0xbd9ae6, 0x000000, CF_NONE, 2),
"@comment" => Highlight.new(0xAAAAAA, 0x000000, CF_ITALIC, 1),
"@boolean.true" => Highlight.new(0x51eeba, 0x000000, CF_NONE, 1),
"@boolean.false" => Highlight.new(0xee513a, 0x000000, CF_NONE, 1),
"@constant.nil" => Highlight.new(0xee8757, 0x000000, CF_NONE, 1),
"@constant" => Highlight.new(0xebda8c, 0x000000, CF_NONE, 1),
"@number" => Highlight.new(0xebda8c, 0x000000, CF_NONE, 2),
"@number.float" => Highlight.new(0xebda8c, 0x000000, CF_NONE, 2),
"@constant.builtin" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 2),
"@punctuation.bracket" => Highlight.new(0xbd9ae6, 0x000000, CF_NONE, 1),
"@operator.ligature" => Highlight.new(0xffffff, 0x000000, CF_ITALIC, 1),
"@operator" => Highlight.new(0xffffff, 0x000000, CF_NONE, 1),
"@punctuation.delimiter" => Highlight.new(0xbd9ae6, 0x000000, CF_NONE, 1),
"@punctuation.special" => Highlight.new(0xe6a24c, 0x000000, CF_NONE, 1),
"@function" => Highlight.new(0xaad84c, 0x000000, CF_NONE, 1),
"@function.builtin" => Highlight.new(0xaad84c, 0xFF0000, CF_NONE, 1),
"@keyword.import" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@function.call" => Highlight.new(0xff5689, 0x000000, CF_NONE, 1),
"@keyword" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.conditional" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.control" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.directive" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.exception" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.function" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.operator" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.repeat" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.return" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@keyword.type" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@label" => Highlight.new(0xfbb152, 0x000000, CF_NONE, 1),
"@string" => Highlight.new(0xaad84c, 0x000000, CF_NONE, 1),
"@string.escape" => Highlight.new(0xe6a24c, 0x000000, CF_NONE, 2),
"@string.regexp" => Highlight.new(0xe6a24c, 0x000000, CF_NONE, 2),
"@type" => Highlight.new(0xaad84c, 0x000000, CF_NONE, 1),
"@variable" => Highlight.new(0xffffff, 0x000000, CF_NONE, 1),
"@variable.builtin" => Highlight.new(0xffffff, 0x000000, CF_NONE, 1),
"@variable.member" => Highlight.new(0xffffff, 0x000000, CF_NONE, 1),
"@variable.parameter" => Highlight.new(0xffffff, 0x000000, CF_NONE, 1),
}

67
__old__/utils.rb Normal file
View File

@@ -0,0 +1,67 @@
Fold = Struct.new(:start, :end, :active, :crc32)
Highlight = Struct.new(:color_fg, :color_bg, :flags, :priority)
Token = Struct.new(:start, :end, :sym)
CF_NONE = 0b00000000
CF_ITALIC = 0b00000001
CF_BOLD = 0b00000010
CF_UNDERLINE = 0b00000100
def ctrl_key(k)
k.ord & 0x1F
end
# Key types
KEY_TYPE = {
0 => :char,
1 => :special,
2 => :mouse,
3 => :none
}.freeze
# Special keys
SPECIAL_KEY = {
0 => :up,
1 => :down,
2 => :left,
3 => :right,
4 => :delete
}.freeze
# Control key
KEY_ESC = "\x1b".freeze
# Mouse states
MOUSE_STATE = {
0 => :press,
1 => :release,
2 => :drag,
3 => :scroll
}.freeze
# Mouse buttons
MOUSE_BUTTON = {
0 => :left,
1 => :middle,
2 => :right,
3 => :scroll,
4 => :none
}.freeze
# Scroll directions
SCROLL_DIR = {
0 => :up,
1 => :down,
2 => :left,
3 => :right,
4 => :none
}.freeze
# Modifiers
MODIFIER = {
0 => :none,
1 => :alt,
2 => :cntrl,
3 => :cntrl_alt,
4 => :shift
}.freeze