Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active March 10, 2020 05:41
Show Gist options
  • Save amirrajan/46593caddfd6ea03e36421c14ce7736c to your computer and use it in GitHub Desktop.
Save amirrajan/46593caddfd6ea03e36421c14ce7736c to your computer and use it in GitHub Desktop.
Oh god what have I done. A horrible starting point to an ELisp to DragonRuby Compiler.
class Tokenizer
attr_accessor :blocks, :current_token, :current_word, :root
def initialize
@id = 0
@root = []
@stack = [@root]
@current_word = ""
end
def process token_char
if token_char == ""
# do nothing
elsif (token_char == " " || token_char == "\n") && @current_word.strip.length > 0
add_to_current_block @current_word if @current_word.strip.length > 0
@current_word = ""
elsif token_char == '('
@stack.push([])
elsif token_char == ')'
add_to_current_block @current_word if @current_word.strip.length > 0
add_to_current_block @stack.pop
@current_word = ""
else
@current_word << token_char if token_char.strip.length > 0
end
puts "#{@root}"
end
def add_to_current_block word
puts "adding #{word} to #{@stack[-1]}"
@stack[-1] << word
puts "#{@stack[-1]}"
end
end
class El2Rb
def self.compile! file
El2Rb.new(file).compile!
end
def initialize file
@file = file
@code = $gtk.read_file file
@tokenizer = Tokenizer.new
$gtk.write_file "#{file}.rb", ""
end
def result_file
"#{@file}.rb"
end
def compile!
log_cyan "================== starting"
idx = 0
while idx < @code.length
@tokenizer.process @code[idx]
idx += 1
end
puts "#{@tokenizer.root}"
log_cyan "================== complete"
# todo convert root to ruby code and write it to file
$gtk.require result_file
end
end
(defun tick (args)
(puts (str hello world)))
require 'app/el.rb'
def tick args
El2Rb.compile! "app/main.el" if args.state.tick_count == 0
end
$gtk.reset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment